< prev index next >
test/javax/xml/jaxp/unittest/transform/TransformerTest.java
Print this page
rev 1024 : 8169631: [JAXP] XALAN: transformation of XML via namespace-unaware SAX input yields a different result than namespace-unaware DOM input
rev 1025 : 8023653: [JAXP] xalan inconsistently parses DOMSource and StreamSource
@@ -29,11 +29,10 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
-import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -72,38 +71,24 @@
* @test
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm -DrunSecMngr=true transform.TransformerTest
* @run testng/othervm transform.TransformerTest
* @summary Transformer Tests
- * @bug 6272879 6305029 6505031 8150704 8162598 8169631
+ * @bug 6272879 6305029 6505031 8023653 8150704 8162598 8169631
*/
@Listeners({jaxp.library.FilePolicy.class})
public class TransformerTest {
- private Transformer createTransformer() throws TransformerException {
- return TransformerFactory.newInstance().newTransformer();
- }
-
- private Transformer createTransformerFromInputstream(InputStream xslStream) throws TransformerException {
- return TransformerFactory.newInstance().newTransformer(new StreamSource(xslStream));
- }
-
- private Transformer createTransformerFromResource(String xslResource) throws TransformerException {
- return TransformerFactory.newInstance().newTransformer(new StreamSource(getClass().getResource(xslResource).toString()));
- }
-
- private Document transformInputStreamToDocument(Transformer transformer, InputStream sourceStream) throws TransformerException {
- DOMResult response = new DOMResult();
- transformer.transform(new StreamSource(sourceStream), response);
- return (Document)response.getNode();
- }
+ private static final String NAMESPACES = "http://xml.org/sax/features/namespaces";
+ private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
+ /*
private StringWriter transformResourceToStringWriter(Transformer transformer, String xmlResource) throws TransformerException {
StringWriter sw = new StringWriter();
transformer.transform(new StreamSource(getClass().getResource(xmlResource).toString()), new StreamResult(sw));
return sw;
}
-
+*/
/**
* Reads the contents of the given file into a string.
* WARNING: this method adds a final line feed even if the last line of the file doesn't contain one.
*
* @param f
@@ -158,12 +143,10 @@
Assert.assertEquals(c.getNamespaceURI(), nsc, "unexpected namespace for " + testNodeName + "->b->c");
}
}
private class XMLReaderFor6305029 implements XMLReader {
- private static final String NAMESPACES = "http://xml.org/sax/features/namespaces";
- private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
private boolean namespaces = true;
private boolean namespacePrefixes = false;
private EntityResolver resolver;
private DTDHandler dtdHandler;
private ContentHandler contentHandler;
@@ -305,14 +288,19 @@
System.out.println("Source before transformation:");
System.out.println("=============================");
System.out.println(sourceXml);
System.out.println();
+ // transform to DOM result
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer t = tf.newTransformer(new StreamSource(new ByteArrayInputStream(xsl.getBytes())));
+ DOMResult result = new DOMResult();
+ t.transform(new StreamSource(new ByteArrayInputStream(sourceXml.getBytes())), result);
+ Document document = (Document)result.getNode();
+
System.out.println("Result after transformation:");
System.out.println("============================");
- Document document = transformInputStreamToDocument(createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())),
- new ByteArrayInputStream(sourceXml.getBytes()));
OutputFormat format = new OutputFormat();
format.setIndenting(true);
new XMLSerializer(System.out, format).serialize(document);
System.out.println();
@@ -338,51 +326,190 @@
final String XML_DOCUMENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<prefix:localName xmlns:prefix=\"namespaceUri\"/>";
// test SAXSource
SAXSource saxSource = new SAXSource(new XMLReaderFor6305029(), new InputSource());
StringWriter resultWriter = new StringWriter();
- createTransformer().transform(saxSource, new StreamResult(resultWriter));
+ TransformerFactory tf = TransformerFactory.newInstance();
+ tf.newTransformer().transform(saxSource, new StreamResult(resultWriter));
AssertJUnit.assertEquals("Identity transform of SAXSource", XML_DOCUMENT, resultWriter.toString());
// test StreamSource
StreamSource streamSource = new StreamSource(new StringReader(XML_DOCUMENT));
resultWriter = new StringWriter();
- createTransformer().transform(streamSource, new StreamResult(resultWriter));
+ tf.newTransformer().transform(streamSource, new StreamResult(resultWriter));
AssertJUnit.assertEquals("Identity transform of StreamSource", XML_DOCUMENT, resultWriter.toString());
}
/*
* @bug 6505031
* @summary Test transformer parses keys and their values coming from different xml documents.
*/
@Test
public final void testBug6505031() throws TransformerException {
- Transformer transformer = createTransformerFromResource("transform.xsl");
- transformer.setParameter("config", getClass().getResource("config.xml").toString());
- transformer.setParameter("mapsFile", getClass().getResource("maps.xml").toString());
- String s = transformResourceToStringWriter(transformer, "template.xml").toString();
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer t = tf.newTransformer(new StreamSource(getClass().getResource("transform.xsl").toString()));
+ t.setParameter("config", getClass().getResource("config.xml").toString());
+ t.setParameter("mapsFile", getClass().getResource("maps.xml").toString());
+ StringWriter sw = new StringWriter();
+ t.transform(new StreamSource(getClass().getResource("template.xml").toString()), new StreamResult(sw));
+ String s = sw.toString();
Assert.assertTrue(s.contains("map1key1value") && s.contains("map2key1value"));
}
/*
+ * @bug 8023653 8169631
+ * @summary Test combinations of namespace awareness settings on XSL transformations
+ */
+ @Test
+ public final void testBug8023653And8169631() throws IOException, TransformerException, SAXException, ParserConfigurationException {
+ final String LINE_SEPARATOR = getSystemProperty("line.separator");
+
+ final String xsl =
+ "<?xml version=\"1.0\"?>" + LINE_SEPARATOR +
+ "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR +
+ " <xsl:template match=\"/\">" + LINE_SEPARATOR +
+ " <xsl:variable name=\"Counter\" select=\"count(//row)\"/>" + LINE_SEPARATOR +
+ " <Counter><xsl:value-of select=\"$Counter\"/></Counter>" + LINE_SEPARATOR +
+ " </xsl:template>" + LINE_SEPARATOR +
+ "</xsl:stylesheet>" + LINE_SEPARATOR;
+
+ final String sourceXml =
+ "<?xml version=\"1.0\"?>" + LINE_SEPARATOR +
+ "<envelope xmlns=\"http://www.sap.com/myns\">" + LINE_SEPARATOR +
+ " <row>1</row>" + LINE_SEPARATOR +
+ " <row>2</row>" + LINE_SEPARATOR +
+ " <row>3</row>" + LINE_SEPARATOR +
+ "</envelope>" + LINE_SEPARATOR;
+
+ System.out.println("Stylesheet:");
+ System.out.println("=============================");
+ System.out.println(xsl);
+ System.out.println();
+
+ System.out.println("Source before transformation:");
+ System.out.println("=============================");
+ System.out.println(sourceXml);
+ System.out.println();
+
+ // create default transformer (namespace aware)
+ TransformerFactory tf1 = TransformerFactory.newInstance();
+ Transformer t1 = tf1.newTransformer(new StreamSource(new ByteArrayInputStream(xsl.getBytes())));
+
+ // create transformer with namespaces feature set to false
+ TransformerFactory tf2 = TransformerFactory.newInstance();
+ tf2.setFeature(NAMESPACES, false);
+ Transformer t2 = tf2.newTransformer(new StreamSource(new ByteArrayInputStream(xsl.getBytes())));
+
+ // test transformation from stream source with namespace support
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ t1.transform(new StreamSource(new ByteArrayInputStream(sourceXml.getBytes())),
+ new StreamResult(baos));
+ String resFromStream = baos.toString();
+
+ // test transformation from stream source without namespace support
+ baos.reset();
+ t2.transform(new StreamSource(new ByteArrayInputStream(sourceXml.getBytes())),
+ new StreamResult(baos));
+ String resFromStreamWONS = baos.toString();
+
+ // now use transformers vice versa
+
+ // test transformation from DOM source with namespace support
+ baos.reset();
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+ Document doc = dbf.newDocumentBuilder().parse(new InputSource(new ByteArrayInputStream(sourceXml.getBytes())));
+ t2.transform(new DOMSource(doc), new StreamResult(baos));
+ String resFromDOM = baos.toString();
+
+ // test transformation from DOM source without namespace support
+ baos.reset();
+ dbf.setNamespaceAware(false);
+ doc = dbf.newDocumentBuilder().parse(new InputSource(new ByteArrayInputStream(sourceXml.getBytes())));
+ t1.transform(new DOMSource(doc), new StreamResult(baos));
+ String resFromDOMWONS = baos.toString();
+
+ // test transformation from SAX source with namespace support
+ baos.reset();
+ SAXParserFactory spf = SAXParserFactory.newInstance();
+ spf.setNamespaceAware(true);
+ XMLReader xmlr = spf.newSAXParser().getXMLReader();
+ t2.transform(new SAXSource(xmlr, new InputSource(new ByteArrayInputStream(sourceXml.getBytes()))), new StreamResult(baos));
+ String resFromSAX = baos.toString();
+
+ // test transformation from SAX source without namespace support
+ baos.reset();
+ spf.setNamespaceAware(false);
+ xmlr = spf.newSAXParser().getXMLReader();
+ t1.transform(new SAXSource(xmlr, new InputSource(new ByteArrayInputStream(sourceXml.getBytes()))), new StreamResult(baos));
+ String resFromSAXWONS = baos.toString();
+
+ System.out.println("Result after transformation from StreamSource with namespace support:");
+ System.out.println("=====================================================================");
+ System.out.println(resFromStream);
+ System.out.println();
+
+ System.out.println("Result after transformation from StreamSource without namespace support:");
+ System.out.println("========================================================================");
+ System.out.println(resFromStreamWONS);
+ System.out.println();
+
+ System.out.println("Result after transformation from DOMSource with namespace support:");
+ System.out.println("==================================================================");
+ System.out.println(resFromDOM);
+ System.out.println();
+
+ System.out.println("Result after transformation from DOMSource without namespace support:");
+ System.out.println("=====================================================================");
+ System.out.println(resFromDOMWONS);
+ System.out.println();
+
+ System.out.println("Result after transformation from SAXSource with namespace support:");
+ System.out.println("==================================================================");
+ System.out.println(resFromSAX);
+ System.out.println();
+
+ System.out.println("Result after transformation from SAXSource without namespace support:");
+ System.out.println("=====================================================================");
+ System.out.println(resFromSAXWONS);
+ System.out.println();
+
+ Assert.assertEquals(resFromStream.contains("<Counter>0</Counter>"), true,
+ "Output from Stream with namespace support should count 0");
+ Assert.assertEquals(resFromStreamWONS.contains("<Counter>3</Counter>"), true,
+ "Output from Stream without namespace support should count 3");
+ Assert.assertEquals(resFromDOM.contains("<Counter>0</Counter>"), true,
+ "Output from DOM with namespace support should count 0");
+ Assert.assertEquals(resFromDOMWONS.contains("<Counter>3</Counter>"), true,
+ "Output from DOM without namespace support should count 3");
+ Assert.assertEquals(resFromSAX.contains("<Counter>0</Counter>"), true,
+ "Output from SAX with namespace support should count 0");
+ Assert.assertEquals(resFromSAXWONS.contains("<Counter>3</Counter>"), true,
+ "Output from SAX without namespace support should count 3");
+ }
+
+ /*
* @bug 8150704
* @summary Test that XSL transformation with lots of temporary result trees will not run out of DTM IDs.
*/
@Test
public final void testBug8150704() throws TransformerException, IOException {
System.out.println("Testing transformation of Bug8150704-1.xml...");
- Transformer transformer = createTransformerFromResource("Bug8150704-1.xsl");
- StringWriter result = transformResourceToStringWriter(transformer, "Bug8150704-1.xml");
- String resultstring = result.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer t = tf.newTransformer(new StreamSource(getClass().getResource("Bug8150704-1.xsl").toString()));
+ StringWriter sw = new StringWriter();
+ t.transform(new StreamSource(getClass().getResource("Bug8150704-1.xml").toString()), new StreamResult(sw));
+ String resultstring = sw.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
String reference = getFileContentAsString(new File(getClass().getResource("Bug8150704-1.ref").getPath()));
Assert.assertEquals(resultstring, reference, "Output of transformation of Bug8150704-1.xml does not match reference");
System.out.println("Passed.");
System.out.println("Testing transformation of Bug8150704-2.xml...");
- transformer = createTransformerFromResource("Bug8150704-2.xsl");
- result = transformResourceToStringWriter(transformer, "Bug8150704-2.xml");
- resultstring = result.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
+ t = tf.newTransformer(new StreamSource(getClass().getResource("Bug8150704-2.xsl").toString()));
+ sw = new StringWriter();
+ t.transform(new StreamSource(getClass().getResource("Bug8150704-2.xml").toString()), new StreamResult(sw));
+ resultstring = sw.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
reference = getFileContentAsString(new File(getClass().getResource("Bug8150704-2.ref").getPath()));
Assert.assertEquals(resultstring, reference, "Output of transformation of Bug8150704-2.xml does not match reference");
System.out.println("Passed.");
}
@@ -425,15 +552,19 @@
System.out.println("Source before transformation:");
System.out.println("=============================");
System.out.println(sourceXml);
System.out.println();
+ // transform to DOM result
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer t = tf.newTransformer(new StreamSource(new ByteArrayInputStream(xsl.getBytes())));
+ DOMResult result = new DOMResult();
+ t.transform(new StreamSource(new ByteArrayInputStream(sourceXml.getBytes())), result);
+ Document document = (Document)result.getNode();
+
System.out.println("Result after transformation:");
System.out.println("============================");
- Document document = transformInputStreamToDocument(
- createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())),
- new ByteArrayInputStream(sourceXml.getBytes()));
OutputFormat format = new OutputFormat();
format.setIndenting(true);
new XMLSerializer(System.out, format).serialize(document);
System.out.println();
checkNodeNS8162598(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null);
@@ -441,75 +572,6 @@
checkNodeNS8162598(document.getElementsByTagName("test3").item(0), null, null, null);
checkNodeNS8162598(document.getElementsByTagName("test4").item(0), null, null, null);
checkNodeNS8162598(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null);
Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(), "unexpected namespace for test6");
}
-
- /*
- * @bug 8169631
- * @summary Test non-namespace aware XSL transformations
- */
- @Test
- public final void testBug8169631() throws IOException, TransformerException, SAXException, ParserConfigurationException {
- final String LINE_SEPARATOR = getSystemProperty("line.separator");
-
- final String xsl =
- "<?xml version=\"1.0\"?>" + LINE_SEPARATOR +
- "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR +
- " <xsl:template match=\"/\">" + LINE_SEPARATOR +
- " <xsl:variable name=\"Counter\" select=\"count(//row)\"/>" + LINE_SEPARATOR +
- " <Counter><xsl:value-of select=\"$Counter\"/></Counter>" + LINE_SEPARATOR +
- " </xsl:template>" + LINE_SEPARATOR +
- "</xsl:stylesheet>" + LINE_SEPARATOR;
-
- final String sourceXml =
- "<?xml version=\"1.0\"?>" + LINE_SEPARATOR +
- "<envelope xmlns=\"http://www.sap.com/myns\">" + LINE_SEPARATOR +
- " <row>1</row>" + LINE_SEPARATOR +
- " <row>2</row>" + LINE_SEPARATOR +
- " <row>3</row>" + LINE_SEPARATOR +
- "</envelope>" + LINE_SEPARATOR;
-
- System.out.println("Stylesheet:");
- System.out.println("=============================");
- System.out.println(xsl);
- System.out.println();
-
- System.out.println("Source before transformation:");
- System.out.println("=============================");
- System.out.println(sourceXml);
- System.out.println();
-
- // create transformer and resources
- Transformer t = createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes()));
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- // transform from DOM source
- //DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- //dbf.setNamespaceAware(false);
- Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().
- parse(new InputSource(new ByteArrayInputStream(sourceXml.getBytes())));
- t.transform(new DOMSource(doc), new StreamResult(baos));
- String resFromDOM = baos.toString();
- baos.reset();
-
- // transform from SAX source
- //SAXParserFactory spf = SAXParserFactory.newInstance();
- //spf.setNamespaceAware(false);
- XMLReader xmlr = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
- t.transform(new SAXSource(xmlr, new InputSource(new ByteArrayInputStream(sourceXml.getBytes()))), new StreamResult(baos));
- String resFromSAX = baos.toString();
-
- System.out.println("Result after transformation from DOMSource:");
- System.out.println("============================");
- System.out.println(resFromDOM);
- System.out.println();
-
- System.out.println("Result after transformation from SAXSource:");
- System.out.println("============================");
- System.out.println(resFromSAX);
- System.out.println();
-
- Assert.assertEquals(resFromDOM.contains("3"), true, "Output from DOM should contain number 3");
- Assert.assertEquals(resFromSAX.contains("3"), true, "Output from SAX should contain number 3");
- }
}
< prev index next >