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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/Loader.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,262 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
    1.30 +
    1.31 +import java.util.Collection;
    1.32 +import java.util.Collections;
    1.33 +
    1.34 +import javax.xml.bind.Unmarshaller;
    1.35 +import javax.xml.bind.ValidationEvent;
    1.36 +import javax.xml.bind.helpers.ValidationEventImpl;
    1.37 +import javax.xml.namespace.QName;
    1.38 +
    1.39 +import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo;
    1.40 +
    1.41 +import org.xml.sax.SAXException;
    1.42 +
    1.43 +/**
    1.44 + * @author Kohsuke Kawaguchi
    1.45 + */
    1.46 +public abstract class Loader {
    1.47 +
    1.48 +    // allow derived classes to change it later
    1.49 +    protected boolean expectText;
    1.50 +
    1.51 +    protected Loader(boolean expectText) {
    1.52 +        this.expectText = expectText;
    1.53 +    }
    1.54 +
    1.55 +    protected Loader() {
    1.56 +    }
    1.57 +
    1.58 +//
    1.59 +//
    1.60 +//
    1.61 +// Contract
    1.62 +//
    1.63 +//
    1.64 +//
    1.65 +    /**
    1.66 +     * Called when the loader is activated, which is when a new start tag is seen
    1.67 +     * and when the parent designated this loader as the child loader.
    1.68 +     *
    1.69 +     * <p>
    1.70 +     * The callee may change <tt>state.loader</tt> to designate another {@link Loader}
    1.71 +     * for the processing. It's the responsibility of the callee to forward the startElement
    1.72 +     * event in such a case.
    1.73 +     *
    1.74 +     * @param ea
    1.75 +     *      info about the start tag. never null.
    1.76 +     */
    1.77 +    public void startElement(UnmarshallingContext.State state,TagName ea) throws SAXException {
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * Called when this loaderis an active loaderand we see a new child start tag.
    1.82 +     *
    1.83 +     * <p>
    1.84 +     * The callee is expected to designate another loaderas a loaderthat processes
    1.85 +     * this element, then it should also register a {@link Receiver}.
    1.86 +     * The designated loaderwill become an active loader.
    1.87 +     *
    1.88 +     * <p>
    1.89 +     * The default implementation reports an error saying an element is unexpected.
    1.90 +     */
    1.91 +    public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
    1.92 +        // notify the error, then recover by ignoring the whole element.
    1.93 +        reportUnexpectedChildElement(ea, true);
    1.94 +        state.loader = Discarder.INSTANCE;
    1.95 +        state.receiver = null;
    1.96 +    }
    1.97 +
    1.98 +    @SuppressWarnings({"StringEquality"})
    1.99 +    protected final void reportUnexpectedChildElement(TagName ea, boolean canRecover) throws SAXException {
   1.100 +        if (canRecover) {
   1.101 +            // this error happens particurly often (when input documents contain a lot of unexpected elements to be ignored),
   1.102 +            // so don't bother computing all the messages and etc if we know that
   1.103 +            // there's no event handler to receive the error in the end. See #286
   1.104 +            UnmarshallingContext context = UnmarshallingContext.getInstance();
   1.105 +            if (!context.parent.hasEventHandler() // is somebody listening?
   1.106 +                    || !context.shouldErrorBeReported()) // should we report error?
   1.107 +                return;
   1.108 +        }
   1.109 +        if(ea.uri!=ea.uri.intern() || ea.local!=ea.local.intern())
   1.110 +            reportError(Messages.UNINTERNED_STRINGS.format(), canRecover );
   1.111 +        else
   1.112 +            reportError(Messages.UNEXPECTED_ELEMENT.format(ea.uri,ea.local,computeExpectedElements()), canRecover );
   1.113 +    }
   1.114 +
   1.115 +    /**
   1.116 +     * Returns a set of tag names expected as possible child elements in this context.
   1.117 +     */
   1.118 +    public Collection<QName> getExpectedChildElements() {
   1.119 +        return Collections.emptyList();
   1.120 +    }
   1.121 +
   1.122 +    /**
   1.123 +     * Returns a set of tag names expected as possible child elements in this context.
   1.124 +     */
   1.125 +    public Collection<QName> getExpectedAttributes() {
   1.126 +        return Collections.emptyList();
   1.127 +    }
   1.128 +
   1.129 +    /**
   1.130 +     * Called when this loaderis an active loaderand we see a chunk of text.
   1.131 +     *
   1.132 +     * The runtime makes sure that adjacent characters (even those separated
   1.133 +     * by comments, PIs, etc) are reported as one event.
   1.134 +     * IOW, you won't see two text event calls in a row.
   1.135 +     */
   1.136 +    public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException {
   1.137 +        // make str printable
   1.138 +        text = text.toString().replace('\r',' ').replace('\n',' ').replace('\t',' ').trim();
   1.139 +        reportError(Messages.UNEXPECTED_TEXT.format(text), true );
   1.140 +    }
   1.141 +
   1.142 +    /**
   1.143 +     * True if this loader expects the {@link #text(UnmarshallingContext.State, CharSequence)} method
   1.144 +     * to be called. False otherwise.
   1.145 +     */
   1.146 +    public final boolean expectText() {
   1.147 +        return expectText;
   1.148 +    }
   1.149 +
   1.150 +
   1.151 +    /**
   1.152 +     * Called when this loaderis an active loaderand we see an end tag.
   1.153 +     */
   1.154 +    public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
   1.155 +    }
   1.156 +
   1.157 +
   1.158 +
   1.159 +
   1.160 +
   1.161 +
   1.162 +
   1.163 +
   1.164 +
   1.165 +
   1.166 +//
   1.167 +//
   1.168 +//
   1.169 +// utility methods
   1.170 +//
   1.171 +//
   1.172 +//
   1.173 +    /**
   1.174 +     * Computes the names of possible root elements for a better error diagnosis.
   1.175 +     */
   1.176 +    private String computeExpectedElements() {
   1.177 +        StringBuilder r = new StringBuilder();
   1.178 +
   1.179 +        for( QName n : getExpectedChildElements() ) {
   1.180 +            if(r.length()!=0)   r.append(',');
   1.181 +            r.append("<{").append(n.getNamespaceURI()).append('}').append(n.getLocalPart()).append('>');
   1.182 +        }
   1.183 +        if(r.length()==0) {
   1.184 +            return "(none)";
   1.185 +        }
   1.186 +
   1.187 +        return r.toString();
   1.188 +    }
   1.189 +
   1.190 +    /**
   1.191 +     * Fires the beforeUnmarshal event if necessary.
   1.192 +     *
   1.193 +     * @param state
   1.194 +     *      state of the newly create child object.
   1.195 +     */
   1.196 +    protected final void fireBeforeUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException {
   1.197 +        if(beanInfo.lookForLifecycleMethods()) {
   1.198 +            UnmarshallingContext context = state.getContext();
   1.199 +            Unmarshaller.Listener listener = context.parent.getListener();
   1.200 +            if(beanInfo.hasBeforeUnmarshalMethod()) {
   1.201 +                beanInfo.invokeBeforeUnmarshalMethod(context.parent, child, state.prev.target);
   1.202 +            }
   1.203 +            if(listener!=null) {
   1.204 +                listener.beforeUnmarshal(child, state.prev.target);
   1.205 +            }
   1.206 +        }
   1.207 +    }
   1.208 +
   1.209 +    /**
   1.210 +     * Fires the afterUnmarshal event if necessary.
   1.211 +     *
   1.212 +     * @param state
   1.213 +     *      state of the parent object
   1.214 +     */
   1.215 +    protected final void fireAfterUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException {
   1.216 +        // fire the event callback
   1.217 +        if(beanInfo.lookForLifecycleMethods()) {
   1.218 +            UnmarshallingContext context = state.getContext();
   1.219 +            Unmarshaller.Listener listener = context.parent.getListener();
   1.220 +            if(beanInfo.hasAfterUnmarshalMethod()) {
   1.221 +                beanInfo.invokeAfterUnmarshalMethod(context.parent, child, state.target);
   1.222 +            }
   1.223 +            if(listener!=null)
   1.224 +                listener.afterUnmarshal(child, state.target);
   1.225 +        }
   1.226 +    }
   1.227 +
   1.228 +
   1.229 +    /**
   1.230 +     * Last resort when something goes terribly wrong within the unmarshaller.
   1.231 +     */
   1.232 +    protected static void handleGenericException(Exception e) throws SAXException {
   1.233 +        handleGenericException(e,false);
   1.234 +    }
   1.235 +
   1.236 +    public static void handleGenericException(Exception e, boolean canRecover) throws SAXException {
   1.237 +        reportError(e.getMessage(), e, canRecover );
   1.238 +    }
   1.239 +
   1.240 +    public static void handleGenericError(Error e) throws SAXException {
   1.241 +        reportError(e.getMessage(), false);
   1.242 +    }
   1.243 +
   1.244 +    protected static void reportError(String msg, boolean canRecover) throws SAXException {
   1.245 +        reportError(msg, null, canRecover );
   1.246 +    }
   1.247 +
   1.248 +    public static void reportError(String msg, Exception nested, boolean canRecover) throws SAXException {
   1.249 +        UnmarshallingContext context = UnmarshallingContext.getInstance();
   1.250 +        context.handleEvent( new ValidationEventImpl(
   1.251 +            canRecover? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR,
   1.252 +            msg,
   1.253 +            context.getLocator().getLocation(),
   1.254 +            nested ), canRecover );
   1.255 +    }
   1.256 +
   1.257 +    /**
   1.258 +     * This method is called by the generated derived class
   1.259 +     * when a datatype parse method throws an exception.
   1.260 +     */
   1.261 +    protected static void handleParseConversionException(UnmarshallingContext.State state, Exception e) throws SAXException {
   1.262 +        // wrap it into a ParseConversionEvent and report it
   1.263 +        state.getContext().handleError(e);
   1.264 +    }
   1.265 +}

mercurial