1 package org.codehaus.xfire.aegis.type.basic; 2 3 import javax.xml.parsers.DocumentBuilder; 4 import javax.xml.parsers.DocumentBuilderFactory; 5 import javax.xml.parsers.ParserConfigurationException; 6 import javax.xml.stream.XMLStreamException; 7 8 import org.codehaus.xfire.MessageContext; 9 import org.codehaus.xfire.XFireRuntimeException; 10 import org.codehaus.xfire.aegis.MessageReader; 11 import org.codehaus.xfire.aegis.MessageWriter; 12 import org.codehaus.xfire.aegis.stax.ElementReader; 13 import org.codehaus.xfire.aegis.stax.ElementWriter; 14 import org.codehaus.xfire.aegis.type.Type; 15 import org.codehaus.xfire.fault.XFireFault; 16 import org.codehaus.xfire.util.STAXUtils; 17 import org.w3c.dom.Document; 18 19 /*** 20 * Reads and writes <code>org.w3c.dom.Document</code> types. 21 * 22 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a> 23 */ 24 public class DocumentType 25 extends Type 26 { 27 private DocumentBuilder builder; 28 29 public DocumentType() 30 { 31 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 32 try 33 { 34 builder = factory.newDocumentBuilder(); 35 } 36 catch (ParserConfigurationException e) 37 { 38 throw new XFireRuntimeException("Couldn't load document builder.", e); 39 } 40 } 41 42 public DocumentType(DocumentBuilder builder) 43 { 44 this.builder = builder; 45 } 46 47 public Object readObject(MessageReader reader, MessageContext context) 48 throws XFireFault 49 { 50 try 51 { 52 return STAXUtils.read(builder, ((ElementReader) reader).getXMLStreamReader()) ; 53 } 54 catch (XMLStreamException e) 55 { 56 throw new XFireFault("Could not parse xml.", e, XFireFault.SENDER); 57 } 58 } 59 60 public void writeObject(Object object, MessageWriter writer, MessageContext context) 61 throws XFireFault 62 { 63 Document doc = (Document) object; 64 65 try 66 { 67 STAXUtils.writeElement(doc.getDocumentElement(), 68 ((ElementWriter) writer).getXMLStreamWriter()); 69 } 70 catch (XMLStreamException e) 71 { 72 throw new XFireFault("Could not write xml.", e, XFireFault.SENDER); 73 } 74 } 75 }