1
2
3
4
5
6
7
8 package de.nierbeck.timeTrack.model.impl.runtime;
9
10 import java.io.IOException;
11
12 import javax.xml.bind.DatatypeConverter;
13 import javax.xml.bind.JAXBException;
14 import javax.xml.bind.UnmarshallerHandler;
15 import javax.xml.bind.helpers.AbstractUnmarshallerImpl;
16
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.Node;
20 import org.xml.sax.InputSource;
21 import org.xml.sax.SAXException;
22 import org.xml.sax.XMLReader;
23 import org.xml.sax.helpers.DefaultHandler;
24
25 import com.sun.xml.bind.DatatypeConverterImpl;
26 import com.sun.xml.bind.unmarshaller.DOMScanner;
27 import com.sun.xml.bind.unmarshaller.InterningXMLReader;
28 import com.sun.xml.bind.validator.DOMLocator;
29 import com.sun.xml.bind.validator.Locator;
30 import com.sun.xml.bind.validator.SAXLocator;
31
32 /***
33 * Default Unmarshall implementation.
34 *
35 * <p>
36 * This class can be extended by the generated code to provide type-safe
37 * unmarshall methods.
38 *
39 * @author <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
40 */
41 public class UnmarshallerImpl extends AbstractUnmarshallerImpl {
42 /*** parent JAXBContext object that created this unmarshaller */
43 private DefaultJAXBContextImpl context = null;
44
45 private final GrammarInfo grammarInfo;
46
47 public UnmarshallerImpl(DefaultJAXBContextImpl context, GrammarInfo gi) {
48
49 this.context = context;
50 this.grammarInfo = gi;
51
52
53 DatatypeConverter
54 .setDatatypeConverter(DatatypeConverterImpl.theInstance);
55 }
56
57 public void setValidating(boolean validating) throws JAXBException {
58 super.setValidating(validating);
59 if (validating == true)
60
61
62 context.getGrammar();
63 }
64
65 public UnmarshallerHandler getUnmarshallerHandler() {
66
67
68
69
70
71
72
73 return new InterningUnmarshallerHandler(
74 createUnmarshallerHandler(new SAXLocator()));
75 }
76
77 /***
78 * Creates and configures a new unmarshalling pipe line. Depending on the
79 * setting, we put a validator as a filter.
80 *
81 * @return A component that implements both UnmarshallerHandler and
82 * ValidationEventHandler. All the parsing errors should be reported
83 * to this error handler for the unmarshalling process to work
84 * correctly.
85 *
86 * @param locator
87 * The object that is responsible to obtain the source location
88 * information for {@link ValidationEvent}s.
89 */
90 private SAXUnmarshallerHandler createUnmarshallerHandler(Locator locator) {
91
92 SAXUnmarshallerHandler unmarshaller = new SAXUnmarshallerHandlerImpl(
93 this, grammarInfo);
94
95 try {
96
97
98 if (isValidating()) {
99
100
101 unmarshaller = ValidatingUnmarshaller.create(context
102 .getGrammar(), unmarshaller, locator);
103 }
104 } catch (JAXBException e) {
105
106
107 e.printStackTrace();
108 }
109
110 return unmarshaller;
111 }
112
113 protected Object unmarshal(XMLReader reader, InputSource source)
114 throws JAXBException {
115
116 SAXLocator locator = new SAXLocator();
117 SAXUnmarshallerHandler handler = createUnmarshallerHandler(locator);
118
119 reader = InterningXMLReader.adapt(reader);
120
121 reader.setContentHandler(handler);
122
123
124
125
126
127
128
129
130
131
132
133 reader.setErrorHandler(new ErrorHandlerAdaptor(handler, locator));
134
135 try {
136 reader.parse(source);
137 } catch (IOException e) {
138 throw new JAXBException(e);
139 } catch (SAXException e) {
140 throw createUnmarshalException(e);
141 }
142
143 Object result = handler.getResult();
144
145
146
147
148 reader.setContentHandler(dummyHandler);
149 reader.setErrorHandler(dummyHandler);
150
151 return result;
152 }
153
154 public final Object unmarshal(Node node) throws JAXBException {
155 try {
156 DOMScanner scanner = new DOMScanner();
157 UnmarshallerHandler handler = new InterningUnmarshallerHandler(
158 createUnmarshallerHandler(new DOMLocator(scanner)));
159
160 if (node instanceof Element)
161 scanner.parse((Element) node, handler);
162 else if (node instanceof Document)
163 scanner.parse(((Document) node).getDocumentElement(), handler);
164 else
165
166 throw new IllegalArgumentException();
167
168 return handler.getResult();
169 } catch (SAXException e) {
170 throw createUnmarshalException(e);
171 }
172 }
173
174 private static final DefaultHandler dummyHandler = new DefaultHandler();
175 }