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  * @run main XOMParserTest
  29  */
  30 import com.sun.org.apache.xerces.internal.impl.Constants;
  31 import com.sun.org.apache.xerces.internal.parsers.*;
  32 import java.io.*;
  33 import javax.xml.transform.*;
  34 import javax.xml.transform.stream.StreamResult;
  35 import javax.xml.transform.stream.StreamSource;
  36 import org.xml.sax.InputSource;
  37 
  38 /**
  39  * <p>Test {@link javax.xml.transform.Transformer} for JDK-8022548: SPECJVM2008
  40  * has errors introduced in 7u40-b34
  41  *
  42  * Test XOM is supported after jaxp 1.5 </p>
  43  *
  44  * @author Joe Wang <huizhe.wang@oracle.com>
  45  *
  46  */
  47 public class XOMParserTest extends TestBase {
  48 
  49     public XOMParserTest(String name) {
  50         super(name);
  51     }
  52 
  53     /**
  54      * @param args the command line arguments
  55      */
  56     public static void main(String[] args) {
  57         XOMParserTest test = new XOMParserTest("XOM parser test");
  58         test.setUp();
  59         test.testTransform();
  60         test.tearDown();
  61     }
  62 
  63     public final void testTransform() {
  64         String inFilename = filePath + "/JDK8022548.xml";
  65         String xslFilename = filePath + "/JDK8022548.xsl";
  66         String outFilename = "JDK8022548.out";
  67 
  68         try (InputStream xslInput = new FileInputStream(xslFilename);
  69              InputStream xmlInput = new FileInputStream(inFilename);
  70              OutputStream out = new FileOutputStream(outFilename);
  71         ) {
  72 
  73 
  74             StringWriter sw = new StringWriter();
  75             // Create transformer factory
  76             TransformerFactory factory = TransformerFactory.newInstance();
  77 
  78             // Use the factory to create a template containing the xsl file
  79             Templates template = factory.newTemplates(new StreamSource(xslInput));
  80             // Use the template to create a transformer
  81             Transformer xformer = template.newTransformer();
  82             // Prepare the input and output files
  83             Source source = new StreamSource(xmlInput);
  84             Result result = new StreamResult(outFilename);
  85             //Result result = new StreamResult(sw);
  86             // Apply the xsl file to the source file and write the result to the output file
  87             xformer.transform(source, result);
  88 
  89             /**
  90              * String out = sw.toString(); if (out.indexOf("<p>") < 0 ) {
  91              * fail(out); }
  92              */
  93             String canonicalizedFileName = outFilename + ".canonicalized";
  94             canonicalize(outFilename, canonicalizedFileName);
  95         } catch (Exception e) {
  96             // unexpected failure
  97             fail(e.getMessage());
  98         }
  99     }
 100 
 101     public void canonicalize(String inName, String outName) {
 102         try (//FileOutputStream outStream = new FileOutputStream(outName);
 103                 FileInputStream inputStream = new FileInputStream(inName);) {
 104             JDK15XML1_0Parser parser = new JDK15XML1_0Parser();
 105             parser.parse(new InputSource(inputStream));
 106             success("test passed");
 107         } catch (Exception e) {
 108             fail(e.getMessage());
 109         }
 110 
 111     }
 112 
 113     class JDK15XML1_0Parser extends SAXParser {
 114 
 115         JDK15XML1_0Parser() throws org.xml.sax.SAXException {
 116 
 117             super(new DTDConfiguration());
 118             // workaround for Java 1.5 beta 2 bugs
 119             com.sun.org.apache.xerces.internal.util.SecurityManager manager =
 120                     new com.sun.org.apache.xerces.internal.util.SecurityManager();
 121             setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY, manager);
 122 
 123         }
 124     }
 125 }