1 /*
2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3  */
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements.  See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License.  You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 package com.sun.org.apache.xalan.internal.xsltc.dom;
22 
23 import com.sun.org.apache.xalan.internal.xsltc.DOM;
24 import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;
25 import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
26 import com.sun.org.apache.xalan.internal.xsltc.TransletException;
27 import com.sun.org.apache.xml.internal.dtm.DTM;
28 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
29 import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
30 import java.util.Map;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.NodeList;
33 
34 /**
35  * @author Jacek Ambroziak
36  * @author Morten Jorgensen
37  */
38 public final class DOMAdapter implements DOM {
39 
40     // Mutually exclusive casting of DOM interface to known implementations
41     private DOMEnhancedForDTM _enhancedDOM;
42 
43     private DOM _dom;
44 
45     private String[] _namesArray;
46     private String[] _urisArray;
47     private int[]    _typesArray;
48     private String[] _namespaceArray;
49 
50     // Cached mappings
51     private short[] _mapping = null;
52     private int[]   _reverse = null;
53     private short[] _NSmapping = null;
54     private short[] _NSreverse = null;
55 
56     private int _multiDOMMask;
57 
DOMAdapter(DOM dom, String[] namesArray, String[] urisArray, int[] typesArray, String[] namespaceArray)58     public DOMAdapter(DOM dom,
59                       String[] namesArray,
60                       String[] urisArray,
61                       int[] typesArray,
62                       String[] namespaceArray) {
63         if (dom instanceof DOMEnhancedForDTM){
64             _enhancedDOM = (DOMEnhancedForDTM) dom;
65         }
66 
67         _dom = dom;
68         _namesArray = namesArray;
69         _urisArray = urisArray;
70         _typesArray = typesArray;
71         _namespaceArray = namespaceArray;
72     }
73 
setupMapping(String[] names, String[] urisArray, int[] typesArray, String[] namespaces)74     public void setupMapping(String[] names, String[] urisArray,
75                              int[] typesArray, String[] namespaces) {
76         _namesArray = names;
77         _urisArray = urisArray;
78         _typesArray = typesArray;
79         _namespaceArray = namespaces;
80     }
81 
getNamesArray()82     public String[] getNamesArray() {
83         return _namesArray;
84     }
85 
getUrisArray()86     public String[] getUrisArray() {
87         return _urisArray;
88     }
89 
getTypesArray()90     public int[] getTypesArray() {
91         return _typesArray;
92     }
93 
getNamespaceArray()94     public String[] getNamespaceArray() {
95         return _namespaceArray;
96     }
97 
getDOMImpl()98     public DOM getDOMImpl() {
99         return _dom;
100     }
101 
getMapping()102     private short[] getMapping() {
103         if (_mapping == null) {
104             if (_enhancedDOM != null) {
105                 _mapping = _enhancedDOM.getMapping(_namesArray, _urisArray,
106                                                    _typesArray);
107             }
108         }
109         return _mapping;
110     }
111 
getReverse()112     private int[] getReverse() {
113         if (_reverse == null) {
114             if (_enhancedDOM != null) {
115                 _reverse = _enhancedDOM.getReverseMapping(_namesArray,
116                                                           _urisArray,
117                                                           _typesArray);
118             }
119         }
120         return _reverse;
121     }
122 
getNSMapping()123     private short[] getNSMapping() {
124         if (_NSmapping == null) {
125             if (_enhancedDOM != null) {
126                 _NSmapping = _enhancedDOM.getNamespaceMapping(_namespaceArray);
127             }
128         }
129         return _NSmapping;
130     }
131 
getNSReverse()132     private short[] getNSReverse() {
133         if (_NSreverse == null) {
134             if (_enhancedDOM != null) {
135                 _NSreverse = _enhancedDOM
136                                   .getReverseNamespaceMapping(_namespaceArray);
137             }
138         }
139         return _NSreverse;
140     }
141 
142     /**
143       * Returns singleton iterator containg the document root
144       */
getIterator()145     public DTMAxisIterator getIterator() {
146         return _dom.getIterator();
147     }
148 
getStringValue()149     public String getStringValue() {
150         return _dom.getStringValue();
151     }
152 
getChildren(final int node)153     public DTMAxisIterator getChildren(final int node) {
154         if (_enhancedDOM != null) {
155             return _enhancedDOM.getChildren(node);
156         }
157         else {
158             DTMAxisIterator iterator = _dom.getChildren(node);
159             return iterator.setStartNode(node);
160         }
161     }
162 
setFilter(StripFilter filter)163     public void setFilter(StripFilter filter) {}
164 
getTypedChildren(final int type)165     public DTMAxisIterator getTypedChildren(final int type) {
166         final int[] reverse = getReverse();
167 
168         if (_enhancedDOM != null) {
169             return _enhancedDOM.getTypedChildren(reverse[type]);
170         }
171         else {
172             return _dom.getTypedChildren(type);
173         }
174     }
175 
getNamespaceAxisIterator(final int axis, final int ns)176     public DTMAxisIterator getNamespaceAxisIterator(final int axis,
177                                                     final int ns) {
178         return _dom.getNamespaceAxisIterator(axis, getNSReverse()[ns]);
179     }
180 
getAxisIterator(final int axis)181     public DTMAxisIterator getAxisIterator(final int axis) {
182         if (_enhancedDOM != null) {
183             return _enhancedDOM.getAxisIterator(axis);
184         }
185         else {
186             return _dom.getAxisIterator(axis);
187         }
188     }
189 
getTypedAxisIterator(final int axis, final int type)190     public DTMAxisIterator getTypedAxisIterator(final int axis,
191                                                 final int type) {
192         final int[] reverse = getReverse();
193         if (_enhancedDOM != null) {
194             return _enhancedDOM.getTypedAxisIterator(axis, reverse[type]);
195         } else {
196             return _dom.getTypedAxisIterator(axis, type);
197         }
198     }
199 
getMultiDOMMask()200     public int getMultiDOMMask() {
201         return _multiDOMMask;
202     }
203 
setMultiDOMMask(int mask)204     public void setMultiDOMMask(int mask) {
205         _multiDOMMask = mask;
206     }
207 
getNthDescendant(int type, int n, boolean includeself)208     public DTMAxisIterator getNthDescendant(int type, int n,
209                                             boolean includeself) {
210         return _dom.getNthDescendant(getReverse()[type], n, includeself);
211     }
212 
getNodeValueIterator(DTMAxisIterator iterator, int type, String value, boolean op)213     public DTMAxisIterator getNodeValueIterator(DTMAxisIterator iterator,
214                                                 int type, String value,
215                                                 boolean op) {
216         return _dom.getNodeValueIterator(iterator, type, value, op);
217     }
218 
orderNodes(DTMAxisIterator source, int node)219     public DTMAxisIterator orderNodes(DTMAxisIterator source, int node) {
220         return _dom.orderNodes(source, node);
221     }
222 
getExpandedTypeID(final int node)223     public int getExpandedTypeID(final int node) {
224         final short[] mapping = getMapping();
225         final int type;
226         if (_enhancedDOM != null) {
227             type = mapping[_enhancedDOM.getExpandedTypeID2(node)];
228         }
229         else {
230                 if(null != mapping)
231                 {
232                 type = mapping[_dom.getExpandedTypeID(node)];
233                 }
234                 else
235                 {
236                         type = _dom.getExpandedTypeID(node);
237                 }
238         }
239         return type;
240     }
241 
getNamespaceType(final int node)242     public int getNamespaceType(final int node) {
243         return getNSMapping()[_dom.getNSType(node)];
244     }
245 
getNSType(int node)246     public int getNSType(int node) {
247         return _dom.getNSType(node);
248     }
249 
getParent(final int node)250     public int getParent(final int node) {
251         return _dom.getParent(node);
252     }
253 
getAttributeNode(final int type, final int element)254     public int getAttributeNode(final int type, final int element) {
255         return _dom.getAttributeNode(getReverse()[type], element);
256     }
257 
getNodeName(final int node)258     public String getNodeName(final int node) {
259         if (node == DTM.NULL) {
260             return "";
261         }
262         return _dom.getNodeName(node);
263     }
264 
getNodeNameX(final int node)265     public String getNodeNameX(final int node)
266     {
267         if (node == DTM.NULL) {
268             return "";
269         }
270         return _dom.getNodeNameX(node);
271     }
272 
getNamespaceName(final int node)273     public String getNamespaceName(final int node)
274     {
275         if (node == DTM.NULL) {
276             return "";
277         }
278         return _dom.getNamespaceName(node);
279     }
280 
getStringValueX(final int node)281     public String getStringValueX(final int node)
282     {
283         if (_enhancedDOM != null) {
284             return _enhancedDOM.getStringValueX(node);
285         }
286         else {
287             if (node == DTM.NULL) {
288                 return "";
289             }
290             return _dom.getStringValueX(node);
291         }
292     }
293 
copy(final int node, SerializationHandler handler)294     public void copy(final int node, SerializationHandler handler)
295         throws TransletException
296     {
297         _dom.copy(node, handler);
298     }
299 
copy(DTMAxisIterator nodes,SerializationHandler handler)300     public void copy(DTMAxisIterator nodes,SerializationHandler handler)
301         throws TransletException
302     {
303         _dom.copy(nodes, handler);
304     }
305 
shallowCopy(final int node, SerializationHandler handler)306     public String shallowCopy(final int node, SerializationHandler handler)
307         throws TransletException
308     {
309         if (_enhancedDOM != null) {
310             return _enhancedDOM.shallowCopy(node, handler);
311         }
312         else {
313             return _dom.shallowCopy(node, handler);
314         }
315     }
316 
lessThan(final int node1, final int node2)317     public boolean lessThan(final int node1, final int node2)
318     {
319         return _dom.lessThan(node1, node2);
320     }
321 
characters(final int textNode, SerializationHandler handler)322     public void characters(final int textNode, SerializationHandler handler)
323       throws TransletException
324     {
325         if (_enhancedDOM != null) {
326             _enhancedDOM.characters(textNode, handler);
327         }
328         else {
329             _dom.characters(textNode, handler);
330         }
331     }
332 
makeNode(int index)333     public Node makeNode(int index)
334     {
335         return _dom.makeNode(index);
336     }
337 
makeNode(DTMAxisIterator iter)338     public Node makeNode(DTMAxisIterator iter)
339     {
340         return _dom.makeNode(iter);
341     }
342 
makeNodeList(int index)343     public NodeList makeNodeList(int index)
344     {
345         return _dom.makeNodeList(index);
346     }
347 
makeNodeList(DTMAxisIterator iter)348     public NodeList makeNodeList(DTMAxisIterator iter)
349     {
350         return _dom.makeNodeList(iter);
351     }
352 
getLanguage(int node)353     public String getLanguage(int node)
354     {
355         return _dom.getLanguage(node);
356     }
357 
getSize()358     public int getSize()
359     {
360         return _dom.getSize();
361     }
362 
setDocumentURI(String uri)363     public void setDocumentURI(String uri)
364     {
365         if (_enhancedDOM != null) {
366             _enhancedDOM.setDocumentURI(uri);
367         }
368     }
369 
getDocumentURI()370     public String getDocumentURI()
371     {
372         if (_enhancedDOM != null) {
373             return _enhancedDOM.getDocumentURI();
374         }
375         else {
376             return "";
377         }
378     }
379 
getDocumentURI(int node)380     public String getDocumentURI(int node)
381     {
382         return _dom.getDocumentURI(node);
383     }
384 
getDocument()385     public int getDocument()
386     {
387         return _dom.getDocument();
388     }
389 
isElement(final int node)390     public boolean isElement(final int node)
391     {
392         return(_dom.isElement(node));
393     }
394 
isAttribute(final int node)395     public boolean isAttribute(final int node)
396     {
397         return(_dom.isAttribute(node));
398     }
399 
getNodeIdent(int nodeHandle)400     public int getNodeIdent(int nodeHandle)
401     {
402         return _dom.getNodeIdent(nodeHandle);
403     }
404 
getNodeHandle(int nodeId)405     public int getNodeHandle(int nodeId)
406     {
407         return _dom.getNodeHandle(nodeId);
408     }
409 
410     /**
411      * Return a instance of a DOM class to be used as an RTF
412      */
getResultTreeFrag(int initSize, int rtfType)413     public DOM getResultTreeFrag(int initSize, int rtfType)
414     {
415         if (_enhancedDOM != null) {
416             return _enhancedDOM.getResultTreeFrag(initSize, rtfType);
417         }
418         else {
419             return _dom.getResultTreeFrag(initSize, rtfType);
420         }
421     }
422 
423     /**
424      * Return a instance of a DOM class to be used as an RTF
425      */
getResultTreeFrag(int initSize, int rtfType, boolean addToManager)426     public DOM getResultTreeFrag(int initSize, int rtfType,
427                                  boolean addToManager)
428     {
429         if (_enhancedDOM != null) {
430             return _enhancedDOM.getResultTreeFrag(initSize, rtfType,
431                                                   addToManager);
432         }
433         else {
434             return _dom.getResultTreeFrag(initSize, rtfType, addToManager);
435         }
436     }
437 
438 
439     /**
440      * Returns a SerializationHandler class wrapped in a SAX adapter.
441      */
getOutputDomBuilder()442     public SerializationHandler getOutputDomBuilder()
443     {
444         return _dom.getOutputDomBuilder();
445     }
446 
lookupNamespace(int node, String prefix)447     public String lookupNamespace(int node, String prefix)
448         throws TransletException
449     {
450         return _dom.lookupNamespace(node, prefix);
451     }
452 
getUnparsedEntityURI(String entity)453     public String getUnparsedEntityURI(String entity) {
454         return _dom.getUnparsedEntityURI(entity);
455     }
456 
getElementsWithIDs()457     public Map<String, Integer> getElementsWithIDs() {
458         return _dom.getElementsWithIDs();
459     }
460 
release()461     public void release() {
462         _dom.release();
463     }
464 }
465