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 com.sun.org.apache.xml.internal.serialize.OutputFormat;
  27 import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
  28 
  29 import java.io.BufferedReader;
  30 import java.io.ByteArrayInputStream;
  31 import java.io.File;
  32 import java.io.FileReader;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.StringReader;
  36 import java.io.StringWriter;
  37 
  38 import javax.xml.transform.Transformer;
  39 import javax.xml.transform.TransformerException;
  40 import javax.xml.transform.TransformerFactory;
  41 import javax.xml.transform.dom.DOMResult;
  42 import javax.xml.transform.sax.SAXSource;
  43 import javax.xml.transform.stream.StreamResult;
  44 import javax.xml.transform.stream.StreamSource;
  45 
  46 import org.testng.Assert;
  47 import org.testng.AssertJUnit;
  48 import org.testng.annotations.Test;
  49 import org.w3c.dom.Document;
  50 import org.w3c.dom.Node;
  51 import org.w3c.dom.NodeList;
  52 import org.xml.sax.ContentHandler;
  53 import org.xml.sax.DTDHandler;
  54 import org.xml.sax.EntityResolver;
  55 import org.xml.sax.ErrorHandler;
  56 import org.xml.sax.InputSource;
  57 import org.xml.sax.SAXException;
  58 import org.xml.sax.SAXNotRecognizedException;
  59 import org.xml.sax.SAXNotSupportedException;
  60 import org.xml.sax.XMLReader;
  61 import org.xml.sax.helpers.AttributesImpl;
  62 
  63 /*
  64  * @summary Transformer Tests
  65  * @bug 6272879 6305029 6505031 8150704 8162598 8162598
  66  */
  67 public class TransformerTest {
  68     private Transformer createTransformer() throws TransformerException {
  69         return TransformerFactory.newInstance().newTransformer();
  70     }
  71 
  72     private Transformer createTransformerFromInputstream(InputStream xslStream) throws TransformerException {
  73         return TransformerFactory.newInstance().newTransformer(new StreamSource(xslStream));
  74     }
  75 
  76     private Transformer createTransformerFromResource(String xslResource) throws TransformerException {
  77         return TransformerFactory.newInstance().newTransformer(new StreamSource(getClass().getResource(xslResource).toString()));
  78     }
  79 
  80     private Document transformInputStreamToDocument(Transformer transformer, InputStream sourceStream) throws TransformerException {
  81         DOMResult response = new DOMResult();
  82         transformer.transform(new StreamSource(sourceStream), response);
  83         return (Document)response.getNode();
  84     }
  85 
  86     private StringWriter transformResourceToStringWriter(Transformer transformer, String xmlResource) throws TransformerException {
  87         StringWriter sw = new StringWriter();
  88         transformer.transform(new StreamSource(getClass().getResource(xmlResource).toString()), new StreamResult(sw));
  89         return sw;
  90     }
  91 
  92     /**
  93      * Reads the contents of the given file into a string.
  94      * WARNING: this method adds a final line feed even if the last line of the file doesn't contain one.
  95      *
  96      * @param f
  97      * The file to read
  98      * @return The content of the file as a string, with line terminators as \"n"
  99      * for all platforms
 100      * @throws IOException
 101      * If there was an error reading
 102      */
 103     private String getFileContentAsString(File f) throws IOException {
 104         try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
 105             String line;
 106             StringBuilder sb = new StringBuilder();
 107             while ((line = reader.readLine()) != null) {
 108                 sb.append(line).append("\n");
 109             }
 110             return sb.toString();
 111         }
 112     }
 113 
 114     private void checkNodeNS8162598(Node test, String nstest, String nsb, String nsc) {
 115         String testNodeName = test.getNodeName();
 116         if (nstest == null) {
 117             Assert.assertNull(test.getNamespaceURI(), "unexpected namespace for " + testNodeName);
 118         } else {
 119             Assert.assertEquals(test.getNamespaceURI(), nstest, "unexpected namespace for " + testNodeName);
 120         }
 121         Node b = test.getChildNodes().item(0);
 122         if (nsb == null) {
 123             Assert.assertNull(b.getNamespaceURI(), "unexpected namespace for " + testNodeName + "->b");
 124         } else {
 125             Assert.assertEquals(b.getNamespaceURI(), nsb, "unexpected namespace for " + testNodeName + "->b");
 126         }
 127         Node c = b.getChildNodes().item(0);
 128         if (nsc == null) {
 129             Assert.assertNull(c.getNamespaceURI(), "unexpected namespace for " + testNodeName + "->b->c");
 130         } else {
 131             Assert.assertEquals(c.getNamespaceURI(), nsc, "unexpected namespace for " + testNodeName + "->b->c");
 132         }
 133     }
 134 
 135     private class XMLReaderFor6305029 implements XMLReader {
 136         private static final String NAMESPACES = "http://xml.org/sax/features/namespaces";
 137         private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
 138         private boolean namespaces = true;
 139         private boolean namespacePrefixes = false;
 140         private EntityResolver resolver;
 141         private DTDHandler dtdHandler;
 142         private ContentHandler contentHandler;
 143         private ErrorHandler errorHandler;
 144 
 145         public boolean getFeature(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
 146             if (name.equals(NAMESPACES)) {
 147                 return namespaces;
 148             } else if (name.equals(NAMESPACE_PREFIXES)) {
 149                 return namespacePrefixes;
 150             } else {
 151                 throw new SAXNotRecognizedException();
 152             }
 153         }
 154 
 155         public void setFeature(final String name, final boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
 156             if (name.equals(NAMESPACES)) {
 157                 namespaces = value;
 158             } else if (name.equals(NAMESPACE_PREFIXES)) {
 159                 namespacePrefixes = value;
 160             } else {
 161                 throw new SAXNotRecognizedException();
 162             }
 163         }
 164 
 165         public Object getProperty(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
 166             return null;
 167         }
 168 
 169         public void setProperty(final String name, final Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
 170         }
 171 
 172         public void setEntityResolver(final EntityResolver theResolver) {
 173             this.resolver = theResolver;
 174         }
 175 
 176         public EntityResolver getEntityResolver() {
 177             return resolver;
 178         }
 179 
 180         public void setDTDHandler(final DTDHandler theHandler) {
 181             dtdHandler = theHandler;
 182         }
 183 
 184         public DTDHandler getDTDHandler() {
 185             return dtdHandler;
 186         }
 187 
 188         public void setContentHandler(final ContentHandler handler) {
 189             contentHandler = handler;
 190         }
 191 
 192         public ContentHandler getContentHandler() {
 193             return contentHandler;
 194         }
 195 
 196         public void setErrorHandler(final ErrorHandler handler) {
 197             errorHandler = handler;
 198         }
 199 
 200         public ErrorHandler getErrorHandler() {
 201             return errorHandler;
 202         }
 203 
 204         public void parse(final InputSource input) throws IOException, SAXException {
 205             parse();
 206         }
 207 
 208         public void parse(final String systemId) throws IOException, SAXException {
 209             parse();
 210         }
 211 
 212         private void parse() throws SAXException {
 213             contentHandler.startDocument();
 214             contentHandler.startPrefixMapping("prefix", "namespaceUri");
 215 
 216             AttributesImpl atts = new AttributesImpl();
 217             if (namespacePrefixes) {
 218                 atts.addAttribute("", "xmlns:prefix", "xmlns:prefix", "CDATA", "namespaceUri");
 219             }
 220 
 221             contentHandler.startElement("namespaceUri", "localName", namespacePrefixes ? "prefix:localName" : "", atts);
 222             contentHandler.endElement("namespaceUri", "localName", namespacePrefixes ? "prefix:localName" : "");
 223             contentHandler.endPrefixMapping("prefix");
 224             contentHandler.endDocument();
 225         }
 226     }
 227 
 228     /*
 229      * @bug 6272879
 230      * @summary Test for JDK-6272879
 231      */
 232     @Test
 233     public final void testBug6272879() throws IOException, TransformerException {
 234         final String LINE_SEPARATOR = System.getProperty("line.separator");
 235 
 236         final String xsl =
 237                 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + LINE_SEPARATOR +
 238                 "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + LINE_SEPARATOR +
 239                 "<xsl:output method=\"xml\" indent=\"no\" encoding=\"ISO-8859-1\"/>" + LINE_SEPARATOR +
 240                 "<xsl:template match=\"/\">" + LINE_SEPARATOR +
 241                 "<xsl:element name=\"TransformateurXML\">" + LINE_SEPARATOR +
 242                 "  <xsl:for-each select=\"XMLUtils/test\">" + LINE_SEPARATOR +
 243                 "  <xsl:element name=\"test2\">" + LINE_SEPARATOR +
 244                 "    <xsl:element name=\"valeur2\">" + LINE_SEPARATOR +
 245                 "      <xsl:attribute name=\"attribut2\">" + LINE_SEPARATOR +
 246                 "        <xsl:value-of select=\"valeur/@attribut\"/>" + LINE_SEPARATOR +
 247                 "      </xsl:attribute>" + LINE_SEPARATOR +
 248                 "      <xsl:value-of select=\"valeur\"/>" + LINE_SEPARATOR +
 249                 "    </xsl:element>" + LINE_SEPARATOR +
 250                 "  </xsl:element>" + LINE_SEPARATOR +
 251                 "  </xsl:for-each>" + LINE_SEPARATOR +
 252                 "</xsl:element>" + LINE_SEPARATOR +
 253                 "</xsl:template>" + LINE_SEPARATOR +
 254                 "</xsl:stylesheet>";
 255 
 256         final String sourceXml =
 257                 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + LINE_SEPARATOR +
 258                 // "<!DOCTYPE XMLUtils [" + LINE_SEPARATOR +
 259                 // "<!ELEMENT XMLUtils (test*)>" + LINE_SEPARATOR +
 260                 // "<!ELEMENT test (valeur*)>" + LINE_SEPARATOR +
 261                 // "<!ELEMENT valeur (#PCDATA)>" + LINE_SEPARATOR +
 262                 // "<!ATTLIST valeur attribut CDATA #REQUIRED>]>" +
 263                 // LINE_SEPARATOR +
 264                 "<XMLUtils>" + LINE_SEPARATOR +
 265                 "  <test>" + LINE_SEPARATOR +
 266                 "    <valeur attribut=\"Attribut 1\">Valeur 1</valeur>" + LINE_SEPARATOR +
 267                 "  </test>" + LINE_SEPARATOR +
 268                 "  <test>" + LINE_SEPARATOR +
 269                 "    <valeur attribut=\"Attribut 2\">Valeur 2</valeur>" + LINE_SEPARATOR +
 270                 "  </test>" + LINE_SEPARATOR +
 271                 "</XMLUtils>";
 272 
 273         System.out.println("Stylesheet:");
 274         System.out.println("=============================");
 275         System.out.println(xsl);
 276         System.out.println();
 277 
 278         System.out.println("Source before transformation:");
 279         System.out.println("=============================");
 280         System.out.println(sourceXml);
 281         System.out.println();
 282 
 283         System.out.println("Result after transformation:");
 284         System.out.println("============================");
 285         Document document = transformInputStreamToDocument(createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())),
 286             new ByteArrayInputStream(sourceXml.getBytes()));
 287         OutputFormat format = new OutputFormat();
 288         format.setIndenting(true);
 289         new XMLSerializer(System.out, format).serialize(document);
 290         System.out.println();
 291 
 292         System.out.println("Node content for element valeur2:");
 293         System.out.println("=================================");
 294         NodeList nodes = document.getElementsByTagName("valeur2");
 295         for (int i = 0; i < nodes.getLength(); i++) {
 296             Node node = nodes.item(i);
 297             System.out.println("  Node value: " + node.getFirstChild().getNodeValue());
 298             System.out.println("  Node attribute: " + node.getAttributes().item(0).getNodeValue());
 299 
 300             AssertJUnit.assertEquals("Node value mismatch", "Valeur " + (i + 1), node.getFirstChild().getNodeValue());
 301             AssertJUnit.assertEquals("Node attribute mismatch", "Attribut " + (i + 1), node.getAttributes().item(0).getNodeValue());
 302         }
 303     }
 304 
 305     /*
 306      * @bug 6305029
 307      * @summary Test for JDK-6305029
 308      */
 309     @Test
 310     public final void testBug6305029() throws TransformerException {
 311         final String XML_DOCUMENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<prefix:localName xmlns:prefix=\"namespaceUri\"/>";
 312 
 313         // test SAXSource
 314         SAXSource saxSource = new SAXSource(new XMLReaderFor6305029(), new InputSource());
 315         StringWriter resultWriter = new StringWriter();
 316         createTransformer().transform(saxSource, new StreamResult(resultWriter));
 317         AssertJUnit.assertEquals("Identity transform of SAXSource", XML_DOCUMENT, resultWriter.toString());
 318 
 319         // test StreamSource
 320         StreamSource streamSource = new StreamSource(new StringReader(XML_DOCUMENT));
 321         resultWriter = new StringWriter();
 322         createTransformer().transform(streamSource, new StreamResult(resultWriter));
 323         AssertJUnit.assertEquals("Identity transform of StreamSource", XML_DOCUMENT, resultWriter.toString());
 324     }
 325 
 326     /*
 327      * @bug 6505031
 328      * @summary Test transformer parses keys and their values coming from different xml documents.
 329      */
 330     @Test
 331     public final void testBug6505031() throws TransformerException {
 332         Transformer transformer = createTransformerFromResource("transform.xsl");
 333         transformer.setParameter("config", getClass().getResource("config.xml").toString());
 334         transformer.setParameter("mapsFile", getClass().getResource("maps.xml").toString());
 335         String s = transformResourceToStringWriter(transformer, "template.xml").toString();
 336         Assert.assertTrue(s.contains("map1key1value") && s.contains("map2key1value"));
 337     }
 338 
 339     /*
 340      * @bug 8150704
 341      * @summary Test that XSL transformation with lots of temporary result trees will not run out of DTM IDs.
 342      */
 343     @Test
 344     public final void testBug8150704() throws TransformerException, IOException {
 345         System.out.println("Testing transformation of Bug8150704-1.xml...");
 346         Transformer transformer = createTransformerFromResource("Bug8150704-1.xsl");
 347         StringWriter result = transformResourceToStringWriter(transformer, "Bug8150704-1.xml");
 348         String resultstring = result.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
 349         String reference = getFileContentAsString(new File(getClass().getResource("Bug8150704-1.ref").getPath()));
 350         Assert.assertEquals(resultstring, reference, "Output of transformation of Bug8150704-1.xml does not match reference");
 351         System.out.println("Passed.");
 352 
 353         System.out.println("Testing transformation of Bug8150704-2.xml...");
 354         transformer = createTransformerFromResource("Bug8150704-2.xsl");
 355         result = transformResourceToStringWriter(transformer, "Bug8150704-2.xml");
 356         resultstring = result.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
 357         reference = getFileContentAsString(new File(getClass().getResource("Bug8150704-2.ref").getPath()));
 358         Assert.assertEquals(resultstring, reference, "Output of transformation of Bug8150704-2.xml does not match reference");
 359         System.out.println("Passed.");
 360     }
 361 
 362     /*
 363      * @bug 8162598
 364      * @summary Test XSLTC handling of namespaces, especially empty namespace definitions to reset the
 365      *          default namespace
 366      */
 367     @Test
 368     public final void testBug8162598() throws IOException, TransformerException {
 369         final String LINE_SEPARATOR = System.getProperty("line.separator");
 370 
 371         final String xsl =
 372             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR +
 373             "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR +
 374             "    <xsl:template match=\"/\">" + LINE_SEPARATOR +
 375             "        <root xmlns=\"ns1\">" + LINE_SEPARATOR +
 376             "            <xsl:call-template name=\"transform\"/>" + LINE_SEPARATOR +
 377             "        </root>" + LINE_SEPARATOR +
 378             "    </xsl:template>" + LINE_SEPARATOR +
 379             "    <xsl:template name=\"transform\">" + LINE_SEPARATOR +
 380             "        <test1 xmlns=\"ns2\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test1>" + LINE_SEPARATOR +
 381             "        <test2 xmlns=\"ns1\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test2>" + LINE_SEPARATOR +
 382             "        <test3><b><c xmlns=\"\"></c></b></test3>" + LINE_SEPARATOR +
 383             "        <test4 xmlns=\"\"><b><c xmlns=\"\"></c></b></test4>" + LINE_SEPARATOR +
 384             "        <test5 xmlns=\"ns1\"><b><c xmlns=\"\"></c></b></test5>" + LINE_SEPARATOR +
 385             "        <test6 xmlns=\"\"/>" + LINE_SEPARATOR +
 386             "    </xsl:template>" + LINE_SEPARATOR +
 387             "</xsl:stylesheet>";
 388 
 389 
 390         final String sourceXml =
 391                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aaa></aaa>" + LINE_SEPARATOR;
 392 
 393         System.out.println("Stylesheet:");
 394         System.out.println("=============================");
 395         System.out.println(xsl);
 396         System.out.println();
 397 
 398         System.out.println("Source before transformation:");
 399         System.out.println("=============================");
 400         System.out.println(sourceXml);
 401         System.out.println();
 402 
 403         System.out.println("Result after transformation:");
 404         System.out.println("============================");
 405         Document document = transformInputStreamToDocument(
 406             createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())),
 407                                              new ByteArrayInputStream(sourceXml.getBytes()));
 408         OutputFormat format = new OutputFormat();
 409         format.setIndenting(true);
 410         new XMLSerializer(System.out, format).serialize(document);
 411         System.out.println();
 412         checkNodeNS8162598(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null);
 413         checkNodeNS8162598(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null);
 414         checkNodeNS8162598(document.getElementsByTagName("test3").item(0), null, null, null);
 415         checkNodeNS8162598(document.getElementsByTagName("test4").item(0), null, null, null);
 416         checkNodeNS8162598(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null);
 417         Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(), "unexpected namespace for test6");
 418     }
 419 }