Debian Codegraph @

net/sf/saxon/dom/DOMEnvelope.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 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.dom;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.event.Sender;
13 import net.sf.saxon.expr.JPConverter;
14 import net.sf.saxon.expr.PJConverter;
15 import net.sf.saxon.expr.XPathContext;
16 import net.sf.saxon.lib.ExternalObjectModel;
17 import net.sf.saxon.om.NodeInfo;
18 import net.sf.saxon.om.Sequence;
19 import net.sf.saxon.pattern.AnyNodeTest;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.type.ItemType;
22 import net.sf.saxon.value.SequenceExtent;
23 import org.w3c.dom.Node;
24 import org.w3c.dom.NodeList;
25
26 import javax.xml.transform.Result;
27 import javax.xml.transform.Source;
28 import javax.xml.transform.dom.DOMSource;
29 import javax.xml.xpath.XPathConstants;
30
31 /**
32 * DOMEnvelope is an object model representation in which DOM interfaces are wrapped around
33 * Saxon NodeInfo nodes: that is, it implements the DOM on top of a Saxon tree implementation
34 * such as the tiny tree or linked tree.
35 */
36
37 public class DOMEnvelope implements ExternalObjectModel {
38
39 private static DOMEnvelope THE_INSTANCE = new DOMEnvelope();
40
41 /**
42 * Get the singular instance (this class is stateless)
43 *
44 * @return the singular instance
45 */
46
47 public static DOMEnvelope getInstance() {
48 return THE_INSTANCE;
49 }
50
51 /**
52 * Public constructor. (Use the singular instance in preference to creating a new one).
53 */
54
55 public DOMEnvelope() {
56 }
57
58 /**
59 * Get the name of a characteristic class, which, if it can be loaded, indicates that the supporting libraries
60 * for this object model implementation are available on the classpath
61 *
62 * @return by convention (but not necessarily) the class that implements a document node in the relevant
63 * external model
64 */
65 public String getDocumentClassName() {
66 return "org.w3c.dom.Document";
67 }
68
69 /**
70 * Get the URI of the external object model as used in the JAXP factory interfaces for obtaining
71 * an XPath implementation
72 */
73
74 public String getIdentifyingURI() {
75 return XPathConstants.DOM_OBJECT_MODEL;
76 }
77
78 public PJConverter getPJConverter(Class<?> targetClass) {
79 if (NodeOverNodeInfo.class.isAssignableFrom(targetClass)) {
80 return new PJConverter() {
81 public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException {
82 return DOMObjectModel.convertXPathValueToObject(value, targetClass);
83 }
84 };
85 } else if (NodeList.class.isAssignableFrom(targetClass)) {
86 return new PJConverter() {
87 public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException {
88 return DOMObjectModel.convertXPathValueToObject(value, targetClass);
89 }
90 };
91 } else {
92 return null;
93 }
94 }
95
96 public JPConverter getJPConverter(Class sourceClass, Configuration config) {
97 if (NodeOverNodeInfo.class.isAssignableFrom(sourceClass)) {
98 return new JPConverter() {
99 /*@Nullable*/
100 public Sequence<?> convert(Object object, XPathContext context) {
101 return convertObjectToXPathValue(object);
102 }
103
104 public ItemType getItemType() {
105 return AnyNodeTest.getInstance();
106 }
107 };
108 } else {
109 return null;
110 }
111 }
112
113 /**
114 * Get a converter that converts a sequence of XPath nodes to this model's representation
115 * of a node list.
116 *
117 * @param node an example of the kind of node used in this model
118 * @return if the model does not recognize this node as one of its own, return null. Otherwise
119 * return a PJConverter that takes a list of XPath nodes (represented as NodeInfo objects) and
120 * returns a collection of nodes in this object model
121 */
122
123 public PJConverter getNodeListCreator(Object node) {
124 //return getPJConverter(NodeList.class);
125 return null;
126 }
127
128 /**
129 * Test whether this object model recognizes a given node as one of its own
130 *
131 * @param object the object to be tested
132 * @return true if the supplied object is a node in this object model
133 */
134
135 public boolean isRecognizedNode(Object object) {
136 return object instanceof NodeOverNodeInfo;
137 }
138
139 /**
140 * Test whether this object model recognizes a given class as representing a
141 * node in that object model. This method will generally be called at compile time.
142 *
143 * @param nodeClass A class that possibly represents nodes
144 * @return true if the class is used to represent nodes in this object model
145 */
146
147 public boolean isRecognizedNodeClass(Class nodeClass) {
148 return NodeOverNodeInfo.class.isAssignableFrom(nodeClass);
149 }
150
151 /**
152 * Test whether this object model recognizes a particular kind of JAXP Result object,
153 * and if it does, return a Receiver that builds an instance of this data model from
154 * a sequence of events. If the Result is not recognised, return null.
155 * <p>This implementation always returns null: it is not possible to construct an instance
156 * of this object model implementation directly as the result of a JAXP transformation.</p>
157 */
158
159 public Receiver getDocumentBuilder(Result result) {
160 return null;
161 }
162
163 /**
164 * Test whether this object model recognizes a particular kind of JAXP Source object,
165 * and if it does, send the contents of the document to a supplied Receiver, and return true.
166 * Otherwise, return false.
167 * <p>This implementation returns true only if the source is a DOMSource whose contained node is a
168 * a "NodeOverNodeInfo".</p>
169 */
170
171 public boolean sendSource(Source source, Receiver receiver) throws XPathException {
172 if (source instanceof DOMSource) {
173 Node startNode = ((DOMSource) source).getNode();
174 if (startNode instanceof NodeOverNodeInfo) {
175 NodeInfo base = ((NodeOverNodeInfo) startNode).getUnderlyingNodeInfo();
176 Sender.send(base, receiver, null);
177 return true;
178 }
179 }
180 return false;
181 }
182
183 /**
184 * Wrap or unwrap a node using this object model to return the corresponding Saxon node. If the supplied
185 * source does not belong to this object model, return null
186 */
187
188 public NodeInfo unravel(Source source, Configuration config) {
189
190 if (source instanceof DOMSource) {
191 Node dsnode = ((DOMSource) source).getNode();
192 if (dsnode instanceof NodeOverNodeInfo) {
193 // Supplied source is a DOM Node wrapping a Saxon node: unwrap it
194 return ((NodeOverNodeInfo) dsnode).getUnderlyingNodeInfo();
195 }
196 }
197 return null;
198 }
199
200 /**
201 * Convert a Java object to an XPath value. If the supplied object is recognized as a representation
202 * of a value using this object model, the object model should convert the value to an XPath value
203 * and return this as the result. If not, it should return null. If the object is recognized but cannot
204 * be converted, an exception should be thrown
205 *
206 * @param object the object to be converted
207 * @return the value after conversion
208 */
209
210 private Sequence<?> convertObjectToXPathValue(Object object) {
211 if (object instanceof NodeList) {
212 // NodeList needs great care, because Xerces element nodes implement the NodeList interface,
213 // with the actual list being the children of the node in question. So we only recognize a
214 // NodeList here if it is non-empty, and if all the nodes within it are NodeOverNodeInfo objects.
215 NodeList list = (NodeList) object;
216 final int len = list.getLength();
217 if (len == 0) {
218 return null;
219 }
220 NodeInfo[] nodes = new NodeInfo[len];
221 for (int i = 0; i < len; i++) {
222 if (list.item(i) instanceof NodeOverNodeInfo) {
223 nodes[i] = ((NodeOverNodeInfo) list.item(i)).getUnderlyingNodeInfo();
224 } else {
225 return null;
226 }
227 }
228 return new SequenceExtent(nodes);
229
230 // Note, we accept the nodes in the order returned by the function; there
231 // is no requirement that this should be document order.
232 } else if (object instanceof NodeOverNodeInfo) {
233 return ((NodeOverNodeInfo) object).getUnderlyingNodeInfo();
234 } else {
235 return null;
236 }
237 }
238
239 }
240
241