Author: heiko.braun(a)jboss.com
Date: 2007-02-06 15:20:56 -0500 (Tue, 06 Feb 2007)
New Revision: 2290
Added:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxrpc/binding/BufferedStreamResult.java
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxrpc/binding/SerializerSupport.java
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBSerializer.java
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/server/ServiceEndpointDTO.java
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/DOMContent.java
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/ObjectContent.java
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/SOAPContent.java
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/SOAPContentAccess.java
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/XMLContent.java
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/XMLFragment.java
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/utils/DOMUtils.java
branches/hbraun/1.2.0/jbossws-tests/src/main/java/org/jboss/test/ws/jaxrpc/encoded/marshalltest/MarshallTestCase.java
Log:
Introduced a BufferedStreamResult that can be used to represent XML_VALID state
Added:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxrpc/binding/BufferedStreamResult.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxrpc/binding/BufferedStreamResult.java
(rev 0)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxrpc/binding/BufferedStreamResult.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -0,0 +1,62 @@
+/*
+ * 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.ws.core.jaxrpc.binding;
+
+import javax.xml.transform.stream.StreamResult;
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.io.Writer;
+
+/**
+ * @author Heiko.Braun(a)jboss.org
+ * @version $Id$
+ * @since 06.02.2007
+ */
+public class BufferedStreamResult extends StreamResult {
+
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+
+ public BufferedStreamResult()
+ {
+ super();
+ }
+
+ public void setOutputStream(OutputStream outputStream)
+ {
+ throw new IllegalArgumentException("Operation not supported");
+ }
+
+ public OutputStream getOutputStream()
+ {
+ return bout;
+ }
+
+ public void setWriter(Writer writer)
+ {
+ throw new IllegalArgumentException("Operation not supported");
+ }
+
+ public Writer getWriter()
+ {
+ return null;
+ }
+}
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxrpc/binding/SerializerSupport.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxrpc/binding/SerializerSupport.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxrpc/binding/SerializerSupport.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -23,28 +23,21 @@
// $Id$
-import java.util.Set;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-
-import javax.xml.namespace.QName;
-import javax.xml.rpc.encoding.Serializer;
-import javax.xml.transform.Source;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.Result;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.stream.StreamResult;
-
import org.jboss.util.NotImplementedException;
import org.jboss.ws.Constants;
import org.jboss.ws.WSException;
-import org.jboss.ws.core.utils.DOMUtils;
+import org.jboss.ws.core.utils.IOUtils;
import org.jboss.xb.binding.NamespaceRegistry;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
+import javax.xml.namespace.QName;
+import javax.xml.rpc.encoding.Serializer;
+import javax.xml.transform.Result;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Set;
+
/**
* The base class for all Serializers.
*
@@ -66,9 +59,11 @@
public abstract Result serialize(QName xmlName, QName xmlType, Object value,
SerializationContext serContext, NamedNodeMap attributes) throws BindingException;
protected Result stringToResult(String xmlFragment) {
- Result result = null;
+ BufferedStreamResult result = null;
try {
- result = new DOMResult( DOMUtils.parse(xmlFragment));
+ ByteArrayInputStream in = new ByteArrayInputStream(xmlFragment.getBytes());
+ result = new BufferedStreamResult();
+ IOUtils.copyStream(result.getOutputStream(), in);
} catch (IOException e) {
WSException.rethrow(e);
}
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBSerializer.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBSerializer.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/jaxws/JAXBSerializer.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -24,6 +24,7 @@
// $Id$
import java.io.StringWriter;
+import java.io.ByteArrayOutputStream;
import java.util.List;
import javax.xml.bind.JAXBContext;
@@ -33,6 +34,7 @@
import javax.xml.ws.WebServiceException;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
+import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMResult;
import org.jboss.logging.Logger;
@@ -40,6 +42,7 @@
import org.jboss.ws.core.jaxrpc.binding.BindingException;
import org.jboss.ws.core.jaxrpc.binding.ComplexTypeSerializer;
import org.jboss.ws.core.jaxrpc.binding.SerializationContext;
+import org.jboss.ws.core.jaxrpc.binding.BufferedStreamResult;
import org.jboss.ws.core.utils.JavaUtils;
import org.jboss.ws.extensions.xop.jaxws.AttachmentMarshallerImpl;
import org.w3c.dom.NamedNodeMap;
@@ -64,7 +67,7 @@
{
log.debug("serialize: [xmlName=" + xmlName + ",xmlType=" +
xmlType + "]");
- DOMResult result = null;
+ Result result = null;
try
{
// It needs to be a valid JAXB type
@@ -79,7 +82,9 @@
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setAttachmentMarshaller(new AttachmentMarshallerImpl());
- result = new DOMResult();
+ // It's safe to pass a stream result, because the SCE will always be in
XML_VALID state afterwards.
+ // This state can safely be written to an outstream. See XMLFragment and
XMLContent as well.
+ result = new BufferedStreamResult();
marshaller.marshal(new JAXBElement(xmlName, javaType, value), result);
log.debug("serialized: " + result);
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/server/ServiceEndpointDTO.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/server/ServiceEndpointDTO.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/server/ServiceEndpointDTO.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -25,7 +25,7 @@
/**
* @author Heiko.Braun(a)jboss.org
- * @version $Id:$
+ * @version $Id$
* @since 02.02.2007
*/
public class ServiceEndpointDTO {
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/DOMContent.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/DOMContent.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/DOMContent.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -27,8 +27,10 @@
import javax.xml.transform.dom.DOMSource;
/**
+ * Represents the DOM_VALID state of an {@link SOAPContentElement}.<br>
+ *
* @author Heiko.Braun(a)jboss.org
- * @version $Id:$
+ * @version $Id$
* @since 05.02.2007
*/
public class DOMContent extends SOAPContent {
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/ObjectContent.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/ObjectContent.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/ObjectContent.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -35,8 +35,10 @@
import java.lang.reflect.Method;
/**
+ * Represents the OBJECT_VALID state of an {@link SOAPContentElement}.<br>
+ *
* @author Heiko.Braun(a)jboss.org
- * @version $Id:$
+ * @version $Id$
* @since 05.02.2007
*/
public class ObjectContent extends SOAPContent {
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/SOAPContent.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/SOAPContent.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/SOAPContent.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -22,8 +22,13 @@
package org.jboss.ws.core.soap;
/**
+ * Represent SOAP message payload that can transition from
+ * one representation to the next.
+ *
+ * @see SOAPContentElement
+ *
* @author Heiko.Braun(a)jboss.org
- * @version $Id:$
+ * @version $Id$
* @since 05.02.2007
*/
public abstract class SOAPContent implements SOAPContentAccess {
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/SOAPContentAccess.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/SOAPContentAccess.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/SOAPContentAccess.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -4,7 +4,7 @@
/**
* @author Heiko.Braun(a)jboss.org
- * @version $Id:$
+ * @version $Id$
* @since 05.02.2007
*/
public interface SOAPContentAccess {
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/XMLContent.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/XMLContent.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/XMLContent.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -55,8 +55,11 @@
import java.util.List;
/**
+ * Represents the XML_VALID state of an {@link SOAPContentElement}.<br>
+ * Aggregates a {@link XMLFragment}.
+ *
* @author Heiko.Braun(a)jboss.org
- * @version $Id:$
+ * @version $Id$
* @since 05.02.2007
*/
class XMLContent extends SOAPContent {
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/XMLFragment.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/XMLFragment.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/soap/XMLFragment.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -22,23 +22,28 @@
package org.jboss.ws.core.soap;
import org.jboss.ws.WSException;
+import org.jboss.ws.core.jaxrpc.binding.BufferedStreamResult;
import org.jboss.ws.core.utils.DOMUtils;
import org.jboss.ws.core.utils.DOMWriter;
import org.w3c.dom.Element;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
/**
+ * A XMLFragment represent either a XML {@link Source} or a {@link Result}.<br>
+ * The basic idea is that any {@link SOAPContentElement} XML_VALID state
+ * (either before unmarshalling or after marshalling) is represented through a single
interface.<br>
+ *
+ * @see SOAPContentElement
+ * @see XMLContent
+ *
* @author Heiko.Braun(a)jboss.org
- * @version $Id:$
+ * @version $Id$
* @since 05.02.2007
*/
public class XMLFragment {
@@ -46,6 +51,8 @@
private Source source;
private Result result;
+ private boolean idempotent = true;
+
public XMLFragment(Source source) {
this.source = source;
}
@@ -66,25 +73,50 @@
return result;
}
- // TODO: replace with less invasive conversion
public String toStringFragment()
{
+ if(!idempotent) throw new IllegalStateException("Trying to call a
non-idempotent operation");
+
if(this.source != null)
return sourceToStringFragement(this.source);
else
return resultToStringFragment(this.result);
}
+ /**
+ * Note this method <b>is not expected to be idempotent</b>.
+ * It depends on the underlying source impl. that backs the implementation.
+ */
public Element toElement()
{
- Element n = null;
- try {
- n = DOMUtils.parse( toStringFragment() );
- } catch (IOException e) {
- WSException.rethrow(e);
+ if(!idempotent) throw new IllegalStateException("Trying to call a
non-idempotent operation");
+
+ Element resultingElement = null;
+
+ try
+ {
+ if(source != null)
+ {
+ resultingElement = DOMUtils.sourceToElement(source);
+
+ // Any Source besides DOMSource is expected not to be idempotent
+ if(! (source instanceof DOMSource) ) idempotent = false;
+
+ }
+ else
+ {
+ resultingElement = DOMUtils.parse( resultToStringFragment(result));
+
+ // Any Result besides DOMResult is expected not to be idempotent
+ if(! (result instanceof DOMResult) ) idempotent = false;
+ }
}
+ catch (IOException e)
+ {
+ WSException.rethrow("Failed to convert to org.w3c.dom.Element", e);
+ }
- return n;
+ return resultingElement;
}
/**
@@ -98,18 +130,28 @@
return new XMLFragment(source);
}
- private static String resultToStringFragment(Result result) {
+ private String resultToStringFragment(Result result) {
if(result instanceof DOMResult)
{
return DOMWriter.printNode( ((DOMResult)result).getNode(), false);
}
+ else if (result instanceof BufferedStreamResult)
+ {
+ BufferedStreamResult br = (BufferedStreamResult)result;
+ byte[] bytes = ((ByteArrayOutputStream)br.getOutputStream()).toByteArray();
+ return new String(bytes);
+ }
- throw new IllegalArgumentException("Unable to process
javax.xml.transform.Result implementation :" + result);
-
+ throw new IllegalArgumentException("Unable to process
javax.xml.transform.Result implementation: " + result);
}
private static String sourceToStringFragement(Source source) {
+
+ throw new IllegalArgumentException("Source should never be converted to
String");
+
+ /*new
RuntimeException("sourceToStringFragement").printStackTrace(System.out);
+
String xmlFragment = null;
try {
@@ -127,6 +169,7 @@
}
return xmlFragment;
+ */
}
public void writeTo(Writer writer) throws IOException
@@ -141,7 +184,13 @@
}
}
- private void writeSource(Writer writer) throws IOException{
+ /**
+ * Should only be called with <code>jbossws.SOAPMessage==TRACE</code>
+ */
+ private void writeSource(Writer writer) throws IOException {
+
+ //new RuntimeException("writeSource").printStackTrace(System.out);
+
if(source instanceof DOMSource)
{
DOMSource domSource = (DOMSource)source;
@@ -177,9 +226,23 @@
DOMWriter dw = new DOMWriter(writer).setPrettyprint(false);
dw.print(domResult.getNode());
}
+ else if(result instanceof BufferedStreamResult)
+ {
+ BufferedStreamResult sr = (BufferedStreamResult)result;
+ ByteArrayOutputStream out = (ByteArrayOutputStream)sr.getOutputStream();
+ try
+ {
+ byte[] bytes = out.toByteArray();
+ writer.write(new String(bytes));
+ }
+ catch (IOException e)
+ {
+ throw new WSException("Failed to write XMLFragment to output
stream", e);
+ }
+ }
else
{
- throw new IllegalArgumentException("Unable to process
javax.xml.transform.Result implementation :" + result);
+ throw new IllegalArgumentException("Unable to process
javax.xml.transform.Result implementation: " + result);
}
}
@@ -189,6 +252,7 @@
}
public String toString() {
- return toStringFragment();
+ String contents = source != null ? "source="+source :
"result="+result;
+ return "XMLFragment {"+contents+",
idempotent="+idempotent+"}";
}
}
Modified:
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/utils/DOMUtils.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/utils/DOMUtils.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-core/src/main/java/org/jboss/ws/core/utils/DOMUtils.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -23,9 +23,7 @@
// $Id$
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@@ -35,8 +33,15 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.stream.StreamResult;
import org.jboss.logging.Logger;
+import org.jboss.ws.WSException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -55,451 +60,518 @@
*/
public final class DOMUtils
{
- private static Logger log = Logger.getLogger(DOMUtils.class);
+ private static Logger log = Logger.getLogger(DOMUtils.class);
- // All elements created by the same thread are created by the same builder and belong
to the same doc
- private static ThreadLocal documentThreadLocal = new ThreadLocal();
- private static ThreadLocal builderThreadLocal = new ThreadLocal() {
- protected Object initialValue() {
- try
- {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setValidating(false);
- factory.setNamespaceAware(true);
- DocumentBuilder builder = factory.newDocumentBuilder();
- builder.setEntityResolver(new JBossWSEntityResolver());
- return builder;
- }
- catch (ParserConfigurationException e)
- {
- throw new RuntimeException("Failed to create DocumentBuilder",
e);
- }
- }
- };
+ // All elements created by the same thread are created by the same builder and belong
to the same doc
+ private static ThreadLocal documentThreadLocal = new ThreadLocal();
+ private static ThreadLocal builderThreadLocal = new ThreadLocal() {
+ protected Object initialValue() {
+ try
+ {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setValidating(false);
+ factory.setNamespaceAware(true);
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ builder.setEntityResolver(new JBossWSEntityResolver());
+ return builder;
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new RuntimeException("Failed to create DocumentBuilder", e);
+ }
+ }
+ };
- // Hide the constructor
- private DOMUtils()
- {
- }
+ // Hide the constructor
+ private DOMUtils()
+ {
+ }
- /** Initialise the the DocumentBuilder
- */
- public static DocumentBuilder getDocumentBuilder()
- {
- DocumentBuilder builder = (DocumentBuilder)builderThreadLocal.get();
- return builder;
- }
+ /** Initialise the the DocumentBuilder
+ */
+ public static DocumentBuilder getDocumentBuilder()
+ {
+ DocumentBuilder builder = (DocumentBuilder)builderThreadLocal.get();
+ return builder;
+ }
- /** Parse the given XML string and return the root Element
- */
- public static Element parse(String xmlString) throws IOException
- {
- try
- {
- return parse(new
ByteArrayInputStream(xmlString.getBytes("UTF-8")));
- }
- catch (IOException e)
- {
- log.error("Cannot parse: " + xmlString);
- throw e;
- }
- }
+ /** Parse the given XML string and return the root Element
+ */
+ public static Element parse(String xmlString) throws IOException
+ {
+ try
+ {
+ return parse(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
+ }
+ catch (IOException e)
+ {
+ log.error("Cannot parse: " + xmlString);
+ throw e;
+ }
+ }
- /** Parse the given XML stream and return the root Element
- */
- public static Element parse(InputStream xmlStream) throws IOException
- {
- try
- {
- Document doc = getDocumentBuilder().parse(xmlStream);
- Element root = doc.getDocumentElement();
- return root;
- }
- catch (SAXException e)
- {
- throw new IOException(e.toString());
- }
- }
+ /** Parse the given XML stream and return the root Element
+ */
+ public static Element parse(InputStream xmlStream) throws IOException
+ {
+ try
+ {
+ Document doc = getDocumentBuilder().parse(xmlStream);
+ Element root = doc.getDocumentElement();
+ return root;
+ }
+ catch (SAXException e)
+ {
+ throw new IOException(e.toString());
+ }
+ }
- /** Parse the given input source and return the root Element
- */
- public static Element parse(InputSource source) throws IOException
- {
- try
- {
- Document doc = getDocumentBuilder().parse(source);
- Element root = doc.getDocumentElement();
- return root;
- }
- catch (SAXException e)
- {
- throw new IOException(e.toString());
- }
- }
+ /** Parse the given input source and return the root Element
+ */
+ public static Element parse(InputSource source) throws IOException
+ {
+ try
+ {
+ Document doc = getDocumentBuilder().parse(source);
+ Element root = doc.getDocumentElement();
+ return root;
+ }
+ catch (SAXException e)
+ {
+ throw new IOException(e.toString());
+ }
+ }
- /** Create an Element for a given name
- */
- public static Element createElement(String localPart)
- {
- Document doc = getOwnerDocument();
- log.trace("createElement {}" + localPart);
- return doc.createElement(localPart);
- }
+ /** Create an Element for a given name
+ */
+ public static Element createElement(String localPart)
+ {
+ Document doc = getOwnerDocument();
+ log.trace("createElement {}" + localPart);
+ return doc.createElement(localPart);
+ }
- /** Create an Element for a given name and prefix
- */
- public static Element createElement(String localPart, String prefix)
- {
- Document doc = getOwnerDocument();
- log.trace("createElement {}" + prefix + ":" + localPart);
- return doc.createElement(prefix + ":" + localPart);
- }
+ /** Create an Element for a given name and prefix
+ */
+ public static Element createElement(String localPart, String prefix)
+ {
+ Document doc = getOwnerDocument();
+ log.trace("createElement {}" + prefix + ":" + localPart);
+ return doc.createElement(prefix + ":" + localPart);
+ }
- /** Create an Element for a given name, prefix and uri
- */
- public static Element createElement(String localPart, String prefix, String uri)
- {
- Document doc = getOwnerDocument();
- if (prefix == null || prefix.length() == 0)
- {
- log.trace("createElement {" + uri + "}" + localPart);
- return doc.createElementNS(uri, localPart);
- }
- else
- {
- log.trace("createElement {" + uri + "}" + prefix +
":" + localPart);
- return doc.createElementNS(uri, prefix + ":" + localPart);
- }
- }
+ /** Create an Element for a given name, prefix and uri
+ */
+ public static Element createElement(String localPart, String prefix, String uri)
+ {
+ Document doc = getOwnerDocument();
+ if (prefix == null || prefix.length() == 0)
+ {
+ log.trace("createElement {" + uri + "}" + localPart);
+ return doc.createElementNS(uri, localPart);
+ }
+ else
+ {
+ log.trace("createElement {" + uri + "}" + prefix +
":" + localPart);
+ return doc.createElementNS(uri, prefix + ":" + localPart);
+ }
+ }
- /** Create an Element for a given QName
- */
- public static Element createElement(QName qname)
- {
- return createElement(qname.getLocalPart(), qname.getPrefix(),
qname.getNamespaceURI());
- }
+ /** Create an Element for a given QName
+ */
+ public static Element createElement(QName qname)
+ {
+ return createElement(qname.getLocalPart(), qname.getPrefix(),
qname.getNamespaceURI());
+ }
- /** Create a org.w3c.dom.Text node
- */
- public static Text createTextNode(String value)
- {
- Document doc = getOwnerDocument();
- return doc.createTextNode(value);
- }
+ /** Create a org.w3c.dom.Text node
+ */
+ public static Text createTextNode(String value)
+ {
+ Document doc = getOwnerDocument();
+ return doc.createTextNode(value);
+ }
- /** Get the qname of the given node.
- */
- public static QName getElementQName(Element el)
- {
- String qualifiedName = el.getNodeName();
- return resolveQName(el, qualifiedName);
- }
+ /** Get the qname of the given node.
+ */
+ public static QName getElementQName(Element el)
+ {
+ String qualifiedName = el.getNodeName();
+ return resolveQName(el, qualifiedName);
+ }
- /** Transform the giveen qualified name into a QName
- */
- public static QName resolveQName(Element el, String qualifiedName)
- {
- QName qname;
- String prefix = "";
- String namespaceURI = "";
- String localPart = qualifiedName;
+ /** Transform the giveen qualified name into a QName
+ */
+ public static QName resolveQName(Element el, String qualifiedName)
+ {
+ QName qname;
+ String prefix = "";
+ String namespaceURI = "";
+ String localPart = qualifiedName;
- int colIndex = qualifiedName.indexOf(":");
- if (colIndex > 0)
- {
- prefix = qualifiedName.substring(0, colIndex);
- localPart = qualifiedName.substring(colIndex + 1);
+ int colIndex = qualifiedName.indexOf(":");
+ if (colIndex > 0)
+ {
+ prefix = qualifiedName.substring(0, colIndex);
+ localPart = qualifiedName.substring(colIndex + 1);
- if ("xmlns".equals(prefix))
+ if ("xmlns".equals(prefix))
+ {
+ namespaceURI = "URI:XML_PREDEFINED_NAMESPACE";
+ }
+ else
+ {
+ Element nsElement = el;
+ while (namespaceURI.equals("") && nsElement != null)
{
- namespaceURI = "URI:XML_PREDEFINED_NAMESPACE";
+ namespaceURI = nsElement.getAttribute("xmlns:" + prefix);
+ if (namespaceURI.equals(""))
+ nsElement = getParentElement(nsElement);
}
- else
- {
- Element nsElement = el;
- while (namespaceURI.equals("") && nsElement != null)
- {
- namespaceURI = nsElement.getAttribute("xmlns:" + prefix);
- if (namespaceURI.equals(""))
- nsElement = getParentElement(nsElement);
- }
- }
+ }
- if (namespaceURI.equals(""))
- throw new IllegalArgumentException("Cannot find namespace uri for:
" + qualifiedName);
- }
+ if (namespaceURI.equals(""))
+ throw new IllegalArgumentException("Cannot find namespace uri for:
" + qualifiedName);
+ }
- qname = new QName(namespaceURI, localPart, prefix);
- return qname;
- }
+ qname = new QName(namespaceURI, localPart, prefix);
+ return qname;
+ }
- /** Get the value from the given attribute
- *
- * @return null if the attribute value is empty or the attribute is not present
- */
- public static String getAttributeValue(Element el, String attrName)
- {
- return getAttributeValue(el, new QName(attrName));
- }
+ /** Get the value from the given attribute
+ *
+ * @return null if the attribute value is empty or the attribute is not present
+ */
+ public static String getAttributeValue(Element el, String attrName)
+ {
+ return getAttributeValue(el, new QName(attrName));
+ }
- /** Get the value from the given attribute
- *
- * @return null if the attribute value is empty or the attribute is not present
- */
- public static String getAttributeValue(Element el, QName attrName)
- {
- String attr = null;
- if ("".equals(attrName.getNamespaceURI()))
- attr = el.getAttribute(attrName.getLocalPart());
- else attr = el.getAttributeNS(attrName.getNamespaceURI(),
attrName.getLocalPart());
+ /** Get the value from the given attribute
+ *
+ * @return null if the attribute value is empty or the attribute is not present
+ */
+ public static String getAttributeValue(Element el, QName attrName)
+ {
+ String attr = null;
+ if ("".equals(attrName.getNamespaceURI()))
+ attr = el.getAttribute(attrName.getLocalPart());
+ else attr = el.getAttributeNS(attrName.getNamespaceURI(),
attrName.getLocalPart());
- if ("".equals(attr))
- attr = null;
+ if ("".equals(attr))
+ attr = null;
- return attr;
- }
+ return attr;
+ }
- /** Get the qname value from the given attribute
- */
- public static QName getAttributeValueAsQName(Element el, String attrName)
- {
- return getAttributeValueAsQName(el, new QName(attrName));
+ /** Get the qname value from the given attribute
+ */
+ public static QName getAttributeValueAsQName(Element el, String attrName)
+ {
+ return getAttributeValueAsQName(el, new QName(attrName));
- }
+ }
- /** Get the qname value from the given attribute
- */
- public static QName getAttributeValueAsQName(Element el, QName attrName)
- {
- QName qname = null;
+ /** Get the qname value from the given attribute
+ */
+ public static QName getAttributeValueAsQName(Element el, QName attrName)
+ {
+ QName qname = null;
- String qualifiedName = getAttributeValue(el, attrName);
- if (qualifiedName != null)
- {
- qname = resolveQName(el, qualifiedName);
- }
+ String qualifiedName = getAttributeValue(el, attrName);
+ if (qualifiedName != null)
+ {
+ qname = resolveQName(el, qualifiedName);
+ }
- return qname;
- }
+ return qname;
+ }
- /** Get the boolean value from the given attribute
- */
- public static boolean getAttributeValueAsBoolean(Element el, String attrName)
- {
- return getAttributeValueAsBoolean(el, new QName(attrName));
- }
+ /** Get the boolean value from the given attribute
+ */
+ public static boolean getAttributeValueAsBoolean(Element el, String attrName)
+ {
+ return getAttributeValueAsBoolean(el, new QName(attrName));
+ }
- /** Get the boolean value from the given attribute
- */
- public static boolean getAttributeValueAsBoolean(Element el, QName attrName)
- {
- String attrVal = getAttributeValue(el, attrName);
- boolean ret = "true".equalsIgnoreCase(attrVal) ||
"1".equalsIgnoreCase(attrVal);
- return ret;
- }
+ /** Get the boolean value from the given attribute
+ */
+ public static boolean getAttributeValueAsBoolean(Element el, QName attrName)
+ {
+ String attrVal = getAttributeValue(el, attrName);
+ boolean ret = "true".equalsIgnoreCase(attrVal) ||
"1".equalsIgnoreCase(attrVal);
+ return ret;
+ }
- /** Get the integer value from the given attribute
- */
- public static Integer getAttributeValueAsInteger(Element el, String attrName)
- {
- return getAttributeValueAsInteger(el, new QName(attrName));
- }
+ /** Get the integer value from the given attribute
+ */
+ public static Integer getAttributeValueAsInteger(Element el, String attrName)
+ {
+ return getAttributeValueAsInteger(el, new QName(attrName));
+ }
- /** Get the integer value from the given attribute
- */
- public static Integer getAttributeValueAsInteger(Element el, QName attrName)
- {
- String attrVal = getAttributeValue(el, attrName);
- return (attrVal != null ? new Integer(attrVal) : null);
- }
+ /** Get the integer value from the given attribute
+ */
+ public static Integer getAttributeValueAsInteger(Element el, QName attrName)
+ {
+ String attrVal = getAttributeValue(el, attrName);
+ return (attrVal != null ? new Integer(attrVal) : null);
+ }
- /** Get the attributes as Map<QName, String>
- */
- public static Map getAttributes(Element el)
- {
- Map attmap = new HashMap();
- NamedNodeMap attribs = el.getAttributes();
- for (int i = 0; i < attribs.getLength(); i++)
- {
- Attr attr = (Attr)attribs.item(i);
- String name = attr.getName();
- QName qname = resolveQName(el, name);
- String value = attr.getNodeValue();
- attmap.put(qname, value);
- }
- return attmap;
- }
+ /** Get the attributes as Map<QName, String>
+ */
+ public static Map getAttributes(Element el)
+ {
+ Map attmap = new HashMap();
+ NamedNodeMap attribs = el.getAttributes();
+ for (int i = 0; i < attribs.getLength(); i++)
+ {
+ Attr attr = (Attr)attribs.item(i);
+ String name = attr.getName();
+ QName qname = resolveQName(el, name);
+ String value = attr.getNodeValue();
+ attmap.put(qname, value);
+ }
+ return attmap;
+ }
- /** Copy attributes between elements
- */
- public static void copyAttributes(Element destElement, Element srcElement)
- {
- NamedNodeMap attribs = srcElement.getAttributes();
- for (int i = 0; i < attribs.getLength(); i++)
- {
- Attr attr = (Attr)attribs.item(i);
- String uri = attr.getNamespaceURI();
- String qname = attr.getName();
- String value = attr.getNodeValue();
-
- // Prevent DOMException: NAMESPACE_ERR: An attempt is made to create or
- // change an object in a way which is incorrect with regard to namespaces.
- if (uri == null && qname.startsWith("xmlns"))
+ /** Copy attributes between elements
+ */
+ public static void copyAttributes(Element destElement, Element srcElement)
+ {
+ NamedNodeMap attribs = srcElement.getAttributes();
+ for (int i = 0; i < attribs.getLength(); i++)
+ {
+ Attr attr = (Attr)attribs.item(i);
+ String uri = attr.getNamespaceURI();
+ String qname = attr.getName();
+ String value = attr.getNodeValue();
+
+ // Prevent DOMException: NAMESPACE_ERR: An attempt is made to create or
+ // change an object in a way which is incorrect with regard to namespaces.
+ if (uri == null && qname.startsWith("xmlns"))
+ {
+ log.trace("Ignore attribute: [uri=" + uri + ",qname=" +
qname + ",value=" + value + "]");
+ }
+ else
+ {
+ destElement.setAttributeNS(uri, qname, value);
+ }
+ }
+ }
+
+ /** True if the node has child elements
+ */
+ public static boolean hasChildElements(Node node)
+ {
+ NodeList nlist = node.getChildNodes();
+ for (int i = 0; i < nlist.getLength(); i++)
+ {
+ Node child = nlist.item(i);
+ if (child.getNodeType() == Node.ELEMENT_NODE)
+ return true;
+ }
+ return false;
+ }
+
+ /** Gets child elements
+ */
+ public static Iterator getChildElements(Node node)
+ {
+ ArrayList list = new ArrayList();
+ NodeList nlist = node.getChildNodes();
+ for (int i = 0; i < nlist.getLength(); i++)
+ {
+ Node child = nlist.item(i);
+ if (child.getNodeType() == Node.ELEMENT_NODE)
+ list.add(child);
+ }
+ return list.iterator();
+ }
+
+ /** Get the concatenated text content, or null.
+ */
+ public static String getTextContent(Node node)
+ {
+ boolean hasTextContent = false;
+ StringBuffer buffer = new StringBuffer();
+ NodeList nlist = node.getChildNodes();
+ for (int i = 0; i < nlist.getLength(); i++)
+ {
+ Node child = nlist.item(i);
+ if (child.getNodeType() == Node.TEXT_NODE)
+ {
+ buffer.append(child.getNodeValue());
+ hasTextContent = true;
+ }
+ }
+ return (hasTextContent ? buffer.toString() : null);
+ }
+
+ /** Gets the first child element
+ */
+ public static Element getFirstChildElement(Node node)
+ {
+ return getFirstChildElementIntern(node, null);
+ }
+
+ /** Gets the first child element for a given local name without namespace
+ */
+ public static Element getFirstChildElement(Node node, String nodeName)
+ {
+ return getFirstChildElementIntern(node, new QName(nodeName));
+ }
+
+ /** Gets the first child element for a given qname
+ */
+ public static Element getFirstChildElement(Node node, QName nodeName)
+ {
+ return getFirstChildElementIntern(node, nodeName);
+ }
+
+ private static Element getFirstChildElementIntern(Node node, QName nodeName)
+ {
+ Element childElement = null;
+ Iterator it = getChildElementsIntern(node, nodeName);
+ if (it.hasNext())
+ {
+ childElement = (Element)it.next();
+ }
+ return childElement;
+ }
+
+ /** Gets the child elements for a given local name without namespace
+ */
+ public static Iterator getChildElements(Node node, String nodeName)
+ {
+ return getChildElementsIntern(node, new QName(nodeName));
+ }
+
+ /** Gets the child element for a given qname
+ */
+ public static Iterator getChildElements(Node node, QName nodeName)
+ {
+ return getChildElementsIntern(node, nodeName);
+ }
+
+ private static Iterator getChildElementsIntern(Node node, QName nodeName)
+ {
+ ArrayList list = new ArrayList();
+ NodeList nlist = node.getChildNodes();
+ for (int i = 0; i < nlist.getLength(); i++)
+ {
+ Node child = nlist.item(i);
+ if (child.getNodeType() == Node.ELEMENT_NODE)
+ {
+ if (nodeName == null)
{
- log.trace("Ignore attribute: [uri=" + uri + ",qname="
+ qname + ",value=" + value + "]");
+ list.add(child);
}
else
{
- destElement.setAttributeNS(uri, qname, value);
+ QName qname;
+ if (nodeName.getNamespaceURI().length() > 0)
+ {
+ qname = new QName(child.getNamespaceURI(), child.getLocalName());
+ }
+ else
+ {
+ qname = new QName(child.getLocalName());
+ }
+ if (qname.equals(nodeName))
+ {
+ list.add(child);
+ }
}
- }
- }
+ }
+ }
+ return list.iterator();
+ }
- /** True if the node has child elements
- */
- public static boolean hasChildElements(Node node)
- {
- NodeList nlist = node.getChildNodes();
- for (int i = 0; i < nlist.getLength(); i++)
- {
- Node child = nlist.item(i);
- if (child.getNodeType() == Node.ELEMENT_NODE)
- return true;
- }
- return false;
- }
+ /** Gets parent element or null if there is none
+ */
+ public static Element getParentElement(Node node)
+ {
+ Node parent = node.getParentNode();
+ return (parent instanceof Element ? (Element)parent : null);
+ }
- /** Gets child elements
- */
- public static Iterator getChildElements(Node node)
- {
- ArrayList list = new ArrayList();
- NodeList nlist = node.getChildNodes();
- for (int i = 0; i < nlist.getLength(); i++)
- {
- Node child = nlist.item(i);
- if (child.getNodeType() == Node.ELEMENT_NODE)
- list.add(child);
- }
- return list.iterator();
- }
+ /** Get the owner document that is associated with the current thread */
+ public static Document getOwnerDocument()
+ {
+ Document doc = (Document)documentThreadLocal.get();
+ if (doc == null)
+ {
+ doc = getDocumentBuilder().newDocument();
+ documentThreadLocal.set(doc);
+ }
+ return doc;
+ }
- /** Get the concatenated text content, or null.
- */
- public static String getTextContent(Node node)
- {
- boolean hasTextContent = false;
- StringBuffer buffer = new StringBuffer();
- NodeList nlist = node.getChildNodes();
- for (int i = 0; i < nlist.getLength(); i++)
- {
- Node child = nlist.item(i);
- if (child.getNodeType() == Node.TEXT_NODE)
+ public static Element sourceToElement(Source source)
+ {
+ Element elementResult = null;
+
+ try {
+ if (source instanceof StreamSource)
+ {
+ StreamSource streamSource = (StreamSource)source;
+
+ InputStream ins = streamSource.getInputStream();
+ if (ins != null)
{
- buffer.append(child.getNodeValue());
- hasTextContent = true;
+ elementResult = DOMUtils.parse(ins);
}
- }
- return (hasTextContent ? buffer.toString() : null);
- }
+ else
+ {
+ Reader reader = streamSource.getReader();
+ elementResult = DOMUtils.parse(new InputSource(reader));
+ }
- /** Gets the first child element
- */
- public static Element getFirstChildElement(Node node)
- {
- return getFirstChildElementIntern(node, null);
- }
+ // reset the excausted input stream
+ String xmlStr = DOMWriter.printNode(elementResult, false);
+ source = new StreamSource(new ByteArrayInputStream(xmlStr.getBytes()));
+ }
+ else if (source instanceof DOMSource)
+ {
+ DOMSource domSource = (DOMSource)source;
+ Node node = domSource.getNode();
+ if (node instanceof Element)
+ {
+ elementResult = (Element)node;
+ }
+ else if (node instanceof Document)
+ {
+ elementResult = ((Document)node).getDocumentElement();
+ }
+ else
+ {
+ throw new WSException("Unsupported Node type: " +
node.getClass().getName());
+ }
+ }
+ else if (source instanceof SAXSource)
+ {
+ // The fact that JAXBSource derives from SAXSource is an implementation
detail.
+ // Thus in general applications are strongly discouraged from accessing
methods defined on SAXSource.
+ // The XMLReader object obtained by the getXMLReader method shall be used
only for parsing the InputSource object returned by the getInputSource method.
- /** Gets the first child element for a given local name without namespace
- */
- public static Element getFirstChildElement(Node node, String nodeName)
- {
- return getFirstChildElementIntern(node, new QName(nodeName));
- }
+ TransformerFactory tf = TransformerFactory.newInstance();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
+ tf.newTransformer().transform(source, new StreamResult(baos));
- /** Gets the first child element for a given qname
- */
- public static Element getFirstChildElement(Node node, QName nodeName)
- {
- return getFirstChildElementIntern(node, nodeName);
- }
+ elementResult = DOMUtils.parse(new
ByteArrayInputStream(baos.toByteArray()));
+ }
+ else
+ {
+ throw new WSException("Source type not implemented: " +
source.getClass().getName());
+ }
- private static Element getFirstChildElementIntern(Node node, QName nodeName)
- {
- Element childElement = null;
- Iterator it = getChildElementsIntern(node, nodeName);
- if (it.hasNext())
- {
- childElement = (Element)it.next();
- }
- return childElement;
- }
+ }
+ catch (Exception e)
+ {
+ WSException.rethrow(e);
+ }
- /** Gets the child elements for a given local name without namespace
- */
- public static Iterator getChildElements(Node node, String nodeName)
- {
- return getChildElementsIntern(node, new QName(nodeName));
- }
-
- /** Gets the child element for a given qname
- */
- public static Iterator getChildElements(Node node, QName nodeName)
- {
- return getChildElementsIntern(node, nodeName);
- }
-
- private static Iterator getChildElementsIntern(Node node, QName nodeName)
- {
- ArrayList list = new ArrayList();
- NodeList nlist = node.getChildNodes();
- for (int i = 0; i < nlist.getLength(); i++)
- {
- Node child = nlist.item(i);
- if (child.getNodeType() == Node.ELEMENT_NODE)
- {
- if (nodeName == null)
- {
- list.add(child);
- }
- else
- {
- QName qname;
- if (nodeName.getNamespaceURI().length() > 0)
- {
- qname = new QName(child.getNamespaceURI(),
child.getLocalName());
- }
- else
- {
- qname = new QName(child.getLocalName());
- }
- if (qname.equals(nodeName))
- {
- list.add(child);
- }
- }
- }
- }
- return list.iterator();
- }
-
- /** Gets parent element or null if there is none
- */
- public static Element getParentElement(Node node)
- {
- Node parent = node.getParentNode();
- return (parent instanceof Element ? (Element)parent : null);
- }
-
- /** Get the owner document that is associated with the current thread */
- public static Document getOwnerDocument()
- {
- Document doc = (Document)documentThreadLocal.get();
- if (doc == null)
- {
- doc = getDocumentBuilder().newDocument();
- documentThreadLocal.set(doc);
- }
- return doc;
- }
+ return elementResult;
+ }
}
Modified:
branches/hbraun/1.2.0/jbossws-tests/src/main/java/org/jboss/test/ws/jaxrpc/encoded/marshalltest/MarshallTestCase.java
===================================================================
---
branches/hbraun/1.2.0/jbossws-tests/src/main/java/org/jboss/test/ws/jaxrpc/encoded/marshalltest/MarshallTestCase.java 2007-02-06
15:07:50 UTC (rev 2289)
+++
branches/hbraun/1.2.0/jbossws-tests/src/main/java/org/jboss/test/ws/jaxrpc/encoded/marshalltest/MarshallTestCase.java 2007-02-06
20:20:56 UTC (rev 2290)
@@ -70,7 +70,7 @@
assertEquals(inObj, retObj);
}
- /*public void testBigDecimalMultiArrayTest() throws Exception
+ public void testBigDecimalMultiArrayTest() throws Exception
{
BigDecimal[] arr1 = new BigDecimal[] { new BigDecimal("100"), new
BigDecimal("200"), new BigDecimal("300") };
BigDecimal[] arr2 = new BigDecimal[] { new BigDecimal("400"), new
BigDecimal("500"), new BigDecimal("600") };
@@ -524,6 +524,5 @@
Short inObj = new Short("1");
Short retObj = port.wrapperShortTest(inObj);
assertEquals(inObj, retObj);
- }
- */
+ }
}