/* * 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.DTM; import java.io.FilePermission; import java.io.IOException; import java.util.ArrayList; import java.util.List; import jaxp.library.JAXPBaseTest; import static jaxp.library.JAXPTestUtilities.compareLinesWithGold; import static org.apache.qetest.dtm.QeDtmConst.GOLDEN_DIR; import static org.apache.qetest.dtm.QeDtmConst.TYPENAME; import static org.apache.qetest.dtm.QeDtmConst.generateDTM; 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. * (Which, by the way, we probably ought to revisit as part of our ongoing * speed/size performance evaluation.) */ public class TestDTM extends JAXPBaseTest { /** * Create AxisIterator and walk CHILD axis. Dump every walked node and * compare with expected golden file. * * @throws IOException if any I/O operation error. */ @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 + "DTM_testcase1.out"; // Create dtm and generate initial context DTM dtm = generateDTM(); // DTM -- which will always be true for a node obtained this way, but // won't be true for "shared" DTMs used to hold XSLT variables int rootNode = dtm.getDocument(); List lines = new ArrayList<>(); lines.add("DOCUMENT PROPERTIES:"); lines.add("DocURI=\"" + dtm.getDocumentBaseURI() + "\" \"SystemID=\"" + dtm.getDocumentSystemIdentifier(rootNode) + "\""); lines.add("StandAlone=\"" + dtm.getDocumentStandalone(rootNode) + "\" DocVersion=\"" + dtm.getDocumentVersion(rootNode) + "\""); lines.add(""); lines.add("DOCUMENT DATA:"); // Simple test: Recursively dump the DTM's content. // We'll want to replace this with more serious examples lines.add("DOCUMENT DATA:"); recursiveDumpNode(dtm, rootNode, lines); setPermissions(new FilePermission(goldFile, "read")); compareLinesWithGold(goldFile, lines); } /** * Format node info to a string list. * @param dtm DTM instance. * @param nodeHandle node handle that represent node will be dumped. * @param indent a format indention. * @return */ private List getNodeInfo(DTM dtm, int nodeHandle, String indent) { List nodeLines = new ArrayList<>(); 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. nodeLines.add(indent + nodeHandle + ": " + TYPENAME[dtm.getNodeType(nodeHandle)] + " " + dtm.getNodeNameX(nodeHandle) + " : " + dtm.getNodeName(nodeHandle) + "\"" + " E-Type=" + dtm.getExpandedTypeID(nodeHandle) + " Level=" + dtm.getLevel(nodeHandle) + " Value=" + vq + value + vq); nodeLines.add(indent + "\tPrefix= " + "\"" + dtm.getPrefix(nodeHandle) + "\"" + " Name= " + "\"" + dtm.getLocalName(nodeHandle) + "\"" + " URI= " + "\"" + dtm.getNamespaceURI(nodeHandle) + "\"" + " Parent=" + dtm.getParent(nodeHandle) + " 1stChild=" + dtm.getFirstChild(nodeHandle) + " NextSib=" + dtm.getNextSibling(nodeHandle)); return nodeLines; } /** * Recursively access every children of the node. Dump every information of * every node to given string list. * @param dtm DTM instance. * @param nodeHandle node handle that will be accessed. * @param lines a string list that recursive accessed node information will * be dumped. */ private void recursiveDumpNode(DTM dtm, int nodeHandle, List lines) { // ITERATE over siblings while (DTM.NULL != nodeHandle) { lines.addAll(getNodeInfo(dtm, nodeHandle, "")); // List the namespaces, if any. Include only node's local // namespaces, not inherited int kid = dtm.getFirstNamespaceNode(nodeHandle, false); if (kid != DTM.NULL) { lines.add("\tNAMESPACES:"); //builder.append("\n\tNAMESPACES:"); for (; kid != DTM.NULL; kid = dtm.getNextNamespaceNode(nodeHandle, kid, false)) { lines.addAll(getNodeInfo(dtm, kid, "\t")); } } // List the attributes, if any kid = dtm.getFirstAttribute(nodeHandle); if (kid != DTM.NULL) { lines.add("\tATTRIBUTES:"); for (; kid != DTM.NULL; kid = dtm.getNextSibling(kid)) { lines.addAll(getNodeInfo(dtm, kid, "\t")); } } // Recurse into the children, if any recursiveDumpNode(dtm, dtm.getFirstChild(nodeHandle), lines); nodeHandle = dtm.getNextSibling(nodeHandle); } } }