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 Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,258 @@ 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 && !UnmarshallingContext.getInstance().parent.hasEventHandler()) 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 + return; 1.105 + if(ea.uri!=ea.uri.intern() || ea.local!=ea.local.intern()) 1.106 + reportError(Messages.UNINTERNED_STRINGS.format(), canRecover ); 1.107 + else 1.108 + reportError(Messages.UNEXPECTED_ELEMENT.format(ea.uri,ea.local,computeExpectedElements()), canRecover ); 1.109 + } 1.110 + 1.111 + /** 1.112 + * Returns a set of tag names expected as possible child elements in this context. 1.113 + */ 1.114 + public Collection<QName> getExpectedChildElements() { 1.115 + return Collections.emptyList(); 1.116 + } 1.117 + 1.118 + /** 1.119 + * Returns a set of tag names expected as possible child elements in this context. 1.120 + */ 1.121 + public Collection<QName> getExpectedAttributes() { 1.122 + return Collections.emptyList(); 1.123 + } 1.124 + 1.125 + /** 1.126 + * Called when this loaderis an active loaderand we see a chunk of text. 1.127 + * 1.128 + * The runtime makes sure that adjacent characters (even those separated 1.129 + * by comments, PIs, etc) are reported as one event. 1.130 + * IOW, you won't see two text event calls in a row. 1.131 + */ 1.132 + public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException { 1.133 + // make str printable 1.134 + text = text.toString().replace('\r',' ').replace('\n',' ').replace('\t',' ').trim(); 1.135 + reportError(Messages.UNEXPECTED_TEXT.format(text), true ); 1.136 + } 1.137 + 1.138 + /** 1.139 + * True if this loader expects the {@link #text(UnmarshallingContext.State, CharSequence)} method 1.140 + * to be called. False otherwise. 1.141 + */ 1.142 + public final boolean expectText() { 1.143 + return expectText; 1.144 + } 1.145 + 1.146 + 1.147 + /** 1.148 + * Called when this loaderis an active loaderand we see an end tag. 1.149 + */ 1.150 + public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.151 + } 1.152 + 1.153 + 1.154 + 1.155 + 1.156 + 1.157 + 1.158 + 1.159 + 1.160 + 1.161 + 1.162 +// 1.163 +// 1.164 +// 1.165 +// utility methods 1.166 +// 1.167 +// 1.168 +// 1.169 + /** 1.170 + * Computes the names of possible root elements for a better error diagnosis. 1.171 + */ 1.172 + private String computeExpectedElements() { 1.173 + StringBuilder r = new StringBuilder(); 1.174 + 1.175 + for( QName n : getExpectedChildElements() ) { 1.176 + if(r.length()!=0) r.append(','); 1.177 + r.append("<{").append(n.getNamespaceURI()).append('}').append(n.getLocalPart()).append('>'); 1.178 + } 1.179 + if(r.length()==0) { 1.180 + return "(none)"; 1.181 + } 1.182 + 1.183 + return r.toString(); 1.184 + } 1.185 + 1.186 + /** 1.187 + * Fires the beforeUnmarshal event if necessary. 1.188 + * 1.189 + * @param state 1.190 + * state of the newly create child object. 1.191 + */ 1.192 + protected final void fireBeforeUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException { 1.193 + if(beanInfo.lookForLifecycleMethods()) { 1.194 + UnmarshallingContext context = state.getContext(); 1.195 + Unmarshaller.Listener listener = context.parent.getListener(); 1.196 + if(beanInfo.hasBeforeUnmarshalMethod()) { 1.197 + beanInfo.invokeBeforeUnmarshalMethod(context.parent, child, state.prev.target); 1.198 + } 1.199 + if(listener!=null) { 1.200 + listener.beforeUnmarshal(child, state.prev.target); 1.201 + } 1.202 + } 1.203 + } 1.204 + 1.205 + /** 1.206 + * Fires the afterUnmarshal event if necessary. 1.207 + * 1.208 + * @param state 1.209 + * state of the parent object 1.210 + */ 1.211 + protected final void fireAfterUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException { 1.212 + // fire the event callback 1.213 + if(beanInfo.lookForLifecycleMethods()) { 1.214 + UnmarshallingContext context = state.getContext(); 1.215 + Unmarshaller.Listener listener = context.parent.getListener(); 1.216 + if(beanInfo.hasAfterUnmarshalMethod()) { 1.217 + beanInfo.invokeAfterUnmarshalMethod(context.parent, child, state.target); 1.218 + } 1.219 + if(listener!=null) 1.220 + listener.afterUnmarshal(child, state.target); 1.221 + } 1.222 + } 1.223 + 1.224 + 1.225 + /** 1.226 + * Last resort when something goes terribly wrong within the unmarshaller. 1.227 + */ 1.228 + protected static void handleGenericException(Exception e) throws SAXException { 1.229 + handleGenericException(e,false); 1.230 + } 1.231 + 1.232 + public static void handleGenericException(Exception e, boolean canRecover) throws SAXException { 1.233 + reportError(e.getMessage(), e, canRecover ); 1.234 + } 1.235 + 1.236 + public static void handleGenericError(Error e) throws SAXException { 1.237 + reportError(e.getMessage(), false); 1.238 + } 1.239 + 1.240 + protected static void reportError(String msg, boolean canRecover) throws SAXException { 1.241 + reportError(msg, null, canRecover ); 1.242 + } 1.243 + 1.244 + public static void reportError(String msg, Exception nested, boolean canRecover) throws SAXException { 1.245 + UnmarshallingContext context = UnmarshallingContext.getInstance(); 1.246 + context.handleEvent( new ValidationEventImpl( 1.247 + canRecover? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR, 1.248 + msg, 1.249 + context.getLocator().getLocation(), 1.250 + nested ), canRecover ); 1.251 + } 1.252 + 1.253 + /** 1.254 + * This method is called by the generated derived class 1.255 + * when a datatype parse method throws an exception. 1.256 + */ 1.257 + protected static void handleParseConversionException(UnmarshallingContext.State state, Exception e) throws SAXException { 1.258 + // wrap it into a ParseConversionEvent and report it 1.259 + state.getContext().handleError(e); 1.260 + } 1.261 +}