Swing/HTMLAttributeID のバックアップ(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/HTMLAttributeID へ行く。
- 1 (2013-09-09 (月) 03:01:50)
- 2 (2013-09-09 (月) 04:06:08)
- 3 (2014-05-22 (木) 14:30:02)
- 4 (2014-08-27 (水) 21:11:19)
- 5 (2014-10-31 (金) 01:40:03)
- 6 (2015-02-27 (金) 12:02:13)
- 7 (2016-12-20 (火) 14:00:43)
- 8 (2017-08-23 (水) 18:41:49)
- 9 (2018-09-11 (火) 14:34:41)
- 10 (2020-09-03 (木) 09:44:21)
- 11 (2022-03-19 (土) 11:33:37)
- 12 (2025-01-03 (金) 08:57:02)
- 13 (2025-01-03 (金) 09:01:23)
- 14 (2025-01-03 (金) 09:02:38)
- 15 (2025-01-03 (金) 09:03:21)
- 16 (2025-01-03 (金) 09:04:02)
- 17 (2025-06-19 (木) 12:41:37)
- 18 (2025-06-19 (木) 12:43:47)
TITLE:JEditorPaneのHTMLDocumentからIDでElementを取得する
Posted by aterai at 2013-09-09
JEditorPaneのHTMLDocumentからIDでElementを取得する
`JEditorPane
に設定した
HTMLDocument
を検索して
id
属性を持つ
Element
`を取得します。
- &jnlp;
- &jar;
- &zip;
サンプルコード
private void traverseElementById(Element element) {
if(element.isLeaf()) {
checkID(element);
}else{
for(int i=0;i<element.getElementCount();i++) {
Element child = element.getElement(i);
checkID(child);
if(!child.isLeaf()) {
traverseElementById(child);
}
}
}
}
private void checkID(Element element) {
AttributeSet attrs = element.getAttributes();
Object elementName = attrs.getAttribute(
AbstractDocument.ElementNameAttribute);
Object name = (elementName != null)
? null : attrs.getAttribute(StyleConstants.NameAttribute);
HTML.Tag tag;
if(name instanceof HTML.Tag) {
tag = (HTML.Tag)name;
}else{
return;
}
textArea.append(String.format("%s%n", tag));
if(tag.isBlock()) { //block
Object bid = attrs.getAttribute(HTML.Attribute.ID);
if(bid!=null) {
textArea.append(String.format("block: id=%s%n", bid));
addHighlight(element, true);
}
}else{ //inline
Enumeration e = attrs.getAttributeNames();
while(e.hasMoreElements()) {
Object obj = attrs.getAttribute(e.nextElement());
//System.out.println("AttributeNames: "+obj);
if(obj instanceof AttributeSet) {
AttributeSet a = (AttributeSet)obj;
Object iid = a.getAttribute(HTML.Attribute.ID);
if(iid!=null) {
textArea.append(String.format("inline: id=%s%n", iid));
addHighlight(element, false);
}
}
}
}
}
View in GitHub: Java, Kotlin解説
- `
Element#getElement(id)
`- `
HTMLDocument#getElements(String)
メソッドを使用して指定した
id
を持つ
Element
`を取得 - これらの`
Element
は、
org.w3c.dom.Element
ではなく、
javax.swing.text.Element
インターフェイスを実装する
HTMLDocument.BlockElement
`など- `
org.w3c.dom.Document#getElementById(String)
`などは使用できない
- `
- 指定した`
id
の
Element
が存在した場合、
editorPane.select(element.getStartOffset(), element.getEndOffset());
`で選択- `
element.getStartOffset()
などで取得されるオフセットは、
JEditorPane
`に表示されない要素や属性は含まれない
- `
- `
- `
Highlight Element[@id]
`- `
id
属性を持つ
Element
`をハイライト表示 - `
HTMLDocument.BlockElement
などには、
html
の要素や属性が後で復元する場合のために
AttributeSet
`に備考として保存されている- ブロック要素とインライン要素で属性の保存されている場所が異なる
- `
DefaultHighlighter#setDrawsLayeredHighlights(false)
`の場合、改行を含むハイライトや選択状態の描画がおかしくなる?
- `
- `
ParserDelegator
`- `
ParserDelegator
を使って文字列をパースし、
HTMLEditorKit.ParserCallback#handleStartTag(...)
でタグの開始を見つけたら、
MutableAttributeSet#getAttribute(HTML.Attribute.ID);
でそのタグの
id
`を取得 - `
javax.swing.text.Element
とは無関係に、
JEditorPane#getText()
で取得した文字列を
html
`として解析している - `
HTMLEditorKit
が設定された
JEditorPane
から
getText()
で取得された文字列には、
<body>
などのタグが自動的に補完されているので、元の
html
`テキストとは異なる点に注意
- `
System.out.println("-- ParserDelegator --");
final Integer id = (Integer)spinner.getValue();
final String text = editorPane.getText();
ParserDelegator delegator = new ParserDelegator();
try {
delegator.parse(new StringReader(text), new HTMLEditorKit.ParserCallback() {
@Override public void handleStartTag(HTML.Tag tag, MutableAttributeSet a, int pos) {
Object attrid = a.getAttribute(HTML.Attribute.ID);
System.out.println(tag + "@id=" + attrid);
if(id.toString().equals(attrid)) {
System.out.println("found: pos="+pos);
int endoffs = text.indexOf(">", pos);
System.out.println(text.substring(pos, endoffs+1));
}
}
}, Boolean.TRUE);
} catch(Exception ex) {
ex.printStackTrace();
}