1 /*
   2  * Copyright (c) 2015, 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.getPathByClassName;
  32 
  33 /**
  34  * This is the utilities test class provide basic support for DTM test.
  35  */
  36 public class QeDtmConst {
  37     /**
  38      * XML source file directory.
  39      */
  40     public static final String XML_DIR = getPathByClassName(QeDtmConst.class, "content"); 
  41 
  42     /**
  43      * Golden validation files directory.
  44      */
  45     public static final String GOLDEN_DIR = getPathByClassName(QeDtmConst.class, "golden");
  46 
  47     /**
  48      * A XML source string for building DTM.
  49      */
  50     public static final String DEFAULT_SOURCE = "<?xml version=\"1.0\"?>\n"
  51             + "<Document xmlns:d=\"www.d.com\" a1=\"hello\" a2=\"goodbye\">"
  52             + "<!-- Default test document -->"
  53             + "<?api a1=\"yes\" a2=\"no\"?>"
  54             + "<A><!-- A Subtree --><B><C><D><E><F xmlns:f=\"www.f.com\" a1=\"down\" a2=\"up\"/></E></D></C></B></A>"
  55             + "<Aa/><Ab/><Ac><Ac1/></Ac>"
  56             + "<Ad xmlns:Ad=\"www.Ad.com\" xmlns:y=\"www.y.com\" xmlns:z=\"www.z.com\">"
  57             + "<Ad1/></Ad>"
  58             + "</Document>";
  59 
  60     /**
  61      * String represent for DOM node type. The sequence of this array following
  62      * same sequence in.
  63      */
  64     public static final String[] TYPENAME = {"NULL",
  65                 "ELEMENT",
  66                 "ATTRIBUTE",
  67                 "TEXT",
  68                 "CDATA_SECTION",
  69                 "ENTITY_REFERENCE",
  70                 "ENTITY",
  71                 "PROCESSING_INSTRUCTION",
  72                 "COMMENT",
  73                 "DOCUMENT",
  74                 "DOCUMENT_TYPE",
  75                 "DOCUMENT_FRAGMENT",
  76                 "NOTATION",
  77                 "NAMESPACE"
  78             };
  79 
  80     /**
  81      * This routine generates a new DTM with stripping whitespace filter. 
  82      * non-incremental, but with indexing (a fairly common case, and
  83      * avoids the special mode used for RTF DTMs).
  84      * @param theSource URL for StreamSource
  85      * @return a non-null DTM reference.
  86      */
  87     public static DTM createDTM(String theSource) {
  88         return DTMManagerDefault.newInstance(new XMLStringFactoryImpl())
  89                 .getDTM(new StreamSource(new StringReader(theSource)), true, 
  90                 (eh, dtm) -> DTMWSFilter.STRIP, false, true);
  91     }
  92 
  93     /**
  94      * This routine generates a new DTM for each test case with no whitespace 
  95      * filtering, non-incremental, but with indexing (a fairly common case, 
  96      * and avoids the special mode used for RTF DTMs).
  97      * @param method 0 create StreamSource directly.
  98      *               1 create StreamSource by StringReader.
  99      * @param theSource the specification of the source object.
 100      * @param lines output string list that will be appended by memory usage.
 101      * @return 
 102      */
 103     public static DTM createDTM(int method, String theSource, List<String> lines) {
 104         // Create DTM and generate initial context
 105         Source source = (method == 1) ?
 106                 new StreamSource(new StringReader(theSource)) :
 107                 new StreamSource(theSource);
 108 
 109         DTMManager manager = DTMManagerDefault.newInstance(new XMLStringFactoryImpl());
 110         return manager.getDTM(source, true, (eh, dtm) -> DTMWSFilter.STRIP, false, true);
 111     } 
 112 
 113     /**
 114      * Create DTM and generate initial context.
 115      * @return a non-null DTM reference with initial context. 
 116      */
 117     public static DTM generateDTM() {
 118         return createDTM(DEFAULT_SOURCE);
 119     }
 120     
 121     /**
 122      * Retrieve string represent for a XML node.
 123      * @param dtm A XML document model table represent.
 124      * @param nodeHandle Node id.
 125      * @return A string represent by given node id. 
 126      */
 127     public static String getNodeName(DTM dtm, int nodeHandle) {
 128         return TYPENAME[dtm.getNodeType(nodeHandle)];
 129     }
 130 }