Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/jaxp/parsers/8022548/XOMParserTest.java
38860 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 802254826* @summary test that a parser can use DTDConfiguration27* @run main XOMParserTest28*/29import com.sun.org.apache.xerces.internal.impl.Constants;30import com.sun.org.apache.xerces.internal.parsers.*;31import java.io.*;32import javax.xml.transform.*;33import javax.xml.transform.stream.StreamResult;34import javax.xml.transform.stream.StreamSource;35import org.xml.sax.InputSource;3637/**38* <p>Test {@link javax.xml.transform.Transformer} for JDK-8022548: SPECJVM200839* has errors introduced in 7u40-b3440*41* Test XOM is supported after jaxp 1.5 </p>42*43* @author Joe Wang <[email protected]>44*45*/46public class XOMParserTest extends TestBase {4748public XOMParserTest(String name) {49super(name);50}5152/**53* @param args the command line arguments54*/55public static void main(String[] args) {56XOMParserTest test = new XOMParserTest("XOM parser test");57test.setUp();58test.testTransform();59test.tearDown();60}6162public final void testTransform() {63String inFilename = filePath + "/JDK8022548.xml";64String xslFilename = filePath + "/JDK8022548.xsl";65String outFilename = "JDK8022548.out";6667try (InputStream xslInput = new FileInputStream(xslFilename);68InputStream xmlInput = new FileInputStream(inFilename);69OutputStream out = new FileOutputStream(outFilename);70) {717273StringWriter sw = new StringWriter();74// Create transformer factory75TransformerFactory factory = TransformerFactory.newInstance();7677// Use the factory to create a template containing the xsl file78Templates template = factory.newTemplates(new StreamSource(xslInput));79// Use the template to create a transformer80Transformer xformer = template.newTransformer();81// Prepare the input and output files82Source source = new StreamSource(xmlInput);83Result result = new StreamResult(outFilename);84//Result result = new StreamResult(sw);85// Apply the xsl file to the source file and write the result to the output file86xformer.transform(source, result);8788/**89* String out = sw.toString(); if (out.indexOf("<p>") < 0 ) {90* fail(out); }91*/92String canonicalizedFileName = outFilename + ".canonicalized";93canonicalize(outFilename, canonicalizedFileName);94} catch (Exception e) {95// unexpected failure96fail(e.getMessage());97}98}99100public void canonicalize(String inName, String outName) {101try (//FileOutputStream outStream = new FileOutputStream(outName);102FileInputStream inputStream = new FileInputStream(inName);) {103JDK15XML1_0Parser parser = new JDK15XML1_0Parser();104parser.parse(new InputSource(inputStream));105success("test passed");106} catch (Exception e) {107fail(e.getMessage());108}109110}111112class JDK15XML1_0Parser extends SAXParser {113114JDK15XML1_0Parser() throws org.xml.sax.SAXException {115116super(new DTDConfiguration());117// workaround for Java 1.5 beta 2 bugs118com.sun.org.apache.xerces.internal.util.SecurityManager manager =119new com.sun.org.apache.xerces.internal.util.SecurityManager();120setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY, manager);121122}123}124}125126127