1
2
3
4
5
6
7
8 package net.sf.saxon.option.jdom2;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.om.AxisInfo;
12 import net.sf.saxon.om.GenericTreeInfo;
13 import net.sf.saxon.om.NodeInfo;
14 import net.sf.saxon.pattern.NodeKindTest;
15 import net.sf.saxon.tree.iter.AxisIterator;
16 import org.jdom2.Attribute;
17 import org.jdom2.Document;
18 import org.jdom2.Element;
19
20 import java.util.HashMap;
21 import java.util.List;
22
23
24
25
26
27
28
29
30
31 public class JDOM2DocumentWrapper extends GenericTreeInfo {
32
33 protected Configuration config;
34 protected long documentNumber;
35 private HashMap<String, Element> idIndex;
36 private HashMap<String, Object> userData;
37
38
39
40
41
42
43
44
45 public JDOM2DocumentWrapper(Document doc, Configuration config) {
46 super(config);
47 setRootNode(wrap(doc));
48 setSystemId(doc.getBaseURI());
49 }
50
51
52
53
54
55
56
57
58
59
60 public JDOM2NodeWrapper wrap(Object node) {
61 return JDOM2NodeWrapper.makeWrapper(node, this);
62 }
63
64
65
66
67
68
69
70
71
72 @Override
73 public NodeInfo selectID(String id, boolean getParent) {
74 if (idIndex == null) {
75 idIndex = new HashMap<String, Element>(100);
76 AxisIterator iter = getRootNode().iterateAxis(AxisInfo.DESCENDANT, NodeKindTest.ELEMENT);
77 NodeInfo node;
78 while ((node = iter.next()) != null) {
79 Element element = (Element) ((JDOM2NodeWrapper) node).node;
80 List attributes = element.getAttributes();
81 for (Object attribute : attributes) {
82 Attribute att = (Attribute) attribute;
83 if (att.getAttributeType() == Attribute.ID_TYPE) {
84 idIndex.put(att.getValue(), element);
85 }
86 }
87 }
88 }
89 Element element = idIndex.get(id);
90 return element == null ? null : wrap(element);
91 }
92
93 }
94
95