1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8022548
  27  * @summary test that a parser can use DTDConfiguration
  28  * @modules java.xml/com.sun.org.apache.xerces.internal.impl
  29  *          java.xml/com.sun.org.apache.xerces.internal.parsers
  30  *          java.xml/com.sun.org.apache.xerces.internal.util
  31  *          java.xml/com.sun.org.apache.xerces.internal.xni.parser
  32  * @run main XOMParserTest
  33  */
  34 import com.sun.org.apache.xerces.internal.impl.Constants;
  35 import com.sun.org.apache.xerces.internal.parsers.*;
  36 import java.io.*;
  37 import javax.xml.transform.*;
  38 import javax.xml.transform.stream.StreamResult;
  39 import javax.xml.transform.stream.StreamSource;
  40 import org.xml.sax.InputSource;
  41 
  42 /**
  43  * <p>Test {@link javax.xml.transform.Transformer} for JDK-8022548: SPECJVM2008
  44  * has errors introduced in 7u40-b34
  45  *
  46  * Test XOM is supported after jaxp 1.5 </p>
  47  *
  48  * @author Joe Wang <huizhe.wang@oracle.com>
  49  *
  50  */
  51 public class XOMParserTest extends TestBase {
  52 
  53     public XOMParserTest(String name) {
  54         super(name);
  55     }
  56 
  57     /**
  58      * @param args the command line arguments
  59      */
  60     public static void main(String[] args) {
  61         XOMParserTest test = new XOMParserTest("XOM parser test");
  62         test.setUp();
  63         test.testTransform();
  64         test.tearDown();
  65     }
  66 
  67     public final void testTransform() {
  68         String inFilename = filePath + "/JDK8022548.xml";
  69         String xslFilename = filePath + "/JDK8022548.xsl";
  70         String outFilename = "JDK8022548.out";
  71 
  72         try (InputStream xslInput = new FileInputStream(xslFilename);
  73              InputStream xmlInput = new FileInputStream(inFilename);
  74              OutputStream out = new FileOutputStream(outFilename);
  75         ) {
  76 
  77 
  78             StringWriter sw = new StringWriter();
  79             // Create transformer factory
  80             TransformerFactory factory = TransformerFactory.newInstance();
  81 
  82             // Use the factory to create a template containing the xsl file
  83             Templates template = factory.newTemplates(new StreamSource(xslInput));
  84             // Use the template to create a transformer
  85             Transformer xformer = template.newTransformer();
  86             // Prepare the input and output files
  87             Source source = new StreamSource(xmlInput);
  88             Result result = new StreamResult(outFilename);
  89             //Result result = new StreamResult(sw);
  90             // Apply the xsl file to the source file and write the result to the output file
  91             xformer.transform(source, result);
  92 
  93             /**
  94              * String out = sw.toString(); if (out.indexOf("<p>") < 0 ) {
  95              * fail(out); }
  96              */
  97             String canonicalizedFileName = outFilename + ".canonicalized";
  98             canonicalize(outFilename, canonicalizedFileName);
  99         } catch (Exception e) {
 100             // unexpected failure
 101             fail(e.getMessage());
 102         }
 103     }
 104 
 105     public void canonicalize(String inName, String outName) {
 106         try (//FileOutputStream outStream = new FileOutputStream(outName);
 107                 FileInputStream inputStream = new FileInputStream(inName);) {
 108             JDK15XML1_0Parser parser = new JDK15XML1_0Parser();
 109             parser.parse(new InputSource(inputStream));
 110             success("test passed");
 111         } catch (Exception e) {
 112             fail(e.getMessage());
 113         }
 114 
 115     }
 116 
 117     class JDK15XML1_0Parser extends SAXParser {
 118 
 119         JDK15XML1_0Parser() throws org.xml.sax.SAXException {
 120 
 121             super(new DTDConfiguration());
 122             // workaround for Java 1.5 beta 2 bugs
 123             com.sun.org.apache.xerces.internal.util.SecurityManager manager =
 124                     new com.sun.org.apache.xerces.internal.util.SecurityManager();
 125             setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY, manager);
 126 
 127         }
 128     }
 129 }