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/StructureLoader.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,284 @@ 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.HashMap; 1.33 +import java.util.Map; 1.34 + 1.35 +import javax.xml.namespace.QName; 1.36 + 1.37 +import com.sun.xml.internal.bind.api.AccessorException; 1.38 +import com.sun.xml.internal.bind.v2.WellKnownNamespace; 1.39 +import com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl; 1.40 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; 1.41 +import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; 1.42 +import com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty; 1.43 +import com.sun.xml.internal.bind.v2.runtime.property.Property; 1.44 +import com.sun.xml.internal.bind.v2.runtime.property.StructureLoaderBuilder; 1.45 +import com.sun.xml.internal.bind.v2.runtime.property.UnmarshallerChain; 1.46 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; 1.47 +import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor; 1.48 +import com.sun.xml.internal.bind.v2.util.QNameMap; 1.49 + 1.50 +import java.util.Iterator; 1.51 +import org.xml.sax.Attributes; 1.52 +import org.xml.sax.SAXException; 1.53 + 1.54 +/** 1.55 + * Loads children of an element. 1.56 + * 1.57 + * <p> 1.58 + * This loader works with a single {@link JaxBeanInfo} and handles 1.59 + * attributes, child elements, or child text. 1.60 + * 1.61 + * @author Kohsuke Kawaguchi 1.62 + */ 1.63 +public final class StructureLoader extends Loader { 1.64 + /** 1.65 + * This map statically stores information of the 1.66 + * unmarshaller loader and can be used while unmarshalling 1.67 + * Since creating new QNames is expensive use this optimized 1.68 + * version of the map 1.69 + */ 1.70 + private final QNameMap<ChildLoader> childUnmarshallers = new QNameMap<ChildLoader>(); 1.71 + 1.72 + /** 1.73 + * Loader that processes elements that didn't match anf of the {@link #childUnmarshallers}. 1.74 + * Can be null. 1.75 + */ 1.76 + private /*final*/ ChildLoader catchAll; 1.77 + 1.78 + /** 1.79 + * If we have a loader for processing text. Otherwise null. 1.80 + */ 1.81 + private /*final*/ ChildLoader textHandler; 1.82 + 1.83 + /** 1.84 + * Unmarshallers for attribute values. 1.85 + * May be null if no attribute is expected and {@link #attCatchAll}==null. 1.86 + */ 1.87 + private /*final*/ QNameMap<TransducedAccessor> attUnmarshallers; 1.88 + 1.89 + /** 1.90 + * This will receive all the attributes 1.91 + * that were not processed. Never be null. 1.92 + */ 1.93 + private /*final*/ Accessor<Object,Map<QName,String>> attCatchAll; 1.94 + 1.95 + private final JaxBeanInfo beanInfo; 1.96 + 1.97 + /** 1.98 + * The number of scopes this dispatcher needs to keep active. 1.99 + */ 1.100 + private /*final*/ int frameSize; 1.101 + 1.102 + // this class is potentially useful for general audience, not just for ClassBeanInfoImpl, 1.103 + // but since right now that is the only user, we make the construction code very specific 1.104 + // to ClassBeanInfoImpl. See rev.1.5 of this file for the original general purpose definition. 1.105 + public StructureLoader(ClassBeanInfoImpl beanInfo) { 1.106 + super(true); 1.107 + this.beanInfo = beanInfo; 1.108 + } 1.109 + 1.110 + /** 1.111 + * Completes the initialization. 1.112 + * 1.113 + * <p> 1.114 + * To fix the cyclic reference issue, the main part of the initialization needs to be done 1.115 + * after a {@link StructureLoader} is set to {@link ClassBeanInfoImpl#loader}. 1.116 + */ 1.117 + public void init( JAXBContextImpl context, ClassBeanInfoImpl beanInfo, Accessor<?,Map<QName,String>> attWildcard) { 1.118 + UnmarshallerChain chain = new UnmarshallerChain(context); 1.119 + for (ClassBeanInfoImpl bi = beanInfo; bi != null; bi = bi.superClazz) { 1.120 + for (int i = bi.properties.length - 1; i >= 0; i--) { 1.121 + Property p = bi.properties[i]; 1.122 + 1.123 + switch(p.getKind()) { 1.124 + case ATTRIBUTE: 1.125 + if(attUnmarshallers==null) 1.126 + attUnmarshallers = new QNameMap<TransducedAccessor>(); 1.127 + AttributeProperty ap = (AttributeProperty) p; 1.128 + attUnmarshallers.put(ap.attName.toQName(),ap.xacc); 1.129 + break; 1.130 + case ELEMENT: 1.131 + case REFERENCE: 1.132 + case MAP: 1.133 + case VALUE: 1.134 + p.buildChildElementUnmarshallers(chain,childUnmarshallers); 1.135 + break; 1.136 + } 1.137 + } 1.138 + } 1.139 + 1.140 + this.frameSize = chain.getScopeSize(); 1.141 + 1.142 + textHandler = childUnmarshallers.get(StructureLoaderBuilder.TEXT_HANDLER); 1.143 + catchAll = childUnmarshallers.get(StructureLoaderBuilder.CATCH_ALL); 1.144 + 1.145 + if(attWildcard!=null) { 1.146 + attCatchAll = (Accessor<Object,Map<QName,String>>) attWildcard; 1.147 + // we use attUnmarshallers==null as a sign to skip the attribute processing 1.148 + // altogether, so if we have an att wildcard we need to have an empty qname map. 1.149 + if(attUnmarshallers==null) 1.150 + attUnmarshallers = EMPTY; 1.151 + } else { 1.152 + attCatchAll = null; 1.153 + } 1.154 + } 1.155 + 1.156 + @Override 1.157 + public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.158 + UnmarshallingContext context = state.getContext(); 1.159 + 1.160 + // create the object to unmarshal 1.161 + Object child; 1.162 + assert !beanInfo.isImmutable(); 1.163 + 1.164 + // let's see if we can reuse the existing peer object 1.165 + child = context.getInnerPeer(); 1.166 + 1.167 + if(child != null && beanInfo.jaxbType!=child.getClass()) 1.168 + child = null; // unexpected type. 1.169 + 1.170 + if(child != null) 1.171 + beanInfo.reset(child,context); 1.172 + 1.173 + if(child == null) 1.174 + child = context.createInstance(beanInfo); 1.175 + 1.176 + context.recordInnerPeer(child); 1.177 + 1.178 + state.target = child; 1.179 + 1.180 + fireBeforeUnmarshal(beanInfo, child, state); 1.181 + 1.182 + 1.183 + context.startScope(frameSize); 1.184 + 1.185 + if(attUnmarshallers!=null) { 1.186 + Attributes atts = ea.atts; 1.187 + for (int i = 0; i < atts.getLength(); i ++){ 1.188 + String auri = atts.getURI(i); 1.189 + // may be empty string based on parser settings 1.190 + String alocal = atts.getLocalName(i); 1.191 + if ("".equals(alocal)) { 1.192 + alocal = atts.getQName(i); 1.193 + } 1.194 + String avalue = atts.getValue(i); 1.195 + TransducedAccessor xacc = attUnmarshallers.get(auri, alocal); 1.196 + try { 1.197 + if(xacc!=null) { 1.198 + xacc.parse(child,avalue); 1.199 + } else if (attCatchAll!=null) { 1.200 + String qname = atts.getQName(i); 1.201 + if(atts.getURI(i).equals(WellKnownNamespace.XML_SCHEMA_INSTANCE)) 1.202 + continue; // xsi:* attributes are meant to be processed by us, not by user apps. 1.203 + Object o = state.target; 1.204 + Map<QName,String> map = attCatchAll.get(o); 1.205 + if(map==null) { 1.206 + // TODO: use ClassFactory.inferImplClass(sig,knownImplClasses) 1.207 + 1.208 + // if null, create a new map. 1.209 + if(attCatchAll.valueType.isAssignableFrom(HashMap.class)) 1.210 + map = new HashMap<QName,String>(); 1.211 + else { 1.212 + // we don't know how to create a map for this. 1.213 + // report an error and back out 1.214 + context.handleError(Messages.UNABLE_TO_CREATE_MAP.format(attCatchAll.valueType)); 1.215 + return; 1.216 + } 1.217 + attCatchAll.set(o,map); 1.218 + } 1.219 + 1.220 + String prefix; 1.221 + int idx = qname.indexOf(':'); 1.222 + if(idx<0) prefix=""; 1.223 + else prefix=qname.substring(0,idx); 1.224 + 1.225 + map.put(new QName(auri,alocal,prefix),avalue); 1.226 + } 1.227 + } catch (AccessorException e) { 1.228 + handleGenericException(e,true); 1.229 + } 1.230 + } 1.231 + } 1.232 + } 1.233 + 1.234 + @Override 1.235 + public void childElement(UnmarshallingContext.State state, TagName arg) throws SAXException { 1.236 + ChildLoader child = childUnmarshallers.get(arg.uri,arg.local); 1.237 + if(child==null) { 1.238 + if ((beanInfo != null) && (beanInfo.getTypeNames() != null)) { 1.239 + Iterator typeNamesIt = beanInfo.getTypeNames().iterator(); 1.240 + QName parentQName = null; 1.241 + if ((typeNamesIt != null) && (typeNamesIt.hasNext()) && (catchAll == null)) { 1.242 + parentQName = (QName) typeNamesIt.next(); 1.243 + String parentUri = parentQName.getNamespaceURI(); 1.244 + child = childUnmarshallers.get(parentUri, arg.local); 1.245 + } 1.246 + } 1.247 + if (child == null) { 1.248 + child = catchAll; 1.249 + if(child==null) { 1.250 + super.childElement(state,arg); 1.251 + return; 1.252 + } 1.253 + } 1.254 + } 1.255 + 1.256 + state.loader = child.loader; 1.257 + state.receiver = child.receiver; 1.258 + } 1.259 + 1.260 + @Override 1.261 + public Collection<QName> getExpectedChildElements() { 1.262 + return childUnmarshallers.keySet(); 1.263 + } 1.264 + 1.265 + @Override 1.266 + public Collection<QName> getExpectedAttributes() { 1.267 + return attUnmarshallers.keySet(); 1.268 + } 1.269 + 1.270 + @Override 1.271 + public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException { 1.272 + if(textHandler!=null) 1.273 + textHandler.loader.text(state,text); 1.274 + } 1.275 + 1.276 + @Override 1.277 + public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.278 + state.getContext().endScope(frameSize); 1.279 + fireAfterUnmarshal(beanInfo, state.target, state.prev); 1.280 + } 1.281 + 1.282 + private static final QNameMap<TransducedAccessor> EMPTY = new QNameMap<TransducedAccessor>(); 1.283 + 1.284 + public JaxBeanInfo getBeanInfo() { 1.285 + return beanInfo; 1.286 + } 1.287 +}