001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package javax.xml.bind.helpers;
018    
019    import java.io.File;
020    import java.io.InputStream;
021    import java.io.Reader;
022    import java.net.URL;
023    import java.net.MalformedURLException;
024    
025    import javax.xml.bind.Unmarshaller;
026    import javax.xml.bind.ValidationEventHandler;
027    import javax.xml.bind.UnmarshallerHandler;
028    import javax.xml.bind.JAXBElement;
029    import javax.xml.bind.UnmarshalException;
030    import javax.xml.bind.JAXBException;
031    import javax.xml.bind.PropertyException;
032    import javax.xml.bind.attachment.AttachmentUnmarshaller;
033    import javax.xml.bind.annotation.adapters.XmlAdapter;
034    import javax.xml.validation.Schema;
035    import javax.xml.transform.Source;
036    import javax.xml.transform.stream.StreamSource;
037    import javax.xml.transform.sax.SAXSource;
038    import javax.xml.transform.dom.DOMSource;
039    import javax.xml.stream.XMLEventReader;
040    import javax.xml.stream.XMLStreamReader;
041    import javax.xml.parsers.ParserConfigurationException;
042    import javax.xml.parsers.SAXParserFactory;
043    
044    import org.w3c.dom.Node;
045    
046    import org.xml.sax.InputSource;
047    import org.xml.sax.SAXException;
048    import org.xml.sax.XMLReader;
049    
050    public abstract class AbstractUnmarshallerImpl implements Unmarshaller {
051    
052        protected boolean validating;
053        private ValidationEventHandler eventHandler;
054        private XMLReader reader;
055    
056        protected UnmarshalException createUnmarshalException(SAXException e) {
057            Exception nested = e.getException();
058            if (nested instanceof UnmarshalException) {
059                return (UnmarshalException)nested;
060            } else if(nested instanceof RuntimeException) {
061                throw (RuntimeException)nested;
062            } else if (nested != null) {
063                return new UnmarshalException(nested);
064            } else {
065                return new UnmarshalException(e);
066            }
067        }
068    
069        protected XMLReader getXMLReader() throws JAXBException {
070            if (reader == null) {
071                try {
072                    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
073                    parserFactory.setNamespaceAware(true);
074                    parserFactory.setValidating(false);
075                    reader = parserFactory.newSAXParser().getXMLReader();
076                } catch(ParserConfigurationException e) {
077                    throw new JAXBException(e);
078                } catch(SAXException e) {
079                    throw new JAXBException(e);
080                }
081            }
082            return reader;
083        }
084    
085        public <A extends XmlAdapter> A getAdapter(Class<A> type) {
086            throw new UnsupportedOperationException();
087        }
088    
089        public AttachmentUnmarshaller getAttachmentUnmarshaller() {
090            throw new UnsupportedOperationException();
091        }
092    
093        public ValidationEventHandler getEventHandler() throws JAXBException {
094            return eventHandler;
095        }
096    
097        public Listener getListener() {
098            throw new UnsupportedOperationException();
099        }
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    }