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