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