KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > axis > jaf > XMLDataContentHandler


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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 JavaDoc;
14 import org.xml.sax.InputSource JavaDoc;
15 import org.jboss.logging.Logger;
16
17 import javax.activation.ActivationDataFlavor JavaDoc;
18 import javax.activation.DataContentHandler JavaDoc;
19 import javax.activation.DataSource JavaDoc;
20 import javax.xml.transform.Source JavaDoc;
21 import javax.xml.transform.dom.DOMSource JavaDoc;
22 import javax.xml.transform.sax.SAXSource JavaDoc;
23 import javax.xml.transform.stream.StreamSource JavaDoc;
24 import java.awt.datatransfer.DataFlavor JavaDoc;
25 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29
30 /** A JAF DataContentHandler for text/xml and application/xml that
31  * supports the javax.xml.transform.Source type required by SAAJ
32  *
33  * @author Scott.Stark@jboss.org
34  * @version $Revision: 1.2.8.2 $
35  */

36 public class XMLDataContentHandler implements DataContentHandler JavaDoc
37 {
38    private static Logger log = Logger.getLogger(XMLDataContentHandler.class);
39
40    static NullEntityResolver resolver = new NullEntityResolver();
41
42    public Object JavaDoc getContent(DataSource JavaDoc dataSource)
43            throws IOException JavaDoc
44    {
45       StreamSource JavaDoc stream = new StreamSource JavaDoc(dataSource.getInputStream());
46       return stream;
47    }
48
49    public Object JavaDoc getTransferData(DataFlavor JavaDoc dataFlavor, DataSource JavaDoc dataSource)
50            throws UnsupportedFlavorException JavaDoc, IOException JavaDoc
51    {
52       Object JavaDoc data = null;
53       // Handle text/xml & application/xml
54
String JavaDoc mimeType = dataFlavor.getMimeType();
55       if (mimeType.startsWith("text/xml")
56               || mimeType.startsWith("application/xml"))
57       {
58          data = new StreamSource JavaDoc(dataSource.getInputStream());
59       }
60       return data;
61    }
62
63    public DataFlavor JavaDoc[] getTransferDataFlavors()
64    {
65       DataFlavor JavaDoc flavors[] = {
66          new ActivationDataFlavor JavaDoc(Source JavaDoc.class, "text/xml", "XML"),
67          new ActivationDataFlavor JavaDoc(Source JavaDoc.class, "application/xml", "XML")
68       };
69       return flavors;
70    }
71
72    public void writeTo(Object JavaDoc obj, String JavaDoc mimeType, OutputStream JavaDoc os)
73            throws IOException JavaDoc
74    {
75       if (mimeType.startsWith("text/xml") == false
76               && mimeType.startsWith("application/xml") == false)
77       {
78          String JavaDoc msg = "Only text/xml, application/xml are supported, mimeType=" + mimeType;
79          throw new IOException JavaDoc(msg);
80       }
81
82       try
83       {
84          if (obj instanceof StreamSource JavaDoc)
85          {
86             StreamSource JavaDoc ss = (StreamSource JavaDoc)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 JavaDoc)
95          {
96             DOMSource JavaDoc ds = (DOMSource JavaDoc)obj;
97             org.w3c.dom.Document JavaDoc 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 JavaDoc)
104          {
105             SAXSource JavaDoc ss = (SAXSource JavaDoc)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 JavaDoc e)
115       {
116          throw e;
117       }
118       catch(Exception JavaDoc e)
119       {
120          log.error("Failed to write xml", e);
121          throw new IOException JavaDoc("Failed to write xml");
122       }
123    }
124
125    static class NullEntityResolver implements EntityResolver JavaDoc
126    {
127       static String JavaDoc dtd = "";
128       static ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(dtd.getBytes());
129
130       public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
131       {
132          return new InputSource JavaDoc(bais);
133       }
134    }
135 }
136
Popular Tags