1 /* 2 * Copyright (c) 2014, 2017, 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.util; 25 26 import java.io.BufferedReader; 27 import java.io.ByteArrayInputStream; 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.io.InputStreamReader; 31 import java.io.StringReader; 32 import java.io.StringWriter; 33 import java.util.stream.Collectors; 34 35 import javax.xml.parsers.DocumentBuilderFactory; 36 import javax.xml.parsers.ParserConfigurationException; 37 import javax.xml.parsers.SAXParserFactory; 38 import javax.xml.stream.XMLInputFactory; 39 import javax.xml.stream.XMLStreamException; 40 import javax.xml.transform.Templates; 41 import javax.xml.transform.Transformer; 42 import javax.xml.transform.TransformerConfigurationException; 43 import javax.xml.transform.TransformerFactory; 44 import javax.xml.transform.dom.DOMResult; 45 import javax.xml.transform.dom.DOMSource; 46 import javax.xml.transform.sax.SAXSource; 47 import javax.xml.transform.stax.StAXSource; 48 import javax.xml.transform.stream.StreamSource; 49 50 import org.w3c.dom.bootstrap.DOMImplementationRegistry; 51 import org.w3c.dom.ls.DOMImplementationLS; 52 import org.w3c.dom.ls.LSOutput; 53 import org.w3c.dom.ls.LSSerializer; 54 import org.xml.sax.InputSource; 55 import org.xml.sax.SAXException; 56 57 /* 58 * Template for Transformer tests 59 */ 60 public abstract class TransformerTestTemplate { 61 private String xsl = null; 62 private String sourceXml = null; 63 64 public TransformerTestTemplate() { 65 super(); 66 } 67 68 public TransformerTestTemplate(String xsl, String sourceXml) { 69 super(); 70 this.xsl = xsl; 71 this.sourceXml = sourceXml; 72 } 73 74 public String getXsl() { 75 return xsl; 76 } 77 78 public void setXsl(String xsl) { 79 this.xsl = xsl; 80 } 81 82 public String getSourceXml() { 83 return sourceXml; 84 } 85 86 public void setSourceXml(String sourceXml) { 87 this.sourceXml = sourceXml; 88 } 89 90 public String fromInputStream(InputStream is) throws IOException { 91 try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { 92 return br.lines().collect(Collectors.joining("\n")); 93 } 94 } 95 96 // print an XML Snippet under a given title 97 public void printSnippet(String title, String snippet) { 98 StringBuilder div = new StringBuilder(); 99 for (int i = 0; i < title.length(); i++) 100 div.append("="); 101 System.out.println(title + "\n" + div + "\n" + snippet + "\n"); 102 } 103 104 // pretty print a DOMResult 105 public String prettyPrintDOMResult(DOMResult dr) throws ClassNotFoundException, InstantiationException, 106 IllegalAccessException, ClassCastException 107 { 108 DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); 109 DOMImplementationLS domImplementationLS = (DOMImplementationLS)registry.getDOMImplementation("LS"); 110 StringWriter writer = new StringWriter(); 111 LSOutput formattedOutput = domImplementationLS.createLSOutput(); 112 formattedOutput.setCharacterStream(writer); 113 LSSerializer domSerializer = domImplementationLS.createLSSerializer(); 114 domSerializer.getDomConfig().setParameter("format-pretty-print", true); 115 domSerializer.getDomConfig().setParameter("xml-declaration", false); 116 domSerializer.write(dr.getNode(), formattedOutput); 117 return writer.toString(); 118 } 119 120 public Transformer getTransformer() throws TransformerConfigurationException { 121 return getTransformer(null); 122 } 123 124 // utility to obtain Transformer from TransformerFactory 125 public Transformer getTransformer(TransformerFactory tf) 126 throws TransformerConfigurationException 127 { 128 if (tf == null) { 129 tf = TransformerFactory.newInstance(); 130 } 131 132 if (xsl == null) { 133 return tf.newTransformer(); 134 } else { 135 return tf.newTransformer( 136 new StreamSource(new ByteArrayInputStream(xsl.getBytes())) 137 ); 138 } 139 } 140 141 // utility to obtain Templates from TransformerFactory 142 public Templates getTemplates(TransformerFactory tf) 143 throws TransformerConfigurationException 144 { 145 return tf.newTemplates( 146 new StreamSource(new ByteArrayInputStream(xsl.getBytes())) 147 ); 148 } 149 150 // utility to construct StreamSource 151 public StreamSource getStreamSource() { 152 return new StreamSource( 153 new ByteArrayInputStream(sourceXml.getBytes()) 154 ); 155 } 156 157 // utility to construct DOMSource from DocumentBuilderFactory 158 public DOMSource getDOMSource(DocumentBuilderFactory dbf) 159 throws SAXException, IOException, ParserConfigurationException 160 { 161 return new DOMSource( 162 dbf.newDocumentBuilder().parse( 163 new InputSource( 164 new ByteArrayInputStream(sourceXml.getBytes()) 165 ) 166 ) 167 ); 168 } 169 170 // utility to construct SAXSource from SAXParserFactory 171 public SAXSource getSAXSource(SAXParserFactory spf) 172 throws SAXException, ParserConfigurationException 173 { 174 return new SAXSource( 175 spf.newSAXParser().getXMLReader(), 176 new InputSource(new ByteArrayInputStream(sourceXml.getBytes())) 177 ); 178 } 179 180 // utility to construct StAXSource from XMLInputFactory 181 public StAXSource getStAXSource(XMLInputFactory xif) 182 throws XMLStreamException 183 { 184 return new StAXSource( 185 xif.createXMLStreamReader(new StringReader(sourceXml)) 186 ); 187 } 188 }