src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/Loader.java

Sun, 31 Aug 2014 16:14:36 +0400

author
aefimov
date
Sun, 31 Aug 2014 16:14:36 +0400
changeset 707
31893650acaf
parent 650
121e938cb9c3
child 760
e530533619ec
permissions
-rw-r--r--

8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos

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

mercurial