[jboss-svn-commits] JBoss Common SVN: r2890 - in common-core/trunk/src: test/java/org/jboss/test/util/test/xml and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Jul 31 10:57:42 EDT 2008
Author: thomas.diesler at jboss.com
Date: 2008-07-31 10:57:42 -0400 (Thu, 31 Jul 2008)
New Revision: 2890
Added:
common-core/trunk/src/test/java/org/jboss/test/util/test/xml/DOMWriterTestCase.java
Modified:
common-core/trunk/src/main/java/org/jboss/util/xml/DOMWriter.java
Log:
Update DOMWriter to latest from JBossWS.
Add DOMWriterTestCase
Modified: common-core/trunk/src/main/java/org/jboss/util/xml/DOMWriter.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/xml/DOMWriter.java 2008-07-30 12:18:00 UTC (rev 2889)
+++ common-core/trunk/src/main/java/org/jboss/util/xml/DOMWriter.java 2008-07-31 14:57:42 UTC (rev 2890)
@@ -78,7 +78,6 @@
*
* @author Andy Clark, IBM
* @author Thomas.Diesler at jboss.org
- * @version $Revision$
*/
@SuppressWarnings("unchecked")
public class DOMWriter
@@ -101,6 +100,8 @@
private Node rootNode;
// True if we want namespace completion
private boolean completeNamespaces = true;
+ // The current default namespace
+ private String currentDefaultNamespace;
public DOMWriter(Writer w)
{
@@ -143,9 +144,6 @@
/**
* Print a node with explicit prettyprinting.
* The defaults for all other DOMWriter properties apply.
- * @param node
- * @param prettyprint
- * @return the node as a string
*
*/
public static String printNode(Node node, boolean prettyprint)
@@ -163,8 +161,6 @@
/**
* Set wheter entities should appear in their canonical form.
* The default is false.
- * @param canonical
- * @return the dom writer
*/
public DOMWriter setCanonical(boolean canonical)
{
@@ -176,8 +172,6 @@
* Set wheter subelements should have their namespaces completed.
* Setting this to false may lead to invalid XML fragments.
* The default is true.
- * @param complete
- * @return the dom writer
*/
public DOMWriter setCompleteNamespaces(boolean complete)
{
@@ -193,8 +187,6 @@
/**
* Set wheter element should be indented.
* The default is false.
- * @param prettyprint
- * @return the dom writer
*/
public DOMWriter setPrettyprint(boolean prettyprint)
{
@@ -210,8 +202,6 @@
/**
* Set wheter the XML declaration should be written.
* The default is false.
- * @param flag
- * @return the dom writer
*/
public DOMWriter setWriteXMLDeclaration(boolean flag)
{
@@ -245,7 +235,7 @@
out.print("?>");
if (prettyprint)
out.println();
-
+
wroteXMLDeclaration = true;
}
@@ -285,6 +275,7 @@
Map nsMap = new HashMap();
String elPrefix = node.getPrefix();
+ String elNamespaceURI = node.getNamespaceURI();
if (elPrefix != null)
{
String nsURI = getNamespaceURI(elPrefix, element, rootNode);
@@ -299,16 +290,34 @@
String atName = attr.getNodeName();
String atValue = normalize(attr.getNodeValue(), canonical);
- if (atPrefix != null && (atPrefix.equals("xmlns") || atPrefix.equals("xml")) == false)
+ if (atName.equals("xmlns"))
+ currentDefaultNamespace = atValue;
+
+ if (atPrefix != null && !atPrefix.equals("xmlns") && !atPrefix.equals("xml"))
{
String nsURI = getNamespaceURI(atPrefix, element, rootNode);
nsMap.put(atPrefix, nsURI);
+ // xsi:type='ns1:SubType', xsi:type='xsd:string'
+ if (atName.equals(atPrefix + ":type") && atValue.indexOf(":") > 0)
+ {
+ // xsi defined on the envelope
+ if (nsURI == null)
+ nsURI = getNamespaceURI(atPrefix, element, null);
+
+ if ("http://www.w3.org/2001/XMLSchema-instance".equals(nsURI))
+ {
+ String typePrefix = atValue.substring(0, atValue.indexOf(":"));
+ String typeURI = getNamespaceURI(typePrefix, element, rootNode);
+ nsMap.put(typePrefix, typeURI);
+ }
+ }
}
out.print(" " + atName + "='" + atValue + "'");
}
- // Add missing namespace declaration
+ // Add namespace declaration for prefixes
+ // that are defined further up the tree
if (completeNamespaces)
{
Iterator itPrefix = nsMap.keySet().iterator();
@@ -324,6 +333,18 @@
}
}
+ // The SAX ContentHandler will by default not add the namespace declaration
+ // <Hello xmlns='http://somens'>World</Hello>
+ if (elPrefix == null && elNamespaceURI != null)
+ {
+ String defaultNamespace = element.getAttribute("xmlns");
+ if (defaultNamespace.length() == 0 && !elNamespaceURI.equals(currentDefaultNamespace))
+ {
+ out.print(" xmlns='" + elNamespaceURI + "'");
+ currentDefaultNamespace = elNamespaceURI;
+ }
+ }
+
if (hasChildNodes)
{
out.print('>');
@@ -529,11 +550,7 @@
return (array);
}
- /** Normalizes the given string.
- * @param s
- * @param canonical
- * @return the normalized string
- */
+ /** Normalizes the given string. */
public static String normalize(String s, boolean canonical)
{
StringBuffer str = new StringBuffer();
@@ -564,7 +581,16 @@
str.append(""");
break;
}
+ case '\'':
+ {
+ str.append("'");
+ break;
+ }
case '\r':
+ {
+ str.append("
");
+ break;
+ }
case '\n':
{
if (canonical)
Added: common-core/trunk/src/test/java/org/jboss/test/util/test/xml/DOMWriterTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/xml/DOMWriterTestCase.java (rev 0)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/xml/DOMWriterTestCase.java 2008-07-31 14:57:42 UTC (rev 2890)
@@ -0,0 +1,263 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.util.test.xml;
+
+// $Id$
+
+import java.io.ByteArrayInputStream;
+import java.io.StringWriter;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.stream.StreamSource;
+
+import junit.framework.TestCase;
+
+import org.jboss.util.xml.DOMUtils;
+import org.jboss.util.xml.DOMWriter;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Test the DOMWriter
+ *
+ * @author Thomas.Diesler at jboss.org
+ * @since 10-Aug-2006
+ */
+public class DOMWriterTestCase extends TestCase
+{
+ /** The element does not contain the required ns declaration.
+ */
+ public void testNamespaceCompletionOne() throws Exception
+ {
+ String inStr =
+ "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
+ "<env:Body>" +
+ "<env:Fault>" +
+ "<faultcode>env:Client</faultcode>" +
+ "<faultstring>Endpoint {http://webmethod.jsr181.ws.test.jboss.org/jaws}TestEndpointPort does not contain operation meta data for: {http://webmethod.jsr181.ws.test.jboss.org/jaws}noWebMethod</faultstring>" +
+ "</env:Fault>" +
+ "</env:Body>" +
+ "</env:Envelope>";
+
+ Element env = DOMUtils.parse(inStr);
+ Element body = DOMUtils.getFirstChildElement(env);
+ Element fault = DOMUtils.getFirstChildElement(body);
+
+ String expStr =
+ "<env:Fault xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
+ "<faultcode>env:Client</faultcode>" +
+ "<faultstring>Endpoint {http://webmethod.jsr181.ws.test.jboss.org/jaws}TestEndpointPort does not contain operation meta data for: {http://webmethod.jsr181.ws.test.jboss.org/jaws}noWebMethod</faultstring>" +
+ "</env:Fault>";
+
+ String wasStr = DOMWriter.printNode(fault, false);
+ assertEquals(expStr, wasStr);
+ }
+
+ /** The element already contains the required ns declaration.
+ */
+ public void testNamespaceCompletionTwo() throws Exception
+ {
+ String inStr =
+ "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
+ "<env:Body>" +
+ "<ns1:rpc xmlns:ns1='http://somens'>" +
+ "<ns1:param1/>" +
+ "<ns1:param2/>" +
+ "</ns1:rpc>" +
+ "</env:Body>" +
+ "</env:Envelope>";
+
+ Element env = DOMUtils.parse(inStr);
+ Element body = DOMUtils.getFirstChildElement(env);
+ Element rpc = DOMUtils.getFirstChildElement(body);
+
+ String expStr =
+ "<ns1:rpc xmlns:ns1='http://somens'>" +
+ "<ns1:param1/>" +
+ "<ns1:param2/>" +
+ "</ns1:rpc>";
+
+ String wasStr = DOMWriter.printNode(rpc, false);
+ assertEquals(expStr, wasStr);
+ }
+
+ /** The element does not contain the required ns declaration, the child does.
+ */
+ public void testNamespaceCompletionThree() throws Exception
+ {
+ String inStr =
+ "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
+ "<env:Body>" +
+ "<ns1:rpc xmlns:ns1='http://somens'>" +
+ "<ns1:param1/>" +
+ "<ns1:param2/>" +
+ "</ns1:rpc>" +
+ "</env:Body>" +
+ "</env:Envelope>";
+
+ Element env = DOMUtils.parse(inStr);
+ Element body = DOMUtils.getFirstChildElement(env);
+
+ String expStr =
+ "<env:Body xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
+ "<ns1:rpc xmlns:ns1='http://somens'>" +
+ "<ns1:param1/>" +
+ "<ns1:param2/>" +
+ "</ns1:rpc>" +
+ "</env:Body>";
+
+ String wasStr = DOMWriter.printNode(body, false);
+ assertEquals(expStr, wasStr);
+ }
+
+ /** The envelope defines a default namespace
+ */
+ public void testNamespaceCompletionDefault() throws Exception
+ {
+ String inStr =
+ "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/' xmlns='http://somens'>" +
+ "<env:Body>" +
+ "<rpc>" +
+ "<param1/>" +
+ "<param2/>" +
+ "</rpc>" +
+ "</env:Body>" +
+ "</env:Envelope>";
+
+ Element env = DOMUtils.parse(inStr);
+ Element body = DOMUtils.getFirstChildElement(env);
+ Element rpc = DOMUtils.getFirstChildElement(body);
+
+ String expStr =
+ "<rpc xmlns='http://somens'>" +
+ "<param1/>" +
+ "<param2/>" +
+ "</rpc>";
+
+ String wasStr = DOMWriter.printNode(rpc, false);
+ assertEquals(expStr, wasStr);
+ }
+
+ /** The element does not contain the required attribute ns declaration.
+ */
+ public void testNamespaceCompletionAttribute() throws Exception
+ {
+ String inStr =
+ "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
+ "<env:Header>" +
+ "<someHeader env:mustUnderstand='1' xml:lang='en'/>" +
+ "</env:Header>" +
+ "<env:Body/>" +
+ "</env:Envelope>";
+
+ Element env = DOMUtils.parse(inStr);
+ Element header = DOMUtils.getFirstChildElement(env);
+ Element headerElement = DOMUtils.getFirstChildElement(header);
+
+ String expStr =
+ "<someHeader env:mustUnderstand='1' xml:lang='en' xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'/>";
+
+ String wasStr = DOMWriter.printNode(headerElement, false);
+ assertEquals(expStr, wasStr);
+ }
+
+ public void testEntity() throws Exception
+ {
+ String expStr =
+ "<xsd:simpleType name='MailRelayConfiguration' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
+ "<xsd:restriction base='xsd:string'>" +
+ "<xsd:enumeration value='Incoming & Outgoing'/>" +
+ "<xsd:enumeration value='None'/>" +
+ "</xsd:restriction>" +
+ "</xsd:simpleType>";
+
+ Element domEl = DOMUtils.parse(expStr);
+ String wasStr = DOMWriter.printNode(domEl, false);
+
+ assertEquals(expStr, wasStr);
+ }
+
+ // [JBWS-762] DOMUtils.parse skips peer comments on Document node
+ public void testDocumentComments() throws Exception
+ {
+ String expStr =
+ "<?xml version='1.0' encoding='UTF-8'?>" +
+ "<!-- Some root comment -->" +
+ "<root>" +
+ "<!-- Some element comment -->" +
+ "<element>some value</element>" +
+ "</root>";
+
+
+ Document doc = DOMUtils.parse(expStr).getOwnerDocument();
+ StringWriter strwr = new StringWriter();
+ new DOMWriter(strwr, "UTF-8").print(doc);
+ String wasStr = strwr.toString();
+
+ assertEquals(expStr, wasStr);
+ }
+
+ public void testElementNamespaceURIElementNS() throws Exception
+ {
+ String xmlIn = "<Hello xmlns='http://somens'><Sub>World</Sub></Hello>";
+
+ Element root = DOMUtils.createElement(new QName("http://somens", "Hello"));
+ assertEquals("http://somens", root.getNamespaceURI());
+ Element child = (Element)root.appendChild(DOMUtils.createElement(new QName("Sub")));
+ child.appendChild(DOMUtils.createTextNode("World"));
+
+ String xmlOut = DOMWriter.printNode(root, false);
+ assertEquals(xmlIn, xmlOut);
+ }
+
+ public void testElementNamespaceURIDocumentParse() throws Exception
+ {
+ String xmlIn = "<Hello xmlns='http://somens'><Sub>World</Sub></Hello>";
+
+ Element root = DOMUtils.parse(xmlIn);
+ assertEquals("http://somens", root.getNamespaceURI());
+
+ String xmlOut = DOMWriter.printNode(root, false);
+ assertEquals(xmlIn, xmlOut);
+ }
+
+ public void testElementNamespaceURITransformer() throws Exception
+ {
+ String xmlIn = "<Hello xmlns='http://somens'><Sub>World</Sub></Hello>";
+ StreamSource source = new StreamSource(new ByteArrayInputStream(xmlIn.getBytes()));
+
+ Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ DOMResult result = new DOMResult();
+ transformer.transform(source, result);
+
+ Element root = ((Document)result.getNode()).getDocumentElement();
+ assertEquals("http://somens", root.getNamespaceURI());
+
+ String xmlOut = DOMWriter.printNode(root, false);
+ assertEquals(xmlIn, xmlOut);
+ }
+}
Property changes on: common-core/trunk/src/test/java/org/jboss/test/util/test/xml/DOMWriterTestCase.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
More information about the jboss-svn-commits
mailing list