View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package javax.xml.bind.helpers;
18  
19  import java.io.File;
20  import java.io.InputStream;
21  import java.io.Reader;
22  import java.net.URL;
23  import java.net.MalformedURLException;
24  
25  import javax.xml.bind.Unmarshaller;
26  import javax.xml.bind.ValidationEventHandler;
27  import javax.xml.bind.UnmarshallerHandler;
28  import javax.xml.bind.JAXBElement;
29  import javax.xml.bind.UnmarshalException;
30  import javax.xml.bind.JAXBException;
31  import javax.xml.bind.PropertyException;
32  import javax.xml.bind.attachment.AttachmentUnmarshaller;
33  import javax.xml.bind.annotation.adapters.XmlAdapter;
34  import javax.xml.validation.Schema;
35  import javax.xml.transform.Source;
36  import javax.xml.transform.stream.StreamSource;
37  import javax.xml.transform.sax.SAXSource;
38  import javax.xml.transform.dom.DOMSource;
39  import javax.xml.stream.XMLEventReader;
40  import javax.xml.stream.XMLStreamReader;
41  import javax.xml.parsers.ParserConfigurationException;
42  import javax.xml.parsers.SAXParserFactory;
43  
44  import org.w3c.dom.Node;
45  
46  import org.xml.sax.InputSource;
47  import org.xml.sax.SAXException;
48  import org.xml.sax.XMLReader;
49  
50  public abstract class AbstractUnmarshallerImpl implements Unmarshaller {
51  
52      protected boolean validating;
53      private ValidationEventHandler eventHandler;
54      private XMLReader reader;
55  
56      protected UnmarshalException createUnmarshalException(SAXException e) {
57          Exception nested = e.getException();
58          if (nested instanceof UnmarshalException) {
59              return (UnmarshalException)nested;
60          } else if(nested instanceof RuntimeException) {
61              throw (RuntimeException)nested;
62          } else if (nested != null) {
63              return new UnmarshalException(nested);
64          } else {
65              return new UnmarshalException(e);
66          }
67      }
68  
69      protected XMLReader getXMLReader() throws JAXBException {
70          if (reader == null) {
71              try {
72                  SAXParserFactory parserFactory = SAXParserFactory.newInstance();
73                  parserFactory.setNamespaceAware(true);
74                  parserFactory.setValidating(false);
75                  reader = parserFactory.newSAXParser().getXMLReader();
76              } catch(ParserConfigurationException e) {
77                  throw new JAXBException(e);
78              } catch(SAXException e) {
79                  throw new JAXBException(e);
80              }
81          }
82          return reader;
83      }
84  
85      public <A extends XmlAdapter> A getAdapter(Class<A> type) {
86          throw new UnsupportedOperationException();
87      }
88  
89      public AttachmentUnmarshaller getAttachmentUnmarshaller() {
90          throw new UnsupportedOperationException();
91      }
92  
93      public ValidationEventHandler getEventHandler() throws JAXBException {
94          return eventHandler;
95      }
96  
97      public Listener getListener() {
98          throw new UnsupportedOperationException();
99      }
100 
101     public Object getProperty(String name) throws PropertyException {
102         if(name == null) {
103             throw new IllegalArgumentException("name must not be null");
104         }
105         throw new PropertyException(name);
106     }
107 
108     public Schema getSchema() {
109         throw new UnsupportedOperationException();
110     }
111 
112     public boolean isValidating() throws JAXBException {
113         return validating;
114     }
115 
116     public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
117         throw new UnsupportedOperationException();
118     }
119 
120     public void setAdapter(XmlAdapter adapter) {
121         if (adapter == null) {
122             throw new IllegalArgumentException();
123         }
124         setAdapter((Class<XmlAdapter>) adapter.getClass(), adapter);
125     }
126 
127     public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) {
128         throw new UnsupportedOperationException();
129     }
130 
131     public void setEventHandler(ValidationEventHandler handler) throws JAXBException {
132         if (handler == null) {
133             handler = new DefaultValidationEventHandler();
134         }
135         eventHandler = handler;
136     }
137 
138     public void setListener(Listener listener) {
139         throw new UnsupportedOperationException();
140     }
141 
142     public void setProperty(String name, Object value) throws PropertyException {
143         if(name == null) {
144             throw new IllegalArgumentException("name must not be null");
145         }
146         throw new PropertyException(name, value);
147     }
148 
149     public void setSchema(Schema schema) {
150         throw new UnsupportedOperationException();
151     }
152 
153     public void setValidating(boolean validating) throws JAXBException {
154         this.validating = validating;
155     }
156 
157     public final Object unmarshal(File file) throws JAXBException {
158         if (file == null) {
159             throw new IllegalArgumentException("file must not be null");
160         }
161         try
162         {
163             String path = file.getAbsolutePath();
164             if (File.separatorChar != '/') {
165                 path = path.replace(File.separatorChar, '/');
166             }
167             if (!path.startsWith("/")) {
168                 path = "/" + path;
169             }
170             if (!path.endsWith("/") && file.isDirectory()) {
171                 path = path + "/";
172             }
173             return unmarshal(new URL("file", "", path));
174         }
175         catch(MalformedURLException e) {
176             throw new IllegalArgumentException(e.getMessage());
177         }
178     }
179 
180     public final Object unmarshal(InputSource source) throws JAXBException {
181         if (source == null) {
182             throw new IllegalArgumentException("source must not be null");
183         }
184         return unmarshal(getXMLReader(), source);
185     }
186 
187     public final Object unmarshal(InputStream is) throws JAXBException {
188         if (is == null) {
189             throw new IllegalArgumentException("is must not be null");
190         }
191         return unmarshal(new InputSource(is));
192     }
193 
194     public <T> JAXBElement<T> unmarshal(Node node, Class<T> declaredType) throws JAXBException {
195         throw new UnsupportedOperationException();
196     }
197 
198     public final Object unmarshal(Reader reader) throws JAXBException {
199         if (reader == null) {
200             throw new IllegalArgumentException("reader must not be null");
201         }
202         return unmarshal(new InputSource(reader));
203     }
204 
205     public Object unmarshal(Source source) throws JAXBException {
206         if (source == null) {
207             throw new IllegalArgumentException("source must not be null");
208         } else if (source instanceof SAXSource) {
209             SAXSource saxSource = (SAXSource) source;
210             XMLReader reader = saxSource.getXMLReader();
211             if (reader == null) {
212                 reader = getXMLReader();
213             }
214             return unmarshal(reader, saxSource.getInputSource());
215         } else if (source instanceof StreamSource) {
216             StreamSource ss = (StreamSource) source;
217             InputSource is = new InputSource();
218             is.setSystemId(ss.getSystemId());
219             is.setByteStream(ss.getInputStream());
220             is.setCharacterStream(ss.getReader());
221             return unmarshal(is);
222         } else if (source instanceof DOMSource)
223             return unmarshal(((DOMSource) source).getNode());
224         else
225             throw new IllegalArgumentException();
226     }
227 
228     protected abstract Object unmarshal(XMLReader xmlreader, InputSource inputsource) throws JAXBException;
229 
230     public <T> JAXBElement<T> unmarshal(Source source, Class<T> declaredType) throws JAXBException {
231         throw new UnsupportedOperationException();
232     }
233 
234     public final Object unmarshal(URL url) throws JAXBException {
235         if(url == null) {
236             throw new IllegalArgumentException("url must not be null");
237         }
238         return unmarshal(new InputSource(url.toExternalForm()));
239     }
240 
241     public Object unmarshal(XMLEventReader reader) throws JAXBException {
242         throw new UnsupportedOperationException();
243     }
244 
245     public <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class<T> declaredType) throws JAXBException {
246         throw new UnsupportedOperationException();
247     }
248 
249     public Object unmarshal(XMLStreamReader reader) throws JAXBException {
250         throw new UnsupportedOperationException();
251     }
252 
253     public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> declaredType) throws JAXBException {
254         throw new UnsupportedOperationException();
255     }
256 }