gpt4 book ai didi

Java Swing 的 HTMLEditorKit 删除了 SPAN 元素的嵌套

转载 作者:行者123 更新时间:2023-12-01 14:05:07 26 4
gpt4 key购买 nike

我正在使用 JTextPane 实现一个简单的 HTML 编辑器, HTMLDocumentHTMLEditorKit 。代码如下:

public class SimpleHTMLEditor extends JFrame {

private static final long serialVersionUID = 1L;

private final JTextPane textPane;

private final HTMLEditorKit edtKit;

private HTMLDocument doc;

public static void main(String[] args) {
final SimpleHTMLEditor editor = new SimpleHTMLEditor();
editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editor.setVisible(true);
}

public SimpleHTMLEditor() {
super("Simple HTML Editor");
textPane = new JTextPane();
edtKit = new HTMLEditorKit();
textPane.setEditorKit(edtKit);
doc = new HTMLDocument();
textPane.setDocument(doc);

final Container content = getContentPane();
content.add(textPane, BorderLayout.CENTER);
content.add(createToolBar(), BorderLayout.NORTH);
setJMenuBar(createMenuBar());
setSize(500, 240);
textPane.requestFocusInWindow();
}

/**
* Creates the toolbar with the combo box that allows for creation and
* use of different conditions with their respective presentation styles.
* @return The toolbar
*/
private JToolBar createToolBar() {
final JToolBar bar = new JToolBar();
return bar;
}

/**
* Creates the menu bar. It contains:
* <li> Actions to read/write HTML file
* <li> Action to display the HTML source in a popup window.
* @return The menu bar
*/
private JMenuBar createMenuBar() {
final JMenuBar menubar = new JMenuBar();
final JMenu mnuFile = new JMenu("File");
menubar.add(mnuFile);
final SaveAction actSave = new SaveAction();
mnuFile.add(actSave);
final LoadAction actLoad = new LoadAction();
mnuFile.add(actLoad);
final JMenuItem mnuPreview = new JMenuItem("Preview");
menubar.add(mnuPreview);
mnuPreview.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
final HTMLPreview previewer = new HTMLPreview(SimpleHTMLEditor.this,
getDocSource());
previewer.setVisible(true);
}
});

return menubar;
}

/**
* Helper method to extract the HTML source code from the HTML document
* @return The HTML source code
*/
private String getDocSource() {
final StringWriter sw = new StringWriter();
try {
edtKit.write(sw, doc, 0, doc.getLength());
} catch (IOException | BadLocationException e1) {
e1.printStackTrace();
}
try {
sw.close();
} catch (final IOException e1) {
e1.printStackTrace();
}
return sw.toString();
}

class SaveAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public SaveAction() {
super("Save to File");
}
@Override
public void actionPerformed(ActionEvent ev) {
final JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(SimpleHTMLEditor.this) != JFileChooser.APPROVE_OPTION)
return;
final File targetFile = chooser.getSelectedFile();
if (targetFile == null)
return;
FileWriter writer = null;
try {
writer = new FileWriter(targetFile);
textPane.write(writer);
} catch (final IOException ex) {
JOptionPane.showMessageDialog(SimpleHTMLEditor.this,
"File Not Saved", "ERROR",
JOptionPane.ERROR_MESSAGE);
} finally {
if (writer != null) {
try {
writer.close();
} catch (final IOException x) {
}
}
}
}
}

class LoadAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public LoadAction() {
super("Load from File");
}
@Override
public void actionPerformed(ActionEvent ev) {
final JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(SimpleHTMLEditor.this) != JFileChooser.APPROVE_OPTION)
return;
final File sourceFile = chooser.getSelectedFile();
if (sourceFile == null)
return;
FileReader reader = null;
try {
reader = new FileReader(sourceFile);
doc = (HTMLDocument)edtKit.createDefaultDocument();
textPane.setDocument(doc);
edtKit.read(reader,doc,0);
} catch (final IOException ex) {
JOptionPane.showMessageDialog(SimpleHTMLEditor.this,
"File '" + sourceFile.getAbsolutePath() +
"' Not Loaded", "ERROR",
JOptionPane.ERROR_MESSAGE);

} catch (final BadLocationException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (final IOException x) {
}
}
}
}
}
}

/**
* Popup window for display of the current contents of the editor as HTML
* source code.
*/
class HTMLPreview extends JDialog {

private static final long serialVersionUID = 1L;

public HTMLPreview(JFrame parent, String source) {
super(parent, "HTML Source", true);

final JPanel pp = new JPanel(new BorderLayout());
pp.setBorder(new EmptyBorder(10, 10, 5, 10));

final JTextArea srcTxtArea = new JTextArea(source, 20, 60);
srcTxtArea.setFont(new Font("Courier", Font.PLAIN, 12));
final JScrollPane sp = new JScrollPane(srcTxtArea);
pp.add(sp, BorderLayout.CENTER);

final JPanel p = new JPanel(new FlowLayout());
final JPanel p1 = new JPanel(new GridLayout(1, 2, 10, 0));

final JButton closeBtn = new JButton("Close");
closeBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
p1.add(closeBtn);
p.add(p1);
pp.add(p, BorderLayout.SOUTH);
getContentPane().add(pp, BorderLayout.CENTER);
pack();
setResizable(true);
setLocationRelativeTo(parent);
}
}

我注意到,当我加载包含嵌套 SPAN 的 HTML 文件时嵌套的元素被默默删除。这是一个 HTML 示例文件:

<html>
<head>
</head>
<body>
<p>
<span title="tag:one">Outer span. <span title="tag:two">Inner span.</span> Outer span continued.</span>
</p>
</body>
</html>

加载该文件并选择“预览”操作后在工具栏上我会看到一个弹出窗口,其中显示了 HTML 源代码看起来如下:

<html>
<head>
</head>
<body>
<p>
<span title="tag:one">Outer span.</span> <span title="tag:two">Inner
span.</span> <span title="tag:one"> Outer span continued.</span>
</p>
</body>
</html>

可以看出,外部 SPAN 元素被默默地分成两个 SPAN 元素,内部 SPAN 元素放置在两个元素之间。在我看来,该行为显示了实现 HTML 编辑器的 Java Swing 组件与 HTML 4.x 标准之间的不兼容性之一,据我所知,HTML 4.x 标准允许嵌套 SPAN 元素。我现在的问题是:有没有一种(希望不是太复杂)的方法解决方法或克服该限制,即使 HTML 编辑器保留读取 HTML 文本时遇到的嵌套 SPAN 元素?

提前非常感谢,apatwork。

最佳答案

要进行查看,请考虑通过 Desktop#browse() 利用用户首选的浏览器。编辑,SO贡献者 @stanislavl已写several relevant articles关于该主题和 answered extensively这里。特别是HTMLEditorKit and Custom tags in the JEditorPane/JTextPane可能会提供一些关于可行性的见解。

关于Java Swing 的 HTMLEditorKit 删除了 SPAN 元素的嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18962722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com