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.File;
24 import java.io.FileOutputStream;
25 import java.io.StringReader;
26 import java.util.Properties;
27
28 import javax.xml.transform.Source;
29 import javax.xml.transform.stream.StreamSource;
30
31 import org.apache.qetest.FileBasedTest;
32 import org.apache.qetest.LinebyLineCheckService;
33 import org.apache.qetest.OutputNameManager;
34 import org.apache.qetest.xsl.XSLTestfileInfo;
35 import org.apache.xml.dtm.Axis;
36 import org.apache.xml.dtm.DTM;
37 import org.apache.xml.dtm.DTMAxisIterator;
38 import org.apache.xml.dtm.DTMManager;
39 import org.apache.xml.dtm.ref.DTMManagerDefault;
40 import org.apache.xpath.objects.XMLStringFactoryImpl;
41
42 //-------------------------------------------------------------------------
43
44 /**
45 * This test creates a DTM and then walks it with axisIterators
46 * for each axis within XPATH
47 * - execute 'build package.trax', 'traxapitest TestDTMIter.java'
48 * - a bunch of convenience variables/initializers are included,
49 * use or delete as is useful
50 * @author Paul Dick
51 * @version $Id$
52 */
53 public class TestDTMIter extends FileBasedTest
54 {
55 /**
56 * Provides nextName(), currentName() functionality for tests
57 * that may produce any number of output files.
58 */
59 protected OutputNameManager outNames;
60
61 /**
62 * Information about an xsl/xml file pair for transforming.
63 * Public members include inputName (for xsl); xmlName; goldName; etc.
64 * If you don't use an .xml file on disk, you don't actually need this.
65 */
66 protected XSLTestfileInfo testFileInfo = new XSLTestfileInfo();
67
68 /** Subdirectory under test\tests\api for our xsl/xml files. */
69 public static final String DTM_SUBDIR = "dtm";
70 public static final String ITER_Prefix = "Iter_";
71
72 public static final String defaultSource=
73 "<?xml version=\"1.0\"?>\n"+
74 "<Document xmlns:d=\"www.d.com\" a1=\"hello\" a2=\"goodbye\">"+
75 "<!-- Default test document -->"+
76 "<?api a1=\"yes\" a2=\"no\"?>"+
77 "<A><!-- A Subtree --><B><C><D><E><F xmlns:f=\"www.f.com\" a1=\"down\" a2=\"up\"/></E></D></C></B></A>"+
78 "<Aa/><Ab/><Ac><Ac1/></Ac>"+
79 "<Ad xmlns:Ad=\"www.Ad.com\" xmlns:y=\"www.y.com\" xmlns:z=\"www.z.com\">"+
80 "<Ad1/></Ad>"+
81 "</Document>";
82
83 static final String[] TYPENAME=
84 { "NULL",
85 "ELEMENT",
86 "ATTRIBUTE",
87 "TEXT",
88 "CDATA_SECTION",
89 "ENTITY_REFERENCE",
90 "ENTITY",
91 "PROCESSING_INSTRUCTION",
92 "COMMENT",
93 "DOCUMENT",
94 "DOCUMENT_TYPE",
95 "DOCUMENT_FRAGMENT",
96 "NOTATION",
97 "NAMESPACE"
98 };
99
100 private int lastNode = 0; // Set by first axis, used by subsequent axis.
101 private String lastName;
102
103 private int lastNode2 = 0; // Set by DESCENDANTORSELF, used in 11 & 12
104 private String lastName2;
105
106 private int ANode = 0; // Used in testcase 7 - 10
107 private String ANodeName;
108
109 private static dtmWSStripper stripper = new dtmWSStripper();
110
111 /** Just initialize test name, comment, numTestCases. */
TestDTMIter()112 public TestDTMIter()
113 {
114 numTestCases = 12;
115 testName = "TestDTMIter";
116 testComment = "Function test of DTM iterators";
117 }
118
119 /**
120 * Initialize this test - Set names of xml/xsl test files,
121 * REPLACE_other_test_file_init.
122 *
123 * @param p Properties to initialize from (if needed)
124 * @return false if we should abort the test; true otherwise
125 */
doTestFileInit(Properties p)126 public boolean doTestFileInit(Properties p)
127 {
128 // Used for all tests; just dump files in dtm subdir
129 File outSubDir = new File(outputDir + File.separator + DTM_SUBDIR);
130 if (!outSubDir.mkdirs())
131 reporter.logWarningMsg("Could not create output dir: " + outSubDir);
132
133 // Initialize an output name manager to that dir with .out extension
134 outNames = new OutputNameManager(outputDir + File.separator + DTM_SUBDIR
135 + File.separator + testName, ".out");
136
137 String testBasePath = inputDir
138 + File.separator
139 + DTM_SUBDIR
140 + File.separator;
141 String goldBasePath = goldDir
142 + File.separator
143 + DTM_SUBDIR
144 + File.separator
145 + ITER_Prefix;
146
147 //testFileInfo.inputName = testBasePath + "REPLACE_xslxml_filename.xsl";
148 //testFileInfo.xmlName = testBasePath + "REPLACE_xslxml_filename.xml";
149 testFileInfo.goldName = goldBasePath;
150
151 return true;
152 }
153
154 /**
155 * Cleanup this test - REPLACE_other_test_file_cleanup.
156 *
157 * @param p Properties to initialize from (if needed)
158 * @return false if we should abort the test; true otherwise
159 */
doTestFileClose(Properties p)160 public boolean doTestFileClose(Properties p)
161 {
162 // Often will be a no-op
163 return true;
164 }
165
166 /**
167 * Create AxisIterator and walk CHILD axis.
168 * @return false if we should abort the test; true otherwise
169 */
testCase1()170 public boolean testCase1()
171 {
172 reporter.testCaseInit("Walk CHILD AxisIterator");
173 StringBuffer buf = new StringBuffer();
174 FileOutputStream fos = openFileStream(outNames.nextName());
175 String gold = testFileInfo.goldName + "testcase1.out";
176
177 // Create dtm and generate initial context
178 DTM dtm = generateDTM();
179
180 // Get various nodes to use as context nodes.
181 int dtmRoot = dtm.getDocument(); // #document
182 String dtmRootName = dtm.getNodeName(dtmRoot); // Used for output
183 int DNode = dtm.getFirstChild(dtmRoot); // <Document>
184 String DNodeName = dtm.getNodeName(DNode);
185 int CNode = dtm.getFirstChild(DNode); // <Comment>
186 int PINode = dtm.getNextSibling(CNode); // <PI>
187 ANode = dtm.getNextSibling(PINode); // <A>, used in testcase 7 - 10
188 ANodeName = dtm.getNodeName(ANode);
189
190
191 // Get a Iterator for CHILD:: axis and query it's direction.
192 DTMAxisIterator iter = dtm.getAxisIterator(Axis.CHILD);
193 iter.setStartNode(DNode);
194 buf.append("#### CHILD from "+DNodeName+", Reverse Axis:" + iter.isReverse() + "\n");
195
196 // Iterate the axis and write node info to output file
197 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
198 {
199 buf.append(getNodeInfo(dtm, itNode, " "));
200 lastNode = itNode; // Setting this GLOBAL IS BAD, but easy. Investigate!!
201 }
202 lastName = dtm.getNodeName(lastNode);
203
204 // Write results and close output file.
205 writeClose(fos, buf);
206
207 // Verify results
208 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
209 myfilechecker.check(reporter, new File(outNames.currentName()),
210 new File(gold),
211 "Testcase1");
212 reporter.testCaseClose();
213 return true;
214 }
215
216 /**
217 * Create AxisIterator and walk PARENT axis.
218 * @return false if we should abort the test; true otherwise
219 */
testCase2()220 public boolean testCase2()
221 {
222 reporter.testCaseInit("Walk PARENT AxisIterator");
223 StringBuffer buf = new StringBuffer();
224 FileOutputStream fos = openFileStream(outNames.nextName());
225 String gold = testFileInfo.goldName + "testcase2.out";
226
227 // Create dtm and generate initial context
228 DTM dtm = generateDTM();
229
230 // Get a Iterator for PARENT:: axis.
231 DTMAxisIterator iter = dtm.getAxisIterator(Axis.PARENT);
232 iter.setStartNode(lastNode);
233
234 // Print out info about the axis
235 buf.append("#### PARENT from "+lastName+", Reverse Axis:" + iter.isReverse() + "\n");
236
237 // Iterate the axis and write node info to output file
238 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
239 buf.append(getNodeInfo(dtm, itNode, " "));
240
241 // Write results and close output file.
242 writeClose(fos, buf);
243
244 // Verify results
245 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
246 myfilechecker.check(reporter, new File(outNames.currentName()),
247 new File(gold),
248 "Testcase2");
249
250 reporter.testCaseClose();
251 return true;
252 }
253
254 /**
255 * Create AxisIterator and walk SELF axis.
256 * @return false if we should abort the test; true otherwise
257 */
testCase3()258 public boolean testCase3()
259 {
260 reporter.testCaseInit("Walk SELF AxisIterator");
261 StringBuffer buf = new StringBuffer();
262 FileOutputStream fos = openFileStream(outNames.nextName());
263 String gold = testFileInfo.goldName + "testcase3.out";
264
265 // Create dtm and generate initial context
266 DTM dtm = generateDTM();
267
268 // Get a Iterator for CHILD:: axis.
269 DTMAxisIterator iter = dtm.getAxisIterator(Axis.SELF);
270 iter.setStartNode(lastNode);
271
272 // Print out info about the axis
273 buf.append("#### SELF from "+lastName+", Reverse Axis:" + iter.isReverse() + "\n");
274
275 // Iterate the axis and write node info to output file
276 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
277 buf.append(getNodeInfo(dtm, itNode, " "));
278
279 // Write results and close output file.
280 writeClose(fos, buf);
281
282 // Verify results
283 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
284 myfilechecker.check(reporter, new File(outNames.currentName()),
285 new File(gold),
286 "Testcase3");
287
288 reporter.testCaseClose();
289 return true;
290 }
291
292 /**
293 * Create AxisIterator and walk NAMESPACE axis.
294 * @return false if we should abort the test; true otherwise
295 */
testCase4()296 public boolean testCase4()
297 {
298 reporter.testCaseInit("Walk NAMESPACE AxisIterator");
299 StringBuffer buf = new StringBuffer();
300 FileOutputStream fos = openFileStream(outNames.nextName());
301 String gold = testFileInfo.goldName + "testcase4.out";
302
303 // Create dtm and generate initial context
304 DTM dtm = generateDTM();
305
306 // Get a Iterator for NAMESPACE:: axis.
307 DTMAxisIterator iter = dtm.getAxisIterator(Axis.NAMESPACE);
308 iter.setStartNode(lastNode);
309
310 // Print out info about the axis
311 buf.append("#### NAMESPACE from "+lastName+", Reverse Axis:" + iter.isReverse() + "\n");
312
313 // Iterate the axis and write node info to output file
314 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
315 buf.append(getNodeInfo(dtm, itNode, " "));
316
317 // Write results and close output file.
318 writeClose(fos, buf);
319
320 // Verify results
321 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
322 myfilechecker.check(reporter, new File(outNames.currentName()),
323 new File(gold),
324 "Testcase4");
325
326 reporter.testCaseClose();
327 return true;
328 }
329
330 /**
331 * Create AxisIterator and walk PRECEDING axis.
332 * @return false if we should abort the test; true otherwise
333 */
testCase5()334 public boolean testCase5()
335 {
336 reporter.testCaseInit("Walk PRECEDING AxisIterator");
337 StringBuffer buf = new StringBuffer();
338 FileOutputStream fos = openFileStream(outNames.nextName());
339 String gold = testFileInfo.goldName + "testcase5.out";
340
341 // Create dtm and generate initial context
342 DTM dtm = generateDTM();
343
344 // Get a Iterator for PRECEDING:: axis.
345 DTMAxisIterator iter = dtm.getAxisIterator(Axis.PRECEDING);
346 iter.setStartNode(lastNode);
347
348 // Print out info about the axis
349 buf.append("#### PRECEDING from "+lastName+", Reverse Axis:" + iter.isReverse() + "\n");
350
351 // Iterate the axis and write node info to output file
352 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
353 buf.append(getNodeInfo(dtm, itNode, " "));
354
355 // Write results and close output file.
356 writeClose(fos, buf);
357
358
359 // Verify results
360 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
361 myfilechecker.check(reporter, new File(outNames.currentName()),
362 new File(gold),
363 "Testcase5");
364
365 reporter.testCaseClose();
366 return true;
367 }
368
369 /**
370 * Create AxisIterator and walk PRECEDINGSIBLING axis.
371 * @return false if we should abort the test; true otherwise
372 */
testCase6()373 public boolean testCase6()
374 {
375 reporter.testCaseInit("Walk PRECEDINGSIBLING AxisIterator");
376 StringBuffer buf = new StringBuffer();
377 FileOutputStream fos = openFileStream(outNames.nextName());
378 String gold = testFileInfo.goldName + "testcase6.out";
379
380 // Create dtm and generate initial context
381 DTM dtm = generateDTM();
382
383 // Get a Iterator for PRECEDINGSIBLING:: axis.
384 DTMAxisIterator iter = dtm.getAxisIterator(Axis.PRECEDINGSIBLING);
385 iter.setStartNode(lastNode);
386
387 // Print out info about the axis
388 buf.append("#### PRECEDINGSIBLING from "+lastName+", Reverse Axis:" + iter.isReverse() + "\n");
389
390 // Iterate the axis and write node info to output file
391 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
392 buf.append(getNodeInfo(dtm, itNode, " "));
393
394 // Write results and close output file.
395 writeClose(fos, buf);
396
397 // Verify results
398 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
399 myfilechecker.check(reporter, new File(outNames.currentName()),
400 new File(gold),
401 "Testcase6");
402 reporter.testCaseClose();
403 return true;
404 }
405
406 /**
407 * Create AxisIterator and walk FOLLOWING axis.
408 * @return false if we should abort the test; true otherwise
409 */
testCase7()410 public boolean testCase7()
411 {
412 reporter.testCaseInit("Walk FOLLOWING AxisIterator");
413 StringBuffer buf = new StringBuffer();
414 FileOutputStream fos = openFileStream(outNames.nextName());
415 String gold = testFileInfo.goldName + "testcase7.out";
416
417 // Create dtm and generate initial context
418 DTM dtm = generateDTM();
419
420 // Get a Iterator for FOLLOWING:: axis.
421 DTMAxisIterator iter = dtm.getAxisIterator(Axis.FOLLOWING);
422 iter.setStartNode(ANode);
423
424 // Print out info about the axis
425 buf.append("#### FOLLOWING from "+ANodeName+", Reverse Axis:" + iter.isReverse() + "\n");
426
427 // Iterate the axis and write node info to output file
428 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
429 buf.append(getNodeInfo(dtm, itNode, " "));
430
431 // Write results and close output file.
432 writeClose(fos, buf);
433
434 // Verify results
435 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
436 myfilechecker.check(reporter, new File(outNames.currentName()),
437 new File(gold),
438 "Testcase7");
439 reporter.testCaseClose();
440 return true;
441 }
442
443 /**
444 * Create AxisIterator and walk FOLLOWINGSIBLING axis.
445 * @return false if we should abort the test; true otherwise
446 */
testCase8()447 public boolean testCase8()
448 {
449 reporter.testCaseInit("Walk FOLLOWINGSIBLING AxisIterator");
450 StringBuffer buf = new StringBuffer();
451 FileOutputStream fos = openFileStream(outNames.nextName());
452 String gold = testFileInfo.goldName + "testcase8.out";
453
454 // Create dtm and generate initial context
455 DTM dtm = generateDTM();
456
457 // Get a Iterator for FOLLOWINGSIBLING:: axis.
458 DTMAxisIterator iter = dtm.getAxisIterator(Axis.FOLLOWINGSIBLING);
459 iter.setStartNode(ANode);
460
461 // Print out info about the axis
462 buf.append("#### FOLLOWINGSIBLING from "+ANodeName+", Reverse Axis:" + iter.isReverse() + "\n");
463
464 // Iterate the axis and write node info to output file
465 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
466 { buf.append(getNodeInfo(dtm, itNode, " "));
467 //lastNode = itNode;
468 }
469
470 // Write results and close output file.
471 writeClose(fos, buf);
472
473 // Verify results
474 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
475 myfilechecker.check(reporter, new File(outNames.currentName()),
476 new File(gold),
477 "Testcase8");
478 reporter.testCaseClose();
479 return true;
480 }
481
482
483 /**
484 * Create AxisIterator and walk DESCENDANT axis.
485 * @return false if we should abort the test; true otherwise
486 */
testCase9()487 public boolean testCase9()
488 {
489 reporter.testCaseInit("Walk DESCENDANT AxisIterator");
490 StringBuffer buf = new StringBuffer();
491 FileOutputStream fos = openFileStream(outNames.nextName());
492 String gold = testFileInfo.goldName + "testcase9.out";
493
494 // Create dtm and generate initial context
495 DTM dtm = generateDTM();
496
497 // Get a Iterator for DESCENDANT:: axis.
498 DTMAxisIterator iter = dtm.getAxisIterator(Axis.DESCENDANT);
499 iter.setStartNode(ANode);
500
501 // Print out info about the axis
502 buf.append("#### DESCENDANT from "+ANodeName+", Reverse Axis:" + iter.isReverse() + "\n");
503
504 // Iterate the axis and write node info to output file
505 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
506 buf.append(getNodeInfo(dtm, itNode, " "));
507
508 // Write results and close output file.
509 writeClose(fos, buf);
510
511 // Verify results
512 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
513 myfilechecker.check(reporter, new File(outNames.currentName()),
514 new File(gold),
515 "Testcase9");
516 reporter.testCaseClose();
517 return true;
518 }
519
520 /**
521 * Create AxisIterator and walk DESCENDANTORSELF axis.
522 * @return false if we should abort the test; true otherwise
523 */
testCase10()524 public boolean testCase10()
525 {
526 reporter.testCaseInit("Walk DESCENDANTORSELF AxisIterator");
527 StringBuffer buf = new StringBuffer();
528 FileOutputStream fos = openFileStream(outNames.nextName());
529 String gold = testFileInfo.goldName + "testcase10.out";
530
531 // Create dtm and generate initial context
532 DTM dtm = generateDTM();
533
534 // Get a Iterator for DESCENDANTORSELF:: axis.
535 DTMAxisIterator iter = dtm.getAxisIterator(Axis.DESCENDANTORSELF);
536 iter.setStartNode(ANode);
537
538 // Print out info about the axis
539 buf.append("#### DESCENDANTORSELF from "+ANodeName+", Reverse Axis:" + iter.isReverse() + "\n");
540
541 // Iterate the axis and write node info to output file
542 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
543 {
544 buf.append(getNodeInfo(dtm, itNode, " "));
545 lastNode2 = itNode;
546 }
547
548 // Write results and close output file.
549 writeClose(fos, buf);
550
551 // Verify results
552 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
553 myfilechecker.check(reporter, new File(outNames.currentName()),
554 new File(gold),
555 "Testcase10");
556 reporter.testCaseClose();
557 return true;
558 }
559
560
561 /**
562 * Create AxisIterator and walk ANCESTOR axis.
563 * @return false if we should abort the test; true otherwise
564 */
testCase11()565 public boolean testCase11()
566 {
567 reporter.testCaseInit("Walk ANCESTOR AxisIterator");
568 StringBuffer buf = new StringBuffer();
569 FileOutputStream fos = openFileStream(outNames.nextName());
570 String gold = testFileInfo.goldName + "testcase11.out";
571
572 // Create dtm and generate initial context
573 DTM dtm = generateDTM();
574
575 // Get a Iterator for ANCESTOR:: axis.
576 DTMAxisIterator iter = dtm.getAxisIterator(Axis.ANCESTOR);
577 iter.setStartNode(lastNode2);
578
579 lastName2 = dtm.getNodeName(lastNode2);
580
581 // Print out info about the axis
582 buf.append("#### ANCESTOR from "+lastName2+", Reverse Axis:" + iter.isReverse() + "\n");
583
584 // Iterate the axis and write node info to output file
585 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
586 buf.append(getNodeInfo(dtm, itNode, " "));
587
588 // Write results and close output file.
589 writeClose(fos, buf);
590
591 // Verify results
592 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
593 myfilechecker.check(reporter, new File(outNames.currentName()),
594 new File(gold),
595 "Testcase11");
596
597 reporter.testCaseClose();
598 return true;
599 }
600
601
602 /**
603 * Create AxisIterator and walk ANCESTORORSELF axis.
604 * @return false if we should abort the test; true otherwise
605 */
testCase12()606 public boolean testCase12()
607 {
608 reporter.testCaseInit("Walk ANCESTORORSELF AxisIterator");
609 StringBuffer buf = new StringBuffer();
610 FileOutputStream fos = openFileStream(outNames.nextName());
611 String gold = testFileInfo.goldName + "testcase12.out";
612
613 // Create dtm and generate initial context
614 DTM dtm = generateDTM();
615
616 // Get a Iterator for ANCESTORORSELF:: axis.
617 DTMAxisIterator iter = dtm.getAxisIterator(Axis.ANCESTORORSELF);
618 iter.setStartNode(lastNode2);
619
620 // Print out info about the axis
621 buf.append("#### ANCESTORORSELF from "+lastName2+", Reverse Axis:" + iter.isReverse() + "\n");
622
623 // Iterate the axis and write node info to output file
624 for (int itNode = iter.next(); DTM.NULL != itNode; itNode = iter.next())
625 buf.append(getNodeInfo(dtm, itNode, " "));
626
627 // Write results and close output file.
628 writeClose(fos, buf);
629
630 // Verify results
631 LinebyLineCheckService myfilechecker = new LinebyLineCheckService();
632 myfilechecker.check(reporter, new File(outNames.currentName()),
633 new File(gold),
634 "Testcase12");
635 reporter.testCaseClose();
636 return true;
637 }
638
usage()639 public String usage()
640 {
641 return ("Common [optional] options supported by TestDTMIter:\n"
642 + "(Note: assumes inputDir=.\\tests\\api)\n");
643 }
644
openFileStream(String name)645 FileOutputStream openFileStream(String name)
646 {
647 FileOutputStream fos = null;
648
649 try
650 { fos = new FileOutputStream(name); }
651
652 catch (Exception e)
653 { reporter.checkFail("Failure opening output file."); }
654
655 return fos;
656 }
657
658 // This routine generates a new DTM for each testcase
generateDTM()659 DTM generateDTM()
660 {
661 // Create DTM and generate initial context
662 Source source = new StreamSource(new StringReader(defaultSource));
663 DTMManager manager= new DTMManagerDefault().newInstance(new XMLStringFactoryImpl());
664 DTM dtm=manager.getDTM(source, true, stripper, false, true);
665
666 return dtm;
667 }
668
writeClose(FileOutputStream fos, StringBuffer buf)669 void writeClose(FileOutputStream fos, StringBuffer buf)
670 {
671 // Write results and close output file.
672 try
673 {
674 fos.write(buf.toString().getBytes("UTF-8"));
675 fos.close();
676 }
677
678 catch (Exception e)
679 { reporter.checkFail("Failure writing output."); }
680 }
681
getNodeInfo(DTM dtm, int nodeHandle, String indent)682 String getNodeInfo(DTM dtm, int nodeHandle, String indent)
683 {
684 // Formatting hack -- suppress quotes when value is null, to distinguish
685 // it from "null".
686 String buf = new String("null");
687 String value=dtm.getNodeValue(nodeHandle);
688 String vq = (value==null) ? "" : "\"";
689
690 // Skip outputing of text nodes. In most cases they clutter the output,
691 // besides I'm only interested in the elemental structure of the dtm.
692 if( TYPENAME[dtm.getNodeType(nodeHandle)] != "TEXT" )
693 {
694 buf = new String(indent+
695 nodeHandle+": "+
696 TYPENAME[dtm.getNodeType(nodeHandle)]+" "+
697 dtm.getNodeName(nodeHandle)+" "+
698 " Level=" + dtm.getLevel(nodeHandle)+" "+
699 "\tValue=" + vq + value + vq + "\n"
700 );
701 }
702 return buf;
703 }
704
705 /**
706 * Main method to run test from the command line - can be left alone.
707 * @param args command line argument array
708 */
main(String[] args)709 public static void main(String[] args)
710 {
711 TestDTMIter app = new TestDTMIter();
712 app.doMain(args);
713 }
714 }
715