author | mkos |
Fri, 23 Aug 2013 09:57:21 +0100 | |
changeset 19645 | 36f707905f2b |
parent 12009 | 4abb694f273a |
child 22679 | d785acd84a14 |
permissions | -rw-r--r-- |
12009 | 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 { |
|
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
97 |
if (canRecover) { |
12009 | 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 |
|
19645
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
101 |
UnmarshallingContext context = UnmarshallingContext.getInstance(); |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
102 |
if (!context.parent.hasEventHandler() // is somebody listening? |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
103 |
|| !context.shouldErrorBeReported()) // should we report error? |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
104 |
return; |
36f707905f2b
8022885: Update JAX-WS RI integration to 2.2.9-b14140
mkos
parents:
12009
diff
changeset
|
105 |
} |
12009 | 106 |
if(ea.uri!=ea.uri.intern() || ea.local!=ea.local.intern()) |
107 |
reportError(Messages.UNINTERNED_STRINGS.format(), canRecover ); |
|
108 |
else |
|
109 |
reportError(Messages.UNEXPECTED_ELEMENT.format(ea.uri,ea.local,computeExpectedElements()), canRecover ); |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Returns a set of tag names expected as possible child elements in this context. |
|
114 |
*/ |
|
115 |
public Collection<QName> getExpectedChildElements() { |
|
116 |
return Collections.emptyList(); |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Returns a set of tag names expected as possible child elements in this context. |
|
121 |
*/ |
|
122 |
public Collection<QName> getExpectedAttributes() { |
|
123 |
return Collections.emptyList(); |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Called when this loaderis an active loaderand we see a chunk of text. |
|
128 |
* |
|
129 |
* The runtime makes sure that adjacent characters (even those separated |
|
130 |
* by comments, PIs, etc) are reported as one event. |
|
131 |
* IOW, you won't see two text event calls in a row. |
|
132 |
*/ |
|
133 |
public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException { |
|
134 |
// make str printable |
|
135 |
text = text.toString().replace('\r',' ').replace('\n',' ').replace('\t',' ').trim(); |
|
136 |
reportError(Messages.UNEXPECTED_TEXT.format(text), true ); |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* True if this loader expects the {@link #text(UnmarshallingContext.State, CharSequence)} method |
|
141 |
* to be called. False otherwise. |
|
142 |
*/ |
|
143 |
public final boolean expectText() { |
|
144 |
return expectText; |
|
145 |
} |
|
146 |
||
147 |
||
148 |
/** |
|
149 |
* Called when this loaderis an active loaderand we see an end tag. |
|
150 |
*/ |
|
151 |
public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException { |
|
152 |
} |
|
153 |
||
154 |
||
155 |
||
156 |
||
157 |
||
158 |
||
159 |
||
160 |
||
161 |
||
162 |
||
163 |
// |
|
164 |
// |
|
165 |
// |
|
166 |
// utility methods |
|
167 |
// |
|
168 |
// |
|
169 |
// |
|
170 |
/** |
|
171 |
* Computes the names of possible root elements for a better error diagnosis. |
|
172 |
*/ |
|
173 |
private String computeExpectedElements() { |
|
174 |
StringBuilder r = new StringBuilder(); |
|
175 |
||
176 |
for( QName n : getExpectedChildElements() ) { |
|
177 |
if(r.length()!=0) r.append(','); |
|
178 |
r.append("<{").append(n.getNamespaceURI()).append('}').append(n.getLocalPart()).append('>'); |
|
179 |
} |
|
180 |
if(r.length()==0) { |
|
181 |
return "(none)"; |
|
182 |
} |
|
183 |
||
184 |
return r.toString(); |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Fires the beforeUnmarshal event if necessary. |
|
189 |
* |
|
190 |
* @param state |
|
191 |
* state of the newly create child object. |
|
192 |
*/ |
|
193 |
protected final void fireBeforeUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException { |
|
194 |
if(beanInfo.lookForLifecycleMethods()) { |
|
195 |
UnmarshallingContext context = state.getContext(); |
|
196 |
Unmarshaller.Listener listener = context.parent.getListener(); |
|
197 |
if(beanInfo.hasBeforeUnmarshalMethod()) { |
|
198 |
beanInfo.invokeBeforeUnmarshalMethod(context.parent, child, state.prev.target); |
|
199 |
} |
|
200 |
if(listener!=null) { |
|
201 |
listener.beforeUnmarshal(child, state.prev.target); |
|
202 |
} |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
/** |
|
207 |
* Fires the afterUnmarshal event if necessary. |
|
208 |
* |
|
209 |
* @param state |
|
210 |
* state of the parent object |
|
211 |
*/ |
|
212 |
protected final void fireAfterUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException { |
|
213 |
// fire the event callback |
|
214 |
if(beanInfo.lookForLifecycleMethods()) { |
|
215 |
UnmarshallingContext context = state.getContext(); |
|
216 |
Unmarshaller.Listener listener = context.parent.getListener(); |
|
217 |
if(beanInfo.hasAfterUnmarshalMethod()) { |
|
218 |
beanInfo.invokeAfterUnmarshalMethod(context.parent, child, state.target); |
|
219 |
} |
|
220 |
if(listener!=null) |
|
221 |
listener.afterUnmarshal(child, state.target); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
||
226 |
/** |
|
227 |
* Last resort when something goes terribly wrong within the unmarshaller. |
|
228 |
*/ |
|
229 |
protected static void handleGenericException(Exception e) throws SAXException { |
|
230 |
handleGenericException(e,false); |
|
231 |
} |
|
232 |
||
233 |
public static void handleGenericException(Exception e, boolean canRecover) throws SAXException { |
|
234 |
reportError(e.getMessage(), e, canRecover ); |
|
235 |
} |
|
236 |
||
237 |
public static void handleGenericError(Error e) throws SAXException { |
|
238 |
reportError(e.getMessage(), false); |
|
239 |
} |
|
240 |
||
241 |
protected static void reportError(String msg, boolean canRecover) throws SAXException { |
|
242 |
reportError(msg, null, canRecover ); |
|
243 |
} |
|
244 |
||
245 |
public static void reportError(String msg, Exception nested, boolean canRecover) throws SAXException { |
|
246 |
UnmarshallingContext context = UnmarshallingContext.getInstance(); |
|
247 |
context.handleEvent( new ValidationEventImpl( |
|
248 |
canRecover? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR, |
|
249 |
msg, |
|
250 |
context.getLocator().getLocation(), |
|
251 |
nested ), canRecover ); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* This method is called by the generated derived class |
|
256 |
* when a datatype parse method throws an exception. |
|
257 |
*/ |
|
258 |
protected static void handleParseConversionException(UnmarshallingContext.State state, Exception e) throws SAXException { |
|
259 |
// wrap it into a ParseConversionEvent and report it |
|
260 |
state.getContext().handleError(e); |
|
261 |
} |
|
262 |
} |