KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > jaf > XMLDataContentHandler


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

12
13 package org.jboss.webservice.jaf;
14
15 import java.awt.datatransfer.DataFlavor JavaDoc;
16 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
17 import java.io.ByteArrayInputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20
21 import javax.activation.ActivationDataFlavor JavaDoc;
22 import javax.activation.DataContentHandler JavaDoc;
23 import javax.activation.DataSource JavaDoc;
24 import javax.mail.internet.ContentType JavaDoc;
25 import javax.xml.transform.Source JavaDoc;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27 import javax.xml.transform.sax.SAXSource JavaDoc;
28 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36
37 /**
38  * A JAF DataContentHandler for text/xml and application/xml that <p/> supports
39  * the javax.xml.transform.Source type required by SAAJ
40  *
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 1.3.4.1 $
43  */

44
45 public class XMLDataContentHandler implements DataContentHandler JavaDoc
46
47 {
48
49    static NullEntityResolver resolver = new NullEntityResolver();
50
51    public Object JavaDoc getContent(DataSource JavaDoc dataSource) throws IOException JavaDoc
52    {
53       StreamSource JavaDoc stream = new StreamSource JavaDoc(dataSource.getInputStream());
54       return stream;
55    }
56
57    public Object JavaDoc getTransferData(DataFlavor JavaDoc dataFlavor, DataSource JavaDoc dataSource) throws UnsupportedFlavorException JavaDoc, IOException JavaDoc
58    {
59       Object JavaDoc data = null;
60
61       // Handle text/xml & application/xml
62
String JavaDoc mimeType = dataFlavor.getMimeType();
63       if (mimeType.startsWith("text/xml") || mimeType.startsWith("application/xml"))
64       {
65          data = new StreamSource JavaDoc(dataSource.getInputStream());
66       }
67
68       return data;
69    }
70
71    public DataFlavor JavaDoc[] getTransferDataFlavors()
72    {
73       DataFlavor JavaDoc flavors[] = { new ActivationDataFlavor JavaDoc(Source JavaDoc.class, "text/xml", "XML"),
74             new ActivationDataFlavor JavaDoc(Source JavaDoc.class, "application/xml", "XML") };
75
76       return flavors;
77    }
78
79    public void writeTo(Object JavaDoc obj, String JavaDoc mimeType, OutputStream JavaDoc os) throws IOException JavaDoc
80    {
81       if (mimeType.startsWith("text/xml") == false && mimeType.startsWith("application/xml") == false)
82       {
83          String JavaDoc msg = "Only text/xml, application/xml are supported, mimeType=" + mimeType;
84
85          throw new IOException JavaDoc(msg);
86       }
87
88       try
89       {
90          if (obj instanceof StreamSource JavaDoc)
91          {
92             StreamSource JavaDoc ss = (StreamSource JavaDoc)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 JavaDoc)
101          {
102             DOMSource JavaDoc ds = (DOMSource JavaDoc)obj;
103             org.w3c.dom.Document JavaDoc 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 JavaDoc)
110          {
111             SAXSource JavaDoc ss = (SAXSource JavaDoc)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 JavaDoc)
120          {
121             String JavaDoc charset = new ContentType JavaDoc(mimeType).getParameter("charset");
122             if (charset == null)
123                charset = "utf-8";
124
125             os.write(obj.toString().getBytes(charset));
126          }
127       }
128       catch (Exception JavaDoc e)
129       {
130          if (e instanceof IOException JavaDoc)
131             throw (IOException JavaDoc)e;
132          else
133          {
134             IOException JavaDoc ioe = new IOException JavaDoc("Failed to write xml");
135             ioe.initCause(e);
136          }
137       }
138    }
139
140    static class NullEntityResolver implements EntityResolver JavaDoc
141    {
142       static String JavaDoc dtd = "";
143
144       static ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(dtd.getBytes());
145
146       public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
147       {
148          return new InputSource JavaDoc(bais);
149       }
150    }
151 }
152
Popular Tags