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