KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jaxb > UnmarshallerImpl


1 /*
2 * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3 *
4 * This file is part of Resin(R) Open Source
5 *
6 * Each copy or derived work must preserve the copyright notice and this
7 * notice unmodified.
8 *
9 * Resin Open Source is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * Resin Open Source is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17 * of NON-INFRINGEMENT. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Resin Open Source; if not, write to the
22 *
23 * Free Software Foundation, Inc.
24 * 59 Temple Place, Suite 330
25 * Boston, MA 02111-1307 USA
26 *
27 * @author Adam Megacz
28 */

29
30 package com.caucho.jaxb;
31
32 import com.caucho.jaxb.adapters.BeanAdapter;
33 import com.caucho.jaxb.skeleton.Skeleton;
34 import com.caucho.util.L10N;
35 import com.caucho.vfs.*;
36 import com.caucho.xml.stream.*;
37
38 import org.w3c.dom.*;
39 import org.xml.sax.*;
40 import org.xml.sax.*;
41
42 import javax.xml.bind.*;
43 import javax.xml.bind.annotation.adapters.*;
44 import javax.xml.bind.attachment.*;
45 import javax.xml.bind.helpers.*;
46 import javax.xml.namespace.QName JavaDoc;
47 import javax.xml.stream.*;
48 import javax.xml.transform.*;
49 import javax.xml.validation.*;
50
51 import java.net.*;
52 import java.io.*;
53 import java.util.*;
54
55 public class UnmarshallerImpl implements Unmarshaller
56 {
57   private static final L10N L = new L10N(UnmarshallerImpl.class);
58
59   private JAXBContextImpl _context;
60   
61   protected boolean validating;
62
63   private AttachmentUnmarshaller _attachmentUnmarshaller = null;
64   private ValidationEventHandler _validationEventHandler = null;
65   private Listener _listener = null;
66   private HashMap<String JavaDoc,Object JavaDoc> _properties = new HashMap<String JavaDoc,Object JavaDoc>();
67   private Schema _schema = null;
68   private XMLReader _xmlreader = null;
69   private XmlAdapter _adapter = null;
70   private UnmarshallerHandler _unmarshallerHandler = null;
71   private HashMap<Class JavaDoc,XmlAdapter> _adapters
72     = new HashMap<Class JavaDoc,XmlAdapter>();
73
74   UnmarshallerImpl(JAXBContextImpl context)
75   {
76     this._context = context;
77   }
78
79   //
80
// unmarshallers.
81
//
82

83   /**
84    * Unmarshall from a DOM node.
85    */

86   public Object JavaDoc unmarshal(Node node) throws JAXBException
87   {
88     throw new UnsupportedOperationException JavaDoc();
89   }
90
91   /**
92    * Unmarshall from an input source.
93    */

94   protected Object JavaDoc unmarshal(XMLReader reader, InputSource source)
95     throws JAXBException
96   {
97     throw new UnsupportedOperationException JavaDoc();
98   }
99
100   /**
101    * Unmarshall from an event reader.
102    */

103   public Object JavaDoc unmarshal(XMLEventReader reader) throws JAXBException
104   {
105     throw new UnsupportedOperationException JavaDoc();
106   }
107
108   public <T> JAXBElement<T> unmarshal(XMLEventReader xmlEventReader,
109                                       Class JavaDoc<T> declaredType)
110     throws JAXBException
111   {
112     throw new UnsupportedOperationException JavaDoc();
113   }
114
115   public Object JavaDoc unmarshal(XMLStreamReader reader)
116     throws JAXBException
117   {
118     try {
119       if (reader.nextTag() != XMLStreamReader.START_ELEMENT)
120     throw new JAXBException(L.l("Expected root element"));
121
122       Skeleton skel = _context.getRootElement(reader.getName());
123
124       if (skel == null)
125     throw new JAXBException(L.l("'{0}' is an unknown root element",
126                     reader.getName()));
127
128       return skel.read(this, reader);
129     } catch (JAXBException e) {
130       throw e;
131     } catch (Exception JavaDoc e) {
132       throw new RuntimeException JavaDoc(e);
133     }
134   }
135
136   /**
137    * Parses the XML based on an InputStream
138    */

139   public Object JavaDoc unmarshal(InputStream is)
140     throws JAXBException
141   {
142     try {
143       XMLStreamReader reader = new XMLStreamReaderImpl(is);
144
145       try {
146     if (reader.nextTag() != XMLStreamReader.START_ELEMENT)
147       throw new JAXBException(L.l("Expected root element"));
148
149     Skeleton skel = _context.getRootElement(reader.getName());
150
151     if (skel == null)
152       throw new JAXBException(L.l("'{0}' is an unknown root element",
153                       reader.getName()));
154
155     return skel.read(this, reader);
156       } finally {
157     reader.close();
158       }
159     } catch (JAXBException e) {
160       throw e;
161     } catch (Exception JavaDoc e) {
162       throw new RuntimeException JavaDoc(e);
163     }
164   }
165
166   public <T> JAXBElement<T> unmarshal(XMLStreamReader reader,
167                                       Class JavaDoc<T> declaredType)
168       throws JAXBException
169   {
170     try {
171       while (reader.getEventType() != XMLStreamReader.START_ELEMENT)
172         reader.next();
173
174       QName JavaDoc name = reader.getName();
175       T val = (T)_context.getSkeleton(declaredType).read(this, reader);
176       return new JAXBElement<T>(name, declaredType, val);
177     }
178     catch (Exception JavaDoc e) {
179       throw new RuntimeException JavaDoc(e);
180     }
181   }
182
183   //
184
// From AbstractUnmarshallerImpl
185
//
186

187   public UnmarshallerHandler getUnmarshallerHandler()
188   {
189     return _unmarshallerHandler;
190   }
191
192   public void setUnmarshallerHandler(UnmarshallerHandler u)
193   {
194     _unmarshallerHandler = u;
195   }
196
197   protected UnmarshalException createUnmarshalException(SAXException e)
198   {
199     return new UnmarshalException(e);
200   }
201
202   public <A extends XmlAdapter> A getAdapter(Class JavaDoc<A> type)
203   {
204     A a = (A)_adapters.get(type);
205
206     if (a == null)
207       return (A)new BeanAdapter();
208
209     return a;
210   }
211
212   public AttachmentUnmarshaller getAttachmentUnmarshaller()
213   {
214     return _attachmentUnmarshaller;
215   }
216
217   public ValidationEventHandler getEventHandler() throws JAXBException
218   {
219     return _validationEventHandler;
220   }
221
222   public Listener getListener()
223   {
224     return _listener;
225   }
226
227   public Object JavaDoc getProperty(String JavaDoc name) throws PropertyException
228   {
229     return _properties.get(name);
230   }
231
232   public Schema getSchema()
233   {
234     return _schema;
235   }
236
237   protected XMLReader getXMLReader() throws JAXBException
238   {
239     return _xmlreader;
240   }
241
242   public boolean isValidating() throws JAXBException
243   {
244     return validating;
245   }
246
247   public <A extends XmlAdapter> void setAdapter(Class JavaDoc<A> type, A adapter)
248   {
249     _adapters.put(type, adapter);
250   }
251
252   public void setAdapter(XmlAdapter adapter)
253   {
254     setAdapter((Class JavaDoc)adapter.getClass(), adapter);
255   }
256
257   public void setAttachmentUnmarshaller(AttachmentUnmarshaller au)
258   {
259     _attachmentUnmarshaller = au;
260   }
261
262   public void setEventHandler(ValidationEventHandler handler)
263     throws JAXBException
264   {
265     _validationEventHandler = handler;
266   }
267
268   public void setListener(Listener listener)
269   {
270     _listener = listener;
271   }
272
273   public void setProperty(String JavaDoc name, Object JavaDoc value) throws PropertyException
274   {
275     _properties.put(name, value);
276   }
277
278   public void setSchema(Schema schema)
279   {
280     _schema = schema;
281   }
282
283   public void setValidating(boolean validating) throws JAXBException
284   {
285     this.validating = validating;
286   }
287
288   public Object JavaDoc unmarshal(File f) throws JAXBException
289   {
290     FileInputStream fis = null;
291     try {
292       fis = new FileInputStream(f);
293       return unmarshal(fis);
294     }
295     catch (IOException e) {
296       throw new JAXBException(e);
297     }
298     finally {
299       try {
300         if (fis != null)
301           fis.close();
302       }
303       catch (IOException e) {
304         throw new JAXBException(e);
305       }
306     }
307   }
308
309   public Object JavaDoc unmarshal(InputSource source) throws JAXBException
310   {
311     throw new UnsupportedOperationException JavaDoc("subclasses must override this");
312   }
313
314   public Object JavaDoc unmarshal(Reader reader) throws JAXBException
315   {
316     try {
317       XMLInputFactory factory = _context.getXMLInputFactory();
318       
319       return unmarshal(factory.createXMLStreamReader(reader));
320     }
321     catch (XMLStreamException e) {
322       throw new JAXBException(e);
323     }
324   }
325
326   public Object JavaDoc unmarshal(Source source) throws JAXBException
327   {
328     try {
329       XMLInputFactory factory = _context.getXMLInputFactory();
330       
331       return unmarshal(factory.createXMLStreamReader(source));
332     }
333     catch (XMLStreamException e) {
334       throw new JAXBException(e);
335     }
336   }
337
338   public <T> JAXBElement<T> unmarshal(Node node, Class JavaDoc<T> declaredType)
339       throws JAXBException
340   {
341     throw new UnsupportedOperationException JavaDoc("subclasses must override this");
342   }
343
344   public <T> JAXBElement<T> unmarshal(Source node, Class JavaDoc<T> declaredType)
345       throws JAXBException
346   {
347     try {
348       XMLInputFactory factory = XMLInputFactory.newInstance();
349       return unmarshal(factory.createXMLStreamReader(node), declaredType);
350     }
351     catch (XMLStreamException e) {
352       throw new JAXBException(e);
353     }
354   }
355
356   public Object JavaDoc unmarshal(URL url) throws JAXBException
357   {
358     try {
359       InputStream is = url.openStream();
360
361       try {
362     return unmarshal(is);
363       } finally {
364     is.close();
365       }
366     } catch (IOException e) {
367       throw new JAXBException(e);
368     }
369   }
370 }
371
Popular Tags