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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  71      * @param ea
  72      *      info about the start tag. never null.
  73      */
  74     public void startElement(UnmarshallingContext.State state,TagName ea) throws SAXException {
  75     }
  76 
  77     /**
  78      * Called when this loaderis an active loaderand we see a new child start tag.
  79      *
  80      * <p>
  81      * The callee is expected to designate another loaderas a loaderthat processes
  82      * this element, then it should also register a {@link Receiver}.
  83      * The designated loaderwill become an active loader.
  84      *
  85      * <p>
  86      * The default implementation reports an error saying an element is unexpected.
  87      */
  88     public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
  89         // notify the error, then recover by ignoring the whole element.
  90         reportUnexpectedChildElement(ea, true);
  91         state.loader = Discarder.INSTANCE;
  92         state.receiver = null;
  93     }
  94 
  95     @SuppressWarnings({"StringEquality"})
  96     protected final void reportUnexpectedChildElement(TagName ea, boolean canRecover) throws SAXException {
  97         if (canRecover) {
  98             // this error happens particurly often (when input documents contain a lot of unexpected elements to be ignored),
  99             // so don't bother computing all the messages and etc if we know that
 100             // there's no event handler to receive the error in the end. See #286
 101             UnmarshallingContext context = UnmarshallingContext.getInstance();
 102             if (!context.parent.hasEventHandler() // is somebody listening?
 103                     || !context.shouldErrorBeReported()) // should we report error?
 104                 return;
 105         }
 106         if(ea.uri!=ea.uri.intern() || ea.local!=ea.local.intern())
 107             reportError(Messages.UNINTERNED_STRINGS.format(), canRecover );
 108         else
 109             reportError(Messages.UNEXPECTED_ELEMENT.format(ea.uri,ea.local,computeExpectedElements()), canRecover );
 110     }
 111 
 112     /**


 178             r.append("<{").append(n.getNamespaceURI()).append('}').append(n.getLocalPart()).append('>');
 179         }
 180         if(r.length()==0) {
 181             return "(none)";
 182         }
 183 
 184         return r.toString();
 185     }
 186 
 187     /**
 188      * Fires the beforeUnmarshal event if necessary.
 189      *
 190      * @param state
 191      *      state of the newly create child object.
 192      */
 193     protected final void fireBeforeUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException {
 194         if(beanInfo.lookForLifecycleMethods()) {
 195             UnmarshallingContext context = state.getContext();
 196             Unmarshaller.Listener listener = context.parent.getListener();
 197             if(beanInfo.hasBeforeUnmarshalMethod()) {
 198                 beanInfo.invokeBeforeUnmarshalMethod(context.parent, child, state.prev.target);
 199             }
 200             if(listener!=null) {
 201                 listener.beforeUnmarshal(child, state.prev.target);
 202             }
 203         }
 204     }
 205 
 206     /**
 207      * Fires the afterUnmarshal event if necessary.
 208      *
 209      * @param state
 210      *      state of the parent object
 211      */
 212     protected final void fireAfterUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException {
 213         // fire the event callback
 214         if(beanInfo.lookForLifecycleMethods()) {
 215             UnmarshallingContext context = state.getContext();
 216             Unmarshaller.Listener listener = context.parent.getListener();
 217             if(beanInfo.hasAfterUnmarshalMethod()) {
 218                 beanInfo.invokeAfterUnmarshalMethod(context.parent, child, state.target);
 219             }
 220             if(listener!=null)
 221                 listener.afterUnmarshal(child, state.target);
 222         }
 223     }
 224 
 225 
 226     /**
 227      * Last resort when something goes terribly wrong within the unmarshaller.
 228      */
 229     protected static void handleGenericException(Exception e) throws SAXException {
 230         handleGenericException(e,false);
 231     }
 232 
 233     public static void handleGenericException(Exception e, boolean canRecover) throws SAXException {
 234         reportError(e.getMessage(), e, canRecover );
 235     }
 236 
 237     public static void handleGenericError(Error e) throws SAXException {
 238         reportError(e.getMessage(), false);
 239     }
 240 
 241     protected static void reportError(String msg, boolean canRecover) throws SAXException {


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  71      * @param ea
  72      *      info about the start tag. never null.
  73      */
  74     public void startElement(UnmarshallingContext.State state,TagName ea) throws SAXException {
  75     }
  76 
  77     /**
  78      * Called when this loaderis an active loaderand we see a new child start tag.
  79      *
  80      * <p>
  81      * The callee is expected to designate another loaderas a loaderthat processes
  82      * this element, then it should also register a {@link Receiver}.
  83      * The designated loaderwill become an active loader.
  84      *
  85      * <p>
  86      * The default implementation reports an error saying an element is unexpected.
  87      */
  88     public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
  89         // notify the error, then recover by ignoring the whole element.
  90         reportUnexpectedChildElement(ea, true);
  91         state.setLoader(Discarder.INSTANCE);
  92         state.setReceiver(null);
  93     }
  94 
  95     @SuppressWarnings({"StringEquality"})
  96     protected final void reportUnexpectedChildElement(TagName ea, boolean canRecover) throws SAXException {
  97         if (canRecover) {
  98             // this error happens particurly often (when input documents contain a lot of unexpected elements to be ignored),
  99             // so don't bother computing all the messages and etc if we know that
 100             // there's no event handler to receive the error in the end. See #286
 101             UnmarshallingContext context = UnmarshallingContext.getInstance();
 102             if (!context.parent.hasEventHandler() // is somebody listening?
 103                     || !context.shouldErrorBeReported()) // should we report error?
 104                 return;
 105         }
 106         if(ea.uri!=ea.uri.intern() || ea.local!=ea.local.intern())
 107             reportError(Messages.UNINTERNED_STRINGS.format(), canRecover );
 108         else
 109             reportError(Messages.UNEXPECTED_ELEMENT.format(ea.uri,ea.local,computeExpectedElements()), canRecover );
 110     }
 111 
 112     /**


 178             r.append("<{").append(n.getNamespaceURI()).append('}').append(n.getLocalPart()).append('>');
 179         }
 180         if(r.length()==0) {
 181             return "(none)";
 182         }
 183 
 184         return r.toString();
 185     }
 186 
 187     /**
 188      * Fires the beforeUnmarshal event if necessary.
 189      *
 190      * @param state
 191      *      state of the newly create child object.
 192      */
 193     protected final void fireBeforeUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException {
 194         if(beanInfo.lookForLifecycleMethods()) {
 195             UnmarshallingContext context = state.getContext();
 196             Unmarshaller.Listener listener = context.parent.getListener();
 197             if(beanInfo.hasBeforeUnmarshalMethod()) {
 198                 beanInfo.invokeBeforeUnmarshalMethod(context.parent, child, state.getPrev().getTarget());
 199             }
 200             if(listener!=null) {
 201                 listener.beforeUnmarshal(child, state.getPrev().getTarget());
 202             }
 203         }
 204     }
 205 
 206     /**
 207      * Fires the afterUnmarshal event if necessary.
 208      *
 209      * @param state
 210      *      state of the parent object
 211      */
 212     protected final void fireAfterUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException {
 213         // fire the event callback
 214         if(beanInfo.lookForLifecycleMethods()) {
 215             UnmarshallingContext context = state.getContext();
 216             Unmarshaller.Listener listener = context.parent.getListener();
 217             if(beanInfo.hasAfterUnmarshalMethod()) {
 218                 beanInfo.invokeAfterUnmarshalMethod(context.parent, child, state.getTarget());
 219             }
 220             if(listener!=null)
 221                 listener.afterUnmarshal(child, state.getTarget());
 222         }
 223     }
 224 
 225 
 226     /**
 227      * Last resort when something goes terribly wrong within the unmarshaller.
 228      */
 229     protected static void handleGenericException(Exception e) throws SAXException {
 230         handleGenericException(e,false);
 231     }
 232 
 233     public static void handleGenericException(Exception e, boolean canRecover) throws SAXException {
 234         reportError(e.getMessage(), e, canRecover );
 235     }
 236 
 237     public static void handleGenericError(Error e) throws SAXException {
 238         reportError(e.getMessage(), false);
 239     }
 240 
 241     protected static void reportError(String msg, boolean canRecover) throws SAXException {