|
1 /* |
|
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.bind.v2.runtime.unmarshaller; |
|
27 |
|
28 import java.util.Collection; |
|
29 import java.util.Collections; |
|
30 |
|
31 import javax.xml.bind.Unmarshaller; |
|
32 import javax.xml.bind.ValidationEvent; |
|
33 import javax.xml.bind.helpers.ValidationEventImpl; |
|
34 import javax.xml.namespace.QName; |
|
35 |
|
36 import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; |
|
37 |
|
38 import org.xml.sax.SAXException; |
|
39 |
|
40 /** |
|
41 * @author Kohsuke Kawaguchi |
|
42 */ |
|
43 public abstract class Loader { |
|
44 |
|
45 // allow derived classes to change it later |
|
46 protected boolean expectText; |
|
47 |
|
48 protected Loader(boolean expectText) { |
|
49 this.expectText = expectText; |
|
50 } |
|
51 |
|
52 protected Loader() { |
|
53 } |
|
54 |
|
55 // |
|
56 // |
|
57 // |
|
58 // Contract |
|
59 // |
|
60 // |
|
61 // |
|
62 /** |
|
63 * Called when the loader is activated, which is when a new start tag is seen |
|
64 * and when the parent designated this loader as the child loader. |
|
65 * |
|
66 * <p> |
|
67 * The callee may change <tt>state.loader</tt> to designate another {@link Loader} |
|
68 * for the processing. It's the responsibility of the callee to forward the startElement |
|
69 * event in such a case. |
|
70 * |
|
71 * @param ea |
|
72 * info about the start tag. never null. |
|
73 */ |
|
74 public void startElement(UnmarshallingContext.State state,TagName ea) throws SAXException { |
|
75 } |
|
76 |
|
77 /** |
|
78 * Called when this loaderis an active loaderand we see a new child start tag. |
|
79 * |
|
80 * <p> |
|
81 * The callee is expected to designate another loaderas a loaderthat processes |
|
82 * this element, then it should also register a {@link Receiver}. |
|
83 * The designated loaderwill become an active loader. |
|
84 * |
|
85 * <p> |
|
86 * The default implementation reports an error saying an element is unexpected. |
|
87 */ |
|
88 public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { |
|
89 // notify the error, then recover by ignoring the whole element. |
|
90 reportUnexpectedChildElement(ea, true); |
|
91 state.loader = Discarder.INSTANCE; |
|
92 state.receiver = null; |
|
93 } |
|
94 |
|
95 @SuppressWarnings({"StringEquality"}) |
|
96 protected final void reportUnexpectedChildElement(TagName ea, boolean canRecover) throws SAXException { |
|
97 if(canRecover && !UnmarshallingContext.getInstance().parent.hasEventHandler()) |
|
98 // this error happens particurly often (when input documents contain a lot of unexpected elements to be ignored), |
|
99 // so don't bother computing all the messages and etc if we know that |
|
100 // there's no event handler to receive the error in the end. See #286 |
|
101 return; |
|
102 if(ea.uri!=ea.uri.intern() || ea.local!=ea.local.intern()) |
|
103 reportError(Messages.UNINTERNED_STRINGS.format(), canRecover ); |
|
104 else |
|
105 reportError(Messages.UNEXPECTED_ELEMENT.format(ea.uri,ea.local,computeExpectedElements()), canRecover ); |
|
106 } |
|
107 |
|
108 /** |
|
109 * Returns a set of tag names expected as possible child elements in this context. |
|
110 */ |
|
111 public Collection<QName> getExpectedChildElements() { |
|
112 return Collections.emptyList(); |
|
113 } |
|
114 |
|
115 /** |
|
116 * Returns a set of tag names expected as possible child elements in this context. |
|
117 */ |
|
118 public Collection<QName> getExpectedAttributes() { |
|
119 return Collections.emptyList(); |
|
120 } |
|
121 |
|
122 /** |
|
123 * Called when this loaderis an active loaderand we see a chunk of text. |
|
124 * |
|
125 * The runtime makes sure that adjacent characters (even those separated |
|
126 * by comments, PIs, etc) are reported as one event. |
|
127 * IOW, you won't see two text event calls in a row. |
|
128 */ |
|
129 public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException { |
|
130 // make str printable |
|
131 text = text.toString().replace('\r',' ').replace('\n',' ').replace('\t',' ').trim(); |
|
132 reportError(Messages.UNEXPECTED_TEXT.format(text), true ); |
|
133 } |
|
134 |
|
135 /** |
|
136 * True if this loader expects the {@link #text(UnmarshallingContext.State, CharSequence)} method |
|
137 * to be called. False otherwise. |
|
138 */ |
|
139 public final boolean expectText() { |
|
140 return expectText; |
|
141 } |
|
142 |
|
143 |
|
144 /** |
|
145 * Called when this loaderis an active loaderand we see an end tag. |
|
146 */ |
|
147 public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException { |
|
148 } |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 // |
|
160 // |
|
161 // |
|
162 // utility methods |
|
163 // |
|
164 // |
|
165 // |
|
166 /** |
|
167 * Computes the names of possible root elements for a better error diagnosis. |
|
168 */ |
|
169 private String computeExpectedElements() { |
|
170 StringBuilder r = new StringBuilder(); |
|
171 |
|
172 for( QName n : getExpectedChildElements() ) { |
|
173 if(r.length()!=0) r.append(','); |
|
174 r.append("<{").append(n.getNamespaceURI()).append('}').append(n.getLocalPart()).append('>'); |
|
175 } |
|
176 if(r.length()==0) { |
|
177 return "(none)"; |
|
178 } |
|
179 |
|
180 return r.toString(); |
|
181 } |
|
182 |
|
183 /** |
|
184 * Fires the beforeUnmarshal event if necessary. |
|
185 * |
|
186 * @param state |
|
187 * state of the newly create child object. |
|
188 */ |
|
189 protected final void fireBeforeUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException { |
|
190 if(beanInfo.lookForLifecycleMethods()) { |
|
191 UnmarshallingContext context = state.getContext(); |
|
192 Unmarshaller.Listener listener = context.parent.getListener(); |
|
193 if(beanInfo.hasBeforeUnmarshalMethod()) { |
|
194 beanInfo.invokeBeforeUnmarshalMethod(context.parent, child, state.prev.target); |
|
195 } |
|
196 if(listener!=null) { |
|
197 listener.beforeUnmarshal(child, state.prev.target); |
|
198 } |
|
199 } |
|
200 } |
|
201 |
|
202 /** |
|
203 * Fires the afterUnmarshal event if necessary. |
|
204 * |
|
205 * @param state |
|
206 * state of the parent object |
|
207 */ |
|
208 protected final void fireAfterUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException { |
|
209 // fire the event callback |
|
210 if(beanInfo.lookForLifecycleMethods()) { |
|
211 UnmarshallingContext context = state.getContext(); |
|
212 Unmarshaller.Listener listener = context.parent.getListener(); |
|
213 if(beanInfo.hasAfterUnmarshalMethod()) { |
|
214 beanInfo.invokeAfterUnmarshalMethod(context.parent, child, state.target); |
|
215 } |
|
216 if(listener!=null) |
|
217 listener.afterUnmarshal(child, state.target); |
|
218 } |
|
219 } |
|
220 |
|
221 |
|
222 /** |
|
223 * Last resort when something goes terribly wrong within the unmarshaller. |
|
224 */ |
|
225 protected static void handleGenericException(Exception e) throws SAXException { |
|
226 handleGenericException(e,false); |
|
227 } |
|
228 |
|
229 public static void handleGenericException(Exception e, boolean canRecover) throws SAXException { |
|
230 reportError(e.getMessage(), e, canRecover ); |
|
231 } |
|
232 |
|
233 public static void handleGenericError(Error e) throws SAXException { |
|
234 reportError(e.getMessage(), false); |
|
235 } |
|
236 |
|
237 protected static void reportError(String msg, boolean canRecover) throws SAXException { |
|
238 reportError(msg, null, canRecover ); |
|
239 } |
|
240 |
|
241 public static void reportError(String msg, Exception nested, boolean canRecover) throws SAXException { |
|
242 UnmarshallingContext context = UnmarshallingContext.getInstance(); |
|
243 context.handleEvent( new ValidationEventImpl( |
|
244 canRecover? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR, |
|
245 msg, |
|
246 context.getLocator().getLocation(), |
|
247 nested ), canRecover ); |
|
248 } |
|
249 |
|
250 /** |
|
251 * This method is called by the generated derived class |
|
252 * when a datatype parse method throws an exception. |
|
253 */ |
|
254 protected static void handleParseConversionException(UnmarshallingContext.State state, Exception e) throws SAXException { |
|
255 // wrap it into a ParseConversionEvent and report it |
|
256 state.getContext().handleError(e); |
|
257 } |
|
258 } |