You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by bu...@apache.org on 2003/04/14 20:06:37 UTC
DO NOT REPLY [Bug 19008] New: -
ClassCastException in XMLUtils.java
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=19008>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND
INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=19008
ClassCastException in XMLUtils.java
Summary: ClassCastException in XMLUtils.java
Product: Axis
Version: 1.1rc2
Platform: All
OS/Version: All
Status: NEW
Severity: Minor
Priority: Other
Component: Basic Architecture
AssignedTo: axis-dev@ws.apache.org
ReportedBy: pankaj_kumar@hp.com
BTW, I had written a small utility class to do the conversion (SOAPMessage -->
Document and Document --> SOAPMessage; assuming that there were no attachments)
for me. This class essentially wrote the SOAPMessage to a bytestream (using
writeTo() method and then parsed the bytestream to get a Document. I used a
similar technique other way round as well.
After getting your mail, I thought I will use getContent/setContent() of
SOAPPart to do the conversion. So I changed my implementation. But then ran
into a ClassCastException problem. Some digging into Axis source code revealed
that following code, in file XMLUtils.java, is causing problem:
public static InputSource sourceToInputSource(Source source) {
// skip
Element domElement = (Element)((DOMSource)source).getNode();
// skip
}
In case the DOMSource is created with a Document, getNode() doesn't return an
Element. This is certainly a bug in Axis code.
Here is my test code (in case someone wants to reproduce the problem ):
Note: My original code is comented out.
--------------------------------------------------------------------------------
-
// File: SOAPUtility.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
public class SOAPUtility {
/***********************************************************************
*****
* class MyByteArrayOutputStream helps in avoiding a copy of the buffer.
* Method getByteArrayInputStream() can access the protected members
buf and count
* of the base class, thus passing the same buffer to the
ByteArrayInputStream.
****************************************************************************/
private static class MyByteArrayOutputStream extends
ByteArrayOutputStream {
public MyByteArrayOutputStream(){
super();
}
public ByteArrayInputStream getByteArrayInputStream(){
return new ByteArrayInputStream(buf, 0, count);
}
}
/*
public static Document toDocument(SOAPMessage soapMsg) throws
ParserConfigurationException, SAXException,
SOAPException, IOException {
MyByteArrayOutputStream baos = new MyByteArrayOutputStream();
soapMsg.writeTo(baos);
ByteArrayInputStream bais = baos.getByteArrayInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(bais);
return doc;
}
*/
public static Document toDocument(SOAPMessage soapMsg) throws
TransformerConfigurationException,
TransformerException, SOAPException, IOException {
Source src = soapMsg.getSOAPPart().getContent();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMResult result = new DOMResult();
transformer.transform(src, result);
return (Document)result.getNode();
}
/*
public static SOAPMessage toSOAPMessage(Document doc) throws
TransformerConfigurationException,
TransformerException, SOAPException, IOException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
MyByteArrayOutputStream baos = new MyByteArrayOutputStream();
transformer.transform(new DOMSource(doc), new StreamResult
(baos));
ByteArrayInputStream bais = baos.getByteArrayInputStream();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage soapMsg = mf.createMessage(new MimeHeaders(), bais);
return soapMsg;
}
*/
public static SOAPMessage toSOAPMessage(Document doc) throws
TransformerConfigurationException,
TransformerException, SOAPException, IOException {
DOMSource src = new DOMSource(doc);
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage soapMsg = mf.createMessage();
soapMsg.getSOAPPart().setContent(src);
return soapMsg;
}
public static void main(String[] args) throws Exception {
Document doc = XmlUtility.readXML("soap.xml");
System.out.println("Document read from file:");
XmlUtility.writeXML(doc, System.out);
System.out.println();
SOAPMessage soapMsg = toSOAPMessage(doc);
System.out.println("Document converted to SOAPMessage:");
soapMsg.writeTo(System.out);
System.out.println();
doc = toDocument(soapMsg);
System.out.println("SOAPMessage converted to Document:");
XmlUtility.writeXML(doc, System.out);
System.out.println();
}
}
--------------------------------------------------------------------------------
-