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