KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > dom > DOMAdapter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: DOMAdapter.java,v 1.23 2004/02/16 22:54:59 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.dom;
21
22 import org.apache.xalan.xsltc.DOM;
23 import org.apache.xalan.xsltc.DOMEnhancedForDTM;
24 import org.apache.xalan.xsltc.StripFilter;
25 import org.apache.xalan.xsltc.TransletException;
26 import org.apache.xalan.xsltc.runtime.Hashtable;
27 import org.apache.xml.dtm.DTM;
28 import org.apache.xml.dtm.DTMAxisIterator;
29 import org.apache.xml.serializer.SerializationHandler;
30
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
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 JavaDoc[] _namesArray;
46     private String JavaDoc[] _urisArray;
47     private int[] _typesArray;
48     private String JavaDoc[] _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 StripFilter _filter = null;
57
58     private int _multiDOMMask;
59     
60     public DOMAdapter(DOM dom,
61                       String JavaDoc[] namesArray,
62                       String JavaDoc[] urisArray,
63                       int[] typesArray,
64                       String JavaDoc[] namespaceArray) {
65         if (dom instanceof DOMEnhancedForDTM){
66             _enhancedDOM = (DOMEnhancedForDTM) dom;
67         }
68
69         _dom = dom;
70         _namesArray = namesArray;
71         _urisArray = urisArray;
72         _typesArray = typesArray;
73         _namespaceArray = namespaceArray;
74     }
75
76     public void setupMapping(String JavaDoc[] names, String JavaDoc[] urisArray,
77                              int[] typesArray, String JavaDoc[] namespaces) {
78         _namesArray = names;
79         _urisArray = urisArray;
80         _typesArray = typesArray;
81         _namespaceArray = namespaces;
82     }
83     
84     public String JavaDoc[] getNamesArray() {
85         return _namesArray;
86     }
87     
88     public String JavaDoc[] getUrisArray() {
89         return _urisArray;
90     }
91     
92     public int[] getTypesArray() {
93         return _typesArray;
94     }
95     
96     public String JavaDoc[] getNamespaceArray() {
97         return _namespaceArray;
98     }
99     
100     public DOM getDOMImpl() {
101         return _dom;
102     }
103
104     private short[] getMapping() {
105         if (_mapping == null) {
106             if (_enhancedDOM != null) {
107                 _mapping = _enhancedDOM.getMapping(_namesArray, _urisArray,
108                                                    _typesArray);
109             }
110         }
111         return _mapping;
112     }
113
114     private int[] getReverse() {
115     if (_reverse == null) {
116             if (_enhancedDOM != null) {
117             _reverse = _enhancedDOM.getReverseMapping(_namesArray,
118                                                           _urisArray,
119                                                           _typesArray);
120             }
121     }
122     return _reverse;
123     }
124
125     private short[] getNSMapping() {
126     if (_NSmapping == null) {
127             if (_enhancedDOM != null) {
128             _NSmapping = _enhancedDOM.getNamespaceMapping(_namespaceArray);
129             }
130     }
131     return _NSmapping;
132     }
133
134     private short[] getNSReverse() {
135     if (_NSreverse == null) {
136             if (_enhancedDOM != null) {
137             _NSreverse = _enhancedDOM
138                                   .getReverseNamespaceMapping(_namespaceArray);
139             }
140     }
141     return _NSreverse;
142     }
143
144     /**
145       * Returns singleton iterator containg the document root
146       */

147     public DTMAxisIterator getIterator() {
148         return _dom.getIterator();
149     }
150     
151     public String JavaDoc getStringValue() {
152         return _dom.getStringValue();
153     }
154     
155     public DTMAxisIterator getChildren(final int node) {
156         if (_enhancedDOM != null) {
157             return _enhancedDOM.getChildren(node);
158         }
159         else {
160             DTMAxisIterator iterator = _dom.getChildren(node);
161             return iterator.setStartNode(node);
162         }
163     }
164
165     public void setFilter(StripFilter filter) {
166     _filter = filter;
167     }
168
169     public DTMAxisIterator getTypedChildren(final int type) {
170         final int[] reverse = getReverse();
171
172         if (_enhancedDOM != null) {
173             return _enhancedDOM.getTypedChildren(reverse[type]);
174         }
175         else {
176             return _dom.getTypedChildren(type);
177         }
178     }
179
180     public DTMAxisIterator getNamespaceAxisIterator(final int axis,
181                                                     final int ns) {
182         return _dom.getNamespaceAxisIterator(axis, getNSReverse()[ns]);
183     }
184
185     public DTMAxisIterator getAxisIterator(final int axis) {
186         if (_enhancedDOM != null) {
187             return _enhancedDOM.getAxisIterator(axis);
188         }
189         else {
190             return _dom.getAxisIterator(axis);
191         }
192     }
193     
194     public DTMAxisIterator getTypedAxisIterator(final int axis,
195                                                 final int type) {
196         final int[] reverse = getReverse();
197         if (_enhancedDOM != null) {
198             return _enhancedDOM.getTypedAxisIterator(axis, reverse[type]);
199         } else {
200             return _dom.getTypedAxisIterator(axis, type);
201         }
202     }
203         
204     public int getMultiDOMMask() {
205     return _multiDOMMask;
206     }
207
208     public void setMultiDOMMask(int mask) {
209     _multiDOMMask = mask;
210     }
211
212     public DTMAxisIterator getNthDescendant(int type, int n,
213                                             boolean includeself) {
214         return _dom.getNthDescendant(getReverse()[type], n, includeself);
215     }
216
217     public DTMAxisIterator getNodeValueIterator(DTMAxisIterator iterator,
218                                                 int type, String JavaDoc value,
219                                                 boolean op) {
220         return _dom.getNodeValueIterator(iterator, type, value, op);
221     }
222
223     public DTMAxisIterator orderNodes(DTMAxisIterator source, int node) {
224         return _dom.orderNodes(source, node);
225     }
226     
227     public int getExpandedTypeID(final int node) {
228         if (_enhancedDOM != null) {
229             return getMapping()[_enhancedDOM.getExpandedTypeID2(node)];
230         }
231         else {
232             return getMapping()[_dom.getExpandedTypeID(node)];
233         }
234     }
235
236     public int getNamespaceType(final int node) {
237         return getNSMapping()[_dom.getNSType(node)];
238     }
239
240     public int getNSType(int node) {
241     return _dom.getNSType(node);
242     }
243     
244     public int getParent(final int node) {
245         return _dom.getParent(node);
246     }
247
248     public int getAttributeNode(final int type, final int element) {
249     return _dom.getAttributeNode(getReverse()[type], element);
250     }
251     
252     public String JavaDoc getNodeName(final int node) {
253         if (node == DTM.NULL) {
254             return "";
255         }
256         return _dom.getNodeName(node);
257     }
258     
259     public String JavaDoc getNodeNameX(final int node)
260     {
261         if (node == DTM.NULL) {
262             return "";
263         }
264         return _dom.getNodeNameX(node);
265     }
266
267     public String JavaDoc getNamespaceName(final int node)
268     {
269         if (node == DTM.NULL) {
270             return "";
271         }
272         return _dom.getNamespaceName(node);
273     }
274     
275     public String JavaDoc getStringValueX(final int node)
276     {
277         if (_enhancedDOM != null) {
278             return _enhancedDOM.getStringValueX(node);
279         }
280         else {
281             if (node == DTM.NULL) {
282                 return "";
283             }
284             return _dom.getStringValueX(node);
285         }
286     }
287     
288     public void copy(final int node, SerializationHandler handler)
289     throws TransletException
290     {
291         _dom.copy(node, handler);
292     }
293     
294     public void copy(DTMAxisIterator nodes,SerializationHandler handler)
295     throws TransletException
296     {
297     _dom.copy(nodes, handler);
298     }
299
300     public String JavaDoc shallowCopy(final int node, SerializationHandler handler)
301     throws TransletException
302     {
303         if (_enhancedDOM != null) {
304             return _enhancedDOM.shallowCopy(node, handler);
305         }
306         else {
307             return _dom.shallowCopy(node, handler);
308         }
309     }
310     
311     public boolean lessThan(final int node1, final int node2)
312     {
313         return _dom.lessThan(node1, node2);
314     }
315     
316     public void characters(final int textNode, SerializationHandler handler)
317       throws TransletException
318     {
319         if (_enhancedDOM != null) {
320             _enhancedDOM.characters(textNode, handler);
321         }
322         else {
323             _dom.characters(textNode, handler);
324         }
325     }
326
327     public Node JavaDoc makeNode(int index)
328     {
329         return _dom.makeNode(index);
330     }
331
332     public Node JavaDoc makeNode(DTMAxisIterator iter)
333     {
334         return _dom.makeNode(iter);
335     }
336
337     public NodeList JavaDoc makeNodeList(int index)
338     {
339         return _dom.makeNodeList(index);
340     }
341
342     public NodeList JavaDoc makeNodeList(DTMAxisIterator iter)
343     {
344         return _dom.makeNodeList(iter);
345     }
346
347     public String JavaDoc getLanguage(int node)
348     {
349         return _dom.getLanguage(node);
350     }
351
352     public int getSize()
353     {
354         return _dom.getSize();
355     }
356
357     public void setDocumentURI(String JavaDoc uri)
358     {
359         if (_enhancedDOM != null) {
360             _enhancedDOM.setDocumentURI(uri);
361         }
362     }
363
364     public String JavaDoc getDocumentURI()
365     {
366         if (_enhancedDOM != null) {
367             return _enhancedDOM.getDocumentURI();
368         }
369         else {
370             return "";
371         }
372     }
373
374     public String JavaDoc getDocumentURI(int node)
375     {
376         return _dom.getDocumentURI(node);
377     }
378
379     public int getDocument()
380     {
381         return _dom.getDocument();
382     }
383
384     public boolean isElement(final int node)
385     {
386         return(_dom.isElement(node));
387     }
388
389     public boolean isAttribute(final int node)
390     {
391         return(_dom.isAttribute(node));
392     }
393     
394     public int getNodeIdent(int nodeHandle)
395     {
396         return _dom.getNodeIdent(nodeHandle);
397     }
398     
399     public int getNodeHandle(int nodeId)
400     {
401         return _dom.getNodeHandle(nodeId);
402     }
403     
404     /**
405      * Return a instance of a DOM class to be used as an RTF
406      */

407     public DOM getResultTreeFrag(int initSize, int rtfType)
408     {
409         if (_enhancedDOM != null) {
410             return _enhancedDOM.getResultTreeFrag(initSize, rtfType);
411         }
412         else {
413             return _dom.getResultTreeFrag(initSize, rtfType);
414         }
415     }
416     
417     /**
418      * Return a instance of a DOM class to be used as an RTF
419      */

420     public DOM getResultTreeFrag(int initSize, int rtfType,
421                                  boolean addToManager)
422     {
423         if (_enhancedDOM != null) {
424             return _enhancedDOM.getResultTreeFrag(initSize, rtfType,
425                                                   addToManager);
426         }
427         else {
428         return _dom.getResultTreeFrag(initSize, rtfType, addToManager);
429     }
430     }
431   
432     
433     /**
434      * Returns a SerializationHandler class wrapped in a SAX adapter.
435      */

436     public SerializationHandler getOutputDomBuilder()
437     {
438         return _dom.getOutputDomBuilder();
439     }
440
441     public String JavaDoc lookupNamespace(int node, String JavaDoc prefix)
442     throws TransletException
443     {
444     return _dom.lookupNamespace(node, prefix);
445     }
446
447     public String JavaDoc getUnparsedEntityURI(String JavaDoc entity) {
448         return _dom.getUnparsedEntityURI(entity);
449     }
450
451     public Hashtable getElementsWithIDs() {
452         return _dom.getElementsWithIDs();
453     }
454 }
455
Popular Tags