1 12 13 package org.jboss.webservice.jaf; 14 15 import java.awt.datatransfer.DataFlavor ; 16 import java.awt.datatransfer.UnsupportedFlavorException ; 17 import java.io.ByteArrayInputStream ; 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 21 import javax.activation.ActivationDataFlavor ; 22 import javax.activation.DataContentHandler ; 23 import javax.activation.DataSource ; 24 import javax.mail.internet.ContentType ; 25 import javax.xml.transform.Source ; 26 import javax.xml.transform.dom.DOMSource ; 27 import javax.xml.transform.sax.SAXSource ; 28 import javax.xml.transform.stream.StreamSource ; 29 30 import org.dom4j.Document; 31 import org.dom4j.io.DOMReader; 32 import org.dom4j.io.SAXReader; 33 import org.dom4j.io.XMLWriter; 34 import org.xml.sax.EntityResolver ; 35 import org.xml.sax.InputSource ; 36 37 44 45 public class XMLDataContentHandler implements DataContentHandler 46 47 { 48 49 static NullEntityResolver resolver = new NullEntityResolver(); 50 51 public Object getContent(DataSource dataSource) throws IOException 52 { 53 StreamSource stream = new StreamSource (dataSource.getInputStream()); 54 return stream; 55 } 56 57 public Object getTransferData(DataFlavor dataFlavor, DataSource dataSource) throws UnsupportedFlavorException , IOException 58 { 59 Object data = null; 60 61 String mimeType = dataFlavor.getMimeType(); 63 if (mimeType.startsWith("text/xml") || mimeType.startsWith("application/xml")) 64 { 65 data = new StreamSource (dataSource.getInputStream()); 66 } 67 68 return data; 69 } 70 71 public DataFlavor [] getTransferDataFlavors() 72 { 73 DataFlavor flavors[] = { new ActivationDataFlavor (Source .class, "text/xml", "XML"), 74 new ActivationDataFlavor (Source .class, "application/xml", "XML") }; 75 76 return flavors; 77 } 78 79 public void writeTo(Object obj, String mimeType, OutputStream os) throws IOException 80 { 81 if (mimeType.startsWith("text/xml") == false && mimeType.startsWith("application/xml") == false) 82 { 83 String msg = "Only text/xml, application/xml are supported, mimeType=" + mimeType; 84 85 throw new IOException (msg); 86 } 87 88 try 89 { 90 if (obj instanceof StreamSource ) 91 { 92 StreamSource ss = (StreamSource )obj; 93 XMLWriter writer = new XMLWriter(os); 94 SAXReader reader = new SAXReader(false); 95 reader.setIncludeExternalDTDDeclarations(false); 96 reader.setEntityResolver(resolver); 97 Document doc = reader.read(ss.getInputStream()); 98 writer.write(doc); 99 } 100 else if (obj instanceof DOMSource ) 101 { 102 DOMSource ds = (DOMSource )obj; 103 org.w3c.dom.Document doc = ds.getNode().getOwnerDocument(); 104 DOMReader reader = new DOMReader(); 105 Document doc2 = reader.read(doc); 106 XMLWriter writer = new XMLWriter(os); 107 writer.write(doc2); 108 } 109 else if (obj instanceof SAXSource ) 110 { 111 SAXSource ss = (SAXSource )obj; 112 XMLWriter writer = new XMLWriter(os); 113 SAXReader reader = new SAXReader(false); 114 reader.setIncludeExternalDTDDeclarations(false); 115 reader.setEntityResolver(resolver); 116 Document doc = reader.read(ss.getInputSource()); 117 writer.write(doc); 118 } 119 else if (obj instanceof String ) 120 { 121 String charset = new ContentType (mimeType).getParameter("charset"); 122 if (charset == null) 123 charset = "utf-8"; 124 125 os.write(obj.toString().getBytes(charset)); 126 } 127 } 128 catch (Exception e) 129 { 130 if (e instanceof IOException ) 131 throw (IOException )e; 132 else 133 { 134 IOException ioe = new IOException ("Failed to write xml"); 135 ioe.initCause(e); 136 } 137 } 138 } 139 140 static class NullEntityResolver implements EntityResolver 141 { 142 static String dtd = ""; 143 144 static ByteArrayInputStream bais = new ByteArrayInputStream (dtd.getBytes()); 145 146 public InputSource resolveEntity(String publicId, String systemId) 147 { 148 return new InputSource (bais); 149 } 150 } 151 } 152 | Popular Tags |