1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.qetest.dtm; 21 22 import com.sun.org.apache.xml.internal.dtm.DTM; 23 import com.sun.org.apache.xml.internal.dtm.DTMManager; 24 import com.sun.org.apache.xml.internal.dtm.DTMWSFilter; 25 import com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault; 26 import com.sun.org.apache.xpath.internal.objects.XMLStringFactoryImpl; 27 import java.io.StringReader; 28 import java.util.List; 29 import javax.xml.transform.Source; 30 import javax.xml.transform.stream.StreamSource; 31 import static jaxp.library.JAXPTestUtilities.FILE_SEP; 32 33 /** 34 * This is the utilities test class provide basic support for DTM test. 35 */ 36 public class QeDtmConst { 37 /** 38 * Current test directory. 39 */ 40 public static final String CLASS_DIR 41 = System.getProperty("test.classes", ".") + FILE_SEP; 42 43 /** 44 * Package name that separates by slash. 45 */ 46 public static final String PACKAGE_NAME = FILE_SEP + 47 QeDtmConst.class.getPackage().getName().replaceAll("[.]", FILE_SEP); 48 49 /** 50 * Java source directory. 51 */ 52 public static final String SRC_DIR = System.getProperty("test.src") 53 .replaceAll("\\" + System.getProperty("file.separator"), "/") 54 + PACKAGE_NAME + FILE_SEP; 55 56 /** 57 * Source XML file directory. 58 */ 59 public static final String XML_DIR = SRC_DIR + "content" + FILE_SEP; 60 61 /** 62 * Golden output file directory. 63 * We pre-define all expected output in golden output file. Test verifies 64 * whether the standard output is same as content of golden file. 65 */ 66 public static final String GOLDEN_DIR = SRC_DIR + "golden" + FILE_SEP; 67 68 public static final String DEFAULT_SOURCE = "<?xml version=\"1.0\"?>\n" 69 + "<Document xmlns:d=\"www.d.com\" a1=\"hello\" a2=\"goodbye\">" 70 + "<!-- Default test document -->" 71 + "<?api a1=\"yes\" a2=\"no\"?>" 72 + "<A><!-- A Subtree --><B><C><D><E><F xmlns:f=\"www.f.com\" a1=\"down\" a2=\"up\"/></E></D></C></B></A>" 73 + "<Aa/><Ab/><Ac><Ac1/></Ac>" 74 + "<Ad xmlns:Ad=\"www.Ad.com\" xmlns:y=\"www.y.com\" xmlns:z=\"www.z.com\">" 75 + "<Ad1/></Ad>" 76 + "</Document>"; 77 78 /** 79 * String represent for DOM node type. The sequence of this array following 80 * same sequence in. 81 */ 82 public static final String[] TYPENAME = {"NULL", 83 "ELEMENT", 84 "ATTRIBUTE", 85 "TEXT", 86 "CDATA_SECTION", 87 "ENTITY_REFERENCE", 88 "ENTITY", 89 "PROCESSING_INSTRUCTION", 90 "COMMENT", 91 "DOCUMENT", 92 "DOCUMENT_TYPE", 93 "DOCUMENT_FRAGMENT", 94 "NOTATION", 95 "NAMESPACE" 96 }; 97 98 /** 99 * This routine generates a new DTM with stripping whitespace filter. 100 * non-incremental, but with indexing (a fairly common case, and 101 * avoids the special mode used for RTF DTMs). 102 * @param theSource URL for StreamSource 103 * @return a non-null DTM reference. 104 */ 105 public static DTM createDTM(String theSource) { 106 return DTMManagerDefault.newInstance(new XMLStringFactoryImpl()) 107 .getDTM(new StreamSource(new StringReader(theSource)), true, 108 (eh, dtm) -> DTMWSFilter.STRIP, false, true); 109 } 110 111 /** 112 * This routine generates a new DTM for each test case with no whitespace 113 * filtering, non-incremental, but with indexing (a fairly common case, 114 * and avoids the special mode used for RTF DTMs). 115 * @param method 0 create StreamSource directly. 116 * 1 create StreamSource by StringReader. 117 * @param theSource the specification of the source object. 118 * @param lines output string list that will be appended by memory usage. 119 * @return 120 */ 121 public static DTM createDTM(int method, String theSource, List<String> lines) { 122 // Create DTM and generate initial context 123 Source source = (method == 1) ? 124 new StreamSource(new StringReader(theSource)) : 125 new StreamSource(theSource); 126 127 DTMManager manager = DTMManagerDefault.newInstance(new XMLStringFactoryImpl()); 128 return manager.getDTM(source, true, (eh, dtm) -> DTMWSFilter.STRIP, false, true); 129 } 130 131 /** 132 * Create DTM and generate initial context. 133 * @return a non-null DTM reference with initial context. 134 */ 135 public static DTM generateDTM() { 136 return createDTM(DEFAULT_SOURCE); 137 } 138 139 /** 140 * Retrieve string represent for a XML node. 141 * @param dtm A XML document model table represent. 142 * @param nodeHandle Node id. 143 * @return A string represent by given node id. 144 */ 145 public static String getNodeName(DTM dtm, int nodeHandle) { 146 return TYPENAME[dtm.getNodeType(nodeHandle)]; 147 } 148 }