/*
* Copyright (c) 2014, 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 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.StringReader;
import java.util.List;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import static jaxp.library.JAXPTestUtilities.FILE_SEP;
/**
* This is the utilities test class provide basic support for DTM test.
*/
public class QeDtmConst {
/**
* Current test directory.
*/
public static final String CLASS_DIR
= System.getProperty("test.classes", ".") + FILE_SEP;
/**
* Package name that separates by slash.
*/
public static final String PACKAGE_NAME = FILE_SEP +
QeDtmConst.class.getPackage().getName().replaceAll("[.]", FILE_SEP);
/**
* Java source directory.
*/
public static final String SRC_DIR = System.getProperty("test.src")
.replaceAll("\\" + System.getProperty("file.separator"), "/")
+ PACKAGE_NAME + FILE_SEP;
/**
* Source XML file directory.
*/
public static final String XML_DIR = SRC_DIR + "content" + FILE_SEP;
/**
* Golden output file directory.
* We pre-define all expected output in golden output file. Test verifies
* whether the standard output is same as content of golden file.
*/
public static final String GOLDEN_DIR = SRC_DIR + "golden" + FILE_SEP;
public static final String DEFAULT_SOURCE = "\n"
+ ""
+ ""
+ ""
+ ""
+ " "
+ ""
+ ""
+ "";
/**
* String represent for DOM node type. The sequence of this array following
* same sequence in.
*/
public static final String[] TYPENAME = {"NULL",
"ELEMENT",
"ATTRIBUTE",
"TEXT",
"CDATA_SECTION",
"ENTITY_REFERENCE",
"ENTITY",
"PROCESSING_INSTRUCTION",
"COMMENT",
"DOCUMENT",
"DOCUMENT_TYPE",
"DOCUMENT_FRAGMENT",
"NOTATION",
"NAMESPACE"
};
/**
* This routine generates a new DTM with stripping whitespace filter.
* non-incremental, but with indexing (a fairly common case, and
* avoids the special mode used for RTF DTMs).
* @param theSource URL for StreamSource
* @return a non-null DTM reference.
*/
public static DTM createDTM(String theSource) {
return DTMManagerDefault.newInstance(new XMLStringFactoryImpl())
.getDTM(new StreamSource(new StringReader(theSource)), true,
(eh, dtm) -> DTMWSFilter.STRIP, false, true);
}
/**
* This routine generates a new DTM for each test case with no whitespace
* filtering, non-incremental, but with indexing (a fairly common case,
* and avoids the special mode used for RTF DTMs).
* @param method 0 create StreamSource directly.
* 1 create StreamSource by StringReader.
* @param theSource the specification of the source object.
* @param lines output string list that will be appended by memory usage.
* @return
*/
public static DTM createDTM(int method, String theSource, List lines) {
// Create DTM and generate initial context
Source source = (method == 1) ?
new StreamSource(new StringReader(theSource)) :
new StreamSource(theSource);
DTMManager manager = DTMManagerDefault.newInstance(new XMLStringFactoryImpl());
return manager.getDTM(source, true, (eh, dtm) -> DTMWSFilter.STRIP, false, true);
}
/**
* Create DTM and generate initial context.
* @return a non-null DTM reference with initial context.
*/
public static DTM generateDTM() {
return createDTM(DEFAULT_SOURCE);
}
/**
* Retrieve string represent for a XML node.
* @param dtm A XML document model table represent.
* @param nodeHandle Node id.
* @return A string represent by given node id.
*/
public static String getNodeName(DTM dtm, int nodeHandle) {
return TYPENAME[dtm.getNodeType(nodeHandle)];
}
}