1 /*
2  * Copyright (c) 2014, 2016, 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 package transform;
25 
26 import java.io.BufferedReader;
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.StringReader;
34 import java.io.StringWriter;
35 
36 import javax.xml.transform.Result;
37 import javax.xml.transform.Source;
38 import javax.xml.transform.Transformer;
39 import javax.xml.transform.TransformerConfigurationException;
40 import javax.xml.transform.TransformerException;
41 import javax.xml.transform.TransformerFactory;
42 import javax.xml.transform.stream.StreamResult;
43 import javax.xml.transform.stream.StreamSource;
44 
45 import org.testng.Assert;
46 import org.testng.annotations.Listeners;
47 import org.testng.annotations.Test;
48 
49 /*
50  * @test
51  * @bug 6935697
52  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
53  * @run testng/othervm -DrunSecMngr=true transform.BugDB12665704Test
54  * @run testng/othervm transform.BugDB12665704Test
55  * @summary Test Transformer can compile large xsl file.
56  */
57 @Listeners({jaxp.library.FilePolicy.class})
58 public class BugDB12665704Test {
59 
60     @Test
testTransform()61     public final void testTransform() {
62 
63         try {
64             String str = new String();
65             ByteArrayOutputStream byte_stream = new ByteArrayOutputStream();
66             File inputFile = new File(getClass().getResource("BugDB12665704.xml").getPath());
67             FileReader in = new FileReader(inputFile);
68             int c;
69 
70             while ((c = in.read()) != -1) {
71                 str = str + new Character((char) c).toString();
72             }
73 
74             in.close();
75 
76             System.out.println(str);
77             byte buf[] = str.getBytes();
78             byte_stream.write(buf);
79             String style_sheet_uri = "BugDB12665704.xsl";
80             byte[] xml_byte_array = byte_stream.toByteArray();
81             InputStream xml_input_stream = new ByteArrayInputStream(xml_byte_array);
82 
83             Source xml_source = new StreamSource(xml_input_stream);
84 
85             TransformerFactory tFactory = TransformerFactory.newInstance();
86             Transformer transformer = tFactory.newTransformer();
87             StreamSource source = new StreamSource(getClass().getResource(style_sheet_uri).toString());
88             transformer = tFactory.newTransformer(source);
89 
90             ByteArrayOutputStream result_output_stream = new ByteArrayOutputStream();
91             Result result = new StreamResult(result_output_stream);
92             transformer.transform(xml_source, result);
93             result_output_stream.close();
94 
95             // expected success
96         } catch (Exception e) {
97             // unexpected failure
98             e.printStackTrace();
99             Assert.fail(e.toString());
100         }
101     }
102 
103     @Test
testSAPTransform()104     public void testSAPTransform() {
105         StringWriter out = new StringWriter();
106         try {
107             String xml = getXML(getClass().getResource("BugDB12665704.xml").getPath());
108             getTransformer().transform(new StreamSource(new StringReader(xml)), new StreamResult(out));
109         } catch (TransformerConfigurationException ex) {
110             // Trace.dump(xslt);
111             // Trace.dump(xml);
112             System.err.println("can't process xslt: " + ex.getMessage() + " (" + ex + ")");
113         } catch (TransformerException ex) {
114             // Trace.dump(xslt);
115             // Trace.dump(xml);
116             System.err.println("can't process xml: " + ex.getMessage() + " (" + ex + ")");
117         } catch (Exception ex) {
118             // Trace.dump(xslt);
119             // Trace.dump(xml);
120             System.err.println("can't create processor: " + ex.getMessage() + " (" + ex + ")");
121         }
122     }
123 
getTransformer()124     Transformer getTransformer() {
125         Transformer transformer = null;
126         try {
127             InputStream xin = this.getClass().getResourceAsStream("BugDB12665704.xsl");
128             StreamSource xslt = new StreamSource(xin);
129             TransformerFactory fc = TransformerFactory.newInstance();
130             transformer = fc.newTransformer(xslt);
131 
132         } catch (Exception e) {
133             // unexpected failure
134             e.printStackTrace();
135             Assert.fail(e.toString());
136         }
137 
138         return transformer;
139     }
140 
getXML(String sourceFile)141     String getXML(String sourceFile) throws IOException {
142         BufferedReader inputStream = null;
143         StringBuilder sb = new StringBuilder();
144         try {
145             inputStream = new BufferedReader(new FileReader(sourceFile));
146             String l;
147 
148             while ((l = inputStream.readLine()) != null) {
149                 sb.append(l);
150             }
151 
152         } finally {
153             if (inputStream != null) {
154                 inputStream.close();
155             }
156         }
157         return sb.toString();
158     }
159 }
160