This commit is contained in:
Lana Steuck 2016-10-13 23:02:30 +00:00
commit db278cc21a
11 changed files with 278 additions and 4 deletions

@ -1342,6 +1342,12 @@ public class Parser implements Constants, ContentHandler {
}
else {
SyntaxTreeNode parent = _parentStack.peek();
if (element.getClass().isAssignableFrom(Import.class) &&
parent.notTypeOf(Import.class)) {
ErrorMsg err = new ErrorMsg(ErrorMsg.IMPORT_PRECEDE_OTHERS_ERR,
prefix+':'+localname);
throw new SAXException(err.toString());
}
parent.addElement(element);
element.setParent(parent);
}

@ -523,6 +523,24 @@ public abstract class SyntaxTreeNode implements Constants {
}
}
/**
* Checks whether any children of this node is not of the specified type.
*
* @param type the type to be checked against
* @return true if there is at least one child that is not of the specified
* type, false otherwise.
*/
public boolean notTypeOf(Class<?> type) {
if (_contents.size() > 0) {
for (SyntaxTreeNode item : _contents) {
if (!item.getClass().isAssignableFrom(type)) {
return true;
}
}
}
return false;
}
/**
* Return true if the node represents a simple RTF.
*

@ -273,6 +273,14 @@ public class ErrorMessages extends ListResourceBundle {
{ErrorMsg.CIRCULAR_INCLUDE_ERR,
"Circular import/include. Stylesheet ''{0}'' already loaded."},
/*
* Note to translators: "xsl:import" and "xsl:include" are keywords that
* should not be translated.
*/
{ErrorMsg.IMPORT_PRECEDE_OTHERS_ERR,
"The xsl:import element children must precede all other element children of "
+ "an xsl:stylesheet element, including any xsl:include element children."},
/*
* Note to translators: A result-tree fragment is a portion of a
* resulting XML document represented as a tree. "<xsl:sort>" is a

@ -70,6 +70,7 @@ public final class ErrorMsg {
public static final String STRAY_ATTRIBUTE_ERR = "STRAY_ATTRIBUTE_ERR";
public static final String ILLEGAL_ATTRIBUTE_ERR = "ILLEGAL_ATTRIBUTE_ERR";
public static final String CIRCULAR_INCLUDE_ERR = "CIRCULAR_INCLUDE_ERR";
public static final String IMPORT_PRECEDE_OTHERS_ERR = "IMPORT_PRECEDE_OTHERS_ERR";
public static final String RESULT_TREE_SORT_ERR = "RESULT_TREE_SORT_ERR";
public static final String SYMBOLS_REDEF_ERR = "SYMBOLS_REDEF_ERR";
public static final String XSL_VERSION_ERR = "XSL_VERSION_ERR";

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -305,9 +305,12 @@ public class StAXStream2SAX implements XMLReader, Locator {
if (prefix == null) { // true for default namespace
prefix = "";
}
_sax.startPrefixMapping(
prefix,
staxStreamReader.getNamespaceURI(i));
String uri = staxStreamReader.getNamespaceURI(i);
if (uri == null && prefix.isEmpty()) { // true for default namespace
uri = "";
}
_sax.startPrefixMapping(prefix, uri);
}
// fire startElement

@ -30,7 +30,9 @@ import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
@ -38,6 +40,7 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stax.StAXResult;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
import org.testng.Assert;
import org.testng.annotations.Listeners;
@ -45,6 +48,7 @@ import org.testng.annotations.Test;
/*
* @test
* @bug 8152530
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm -DrunSecMngr=true transform.StAXSourceTest
* @run testng/othervm transform.StAXSourceTest
@ -52,6 +56,33 @@ import org.testng.annotations.Test;
*/
@Listeners({jaxp.library.FilePolicy.class})
public class StAXSourceTest {
/**
* @bug 8152530
* Verifies that StAXSource handles empty namespace properly. NPE was thrown
* before the fix.
* @throws Exception if the test fails
*/
@Test
public final void testStAXSourceWEmptyNS() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<EntityList>\n"
+ " <Entity xmlns=\"\">\n"
+ " </Entity>\n"
+ " <Entity xmlns=\"\">\n"
+ " </Entity>\n"
+ "</EntityList> ";
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
xsr.nextTag();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT && xsr.getLocalName().equals("Entity")) {
StringWriter stringResult = new StringWriter();
t.transform(new StAXSource(xsr), new StreamResult(stringResult));
System.out.println("result: \n" + stringResult.toString());
}
}
@Test
public final void testStAXSource() throws XMLStreamException {

@ -0,0 +1,149 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package transform;
import java.io.StringReader;
import org.xml.sax.InputSource;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.TransformerConfigurationException;
import org.testng.annotations.Listeners;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* @test
* @bug 8058152
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm -DrunSecMngr=true transform.StylesheetTest
* @run testng/othervm transform.StylesheetTest
* @summary this test contains test cases for verifying stylesheet
*/
@Listeners(jaxp.library.FilePolicy.class)
public class StylesheetTest {
/**
* @bug 8058152
* Verifies that an error is reported if the xsl:import element
* is not at the top of the stylesheet.
* @throws TransformerConfigurationException
*/
@Test(dataProvider = "invalidImport", expectedExceptions = TransformerConfigurationException.class)
public void testInvalidImport(String xsl) throws TransformerConfigurationException {
StringReader xsl1 = new StringReader(xsl);
TransformerFactory factory = TransformerFactory.newInstance();
SAXSource xslSource = new SAXSource(new InputSource(xsl1));
Transformer transformer = factory.newTransformer(xslSource);
}
/**
* @bug 8058152
* Verifies that valid xsl:import elements are accepted
* @throws TransformerConfigurationException
*/
@Test(dataProvider = "validImport")
public void testValidImport(String file) throws TransformerConfigurationException {
String xsl = getClass().getResource(file).getFile();
TransformerFactory factory = TransformerFactory.newInstance();
SAXSource xslSource = new SAXSource(new InputSource(xsl));
Transformer transformer = factory.newTransformer(xslSource);
}
/*
DataProvider: for testing with xsl:import placed incorrectly
Data: stylesheet
*/
@DataProvider(name = "invalidImport")
public Object[][] getInvalid() {
return new Object[][]{
// xsl:import after template and include elements
{"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n"
+ "\n"
+ " <xsl:template match=\"content\">\n"
+ " <html>\n"
+ " <xsl:apply-templates/>\n"
+ " </html>\n"
+ " </xsl:template>\n"
+ " \n"
+ " <xsl:include href=\"XSLInclude_header.xsl\"/>\n"
+ "\n"
+ " <xsl:template match=\"content/title\">\n"
+ " <h1>\n"
+ " <xsl:apply-templates/>\n"
+ " </h1>\n"
+ " </xsl:template>\n"
+ " \n"
+ " <xsl:import href=\"XSLInclude_footer.xsl\"/>\n"
+ "\n"
+ "</xsl:stylesheet>"},
// xsl:import inside template
{"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n"
+ "\n"
+ " <xsl:template match=\"content\">\n"
+ " <xsl:import href=\"XSLInclude_header.xsl\"/>"
+ " <html>\n"
+ " <xsl:apply-templates/>\n"
+ " </html>\n"
+ " </xsl:template>\n"
+ "\n"
+ "</xsl:stylesheet>"},
// xsl:import after xsl:include
{"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n"
+ " <xsl:include href=\"XSLInclude_header.xsl\"/>\n"
+ " <xsl:import href=\"XSLInclude_footer.xsl\"/>\n"
+ "\n"
+ " <xsl:template match=\"content/title\">\n"
+ " <h1>\n"
+ " <xsl:apply-templates/>\n"
+ " </h1>\n"
+ " </xsl:template>\n"
+ "\n"
+ "</xsl:stylesheet>"}
};
}
/*
DataProvider: for testing with xsl:import placed correctly
Data: path to stylesheet
*/
@DataProvider(name = "validImport")
public Object[][] getValid() {
return new Object[][]{
// xsl:import at the top
{"XSLInclude_main.xsl"},
// two xsl:import elements at the top
{"XSLImport.xsl"}
};
}
}

@ -0,0 +1,21 @@
<?xml version="1.1" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:import href="XSLInclude_header.xsl"/>
<xsl:import href="XSLInclude_footer.xsl"/>
<xsl:template match="content">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="content/title">
<h1>
<xsl:apply-templates/>
</h1>
</xsl:template>
</xsl:stylesheet>

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="footer">
<dv id="footer"><xsl:apply-templates/></dv>
</xsl:template>
</xsl:stylesheet>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="header">
<h4><xsl:apply-templates/></h4>
</xsl:template>
</xsl:stylesheet>

@ -0,0 +1,20 @@
<?xml version="1.1" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:import href="XSLInclude_header.xsl"/>
<xsl:template match="content">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="content/title">
<h1>
<xsl:apply-templates/>
</h1>
</xsl:template>
<xsl:include href="XSLInclude_footer.xsl"/>
</xsl:stylesheet>