aoqi@0: /* aefimov@650: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime.unmarshaller; aoqi@0: aoqi@0: import java.util.Collection; aoqi@0: import java.util.Collections; aoqi@0: aoqi@0: import javax.xml.bind.Unmarshaller; aoqi@0: import javax.xml.bind.ValidationEvent; aoqi@0: import javax.xml.bind.helpers.ValidationEventImpl; aoqi@0: import javax.xml.namespace.QName; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; aoqi@0: aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public abstract class Loader { aoqi@0: aoqi@0: // allow derived classes to change it later aoqi@0: protected boolean expectText; aoqi@0: aoqi@0: protected Loader(boolean expectText) { aoqi@0: this.expectText = expectText; aoqi@0: } aoqi@0: aoqi@0: protected Loader() { aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // aoqi@0: // aoqi@0: // Contract aoqi@0: // aoqi@0: // aoqi@0: // aoqi@0: /** aoqi@0: * Called when the loader is activated, which is when a new start tag is seen aoqi@0: * and when the parent designated this loader as the child loader. aoqi@0: * aoqi@0: *

aoqi@0: * The callee may change state.loader to designate another {@link Loader} aoqi@0: * for the processing. It's the responsibility of the callee to forward the startElement aoqi@0: * event in such a case. aoqi@0: * aoqi@0: * @param ea aoqi@0: * info about the start tag. never null. aoqi@0: */ aoqi@0: public void startElement(UnmarshallingContext.State state,TagName ea) throws SAXException { aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Called when this loaderis an active loaderand we see a new child start tag. aoqi@0: * aoqi@0: *

aoqi@0: * The callee is expected to designate another loaderas a loaderthat processes aoqi@0: * this element, then it should also register a {@link Receiver}. aoqi@0: * The designated loaderwill become an active loader. aoqi@0: * aoqi@0: *

aoqi@0: * The default implementation reports an error saying an element is unexpected. aoqi@0: */ aoqi@0: public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { aoqi@0: // notify the error, then recover by ignoring the whole element. aoqi@0: reportUnexpectedChildElement(ea, true); aefimov@650: state.setLoader(Discarder.INSTANCE); aefimov@650: state.setReceiver(null); aoqi@0: } aoqi@0: aoqi@0: @SuppressWarnings({"StringEquality"}) aoqi@0: protected final void reportUnexpectedChildElement(TagName ea, boolean canRecover) throws SAXException { aoqi@0: if (canRecover) { aoqi@0: // this error happens particurly often (when input documents contain a lot of unexpected elements to be ignored), aoqi@0: // so don't bother computing all the messages and etc if we know that aoqi@0: // there's no event handler to receive the error in the end. See #286 aoqi@0: UnmarshallingContext context = UnmarshallingContext.getInstance(); aoqi@0: if (!context.parent.hasEventHandler() // is somebody listening? aoqi@0: || !context.shouldErrorBeReported()) // should we report error? aoqi@0: return; aoqi@0: } aoqi@0: if(ea.uri!=ea.uri.intern() || ea.local!=ea.local.intern()) aoqi@0: reportError(Messages.UNINTERNED_STRINGS.format(), canRecover ); aoqi@0: else aoqi@0: reportError(Messages.UNEXPECTED_ELEMENT.format(ea.uri,ea.local,computeExpectedElements()), canRecover ); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a set of tag names expected as possible child elements in this context. aoqi@0: */ aoqi@0: public Collection getExpectedChildElements() { aoqi@0: return Collections.emptyList(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a set of tag names expected as possible child elements in this context. aoqi@0: */ aoqi@0: public Collection getExpectedAttributes() { aoqi@0: return Collections.emptyList(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Called when this loaderis an active loaderand we see a chunk of text. aoqi@0: * aoqi@0: * The runtime makes sure that adjacent characters (even those separated aoqi@0: * by comments, PIs, etc) are reported as one event. aoqi@0: * IOW, you won't see two text event calls in a row. aoqi@0: */ aoqi@0: public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException { aoqi@0: // make str printable aoqi@0: text = text.toString().replace('\r',' ').replace('\n',' ').replace('\t',' ').trim(); aoqi@0: reportError(Messages.UNEXPECTED_TEXT.format(text), true ); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * True if this loader expects the {@link #text(UnmarshallingContext.State, CharSequence)} method aoqi@0: * to be called. False otherwise. aoqi@0: */ aoqi@0: public final boolean expectText() { aoqi@0: return expectText; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Called when this loaderis an active loaderand we see an end tag. aoqi@0: */ aoqi@0: public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException { aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: // aoqi@0: // aoqi@0: // aoqi@0: // utility methods aoqi@0: // aoqi@0: // aoqi@0: // aoqi@0: /** aoqi@0: * Computes the names of possible root elements for a better error diagnosis. aoqi@0: */ aoqi@0: private String computeExpectedElements() { aoqi@0: StringBuilder r = new StringBuilder(); aoqi@0: aoqi@0: for( QName n : getExpectedChildElements() ) { aoqi@0: if(r.length()!=0) r.append(','); aoqi@0: r.append("<{").append(n.getNamespaceURI()).append('}').append(n.getLocalPart()).append('>'); aoqi@0: } aoqi@0: if(r.length()==0) { aoqi@0: return "(none)"; aoqi@0: } aoqi@0: aoqi@0: return r.toString(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Fires the beforeUnmarshal event if necessary. aoqi@0: * aoqi@0: * @param state aoqi@0: * state of the newly create child object. aoqi@0: */ aoqi@0: protected final void fireBeforeUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException { aoqi@0: if(beanInfo.lookForLifecycleMethods()) { aoqi@0: UnmarshallingContext context = state.getContext(); aoqi@0: Unmarshaller.Listener listener = context.parent.getListener(); aoqi@0: if(beanInfo.hasBeforeUnmarshalMethod()) { aefimov@650: beanInfo.invokeBeforeUnmarshalMethod(context.parent, child, state.getPrev().getTarget()); aoqi@0: } aoqi@0: if(listener!=null) { aefimov@650: listener.beforeUnmarshal(child, state.getPrev().getTarget()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Fires the afterUnmarshal event if necessary. aoqi@0: * aoqi@0: * @param state aoqi@0: * state of the parent object aoqi@0: */ aoqi@0: protected final void fireAfterUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException { aoqi@0: // fire the event callback aoqi@0: if(beanInfo.lookForLifecycleMethods()) { aoqi@0: UnmarshallingContext context = state.getContext(); aoqi@0: Unmarshaller.Listener listener = context.parent.getListener(); aoqi@0: if(beanInfo.hasAfterUnmarshalMethod()) { aefimov@650: beanInfo.invokeAfterUnmarshalMethod(context.parent, child, state.getTarget()); aoqi@0: } aoqi@0: if(listener!=null) aefimov@650: listener.afterUnmarshal(child, state.getTarget()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Last resort when something goes terribly wrong within the unmarshaller. aoqi@0: */ aoqi@0: protected static void handleGenericException(Exception e) throws SAXException { aoqi@0: handleGenericException(e,false); aoqi@0: } aoqi@0: aoqi@0: public static void handleGenericException(Exception e, boolean canRecover) throws SAXException { aoqi@0: reportError(e.getMessage(), e, canRecover ); aoqi@0: } aoqi@0: aoqi@0: public static void handleGenericError(Error e) throws SAXException { aoqi@0: reportError(e.getMessage(), false); aoqi@0: } aoqi@0: aoqi@0: protected static void reportError(String msg, boolean canRecover) throws SAXException { aoqi@0: reportError(msg, null, canRecover ); aoqi@0: } aoqi@0: aoqi@0: public static void reportError(String msg, Exception nested, boolean canRecover) throws SAXException { aoqi@0: UnmarshallingContext context = UnmarshallingContext.getInstance(); aoqi@0: context.handleEvent( new ValidationEventImpl( aoqi@0: canRecover? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR, aoqi@0: msg, aoqi@0: context.getLocator().getLocation(), aoqi@0: nested ), canRecover ); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * This method is called by the generated derived class aoqi@0: * when a datatype parse method throws an exception. aoqi@0: */ aoqi@0: protected static void handleParseConversionException(UnmarshallingContext.State state, Exception e) throws SAXException { aoqi@0: // wrap it into a ParseConversionEvent and report it aoqi@0: state.getContext().handleError(e); aoqi@0: } aoqi@0: }