4

我相信JEditorPane。我需要简单的编辑器。我已经解决了加载和修改包含自定义(两个)标签的 HTML 的问题(请参阅我的旧帖子)。它可以正确显示文档,我现在什至可以编辑它。我可以编写文本、删除字符或我的自定义元素。我赢得了一场战斗,但还没有赢得这场战争。遗憾的是,下一步也是非常成问题的。我无法插入我的自定义标签。

我有一个自定义操作:

import my.own.HTMLEditorKit; //extends standard HTMLEditorKit
import my.own.HTMLDocument; //extends standard HTMLDocument

class InsertElementAction extends StyledTextAction {
    private static final long serialVersionUID = 1L;

    public InsertElementAction(String actionName) {
        super(actionName);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JEditorPane editor = getEditor(e);

        if (editor == null)
            return;

        HTMLDocument doc = (HTMLDocument) editor.getDocument();
        HTMLEditorKit ekit = (HTMLEditorKit) editor.getEditorKit();
        int offset = editor.getSelectionStart();

        try {
            ekit.insertHTML(doc, offset, "<span>ahoj</span>", 0, 0, HTML.Tag.SPAN);
            Element ele = doc.getRootElements()[0];
            ele = ele.getElement(1).getElement(0);
            doc.setInnerHTML(ele, "<bar medium=\"#DEFAULT\" type=\"packaged\" source=\"identifier\" />");
        }
        catch (BadLocationException ble) {
            throw new Error(ble);
        }
        catch (IOException ioe) {
            throw new Error(ioe);
        }
    }
}

它工作正常。我可以插入span元素。但我不能以这种方式插入非标准标签。我可以只插入code,span等等,但不能插入我的标签。对于我的标签,我不得不使用这个:

ekit.insertHTML(doc, offset, "x<bar medium=\"#DEFAULT\" type=\"packaged\" source=\"identifier\" />x", 0, 0, null);

有两个关键问题

  1. 自定义标签必须以非空白字符(此处为 x)为界
  2. 当前元素的主体被拆分

当我将span元素插入<p>paragraph</p>时,我得到<p>par<span>ahoj</span>agraph</p>了预期。然而,未知标签总是作为元素的子body元素插入,结果(例如未知标签x)是<p>par</p><x>ahoj</x><p>agraph</p>.

这项工作令人筋疲力尽。几周以来,我一直坚信这个相对简单的任务。我已经浪费了。如果插入不起作用,我可以将其全部废弃...

4

2 回答 2

2

希望这有助于 http://java-sl.com/custom_tag_html_kit.html

于 2011-09-30T10:35:02.260 回答
2

我找到了解决方法。标签以这种方式插入:

ModifiedHTMLDocument doc = (ModifiedHTMLDocument) editor.getDocument();
int offset = editor.getSelectionStart();
//insert our special tag (if the tag is not bounded with non-whitespace character, nothing happens)
doc.insertHTML(offset, "-<specialTag />-");
//remove leading and trailing minuses
doc.remove(offset, 1); //at the current position is the minus before tag inserted
doc.remove(offset + 1, 1); //the next sign is minus after new tag (the tag is nowhere)
//Note: no, you really cannot do that: doc.remove(offset, 2), because then the tag is deleted

我的ModifiedHTMLDocument包含一个方法insertHTML(),它调用反射隐藏的方法:

public void insertHTML(int offset, String htmlText) throws BadLocationException, IOException {
    if (getParser() == null)
        throw new IllegalStateException("No HTMLEditorKit.Parser");

    Element elem = getCurrentElement(offset);

    //the method insertHTML is not visible
    try {
        Method insertHTML = javax.swing.text.html.HTMLDocument.class.getDeclaredMethod("insertHTML",
                new Class[] {Element.class, int.class, String.class, boolean.class});
        insertHTML.setAccessible(true);
        insertHTML.invoke(this, new Object[] {elem, offset, htmlText, false});
    }
    catch (Exception e) {
        throw new IOException("The method insertHTML() could not be invoked", e);
    }
}

我们砖盒的最后一块是寻找当前元素的方法:

public Element getCurrentElement(int offset) {
    ElementIterator ei = new ElementIterator(this);
    Element elem, currentElem = null;
    int elemLength = Integer.MAX_VALUE;

    while ((elem = ei.next()) != null) { //looking for closest element
        int start = elem.getStartOffset(), end = elem.getEndOffset(), len = end - start;
        if (elem.isLeaf() || elem.getName().equals("html"))
            continue;
        if (start <= offset && offset < end && len <= elemLength) {
            currentElem = elem;
            elemLength = len;
        }
    }

    return currentElem;
}

该方法也是ModifiedHTMLDocument该类的成员。

该解决方案并不纯粹,但它暂时解决了问题。我希望我能找到更好的套件。我正在考虑JWebEngine。那应该替代 current 差HTMLEditorKit,但我不知道它是否允许我添加我的自定义标签。

于 2011-10-03T10:20:01.293 回答