/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.qetest.dtm;
import com.sun.org.apache.xml.internal.dtm.Axis;
import com.sun.org.apache.xml.internal.dtm.DTM;
import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
import com.sun.org.apache.xml.internal.dtm.DTMManager;
import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;
import com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault;
import com.sun.org.apache.xpath.internal.objects.XMLStringFactoryImpl;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import jaxp.library.JAXPFileBaseTest;
import static jaxp.library.JAXPTestUtilities.compareWithGold;
import static jaxp.library.JAXPTestUtilities.getNextFile;
import static org.apache.qetest.dtm.QeDtmConst.GOLDEN_DIR;
import static org.apache.qetest.dtm.QeDtmConst.TYPENAME;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
/**
*
* Loads an XML document from a file (or, if no filename is supplied,
* an internal string), then dumps its contents. Replaces the old
* version, which was specific to the ultra-compressed implementation.
*/
public class TestDTMIterator extends JAXPFileBaseTest {
/**
* Test DTMIterator on a well-formed XML string. Checking on expected output.
* @throws IOException if any error on File I/O operation.
*/
@Test
public void test() throws IOException {
setPermissions(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.utils"),
new RuntimePermission("accessClassInPackage.com.sun.org.apache.xpath.internal.objects"));
String goldFile = GOLDEN_DIR + "TestDTMIterator.out";
String defaultSource = "\n"
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ " "
+ ""
+ ""
+ "";
try(StringReader sw = new StringReader(defaultSource)) {
Source source = new StreamSource(sw);
// Get a DTM manager, and ask it to load the DTM "uniquely",
// with no whitespace filtering, nonincremental, but _with_
// indexing (a fairly common case, and avoids the special
// mode used for RTF DTMs).
// For testing with some of David Marston's files I do want to strip whitespace.
DTMManager manager = DTMManagerDefault.newInstance(new XMLStringFactoryImpl());
DTM dtm = manager.getDTM(source, true, (e, d) -> DTMWSFilter.STRIP, false, true);
// Get various nodes to use as context nodes.
int dtmRoot = dtm.getDocument(); // #document
int DNode = dtm.getFirstChild(dtmRoot); //
int CNode = dtm.getFirstChild(DNode); //
int PINode = dtm.getNextSibling(CNode); //
int ANode = dtm.getNextSibling(PINode); //
int lastNode = 0;
// Get a Iterator for CHILD:: axis.
DTMAxisIterator iter = dtm.getAxisIterator(Axis.CHILD);
iter.setStartNode(DNode);
String outputFile = getNextFile(this.getClass());
try(PrintWriter pw = new PrintWriter(outputFile)) {
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
lastNode = itNode;
}
// Get iterator for PARENT:: Axis
iter = dtm.getAxisIterator(Axis.PARENT);
iter.setStartNode(lastNode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
}
// Get iterator for SELF:: Axis
iter = dtm.getAxisIterator(Axis.SELF);
iter.setStartNode(lastNode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
}
// Get iterator for NAMESPACE:: Axis
iter = dtm.getAxisIterator(Axis.NAMESPACE);
iter.setStartNode(lastNode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
}
// Get iterator for PRECEDING:: Axis
iter = dtm.getAxisIterator(Axis.PRECEDING);
iter.setStartNode(lastNode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
}
// Get iterator for PRECEDINGSIBLING:: Axis
iter = dtm.getAxisIterator(Axis.PRECEDINGSIBLING);
iter.setStartNode(lastNode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
}
// Get iterator for FOLLOWING:: Axis
iter = dtm.getAxisIterator(Axis.FOLLOWING);
iter.setStartNode(ANode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
}
// Get iterator for FOLLOWINGSIBLING:: Axis
iter = dtm.getAxisIterator(Axis.FOLLOWINGSIBLING);
iter.setStartNode(ANode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
}
// Get a iterator for DESCENDANT:: axis.
iter = dtm.getAxisIterator(Axis.DESCENDANT);
iter.setStartNode(ANode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
lastNode = itNode;
}
// Get iterator for DESCENDANTORSELF:: Axis
iter = dtm.getAxisIterator(Axis.DESCENDANTORSELF);
iter.setStartNode(ANode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
lastNode = itNode;
}
// The output from Ancestor and Ancestor-or-self is the topic
// of Bugzilla 7886
// Get iterator for ANCESTOR:: Axis
iter = dtm.getAxisIterator(Axis.ANCESTOR);
iter.setStartNode(lastNode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
}
// Get iterator for ANCESTORORSELF:: Axis
iter = dtm.getAxisIterator(Axis.ANCESTORORSELF);
iter.setStartNode(lastNode);
// Iterate the axis and print out node info.
for (int itNode = iter.next(); DTM.NULL != itNode;
itNode = iter.next()) {
printNode(dtm, itNode, " ", pw);
}
}
assertTrue(compareWithGold(goldFile, outputFile));
}
}
/**
* Print a node info by given node handle.
* @param dtm DTM instance.
* @param nodeHandle node handle.
* @param indent a format indention.
*/
private void printNode(DTM dtm, int nodeHandle, String indent, PrintWriter pw) {
// Briefly display this node
// Don't bother displaying namespaces or attrs; we do that at the
// next level up.
// %REVIEW% Add namespace info, type info, ...
// Formatting hack -- suppress quotes when value is null, to distinguish
// it from "null".
String value = dtm.getNodeValue(nodeHandle);
String vq = (value == null) ? "" : "\"";
// Skip outputing of text nodes. In most cases they clutter the output,
// besides I'm only interested in the elemental structure of the dtm.
if (!"TEXT".equals(TYPENAME[dtm.getNodeType(nodeHandle)])) {
pw.println(indent
+ +nodeHandle + ": "
+ TYPENAME[dtm.getNodeType(nodeHandle)] + " "
+ dtm.getNodeName(nodeHandle) + " "
+ " Level=" + dtm.getLevel(nodeHandle) + " "
+ "\tValue=" + vq + value + vq
);
}
}
}