# HG changeset patch # User clanger # Date 1469709283 -7200 # Thu Jul 28 14:34:43 2016 +0200 # Node ID 6ccb0753eaba191c8154eee19069e2d7f2436bc5 # Parent 74241304e87b0d463391a8ecab40979b5af86dc2 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/LiteralElement.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/LiteralElement.java --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/LiteralElement.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/LiteralElement.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -9,7 +9,7 @@ * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -17,9 +17,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* - * $Id: LiteralElement.java,v 1.2.4.1 2005/09/13 12:38:33 pvedula Exp $ - */ package com.sun.org.apache.xalan.internal.xsltc.compiler; @@ -58,8 +55,6 @@ // names are not known at compile time. private boolean _allAttributesUnique = false; - private final static String XMLNS_STRING = "xmlns"; - /** * Returns the QName for this literal element */ @@ -140,8 +135,8 @@ // Treat default namespace as "" and not null if (prefix == null) prefix = Constants.EMPTYSTRING; - else if (prefix.equals(XMLNS_STRING)) - return(XMLNS_STRING); + else if (prefix.equals(XMLNS_PREFIX)) + return(XMLNS_PREFIX); // Check if we must translate the prefix final String alternative = stable.lookupPrefixAlias(prefix); @@ -264,7 +259,7 @@ // Ignore special attributes (e.g. xmlns:prefix and xmlns) final String prefix = qname.getPrefix(); if (prefix != null && prefix.equals(XMLNS_PREFIX) || - prefix == null && qname.getLocalPart().equals("xmlns") || + prefix == null && qname.getLocalPart().equals(XMLNS_PREFIX) || uri != null && uri.equals(XSLT_URI)) { continue; @@ -337,9 +332,9 @@ il.append(methodGen.startElement()); // The value of an attribute may depend on a (sibling) variable - int j=0; + int j = 0; while (j < elementCount()) { - final SyntaxTreeNode item = (SyntaxTreeNode) elementAt(j); + final SyntaxTreeNode item = elementAt(j); if (item instanceof Variable) { item.translate(classGen, methodGen); } @@ -348,35 +343,12 @@ // Compile code to emit namespace attributes if (_accessedPrefixes != null) { - boolean declaresDefaultNS = false; - for (Map.Entry entry : _accessedPrefixes.entrySet()) { final String prefix = entry.getKey(); final String uri = entry.getValue(); - - if (uri != Constants.EMPTYSTRING || - prefix != Constants.EMPTYSTRING) - { - if (prefix == Constants.EMPTYSTRING) { - declaresDefaultNS = true; - } - il.append(methodGen.loadHandler()); - il.append(new PUSH(cpg,prefix)); - il.append(new PUSH(cpg,uri)); - il.append(methodGen.namespace()); - } - } - - /* - * If our XslElement parent redeclares the default NS, and this - * element doesn't, it must be redeclared one more time. - */ - if (!declaresDefaultNS && (_parent instanceof XslElement) - && ((XslElement) _parent).declaresDefaultNS()) - { il.append(methodGen.loadHandler()); - il.append(new PUSH(cpg, Constants.EMPTYSTRING)); - il.append(new PUSH(cpg, Constants.EMPTYSTRING)); + il.append(new PUSH(cpg, prefix)); + il.append(new PUSH(cpg, uri)); il.append(methodGen.namespace()); } } diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -17,9 +17,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* - * $Id: Parser.java,v 1.2.4.1 2005/09/13 12:14:32 pvedula Exp $ - */ package com.sun.org.apache.xalan.internal.xsltc.compiler; @@ -37,6 +34,7 @@ import java.io.File; import java.io.IOException; import java.io.StringReader; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -44,7 +42,6 @@ import java.util.Properties; import java.util.Stack; import java.util.StringTokenizer; -import java.util.Vector; import javax.xml.XMLConstants; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; @@ -73,15 +70,15 @@ private Locator _locator = null; - private XSLTC _xsltc; // Reference to the compiler object. - private XPathParser _xpathParser; // Reference to the XPath parser. - private Vector _errors; // Contains all compilation errors - private Vector _warnings; // Contains all compilation errors + private XSLTC _xsltc; // Reference to the compiler object. + private XPathParser _xpathParser; // Reference to the XPath parser. + private ArrayList _errors; // Contains all compilation errors + private ArrayList _warnings; // Contains all compilation warnings private Map _instructionClasses; // Maps instructions to classes private Map _instructionAttrs; // reqd and opt attrs - private Map _qNames; - private Map _namespaces; + private Map _qNames; + private Map> _namespaces; private QName _useAttributeSets; private QName _excludeResultPrefixes; private QName _extensionElementPrefixes; @@ -113,8 +110,8 @@ _instructionAttrs = new HashMap<>(); _variableScope = new HashMap<>(); _template = null; - _errors = new Vector(); - _warnings = new Vector(); + _errors = new ArrayList<>(); + _warnings = new ArrayList<>(); _symbolTable = new SymbolTable(); _xpathParser = new XPathParser(this); _currentStylesheet = null; @@ -139,7 +136,6 @@ public void setOutput(Output output) { if (_output != null) { if (_output.getImportPrecedence() <= output.getImportPrecedence()) { - String cdata = _output.getCdata(); output.mergeOutput(_output); _output.disable(); _output = output; @@ -172,13 +168,14 @@ private void addVariableOrParam(VariableBase var) { Object existing = _variableScope.get(var.getName().getStringRep()); if (existing != null) { - if (existing instanceof Stack) { - Stack stack = (Stack)existing; + if (existing instanceof Stack) { + @SuppressWarnings("unchecked") + Stack stack = (Stack)existing; stack.push(var); } else if (existing instanceof VariableBase) { - Stack stack = new Stack(); - stack.push(existing); + Stack stack = new Stack<>(); + stack.push((VariableBase)existing); stack.push(var); _variableScope.put(var.getName().getStringRep(), stack); } @@ -190,8 +187,8 @@ public void removeVariable(QName name) { Object existing = _variableScope.get(name.getStringRep()); - if (existing instanceof Stack) { - Stack stack = (Stack)existing; + if (existing instanceof Stack) { + Stack stack = (Stack)existing; if (!stack.isEmpty()) stack.pop(); if (!stack.isEmpty()) return; } @@ -201,13 +198,14 @@ public VariableBase lookupVariable(QName name) { Object existing = _variableScope.get(name.getStringRep()); if (existing instanceof VariableBase) { - return((VariableBase)existing); + return (VariableBase)existing; } - else if (existing instanceof Stack) { - Stack stack = (Stack)existing; - return((VariableBase)stack.peek()); + else if (existing instanceof Stack) { + @SuppressWarnings("unchecked") + Stack stack = (Stack)existing; + return stack.peek(); } - return(null); + return null; } public void setXSLTC(XSLTC xsltc) { @@ -397,10 +395,9 @@ try { if (stylesheet != null) { stylesheet.parseContents(this); - final int precedence = stylesheet.getImportPrecedence(); final Iterator elements = stylesheet.elements(); while (elements.hasNext()) { - Object child = elements.next(); + SyntaxTreeNode child = elements.next(); if (child instanceof Text) { final int l = getLineNumber(); ErrorMsg err = @@ -704,8 +701,6 @@ new String[] {"stylesheet-prefix", "result-prefix"}); } - - /** * Initialize the _instructionClasses map, which maps XSL element * names to Java classes in this package. @@ -779,6 +774,7 @@ /** * Add primops and base functions to the symbol table. */ + @SuppressWarnings("unused") private void initSymbolTable() { MethodType I_V = new MethodType(Type.Int, Type.Void); MethodType I_R = new MethodType(Type.Int, Type.Real); @@ -971,12 +967,12 @@ String local, Attributes attributes) { SyntaxTreeNode node = null; - QName qname = getQName(uri, prefix, local); + QName qname = getQName(uri, prefix, local); String className = _instructionClasses.get(qname.getStringRep()); if (className != null) { try { - final Class clazz = ObjectFactory.findProviderClass(className, true); + final Class clazz = ObjectFactory.findProviderClass(className, true); node = (SyntaxTreeNode)clazz.newInstance(); node.setQName(qname); node.setParser(this); @@ -1023,7 +1019,7 @@ else { Stylesheet sheet = _xsltc.getStylesheet(); if ((sheet != null) && (sheet.isExtension(uri))) { - if (sheet != (SyntaxTreeNode)_parentStack.peek()) { + if (sheet != _parentStack.peek()) { node = new UnsupportedElement(uri, prefix, local, true); UnsupportedElement elem = (UnsupportedElement)node; ErrorMsg msg = @@ -1191,7 +1187,7 @@ if (size > 0) { System.err.println(new ErrorMsg(ErrorMsg.COMPILER_ERROR_KEY)); for (int i = 0; i < size; i++) { - System.err.println(" " + _errors.elementAt(i)); + System.err.println(" " + _errors.get(i)); } } } @@ -1204,7 +1200,7 @@ if (size > 0) { System.err.println(new ErrorMsg(ErrorMsg.COMPILER_WARNING_KEY)); for (int i = 0; i < size; i++) { - System.err.println(" " + _warnings.elementAt(i)); + System.err.println(" " + _warnings.get(i)); } } } @@ -1217,42 +1213,42 @@ case Constants.INTERNAL: // Unexpected internal errors, such as null-ptr exceptions, etc. // Immediately terminates compilation, no translet produced - _errors.addElement(error); + _errors.add(error); break; case Constants.UNSUPPORTED: // XSLT elements that are not implemented and unsupported ext. // Immediately terminates compilation, no translet produced - _errors.addElement(error); + _errors.add(error); break; case Constants.FATAL: // Fatal error in the stylesheet input (parsing or content) // Immediately terminates compilation, no translet produced - _errors.addElement(error); + _errors.add(error); break; case Constants.ERROR: // Other error in the stylesheet input (parsing or content) // Does not terminate compilation, no translet produced - _errors.addElement(error); + _errors.add(error); break; case Constants.WARNING: // Other error in the stylesheet input (content errors only) // Does not terminate compilation, a translet is produced - _warnings.addElement(error); + _warnings.add(error); break; } } - public Vector getErrors() { + public ArrayList getErrors() { return _errors; } - public Vector getWarnings() { + public ArrayList getWarnings() { return _warnings; } /************************ SAX2 ContentHandler INTERFACE *****************/ - private Stack _parentStack = null; + private Stack _parentStack = null; private Map _prefixMapping = null; /** @@ -1262,7 +1258,7 @@ _root = null; _target = null; _prefixMapping = null; - _parentStack = new Stack(); + _parentStack = new Stack<>(); } /** @@ -1318,7 +1314,7 @@ _root = element; } else { - SyntaxTreeNode parent = (SyntaxTreeNode)_parentStack.peek(); + SyntaxTreeNode parent = _parentStack.peek(); parent.addElement(element); element.setParent(parent); } @@ -1349,7 +1345,7 @@ */ public void characters(char[] ch, int start, int length) { String string = new String(ch, start, length); - SyntaxTreeNode parent = (SyntaxTreeNode)_parentStack.peek(); + SyntaxTreeNode parent = _parentStack.peek(); if (string.length() == 0) return; diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/SyntaxTreeNode.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/SyntaxTreeNode.java --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/SyntaxTreeNode.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/SyntaxTreeNode.java @@ -73,7 +73,7 @@ protected QName _qname; // The element QName private int _line; // Source file line number protected AttributesImpl _attributes = null; // Attributes of this element - private Map _prefixMapping = null; // Namespace declarations + private Map _prefixMapping = null; // Namespace declarations // Sentinel - used to denote unrecognised syntaxt tree nodes. protected static final SyntaxTreeNode Dummy = new AbsolutePathPattern(null); @@ -828,7 +828,7 @@ * @param pos The child node's position. * @return The child node. */ - protected final Object elementAt(int pos) { + protected final SyntaxTreeNode elementAt(int pos) { return _contents.get(pos); } diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XSLTC.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XSLTC.java --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XSLTC.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XSLTC.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -17,9 +17,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* - * $Id: XSLTC.java,v 1.2.4.1 2005/09/05 09:51:38 pvedula Exp $ - */ package com.sun.org.apache.xalan.internal.xsltc.compiler; @@ -41,6 +38,7 @@ import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.Enumeration; @@ -158,11 +156,11 @@ * Extension function class loader variables */ - /* Class loader reference that will be used to external extension functions loading */ + /* Class loader reference that will be used for external extension functions loading */ private ClassLoader _extensionClassLoader; /** - * HashSet with the loaded classes + * HashMap with the loaded classes */ private final Map _externalExtensionFunctions; @@ -286,7 +284,7 @@ } /* - * Function loads an external external extension functions. + * Function loads an external extension function. * The filtering of function types (external,internal) takes place in FunctionCall class * */ @@ -604,7 +602,7 @@ * Get a Vector containing all compile error messages * @return A Vector containing all compile error messages */ - public Vector getErrors() { + public ArrayList getErrors() { return _parser.getErrors(); } @@ -612,7 +610,7 @@ * Get a Vector containing all compile warning messages * @return A Vector containing all compile error messages */ - public Vector getWarnings() { + public ArrayList getWarnings() { return _parser.getWarnings(); } diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XslElement.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XslElement.java --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XslElement.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XslElement.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -17,16 +17,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* - * $Id: XslElement.java,v 1.2.4.1 2005/09/12 11:39:55 pvedula Exp $ - */ package com.sun.org.apache.xalan.internal.xsltc.compiler; import com.sun.org.apache.bcel.internal.generic.ALOAD; import com.sun.org.apache.bcel.internal.generic.ASTORE; import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen; -import com.sun.org.apache.bcel.internal.generic.ICONST; import com.sun.org.apache.bcel.internal.generic.INVOKESTATIC; import com.sun.org.apache.bcel.internal.generic.InstructionList; import com.sun.org.apache.bcel.internal.generic.LocalVariableGen; @@ -61,14 +57,6 @@ displayContents(indent + IndentIncrement); } - /** - * This method is now deprecated. The new implemation of this class - * never declares the default NS. - */ - public boolean declaresDefaultNS() { - return false; - } - public void parseContents(Parser parser) { final SymbolTable stable = parser.getSymbolTable(); @@ -211,7 +199,6 @@ * on the handler (vii) evaluates the contents (viii) calls endElement(). */ public void translate(ClassGenerator classGen, MethodGenerator methodGen) { - LocalVariableGen local = null; final ConstantPoolGen cpg = classGen.getConstantPool(); final InstructionList il = methodGen.getInstructionList(); diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TemplatesHandlerImpl.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TemplatesHandlerImpl.java --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TemplatesHandlerImpl.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TemplatesHandlerImpl.java @@ -1,6 +1,5 @@ /* - * reserved comment block - * DO NOT REMOVE OR ALTER! + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. */ /* * Copyright 2001-2004 The Apache Software Foundation. @@ -17,9 +16,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* - * $Id: TemplatesHandlerImpl.java,v 1.2.4.1 2005/09/06 12:09:03 pvedula Exp $ - */ package com.sun.org.apache.xalan.internal.xsltc.trax; @@ -44,7 +40,7 @@ import org.xml.sax.SAXException; import org.xml.sax.Attributes; -import java.util.Vector; +import java.util.ArrayList; /** * Implementation of a JAXP1.1 TemplatesHandler @@ -269,12 +265,12 @@ } else { StringBuffer errorMessage = new StringBuffer(); - Vector errors = _parser.getErrors(); + ArrayList errors = _parser.getErrors(); final int count = errors.size(); for (int i = 0; i < count; i++) { if (errorMessage.length() > 0) errorMessage.append('\n'); - errorMessage.append(errors.elementAt(i).toString()); + errorMessage.append(errors.get(i).toString()); } throw new SAXException(ErrorMsg.JAXP_COMPILE_ERR, new TransformerException(errorMessage.toString())); } diff --git a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java --- a/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -17,9 +17,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* - * $Id: TransformerFactoryImpl.java,v 1.8 2007/04/09 21:30:41 joehw Exp $ - */ package com.sun.org.apache.xalan.internal.xsltc.trax; @@ -48,6 +45,7 @@ import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; +import java.util.ArrayList; import java.util.Enumeration; import java.util.Map; import java.util.Properties; @@ -598,7 +596,7 @@ } // Inefficient, but array is small - for (int i =0; i < features.length; i++) { + for (int i = 0; i < features.length; i++) { if (name.equals(features[i])) { return true; } @@ -805,7 +803,7 @@ /** * Pass warning messages from the compiler to the error listener */ - private void passWarningsToListener(Vector messages) + private void passWarningsToListener(ArrayList messages) throws TransformerException { if (_errorListener == null || messages == null) { @@ -814,7 +812,7 @@ // Pass messages to listener, one by one final int count = messages.size(); for (int pos = 0; pos < count; pos++) { - ErrorMsg msg = (ErrorMsg)messages.elementAt(pos); + ErrorMsg msg = messages.get(pos); // Workaround for the TCK failure ErrorListener.errorTests.error001. if (msg.isWarningError()) _errorListener.error( @@ -828,7 +826,7 @@ /** * Pass error messages from the compiler to the error listener */ - private void passErrorsToListener(Vector messages) { + private void passErrorsToListener(ArrayList messages) { try { if (_errorListener == null || messages == null) { return; @@ -836,7 +834,7 @@ // Pass messages to listener, one by one final int count = messages.size(); for (int pos = 0; pos < count; pos++) { - String message = messages.elementAt(pos).toString(); + String message = messages.get(pos).toString(); _errorListener.error(new TransformerException(message)); } } @@ -892,7 +890,7 @@ String transletClassName = getTransletBaseName(source); if (_packageName != null) - transletClassName = _packageName + "." + transletClassName; + transletClassName = _packageName + "." + transletClassName; if (_jarFileName != null) bytecodes = getBytecodesFromJar(source, transletClassName); @@ -1011,10 +1009,10 @@ // Check that the transformation went well before returning if (bytecodes == null) { - Vector errs = xsltc.getErrors(); + ArrayList errs = xsltc.getErrors(); ErrorMsg err; if (errs != null) { - err = (ErrorMsg)errs.elementAt(errs.size()-1); + err = errs.get(errs.size()-1); } else { err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR); } diff --git a/test/javax/xml/jaxp/unittest/transform/TransformerTest.java b/test/javax/xml/jaxp/unittest/transform/TransformerTest.java --- a/test/javax/xml/jaxp/unittest/transform/TransformerTest.java +++ b/test/javax/xml/jaxp/unittest/transform/TransformerTest.java @@ -62,7 +62,7 @@ /* * @summary Transformer Tests - * @bug 6272879 6305029 6505031 8150704 + * @bug 6272879 6305029 6505031 8150704 8162598 8162598 */ public class TransformerTest { private Transformer createTransformer() throws TransformerException { @@ -111,6 +111,27 @@ } } + private void checkNodeNS8162598(Node test, String nstest, String nsb, String nsc) { + String testNodeName = test.getNodeName(); + if (nstest == null) { + Assert.assertNull(test.getNamespaceURI(), "unexpected namespace for " + testNodeName); + } else { + Assert.assertEquals(test.getNamespaceURI(), nstest, "unexpected namespace for " + testNodeName); + } + Node b = test.getChildNodes().item(0); + if (nsb == null) { + Assert.assertNull(b.getNamespaceURI(), "unexpected namespace for " + testNodeName + "->b"); + } else { + Assert.assertEquals(b.getNamespaceURI(), nsb, "unexpected namespace for " + testNodeName + "->b"); + } + Node c = b.getChildNodes().item(0); + if (nsc == null) { + Assert.assertNull(c.getNamespaceURI(), "unexpected namespace for " + testNodeName + "->b->c"); + } else { + 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"; @@ -249,22 +270,19 @@ " " + LINE_SEPARATOR + ""; - Document document; - Node node; - System.out.println("Stylesheet:"); - System.out.println("=================================="); + System.out.println("============================="); System.out.println(xsl); System.out.println(); - System.out.println("Source file before transformation:"); - System.out.println("=================================="); + System.out.println("Source before transformation:"); + System.out.println("============================="); System.out.println(sourceXml); System.out.println(); - System.out.println("Source file after transformation:"); - System.out.println("================================="); - document = transformInputStreamToDocument(createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())), + 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); @@ -274,9 +292,8 @@ System.out.println("Node content for element valeur2:"); System.out.println("================================="); NodeList nodes = document.getElementsByTagName("valeur2"); - nodes = document.getElementsByTagName("valeur2"); for (int i = 0; i < nodes.getLength(); i++) { - node = nodes.item(i); + Node node = nodes.item(i); System.out.println(" Node value: " + node.getFirstChild().getNodeValue()); System.out.println(" Node attribute: " + node.getAttributes().item(0).getNodeValue()); @@ -341,4 +358,62 @@ Assert.assertEquals(resultstring, reference, "Output of transformation of Bug8150704-2.xml does not match reference"); System.out.println("Passed."); } + + /* + * @bug 8162598 + * @summary Test XSLTC handling of namespaces, especially empty namespace definitions to reset the + * default namespace + */ + @Test + public final void testBug8162598() throws IOException, TransformerException { + final String LINE_SEPARATOR = System.getProperty("line.separator"); + + final String xsl = + "" + LINE_SEPARATOR + + "" + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + " " + LINE_SEPARATOR + + ""; + + + final String sourceXml = + "" + 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(); + + 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); + checkNodeNS8162598(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null); + 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"); + } }