• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id$
20  */
21 package org.apache.qetest.dtm;
22 
23 import java.io.StringReader;
24 
25 import javax.xml.transform.Source;
26 import javax.xml.transform.stream.StreamSource;
27 
28 import org.apache.xml.dtm.Axis;
29 import org.apache.xml.dtm.DTM;
30 import org.apache.xml.dtm.DTMAxisTraverser;
31 import org.apache.xml.dtm.DTMManager;
32 import org.apache.xml.dtm.ref.DTMManagerDefault;
33 import org.apache.xpath.objects.XMLStringFactoryImpl;
34 
35 
36 /**
37  * Unit test for DTMManager/DTM
38  *
39  * Loads an XML document from a file (or, if no filename is supplied,
40  * an internal string), then dumps its contents. Replaces the old
41  * version, which was specific to the ultra-compressed implementation.
42  * (Which, by the way, we probably ought to revisit as part of our ongoing
43  * speed/size performance evaluation.)
44  *
45  * %REVIEW% Extend to test DOM2DTM, incremental, DOM view of the DTM,
46  * whitespace-filtered, indexed/nonindexed, ...
47  * */
48 public class TestDTMTraverser {
49 
50 /*class myWSStripper implements DTMWSFilter {
51 
52 void myWWStripper()
53   { }
54 
55 public short getShouldStripSpace(int elementHandle, DTM dtm)
56   {
57  	return DTMWSFilter.STRIP;
58   }
59 
60 }
61 */
62 static final String[] TYPENAME=
63   { "NULL",
64     "ELEMENT",
65     "ATTRIBUTE",
66     "TEXT",
67     "CDATA_SECTION",
68     "ENTITY_REFERENCE",
69     "ENTITY",
70     "PROCESSING_INSTRUCTION",
71     "COMMENT",
72     "DOCUMENT",
73     "DOCUMENT_TYPE",
74     "DOCUMENT_FRAGMENT",
75     "NOTATION",
76     "NAMESPACE"
77   };
78 
main(String argv[])79   public static void main(String argv[])
80   {
81   	System.out.println("\nHELLO THERE AND WELCOME TO THE WACKY WORLD OF TRAVERSERS \n");
82     try
83     {
84 		// Pick our input source
85 		Source source=null;
86 		if(argv.length<1)
87 		{
88 			String defaultSource=
89  		"<?xml version=\"1.0\"?>\n"+
90  		"<Document xmlns:d=\"www.d.com\" a1=\"hello\" a2=\"goodbye\">"+
91  		"<!-- Default test document -->"+
92  		"<?api a1=\"yes\" a2=\"no\"?>"+
93  		"<A><!-- A Subtree --><B><C><D><E><F xmlns:f=\"www.f.com\" a1=\"down\" a2=\"up\"/></E></D></C></B></A>"+
94  		"<Aa/><Ab/><Ac><Ac1/></Ac>"+
95  		"<Ad xmlns:Ad=\"www.Ad.com\" xmlns:y=\"www.y.com\" xmlns:z=\"www.z.com\">"+
96  		"<Ad1/></Ad>"+
97  		"</Document>";
98 
99 			source=new StreamSource(new StringReader(defaultSource));
100 		}
101 		else if (argv.length>1 &&  "X".equalsIgnoreCase(argv[1]))
102 		{
103 			// XNI stream startup goes here
104 			// Remember to perform Schema validation, to obtain PSVI annotations
105 		}
106 		else
107 		{
108 			// Read from a URI via whatever mechanism the DTMManager prefers
109 			source=new StreamSource(argv[0]);
110 		}
111 
112       // Get a DTM manager, and ask it to load the DTM "uniquely",
113       // with no whitespace filtering, nonincremental, but _with_
114       // indexing (a fairly common case, and avoids the special
115       // mode used for RTF DTMs).
116 
117 	  // For testing with some of David Marston's files I do want to strip whitespace.
118 	  dtmWSStripper stripper = new dtmWSStripper();
119 
120       DTMManager manager= new DTMManagerDefault().newInstance(new XMLStringFactoryImpl());
121       DTM dtm=manager.getDTM(source, true, stripper, false, true);
122 
123 	  // Get various nodes to use as context nodes.
124 	  int dtmRoot = dtm.getDocument();					// #document
125 	  String dtmRootName = dtm.getNodeName(dtmRoot);	// Used for output
126 	  int DNode = dtm.getFirstChild(dtmRoot);			// <Document>
127 	  String DNodeName = dtm.getNodeName(DNode);
128 	  int CNode = dtm.getFirstChild(DNode);				// <Comment>
129 	  int PINode = dtm.getNextSibling(CNode);			// <PI>
130 	  int ANode = dtm.getNextSibling(PINode);			// <A>
131 	  String ANodeName = dtm.getNodeName(ANode);
132 	  int lastNode = 0;
133 
134 
135 	  // Get a traverser for Child:: axis.
136 	  System.out.println("\n#### CHILD from "+"<"+DNodeName+">");
137       DTMAxisTraverser at = dtm.getAxisTraverser(Axis.CHILD);
138 
139 	  // Traverse the axis and print out node info.
140       for (int atNode = at.first(DNode); DTM.NULL != atNode;
141               atNode = at.next(DNode, atNode))
142 		{  printNode(dtm, atNode, " ");
143 		   lastNode = atNode;
144 		}
145 
146 	  // Get a traverser for Parent:: axis.
147 	  String lastNodeName = dtm.getNodeName(lastNode);
148 	  System.out.println("\n#### PARENT from "+"<"+lastNodeName+">");
149       at = dtm.getAxisTraverser(Axis.PARENT);
150 
151 	  // Traverse the axis and print out node info.
152       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
153               atNode = at.next(lastNode, atNode))
154 		  printNode(dtm, atNode, " ");
155 
156 	  // Get a from Self:: axis.
157 	  System.out.println("\n#### SELF from "+"<"+lastNodeName+">");
158       at = dtm.getAxisTraverser(Axis.SELF);
159 
160 	  // Traverse the axis and print out node info.
161       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
162               atNode = at.next(lastNode, atNode))
163 		  printNode(dtm, atNode, " ");
164 
165 	  // Get a traverser for NameSpaceDecls:: axis.
166 	  System.out.println("\n#### NAMESPACEDECLS from "+"<"+lastNodeName+">");
167       at = dtm.getAxisTraverser(Axis.NAMESPACEDECLS);
168 
169 	  // Traverse the axis and print out node info.
170       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
171               atNode = at.next(lastNode, atNode))
172 		  printNode(dtm, atNode, " ");
173 
174 	  // Get a traverser for Namespace:: axis.
175 	  System.out.println("\n#### NAMESPACE from "+"<"+lastNodeName+">");
176       at = dtm.getAxisTraverser(Axis.NAMESPACE);
177 
178 	  // Traverse the axis and print out node info.
179       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
180               atNode = at.next(lastNode, atNode))
181 		  printNode(dtm, atNode, " ");
182 
183 	  // Get a traverser for Preceding:: axis.
184 	  System.out.println("\n#### PRECEDING from "+"<"+lastNodeName+">");
185       at = dtm.getAxisTraverser(Axis.PRECEDING);
186 
187 	  // Traverse the axis and print out node info.
188       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
189               atNode = at.next(lastNode, atNode))
190 		printNode(dtm, atNode, " ");
191 
192 	  // Get a traverser for PRECEDING-SIBLING:: axis.
193 	  System.out.println("\n#### PRECEDINGSIBLING from "+"<"+lastNodeName+">");
194       at = dtm.getAxisTraverser(Axis.PRECEDINGSIBLING);
195 
196 	  // Traverse the axis and print out node info.
197       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
198               atNode = at.next(lastNode, atNode))
199 		printNode(dtm, atNode, " ");
200 
201 	  // Get a traverser for PRECEDINGANDANCESTOR:: axis.
202 	  System.out.println("\n#### PRECEDINGANDANCESTOR from "+"<"+lastNodeName+">");
203       at = dtm.getAxisTraverser(Axis.PRECEDINGANDANCESTOR);
204 
205 	  // Traverse the axis and print out node info.
206       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
207               atNode = at.next(lastNode, atNode))
208 		printNode(dtm, atNode, " ");
209 
210 	  // Get a traverser for Attribute:: axis.
211 	  System.out.println("\n#### ATTRIBUTE from "+"<"+DNodeName+">");
212       at = dtm.getAxisTraverser(Axis.ATTRIBUTE);
213 
214 	  // Traverse the axis and print out node info.
215       for (int atNode = at.first(DNode); DTM.NULL != atNode;
216               atNode = at.next(DNode, atNode))
217 		printNode(dtm, atNode, " ");
218 
219 	  // Get a traverser for Following:: axis.
220 	  System.out.println("\n#### FOLLOWING from "+"<"+ANodeName+">");
221       at = dtm.getAxisTraverser(Axis.FOLLOWING);
222 
223 	  // Traverse the axis and print out node info.
224       for (int atNode = at.first(ANode); DTM.NULL != atNode;
225               atNode = at.next(ANode, atNode))
226 		printNode(dtm, atNode, " ");
227 
228 	  // Get a traverser for FollowingSibling:: axis.
229 	  System.out.println("\n#### FOLLOWINGSIBLING from "+"<"+ANodeName+">");
230       at = dtm.getAxisTraverser(Axis.FOLLOWINGSIBLING);
231 
232 	  // Traverse the axis and print out node info.
233       for (int atNode = at.first(ANode); DTM.NULL != atNode;
234               atNode = at.next(ANode, atNode))
235 		printNode(dtm, atNode, " ");
236 
237 	  // Get a traverser for  DESCENDANT:: axis.
238 	  System.out.println("\n#### DESCENDANT from "+"<"+ANodeName+">");
239       at = dtm.getAxisTraverser(Axis.DESCENDANT);
240 
241 	  // Traverse the axis and print out node info.
242 	  for (int atNode = at.first(ANode); DTM.NULL != atNode;
243               atNode = at.next(ANode, atNode))
244 		  printNode(dtm, atNode, " ");
245 
246 
247 	  // Get a traverser for  DESCENDANTORSELF:: axis.
248 	  System.out.println("\n#### DESCENDANT-OR-SELF from "+"<"+ANodeName+">");
249       at = dtm.getAxisTraverser(Axis.DESCENDANTORSELF);
250 
251 	  // Traverse the axis and print out node info.
252 	  for (int atNode = at.first(ANode); DTM.NULL != atNode;
253               atNode = at.next(ANode, atNode))
254 		{
255 			printNode(dtm, atNode, " ");
256 			lastNode = atNode;
257 		}
258 
259 	  // Get a traverser for ANCESTOR:: axis.
260 	  lastNodeName = dtm.getNodeName(lastNode);
261 	  System.out.println("\n#### ANCESTOR from "+"<"+lastNodeName+">");
262       at = dtm.getAxisTraverser(Axis.ANCESTOR);
263 
264 	  // Traverse the axis and print out node info.
265       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
266               atNode = at.next(lastNode, atNode))
267 		printNode(dtm, atNode, " ");
268 
269 	  // Get a traverser for ANCESTORORSELF:: axis.
270 	  System.out.println("\n#### ANCESTOR-OR-SELF from "+"<"+lastNodeName+">");
271       at = dtm.getAxisTraverser(Axis.ANCESTORORSELF);
272 
273 	  // Traverse the axis and print out node info.
274       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
275               atNode = at.next(lastNode, atNode))
276 		printNode(dtm, atNode, " ");
277 
278 	  // Get a traverser for ALLFROMNODE:: axis.
279 	  System.out.println("\n#### ALL-FROM-NODE from "+"<"+lastNodeName+">");
280       at = dtm.getAxisTraverser(Axis.ALLFROMNODE);
281 
282 	  // Traverse the axis and print out node info.
283       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
284               atNode = at.next(lastNode, atNode))
285 		printNode(dtm, atNode, " ");
286 
287 	  // ABSOLUTE AXIS TESTS
288 	  // These next axis are all Absolute. They all default to the root of the dtm
289 	  // tree,  regardless of what we call first() with.
290 	  // Get a traverser for ALL:: axis.
291 	  System.out.println("\n#### ALL(absolute) from "+"<"+dtmRootName+">");
292       at = dtm.getAxisTraverser(Axis.ALL);
293 
294 	  // Traverse the axis and print out node info.
295       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
296               atNode = at.next(lastNode, atNode))
297 		printNode(dtm, atNode, " ");
298 
299 	  // Get a traverser for DESCENDANTSFROMROOT:: axis.
300 	  System.out.println("\n#### DESCENDANTSFROMROOT(absolute) from "+"<"+dtmRootName+">");
301       at = dtm.getAxisTraverser(Axis.DESCENDANTSFROMROOT);
302 
303 	  // Traverse the axis and print out node info.
304       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
305               atNode = at.next(lastNode, atNode))
306 		printNode(dtm, atNode, " ");
307 
308 	  // Get a traverser for DESCENDANTSORSELFFROMROOT:: axis.
309 	  System.out.println("\n#### DESCENDANTSORSELFFROMROOT(absolute) from "+"<"+dtmRootName+">");
310       at = dtm.getAxisTraverser(Axis.DESCENDANTSORSELFFROMROOT);
311 
312 	  // Traverse the axis and print out node info.
313       for (int atNode = at.first(lastNode); DTM.NULL != atNode;
314               atNode = at.next(lastNode, atNode))
315 		printNode(dtm, atNode, " ");
316 
317     }
318     catch(Exception e)
319       {
320         e.printStackTrace();
321       }
322   }
323 
printNode(DTM dtm,int nodeHandle,String indent)324   static void printNode(DTM dtm,int nodeHandle,String indent)
325   {
326     // Briefly display this node
327     // Don't bother displaying namespaces or attrs; we do that at the
328     // next level up.
329     // %REVIEW% Add namespace info, type info, ...
330 
331     // Formatting hack -- suppress quotes when value is null, to distinguish
332     // it from "null".
333     String value=dtm.getNodeValue(nodeHandle);
334     String vq=(value==null) ? "" : "\"";
335 
336     // Skip outputing of text nodes. In most cases they clutter the output,
337 	// besides I'm only interested in the elemental structure of the dtm.
338     if( TYPENAME[dtm.getNodeType(nodeHandle)] != "TEXT" )
339 	{
340     	System.out.println(indent+
341 		       +nodeHandle+": "+
342 		       TYPENAME[dtm.getNodeType(nodeHandle)]+" "+
343 			   dtm.getNodeName(nodeHandle)+" "+
344 			   " Level=" + dtm.getLevel(nodeHandle)+" "+
345 		       "\tValue=" + vq + value + vq
346 		       );
347 	}
348   }
349 
350 }
351