Path: blob/master/test/jdk/javax/xml/jaxp/parsers/8022548/XOMParserTest.java
41154 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* @modules java.xml/com.sun.org.apache.xerces.internal.impl28* java.xml/com.sun.org.apache.xerces.internal.parsers29* java.xml/com.sun.org.apache.xerces.internal.util30* java.xml/com.sun.org.apache.xerces.internal.xni.parser31* @run main XOMParserTest32*/33import com.sun.org.apache.xerces.internal.impl.Constants;34import com.sun.org.apache.xerces.internal.parsers.*;35import java.io.*;36import javax.xml.transform.*;37import javax.xml.transform.stream.StreamResult;38import javax.xml.transform.stream.StreamSource;39import org.xml.sax.InputSource;4041/**42* <p>Test {@link javax.xml.transform.Transformer} for JDK-8022548: SPECJVM200843* has errors introduced in 7u40-b3444*45* Test XOM is supported after jaxp 1.5 </p>46*47* @author Joe Wang <[email protected]>48*49*/50public class XOMParserTest extends TestBase {5152public XOMParserTest(String name) {53super(name);54}5556/**57* @param args the command line arguments58*/59public static void main(String[] args) {60XOMParserTest test = new XOMParserTest("XOM parser test");61test.setUp();62test.testTransform();63test.tearDown();64}6566public final void testTransform() {67String inFilename = filePath + "/JDK8022548.xml";68String xslFilename = filePath + "/JDK8022548.xsl";69String outFilename = "JDK8022548.out";7071try (InputStream xslInput = new FileInputStream(xslFilename);72InputStream xmlInput = new FileInputStream(inFilename);73OutputStream out = new FileOutputStream(outFilename);74) {757677StringWriter sw = new StringWriter();78// Create transformer factory79TransformerFactory factory = TransformerFactory.newInstance();8081// Use the factory to create a template containing the xsl file82Templates template = factory.newTemplates(new StreamSource(xslInput));83// Use the template to create a transformer84Transformer xformer = template.newTransformer();85// Prepare the input and output files86Source source = new StreamSource(xmlInput);87Result result = new StreamResult(outFilename);88//Result result = new StreamResult(sw);89// Apply the xsl file to the source file and write the result to the output file90xformer.transform(source, result);9192/**93* String out = sw.toString(); if (out.indexOf("<p>") < 0 ) {94* fail(out); }95*/96String canonicalizedFileName = outFilename + ".canonicalized";97canonicalize(outFilename, canonicalizedFileName);98} catch (Exception e) {99// unexpected failure100fail(e.getMessage());101}102}103104public void canonicalize(String inName, String outName) {105try (//FileOutputStream outStream = new FileOutputStream(outName);106FileInputStream inputStream = new FileInputStream(inName);) {107JDK15XML1_0Parser parser = new JDK15XML1_0Parser();108parser.parse(new InputSource(inputStream));109success("test passed");110} catch (Exception e) {111fail(e.getMessage());112}113114}115116class JDK15XML1_0Parser extends SAXParser {117118JDK15XML1_0Parser() throws org.xml.sax.SAXException {119120super(new DTDConfiguration());121// workaround for Java 1.5 beta 2 bugs122com.sun.org.apache.xerces.internal.util.SecurityManager manager =123new com.sun.org.apache.xerces.internal.util.SecurityManager();124setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY, manager);125126}127}128}129130131