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