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

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 650
121e938cb9c3
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
aoqi@0 27
aoqi@0 28 import java.util.Collection;
aoqi@0 29 import java.util.HashMap;
aoqi@0 30 import java.util.Map;
aoqi@0 31
aoqi@0 32 import javax.xml.namespace.QName;
aoqi@0 33
aoqi@0 34 import com.sun.xml.internal.bind.api.AccessorException;
aoqi@0 35 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
aoqi@0 36 import com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl;
aoqi@0 37 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
aoqi@0 38 import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo;
aoqi@0 39 import com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty;
aoqi@0 40 import com.sun.xml.internal.bind.v2.runtime.property.Property;
aoqi@0 41 import com.sun.xml.internal.bind.v2.runtime.property.StructureLoaderBuilder;
aoqi@0 42 import com.sun.xml.internal.bind.v2.runtime.property.UnmarshallerChain;
aoqi@0 43 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
aoqi@0 44 import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor;
aoqi@0 45 import com.sun.xml.internal.bind.v2.util.QNameMap;
aoqi@0 46
aoqi@0 47 import java.util.Iterator;
aoqi@0 48 import org.xml.sax.Attributes;
aoqi@0 49 import org.xml.sax.SAXException;
aoqi@0 50
aoqi@0 51 /**
aoqi@0 52 * Loads children of an element.
aoqi@0 53 *
aoqi@0 54 * <p>
aoqi@0 55 * This loader works with a single {@link JaxBeanInfo} and handles
aoqi@0 56 * attributes, child elements, or child text.
aoqi@0 57 *
aoqi@0 58 * @author Kohsuke Kawaguchi
aoqi@0 59 */
aoqi@0 60 public final class StructureLoader extends Loader {
aoqi@0 61 /**
aoqi@0 62 * This map statically stores information of the
aoqi@0 63 * unmarshaller loader and can be used while unmarshalling
aoqi@0 64 * Since creating new QNames is expensive use this optimized
aoqi@0 65 * version of the map
aoqi@0 66 */
aoqi@0 67 private final QNameMap<ChildLoader> childUnmarshallers = new QNameMap<ChildLoader>();
aoqi@0 68
aoqi@0 69 /**
aoqi@0 70 * Loader that processes elements that didn't match anf of the {@link #childUnmarshallers}.
aoqi@0 71 * Can be null.
aoqi@0 72 */
aoqi@0 73 private /*final*/ ChildLoader catchAll;
aoqi@0 74
aoqi@0 75 /**
aoqi@0 76 * If we have a loader for processing text. Otherwise null.
aoqi@0 77 */
aoqi@0 78 private /*final*/ ChildLoader textHandler;
aoqi@0 79
aoqi@0 80 /**
aoqi@0 81 * Unmarshallers for attribute values.
aoqi@0 82 * May be null if no attribute is expected and {@link #attCatchAll}==null.
aoqi@0 83 */
aoqi@0 84 private /*final*/ QNameMap<TransducedAccessor> attUnmarshallers;
aoqi@0 85
aoqi@0 86 /**
aoqi@0 87 * This will receive all the attributes
aoqi@0 88 * that were not processed. Never be null.
aoqi@0 89 */
aoqi@0 90 private /*final*/ Accessor<Object,Map<QName,String>> attCatchAll;
aoqi@0 91
aoqi@0 92 private final JaxBeanInfo beanInfo;
aoqi@0 93
aoqi@0 94 /**
aoqi@0 95 * The number of scopes this dispatcher needs to keep active.
aoqi@0 96 */
aoqi@0 97 private /*final*/ int frameSize;
aoqi@0 98
aoqi@0 99 // this class is potentially useful for general audience, not just for ClassBeanInfoImpl,
aoqi@0 100 // but since right now that is the only user, we make the construction code very specific
aoqi@0 101 // to ClassBeanInfoImpl. See rev.1.5 of this file for the original general purpose definition.
aoqi@0 102 public StructureLoader(ClassBeanInfoImpl beanInfo) {
aoqi@0 103 super(true);
aoqi@0 104 this.beanInfo = beanInfo;
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 /**
aoqi@0 108 * Completes the initialization.
aoqi@0 109 *
aoqi@0 110 * <p>
aoqi@0 111 * To fix the cyclic reference issue, the main part of the initialization needs to be done
aoqi@0 112 * after a {@link StructureLoader} is set to {@link ClassBeanInfoImpl#loader}.
aoqi@0 113 */
aoqi@0 114 public void init( JAXBContextImpl context, ClassBeanInfoImpl beanInfo, Accessor<?,Map<QName,String>> attWildcard) {
aoqi@0 115 UnmarshallerChain chain = new UnmarshallerChain(context);
aoqi@0 116 for (ClassBeanInfoImpl bi = beanInfo; bi != null; bi = bi.superClazz) {
aoqi@0 117 for (int i = bi.properties.length - 1; i >= 0; i--) {
aoqi@0 118 Property p = bi.properties[i];
aoqi@0 119
aoqi@0 120 switch(p.getKind()) {
aoqi@0 121 case ATTRIBUTE:
aoqi@0 122 if(attUnmarshallers==null)
aoqi@0 123 attUnmarshallers = new QNameMap<TransducedAccessor>();
aoqi@0 124 AttributeProperty ap = (AttributeProperty) p;
aoqi@0 125 attUnmarshallers.put(ap.attName.toQName(),ap.xacc);
aoqi@0 126 break;
aoqi@0 127 case ELEMENT:
aoqi@0 128 case REFERENCE:
aoqi@0 129 case MAP:
aoqi@0 130 case VALUE:
aoqi@0 131 p.buildChildElementUnmarshallers(chain,childUnmarshallers);
aoqi@0 132 break;
aoqi@0 133 }
aoqi@0 134 }
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 this.frameSize = chain.getScopeSize();
aoqi@0 138
aoqi@0 139 textHandler = childUnmarshallers.get(StructureLoaderBuilder.TEXT_HANDLER);
aoqi@0 140 catchAll = childUnmarshallers.get(StructureLoaderBuilder.CATCH_ALL);
aoqi@0 141
aoqi@0 142 if(attWildcard!=null) {
aoqi@0 143 attCatchAll = (Accessor<Object,Map<QName,String>>) attWildcard;
aoqi@0 144 // we use attUnmarshallers==null as a sign to skip the attribute processing
aoqi@0 145 // altogether, so if we have an att wildcard we need to have an empty qname map.
aoqi@0 146 if(attUnmarshallers==null)
aoqi@0 147 attUnmarshallers = EMPTY;
aoqi@0 148 } else {
aoqi@0 149 attCatchAll = null;
aoqi@0 150 }
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 @Override
aoqi@0 154 public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
aoqi@0 155 UnmarshallingContext context = state.getContext();
aoqi@0 156
aoqi@0 157 // create the object to unmarshal
aoqi@0 158 Object child;
aoqi@0 159 assert !beanInfo.isImmutable();
aoqi@0 160
aoqi@0 161 // let's see if we can reuse the existing peer object
aoqi@0 162 child = context.getInnerPeer();
aoqi@0 163
aoqi@0 164 if(child != null && beanInfo.jaxbType!=child.getClass())
aoqi@0 165 child = null; // unexpected type.
aoqi@0 166
aoqi@0 167 if(child != null)
aoqi@0 168 beanInfo.reset(child,context);
aoqi@0 169
aoqi@0 170 if(child == null)
aoqi@0 171 child = context.createInstance(beanInfo);
aoqi@0 172
aoqi@0 173 context.recordInnerPeer(child);
aoqi@0 174
aoqi@0 175 state.target = child;
aoqi@0 176
aoqi@0 177 fireBeforeUnmarshal(beanInfo, child, state);
aoqi@0 178
aoqi@0 179
aoqi@0 180 context.startScope(frameSize);
aoqi@0 181
aoqi@0 182 if(attUnmarshallers!=null) {
aoqi@0 183 Attributes atts = ea.atts;
aoqi@0 184 for (int i = 0; i < atts.getLength(); i ++){
aoqi@0 185 String auri = atts.getURI(i);
aoqi@0 186 // may be empty string based on parser settings
aoqi@0 187 String alocal = atts.getLocalName(i);
aoqi@0 188 if ("".equals(alocal)) {
aoqi@0 189 alocal = atts.getQName(i);
aoqi@0 190 }
aoqi@0 191 String avalue = atts.getValue(i);
aoqi@0 192 TransducedAccessor xacc = attUnmarshallers.get(auri, alocal);
aoqi@0 193 try {
aoqi@0 194 if(xacc!=null) {
aoqi@0 195 xacc.parse(child,avalue);
aoqi@0 196 } else if (attCatchAll!=null) {
aoqi@0 197 String qname = atts.getQName(i);
aoqi@0 198 if(atts.getURI(i).equals(WellKnownNamespace.XML_SCHEMA_INSTANCE))
aoqi@0 199 continue; // xsi:* attributes are meant to be processed by us, not by user apps.
aoqi@0 200 Object o = state.target;
aoqi@0 201 Map<QName,String> map = attCatchAll.get(o);
aoqi@0 202 if(map==null) {
aoqi@0 203 // TODO: use ClassFactory.inferImplClass(sig,knownImplClasses)
aoqi@0 204
aoqi@0 205 // if null, create a new map.
aoqi@0 206 if(attCatchAll.valueType.isAssignableFrom(HashMap.class))
aoqi@0 207 map = new HashMap<QName,String>();
aoqi@0 208 else {
aoqi@0 209 // we don't know how to create a map for this.
aoqi@0 210 // report an error and back out
aoqi@0 211 context.handleError(Messages.UNABLE_TO_CREATE_MAP.format(attCatchAll.valueType));
aoqi@0 212 return;
aoqi@0 213 }
aoqi@0 214 attCatchAll.set(o,map);
aoqi@0 215 }
aoqi@0 216
aoqi@0 217 String prefix;
aoqi@0 218 int idx = qname.indexOf(':');
aoqi@0 219 if(idx<0) prefix="";
aoqi@0 220 else prefix=qname.substring(0,idx);
aoqi@0 221
aoqi@0 222 map.put(new QName(auri,alocal,prefix),avalue);
aoqi@0 223 }
aoqi@0 224 } catch (AccessorException e) {
aoqi@0 225 handleGenericException(e,true);
aoqi@0 226 }
aoqi@0 227 }
aoqi@0 228 }
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 @Override
aoqi@0 232 public void childElement(UnmarshallingContext.State state, TagName arg) throws SAXException {
aoqi@0 233 ChildLoader child = childUnmarshallers.get(arg.uri,arg.local);
aoqi@0 234 if(child==null) {
aoqi@0 235 if ((beanInfo != null) && (beanInfo.getTypeNames() != null)) {
aoqi@0 236 Iterator typeNamesIt = beanInfo.getTypeNames().iterator();
aoqi@0 237 QName parentQName = null;
aoqi@0 238 if ((typeNamesIt != null) && (typeNamesIt.hasNext()) && (catchAll == null)) {
aoqi@0 239 parentQName = (QName) typeNamesIt.next();
aoqi@0 240 String parentUri = parentQName.getNamespaceURI();
aoqi@0 241 child = childUnmarshallers.get(parentUri, arg.local);
aoqi@0 242 }
aoqi@0 243 }
aoqi@0 244 if (child == null) {
aoqi@0 245 child = catchAll;
aoqi@0 246 if(child==null) {
aoqi@0 247 super.childElement(state,arg);
aoqi@0 248 return;
aoqi@0 249 }
aoqi@0 250 }
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 state.loader = child.loader;
aoqi@0 254 state.receiver = child.receiver;
aoqi@0 255 }
aoqi@0 256
aoqi@0 257 @Override
aoqi@0 258 public Collection<QName> getExpectedChildElements() {
aoqi@0 259 return childUnmarshallers.keySet();
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 @Override
aoqi@0 263 public Collection<QName> getExpectedAttributes() {
aoqi@0 264 return attUnmarshallers.keySet();
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 @Override
aoqi@0 268 public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException {
aoqi@0 269 if(textHandler!=null)
aoqi@0 270 textHandler.loader.text(state,text);
aoqi@0 271 }
aoqi@0 272
aoqi@0 273 @Override
aoqi@0 274 public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
aoqi@0 275 state.getContext().endScope(frameSize);
aoqi@0 276 fireAfterUnmarshal(beanInfo, state.target, state.prev);
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 private static final QNameMap<TransducedAccessor> EMPTY = new QNameMap<TransducedAccessor>();
aoqi@0 280
aoqi@0 281 public JaxBeanInfo getBeanInfo() {
aoqi@0 282 return beanInfo;
aoqi@0 283 }
aoqi@0 284 }

mercurial