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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial