001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package javax.xml.bind.helpers;
031:
032: import org.w3c.dom.Node;
033: import org.xml.sax.InputSource;
034: import org.xml.sax.SAXException;
035: import org.xml.sax.XMLReader;
036: import org.xml.sax.helpers.XMLReaderFactory;
037:
038: import javax.xml.bind.*;
039: import javax.xml.bind.annotation.adapters.XmlAdapter;
040: import javax.xml.bind.attachment.AttachmentUnmarshaller;
041: import javax.xml.stream.XMLEventReader;
042: import javax.xml.stream.XMLInputFactory;
043: import javax.xml.stream.XMLStreamException;
044: import javax.xml.stream.XMLStreamReader;
045: import javax.xml.transform.Source;
046: import javax.xml.validation.Schema;
047: import java.io.File;
048: import java.io.FileInputStream;
049: import java.io.IOException;
050: import java.io.InputStream;
051: import java.io.Reader;
052: import java.net.URL;
053: import java.util.HashMap;
054:
055: public abstract class AbstractUnmarshallerImpl implements Unmarshaller {
056: private static XMLReader _xmlReader;
057:
058: static {
059: try {
060: _xmlReader = XMLReaderFactory.createXMLReader();
061: } catch (SAXException e) {
062: // XXX
063: }
064: }
065:
066: private static final ValidationEventHandler _defaultValidationEventHandler = new DefaultValidationEventHandler() {
067: public boolean handleEvent(ValidationEvent event) {
068: if (event == null)
069: throw new IllegalArgumentException();
070:
071: return event.getSeverity() != ValidationEvent.FATAL_ERROR;
072: }
073: };
074:
075: private XMLInputFactory _factory;
076: protected boolean validating;
077:
078: private AttachmentUnmarshaller _attachmentUnmarshaller = null;
079: private ValidationEventHandler _validationEventHandler = _defaultValidationEventHandler;
080: private Listener _listener = null;
081: private HashMap<String, Object> _properties = new HashMap<String, Object>();
082: private Schema _schema = null;
083: private XmlAdapter _adapter = null;
084: private HashMap<Class, XmlAdapter> _adapters = new HashMap<Class, XmlAdapter>();
085:
086: public AbstractUnmarshallerImpl() {
087: }
088:
089: protected UnmarshalException createUnmarshalException(SAXException e) {
090: return new UnmarshalException(e);
091: }
092:
093: public <A extends XmlAdapter> A getAdapter(Class<A> type) {
094: return (A) _adapters.get(type);
095: }
096:
097: public AttachmentUnmarshaller getAttachmentUnmarshaller() {
098: return _attachmentUnmarshaller;
099: }
100:
101: public ValidationEventHandler getEventHandler()
102: throws JAXBException {
103: return _validationEventHandler;
104: }
105:
106: public Listener getListener() {
107: return _listener;
108: }
109:
110: public Object getProperty(String name) throws PropertyException {
111: return _properties.get(name);
112: }
113:
114: public Schema getSchema() {
115: return _schema;
116: }
117:
118: protected XMLReader getXMLReader() throws JAXBException {
119: return _xmlReader;
120: }
121:
122: public boolean isValidating() throws JAXBException {
123: return validating;
124: }
125:
126: public <A extends XmlAdapter> void setAdapter(Class<A> type,
127: A adapter) {
128: _adapters.put(type, adapter);
129: }
130:
131: public void setAdapter(XmlAdapter adapter) {
132: setAdapter((Class) adapter.getClass(), adapter);
133: }
134:
135: public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) {
136: _attachmentUnmarshaller = au;
137: }
138:
139: public void setEventHandler(ValidationEventHandler handler)
140: throws JAXBException {
141: _validationEventHandler = handler;
142: }
143:
144: public void setListener(Listener listener) {
145: _listener = listener;
146: }
147:
148: public void setProperty(String name, Object value)
149: throws PropertyException {
150: _properties.put(name, value);
151: }
152:
153: public void setSchema(Schema schema) {
154: _schema = schema;
155: }
156:
157: public void setValidating(boolean validating) throws JAXBException {
158: this .validating = validating;
159: }
160:
161: private XMLInputFactory getXMLInputFactory() {
162: if (_factory == null)
163: _factory = XMLInputFactory.newInstance();
164:
165: return _factory;
166: }
167:
168: public final Object unmarshal(File f) throws JAXBException {
169: FileInputStream fis = null;
170: try {
171: fis = new FileInputStream(f);
172: return unmarshal(fis);
173: } catch (IOException e) {
174: throw new JAXBException(e);
175: } finally {
176: try {
177: if (fis != null)
178: fis.close();
179: } catch (IOException e) {
180: throw new JAXBException(e);
181: }
182: }
183: }
184:
185: public final Object unmarshal(InputSource source)
186: throws JAXBException {
187: throw new UnsupportedOperationException(
188: "subclasses must override this");
189: }
190:
191: public final Object unmarshal(InputStream is) throws JAXBException {
192: try {
193: XMLInputFactory factory = XMLInputFactory.newInstance();
194: return unmarshal(factory.createXMLStreamReader(is));
195: } catch (XMLStreamException e) {
196: throw new JAXBException(e);
197: }
198: }
199:
200: public final Object unmarshal(Reader reader) throws JAXBException {
201: try {
202: XMLInputFactory factory = getXMLInputFactory();
203:
204: return unmarshal(factory.createXMLStreamReader(reader));
205: } catch (XMLStreamException e) {
206: throw new JAXBException(e);
207: }
208: }
209:
210: public Object unmarshal(Source source) throws JAXBException {
211: try {
212: XMLInputFactory factory = getXMLInputFactory();
213:
214: return unmarshal(factory.createXMLStreamReader(source));
215: } catch (XMLStreamException e) {
216: throw new JAXBException(e);
217: }
218: }
219:
220: public <T> JAXBElement<T> unmarshal(Node node, Class<T> declaredType)
221: throws JAXBException {
222: throw new UnsupportedOperationException(
223: "subclasses must override this");
224: }
225:
226: public <T> JAXBElement<T> unmarshal(Source node,
227: Class<T> declaredType) throws JAXBException {
228: try {
229: XMLInputFactory factory = XMLInputFactory.newInstance();
230: return unmarshal(factory.createXMLStreamReader(node),
231: declaredType);
232: } catch (XMLStreamException e) {
233: throw new JAXBException(e);
234: }
235: }
236:
237: public final Object unmarshal(URL url) throws JAXBException {
238: try {
239: return unmarshal(url.openStream());
240: } catch (IOException e) {
241: throw new JAXBException(e);
242: }
243: }
244:
245: public Object unmarshal(XMLEventReader reader) throws JAXBException {
246: throw new UnsupportedOperationException(
247: "subclasses must override this");
248: }
249:
250: public <T> JAXBElement<T> unmarshal(XMLEventReader xmlEventReader,
251: Class<T> declaredType) throws JAXBException {
252: throw new UnsupportedOperationException(
253: "subclasses must override this");
254: }
255:
256: public <T> JAXBElement<T> unmarshal(
257: XMLStreamReader xmlStreamReader, Class<T> declaredType)
258: throws JAXBException {
259: throw new UnsupportedOperationException(
260: "subclasses must override this");
261: }
262:
263: public Object unmarshal(XMLStreamReader reader)
264: throws JAXBException {
265: throw new UnsupportedOperationException(
266: "subclasses must override this");
267: }
268:
269: public abstract UnmarshallerHandler getUnmarshallerHandler();
270:
271: protected abstract Object unmarshal(XMLReader reader,
272: InputSource source) throws JAXBException;
273: }
|