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

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 650
121e938cb9c3
parent 637
9c07ef4934dd
permissions
-rw-r--r--

merge

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

mercurial