1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018-2020 Saxonica Limited
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * The tree information for a tree acting as a wrapper for a JDOM2 Document.
25 *
26 * @since 9.7: this class no longer implements NodeInfo; the document node itself
27 * is now an instance of JDOM2NodeWrapper.
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 * Create a Saxon wrapper for a JDOM document
40 *
41 * @param doc The JDOM document
42 * @param config The Saxon Configuration
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 * Wrap a node in the JDOM document.
54 *
55 * @param node The node to be wrapped. This must be a node in the same document
56 * (the system does not check for this).
57 * @return the wrapping NodeInfo object
58 */
59
60 public JDOM2NodeWrapper wrap(Object node) {
61 return JDOM2NodeWrapper.makeWrapper(node, this);
62 }
63
64 /**
65 * Get the element with a given ID, if any
66 *
67 * @param id the required ID value
68 * @param getParent
69 * @return the element node with the given ID if there is one, otherwise null.
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 // Original Code is Copyright (c) 2009-2020 Saxonica Limited