aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, 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);
aoqi@0: state.loader = Discarder.INSTANCE;
aoqi@0: state.receiver = 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