我正在尝试实现一个小的HTML编辑器,允许将前景色和背景色应用到文本的某些部分。我希望以HTML格式存储文本,并将着色信息保存在文件中。当使用不同样式编辑文本时,我当前的实现功能正常(样式定义前景色和背景色信息)。但是,当将编辑过的文档存储在文件中时,我会得到不正确的结构化HTML,并且缺少使用特定样式输入的文本部分。这是我的代码:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.text.AttributeSet;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class SimpleEditor extends JFrame {
private static final long serialVersionUID = 1L;
private final JTextPane textPane;
private final HTMLEditorKit edtKit;
private final HTMLDocument doc;
private final StyleSheet predefStyles;
public static void main(String[] args) {
final SimpleEditor editor = new SimpleEditor();
editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editor.setVisible(true);
}
public SimpleEditor() {
super("Very Simple HTML Editor");
textPane = new JTextPane();
edtKit = new HTMLEditorKit();
textPane.setEditorKit(edtKit);
predefStyles = new StyleSheet();
predefStyles.addRule("MyStyle1 { color:#cc0000; background-color:silver }\n" +
"MyStyle2 { color:#0000cc; background-color:aqua }");
doc = new HTMLDocument(predefStyles);
textPane.setDocument(doc);
final Container content = getContentPane();
content.add(textPane, BorderLayout.CENTER);
content.add(createToolBar(), BorderLayout.NORTH);
setJMenuBar(createMenuBar());
setSize(500, 240);
}
private JToolBar createToolBar() {
final Vector<String> styleNames = new Vector<String>();
final Enumeration<?> names = predefStyles.getStyleNames();
while (names.hasMoreElements()) {
styleNames.add((String) names.nextElement());
}
final DefaultComboBoxModel<String> stylesModel =
new DefaultComboBoxModel<String>(styleNames);
final JComboBox<String> cbStyleSel = new JComboBox<String>(stylesModel);
final JToolBar bar = new JToolBar();
cbStyleSel.setEditable(false);
cbStyleSel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
e.getSource();
@SuppressWarnings("unchecked")
final JComboBox<CondStyle> cboStyleSel =
(JComboBox<CondStyle>) e.getSource();
final String selItem = (String) cboStyleSel.getSelectedItem();
final Style style = textPane.getStyle(selItem);
textPane.setCharacterAttributes(extractStyleAttribs(style), true);
SimpleEditor.this.requestFocus();
}
});
bar.add(cbStyleSel);
return bar;
}
/**
* Extracts the style attributes except the style's name
* @param aStyle The style to be processed
* @return The visual attributes extracted from the style
*/
AttributeSet extractStyleAttribs(Style aStyle) {
final MutableAttributeSet attribs = new SimpleAttributeSet();
final Enumeration<?> attribNames = aStyle.getAttributeNames();
while (attribNames.hasMoreElements()) {
final Object attribName = attribNames.nextElement();
if (attribName == Style.NameAttribute) {
continue;
}
attribs.addAttribute(attribName, aStyle.getAttribute(attribName));
}
return attribs;
}
private JMenuBar createMenuBar() {
final JMenuBar menubar = new JMenuBar();
final JMenu mnuFile = new JMenu("File");
menubar.add(mnuFile);
final ExitAction actSave = new ExitAction();
mnuFile.add(actSave);
return menubar;
}
class ExitAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public ExitAction() {
super("Save & Exit");
}
@Override
public void actionPerformed(ActionEvent ev) {
final JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(SimpleEditor.this) !=
JFileChooser.APPROVE_OPTION)
return;
final File file = chooser.getSelectedFile();
if (file == null)
return;
FileWriter writer = null;
try {
writer = new FileWriter(file);
textPane.write(writer);
} catch (final IOException ex) {
JOptionPane.showMessageDialog(SimpleEditor.this,
"File Not Saved", "ERROR",
JOptionPane.ERROR_MESSAGE);
} finally {
if (writer != null) {
try {
writer.close();
} catch (final IOException x) {
}
}
}
System.exit(0);
}
}
}
这里输入了一些带样式文本的应用程序的屏幕截图:
保存文档(textPane.write(writer);
)时创建的HTML文件格式不正确。 <body> ... </body>
部分如下所示:
<body>
<p style="margin-top: 0">
default text
</p>
<p style="margin-top: 0">
<font color="#cc0000"><p style="background-color: silver">
</font> </p>
<p style="margin-top: 0">
<font color="#0000cc"><p style="background-color: aqua">
</font> </p>
<p style="margin-top: 0">
<font color="#333333" face="Dialog" size="3"><p FONT_ATTRIBUTE_KEY="javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=plain,size=12]">
</font> </p>
</body>
很明显,生成的HTML代码不正确。在<p>
元素内部,创建了一个新的<font>
元素,用于定义样式的前景色。然后创建另一个具有背景颜色定义的<p>
元素。但是文本丢失了。然后,不是关闭</p>
标记,而是关闭</font>
标记,然后关闭外部</p>
元素的<p>
标记。
我究竟做错了什么?