KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > impl > JMUnmarshallerImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.impl;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import javax.xml.bind.JAXBException;
27 import javax.xml.bind.UnmarshalException;
28 import javax.xml.bind.UnmarshallerHandler;
29 import javax.xml.parsers.ParserConfigurationException JavaDoc;
30 import javax.xml.parsers.SAXParser JavaDoc;
31 import javax.xml.parsers.SAXParserFactory JavaDoc;
32 import javax.xml.transform.Source JavaDoc;
33 import javax.xml.transform.dom.DOMSource JavaDoc;
34 import javax.xml.transform.sax.SAXSource JavaDoc;
35 import javax.xml.transform.stream.StreamSource JavaDoc;
36
37 import org.apache.ws.jaxme.JMUnmarshaller;
38 import org.apache.ws.jaxme.util.DOMSerializer;
39 import org.w3c.dom.Node JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.XMLReader JavaDoc;
43
44
45 /** JaxMe's {@link javax.xml.bind.Unmarshaller} implementation.
46  */

47 public class JMUnmarshallerImpl extends JMControllerImpl implements JMUnmarshaller {
48     private static final SAXParserFactory JavaDoc spf;
49     static {
50         spf = SAXParserFactory.newInstance();
51         spf.setValidating(false);
52         spf.setNamespaceAware(true);
53     }
54     
55     private boolean validating;
56
57     public boolean isValidating() { return validating; }
58     public void setValidating(boolean pValidating) { validating = pValidating; }
59     
60     public Object JavaDoc unmarshal(URL JavaDoc pURL) throws JAXBException {
61         InputSource JavaDoc isource;
62         try {
63             isource = new InputSource JavaDoc(pURL.openStream());
64             isource.setSystemId(pURL.toString());
65         } catch (IOException JavaDoc e) {
66             throw new UnmarshalException("Failed to open URL " + pURL, e);
67         }
68         return unmarshal(isource);
69     }
70     
71     public Object JavaDoc unmarshal(File JavaDoc pFile) throws JAXBException {
72         InputSource JavaDoc isource;
73         try {
74             isource = new InputSource JavaDoc(new FileInputStream JavaDoc(pFile));
75             isource.setSystemId(pFile.toURL().toString());
76         } catch (IOException JavaDoc e) {
77             throw new UnmarshalException("Failed to open file " + pFile, e);
78         }
79         return unmarshal(isource);
80     }
81     
82     public Object JavaDoc unmarshal(InputStream JavaDoc pStream) throws JAXBException {
83         return unmarshal(new InputSource JavaDoc(pStream));
84     }
85     
86     public Object JavaDoc unmarshal(InputSource JavaDoc pSource) throws JAXBException {
87         UnmarshallerHandler uh = getUnmarshallerHandler();
88         try {
89             SAXParser JavaDoc sp = spf.newSAXParser();
90             XMLReader JavaDoc xr = sp.getXMLReader();
91             xr.setContentHandler(uh);
92             xr.parse(pSource);
93         } catch (SAXException JavaDoc e) {
94             if (e.getException() != null) {
95                 throw new UnmarshalException(e.getException());
96             } else {
97                 throw new UnmarshalException(e);
98             }
99         } catch (IOException JavaDoc e) {
100             throw new UnmarshalException(e);
101         } catch (ParserConfigurationException JavaDoc e) {
102             throw new UnmarshalException(e);
103         }
104         return uh.getResult();
105     }
106     
107     public Object JavaDoc unmarshal(Node JavaDoc pNode) throws JAXBException {
108         UnmarshallerHandler uh = getUnmarshallerHandler();
109         DOMSerializer ds = new DOMSerializer();
110         try {
111             ds.serialize(pNode, uh);
112         } catch (SAXException JavaDoc e) {
113             if (e.getException() != null) {
114                 throw new UnmarshalException(e.getException());
115             } else {
116                 throw new UnmarshalException(e);
117             }
118         }
119         return uh.getResult();
120     }
121     
122     public Object JavaDoc unmarshal(Source JavaDoc pSource) throws JAXBException {
123         if (pSource instanceof SAXSource JavaDoc) {
124             SAXSource JavaDoc ss = (SAXSource JavaDoc) pSource;
125             InputSource JavaDoc is = ss.getInputSource();
126             if (is == null) {
127                 throw new UnmarshalException("The SAXResult doesn't have its InputSource set.");
128             }
129             XMLReader JavaDoc xr = ss.getXMLReader();
130             if (xr == null) {
131                 return unmarshal(is);
132             } else {
133                 UnmarshallerHandler uh = getUnmarshallerHandler();
134                 xr.setContentHandler(uh);
135                 try {
136                     xr.parse(is);
137                 } catch (IOException JavaDoc e) {
138                     throw new JAXBException(e);
139                 } catch (SAXException JavaDoc e) {
140                     if (e.getException() != null) {
141                         throw new JAXBException(e.getException());
142                     } else {
143                         throw new JAXBException(e);
144                     }
145                 }
146                 return uh.getResult();
147             }
148         } else if (pSource instanceof StreamSource JavaDoc) {
149             StreamSource JavaDoc ss = (StreamSource JavaDoc) pSource;
150             InputSource JavaDoc iSource = new InputSource JavaDoc();
151             iSource.setPublicId(ss.getPublicId());
152             iSource.setSystemId(ss.getSystemId());
153             Reader JavaDoc r = ss.getReader();
154             if (r == null) {
155                 InputStream JavaDoc is = ss.getInputStream();
156                 if (is == null) {
157                     throw new IllegalArgumentException JavaDoc("The StreamSource doesn't have its Reader or InputStream set.");
158                 } else {
159                     iSource.setByteStream(is);
160                 }
161             } else {
162                 iSource.setCharacterStream(r);
163             }
164             return unmarshal(iSource);
165         } else if (pSource instanceof DOMSource JavaDoc) {
166             Node JavaDoc node = ((DOMSource JavaDoc) pSource).getNode();
167             if (node == null) {
168                 throw new IllegalArgumentException JavaDoc("The DOMSource doesn't have its Node set.");
169             }
170             return unmarshal(node);
171         } else {
172             throw new IllegalArgumentException JavaDoc("Unknown type of Source: " + pSource.getClass().getName() +
173             ", only SAXSource, StreamSource and DOMSource are supported.");
174         }
175     }
176     
177     public UnmarshallerHandler getUnmarshallerHandler() {
178         return new JMUnmarshallerHandlerImpl(this);
179     }
180 }
181
Popular Tags