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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
ohair@286 27
ohair@286 28 import java.lang.reflect.InvocationTargetException;
ohair@286 29 import java.lang.reflect.Method;
ohair@286 30 import java.util.ArrayList;
ohair@286 31 import java.util.Collection;
ohair@286 32 import java.util.Collections;
ohair@286 33 import java.util.HashMap;
ohair@286 34 import java.util.Iterator;
ohair@286 35 import java.util.List;
ohair@286 36 import java.util.Map;
ohair@286 37 import java.util.concurrent.Callable;
ohair@286 38
ohair@286 39 import javax.xml.XMLConstants;
ohair@286 40 import javax.xml.bind.JAXBElement;
ohair@286 41 import javax.xml.bind.UnmarshalException;
ohair@286 42 import javax.xml.bind.Unmarshaller;
ohair@286 43 import javax.xml.bind.ValidationEvent;
ohair@286 44 import javax.xml.bind.ValidationEventHandler;
ohair@286 45 import javax.xml.bind.ValidationEventLocator;
ohair@286 46 import javax.xml.bind.helpers.ValidationEventImpl;
ohair@286 47 import javax.xml.namespace.NamespaceContext;
ohair@286 48 import javax.xml.namespace.QName;
ohair@286 49
ohair@286 50 import com.sun.istack.internal.NotNull;
ohair@286 51 import com.sun.istack.internal.Nullable;
ohair@286 52 import com.sun.istack.internal.SAXParseException2;
ohair@286 53 import com.sun.xml.internal.bind.IDResolver;
ohair@286 54 import com.sun.xml.internal.bind.api.AccessorException;
ohair@286 55 import com.sun.xml.internal.bind.api.ClassResolver;
ohair@286 56 import com.sun.xml.internal.bind.unmarshaller.InfosetScanner;
ohair@286 57 import com.sun.xml.internal.bind.v2.ClassFactory;
ohair@286 58 import com.sun.xml.internal.bind.v2.runtime.AssociationMap;
ohair@286 59 import com.sun.xml.internal.bind.v2.runtime.Coordinator;
ohair@286 60 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
ohair@286 61 import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo;
ohair@286 62
ohair@286 63 import org.xml.sax.ErrorHandler;
ohair@286 64 import org.xml.sax.SAXException;
ohair@286 65 import org.xml.sax.helpers.LocatorImpl;
ohair@286 66
ohair@286 67 /**
ohair@286 68 * Center of the unmarshalling.
ohair@286 69 *
ohair@286 70 * <p>
ohair@286 71 * This object is responsible for coordinating {@link Loader}s to
ohair@286 72 * perform the whole unmarshalling.
ohair@286 73 *
ohair@286 74 * @author Kohsuke Kawaguchi
ohair@286 75 */
ohair@286 76 public final class UnmarshallingContext extends Coordinator
ohair@286 77 implements NamespaceContext, ValidationEventHandler, ErrorHandler, XmlVisitor, XmlVisitor.TextPredictor {
ohair@286 78
ohair@286 79 /**
ohair@286 80 * Root state.
ohair@286 81 */
ohair@286 82 private final State root;
ohair@286 83
ohair@286 84 /**
ohair@286 85 * The currently active state.
ohair@286 86 */
ohair@286 87 private State current;
ohair@286 88
ohair@286 89 private static final LocatorEx DUMMY_INSTANCE;
ohair@286 90
ohair@286 91 static {
ohair@286 92 LocatorImpl loc = new LocatorImpl();
ohair@286 93 loc.setPublicId(null);
ohair@286 94 loc.setSystemId(null);
ohair@286 95 loc.setLineNumber(-1);
ohair@286 96 loc.setColumnNumber(-1);
ohair@286 97 DUMMY_INSTANCE = new LocatorExWrapper(loc);
ohair@286 98 }
ohair@286 99
ohair@286 100 private @NotNull LocatorEx locator = DUMMY_INSTANCE;
ohair@286 101
ohair@286 102 /** Root object that is being unmarshalled. */
ohair@286 103 private Object result;
ohair@286 104
ohair@286 105 /**
ohair@286 106 * If non-null, this unmarshaller will unmarshal {@code JAXBElement<EXPECTEDTYPE>}
ohair@286 107 * regardless of the tag name, as opposed to deciding the root object by using
ohair@286 108 * the tag name.
ohair@286 109 *
ohair@286 110 * The property has a package-level access, because we cannot copy this value
ohair@286 111 * to {@link UnmarshallingContext} when it is created. The property
ohair@286 112 * on {@link Unmarshaller} could be changed after the handler is created.
ohair@286 113 */
ohair@286 114 private JaxBeanInfo expectedType;
ohair@286 115
ohair@286 116 /**
ohair@286 117 * Handles ID/IDREF.
ohair@286 118 */
ohair@286 119 private IDResolver idResolver;
ohair@286 120
ohair@286 121 /**
ohair@286 122 * This flag is set to true at the startDocument event
ohair@286 123 * and false at the endDocument event.
ohair@286 124 *
ohair@286 125 * Until the first document is unmarshalled, we don't
ohair@286 126 * want to return an object. So this variable is initialized
ohair@286 127 * to true.
ohair@286 128 */
ohair@286 129 private boolean isUnmarshalInProgress = true;
ohair@286 130 private boolean aborted = false;
ohair@286 131
ohair@286 132 public final UnmarshallerImpl parent;
ohair@286 133
ohair@286 134 /**
ohair@286 135 * If the unmarshaller is doing associative unmarshalling,
ohair@286 136 * this field is initialized to non-null.
ohair@286 137 */
ohair@286 138 private final AssociationMap assoc;
ohair@286 139
ohair@286 140 /**
ohair@286 141 * Indicates whether we are doing in-place unmarshalling
ohair@286 142 * or not.
ohair@286 143 *
ohair@286 144 * <p>
ohair@286 145 * This flag is unused when {@link #assoc}==null.
ohair@286 146 * If it's non-null, then <tt>true</tt> indicates
ohair@286 147 * that we are doing in-place associative unmarshalling.
ohair@286 148 * If <tt>false</tt>, then we are doing associative unmarshalling
ohair@286 149 * without object reuse.
ohair@286 150 */
ohair@286 151 private boolean isInplaceMode;
ohair@286 152
ohair@286 153 /**
ohair@286 154 * This object is consulted to get the element object for
ohair@286 155 * the current element event.
ohair@286 156 *
ohair@286 157 * This is used when we are building an association map.
ohair@286 158 */
ohair@286 159 private InfosetScanner scanner;
ohair@286 160
ohair@286 161 private Object currentElement;
ohair@286 162
ohair@286 163 /**
ohair@286 164 * @see XmlVisitor#startDocument(LocatorEx, NamespaceContext)
ohair@286 165 */
ohair@286 166 private NamespaceContext environmentNamespaceContext;
ohair@286 167
ohair@286 168 /**
ohair@286 169 * Used to discover additional classes when we hit unknown elements/types.
ohair@286 170 */
ohair@286 171 public @Nullable ClassResolver classResolver;
ohair@286 172
ohair@286 173 /**
ohair@286 174 * User-supplied {@link ClassLoader} for converting name to {@link Class}.
ohair@286 175 * For backward compatibility, when null, use thread context classloader.
ohair@286 176 */
ohair@286 177 public @Nullable ClassLoader classLoader;
ohair@286 178
ohair@286 179 /**
ohair@286 180 * State information for each element.
ohair@286 181 */
ohair@286 182 public final class State {
ohair@286 183 /**
ohair@286 184 * Loader that owns this element.
ohair@286 185 */
ohair@286 186 public Loader loader;
ohair@286 187 /**
ohair@286 188 * Once {@link #loader} is completed, this receiver
ohair@286 189 * receives the result.
ohair@286 190 */
ohair@286 191 public Receiver receiver;
ohair@286 192
ohair@286 193 public Intercepter intercepter;
ohair@286 194
ohair@286 195
ohair@286 196 /**
ohair@286 197 * Object being unmarshalled by this {@link #loader}.
ohair@286 198 */
ohair@286 199 public Object target;
ohair@286 200
ohair@286 201 /**
ohair@286 202 * Hack for making JAXBElement unmarshalling work.
ohair@286 203 *
ohair@286 204 * <p>
ohair@286 205 * While the unmarshalling is in progress, the {@link #target} field stores the object being unmarshalled.
ohair@286 206 * This makes it convenient to keep track of the unmarshalling activity in context of XML infoset, but
ohair@286 207 * since there's only one {@link State} per element, this mechanism only works when there's one object
ohair@286 208 * per element, which breaks down when we have {@link JAXBElement}, since the presence of JAXBElement
ohair@286 209 * requires that we have two objects unmarshalled (a JAXBElement X and a value object Y bound to an XML type.)
ohair@286 210 *
ohair@286 211 * <p>
ohair@286 212 * So to make room for storing both, this {@link #backup} field is used. When we create X instance
ohair@286 213 * in the above example, we set that to {@code state.prev.target} and displace its old value to
ohair@286 214 * {@code state.prev.backup} (where Y goes to {@code state.target}.) Upon the completion of the unmarshalling
ohair@286 215 * of Y, we revert this.
ohair@286 216 *
ohair@286 217 * <p>
ohair@286 218 * While this attributes X incorrectly to its parent element, this preserves the parent/child
ohair@286 219 * relationship between unmarshalled objects and {@link State} parent/child relationship, and
ohair@286 220 * it thereby makes {@link Receiver} mechanism simpler.
ohair@286 221 *
ohair@286 222 * <p>
ohair@286 223 * Yes, I know this is a hack, and no, I'm not proud of it.
ohair@286 224 *
ohair@286 225 * @see ElementBeanInfoImpl.IntercepterLoader#startElement(State, TagName)
ohair@286 226 * @see ElementBeanInfoImpl.IntercepterLoader#intercept(State, Object)
ohair@286 227 */
ohair@286 228 public Object backup;
ohair@286 229
ohair@286 230 /**
ohair@286 231 * Number of {@link UnmarshallingContext#nsBind}s declared thus far.
ohair@286 232 * (The value of {@link UnmarshallingContext#nsLen} when this state is pushed.
ohair@286 233 */
ohair@286 234 private int numNsDecl;
ohair@286 235
ohair@286 236 /**
ohair@286 237 * If this element has an element default value.
ohair@286 238 *
ohair@286 239 * This should be set by either a parent {@link Loader} when
ohair@286 240 * {@link Loader#childElement(State, TagName)} is called
ohair@286 241 * or by a child {@link Loader} when
ohair@286 242 * {@link Loader#startElement(State, TagName)} is called.
ohair@286 243 */
ohair@286 244 public String elementDefaultValue;
ohair@286 245
ohair@286 246 /**
ohair@286 247 * {@link State} for the parent element
ohair@286 248 *
ohair@286 249 * {@link State} objects form a doubly linked list.
ohair@286 250 */
ohair@286 251 public State prev;
ohair@286 252 private State next;
ohair@286 253
ohair@286 254 public boolean nil = false;
ohair@286 255
ohair@286 256 /**
ohair@286 257 * Gets the context.
ohair@286 258 */
ohair@286 259 public UnmarshallingContext getContext() {
ohair@286 260 return UnmarshallingContext.this;
ohair@286 261 }
ohair@286 262
ohair@286 263 private State(State prev) {
ohair@286 264 this.prev = prev;
ohair@286 265 if(prev!=null)
ohair@286 266 prev.next = this;
ohair@286 267 }
ohair@286 268
ohair@286 269 private void push() {
ohair@286 270 if(next==null)
ohair@286 271 allocateMoreStates();
ohair@286 272 State n = next;
ohair@286 273 n.numNsDecl = nsLen;
ohair@286 274 current = n;
ohair@286 275 }
ohair@286 276
ohair@286 277 private void pop() {
ohair@286 278 assert prev!=null;
ohair@286 279 loader = null;
ohair@286 280 nil = false;
ohair@286 281 receiver = null;
ohair@286 282 intercepter = null;
ohair@286 283 elementDefaultValue = null;
ohair@286 284 target = null;
ohair@286 285 current = prev;
ohair@286 286 }
ohair@286 287 }
ohair@286 288
ohair@286 289 /**
ohair@286 290 * Stub to the user-specified factory method.
ohair@286 291 */
ohair@286 292 private static class Factory {
ohair@286 293 private final Object factorInstance;
ohair@286 294 private final Method method;
ohair@286 295
ohair@286 296 public Factory(Object factorInstance, Method method) {
ohair@286 297 this.factorInstance = factorInstance;
ohair@286 298 this.method = method;
ohair@286 299 }
ohair@286 300
ohair@286 301 public Object createInstance() throws SAXException {
ohair@286 302 try {
ohair@286 303 return method.invoke(factorInstance);
ohair@286 304 } catch (IllegalAccessException e) {
ohair@286 305 getInstance().handleError(e,false);
ohair@286 306 } catch (InvocationTargetException e) {
ohair@286 307 getInstance().handleError(e,false);
ohair@286 308 }
ohair@286 309 return null; // can never be executed
ohair@286 310 }
ohair@286 311 }
ohair@286 312
ohair@286 313
ohair@286 314 /**
ohair@286 315 * Creates a new unmarshaller.
ohair@286 316 *
ohair@286 317 * @param assoc
ohair@286 318 * Must be both non-null when the unmarshaller does the
ohair@286 319 * in-place unmarshalling. Otherwise must be both null.
ohair@286 320 */
ohair@286 321 public UnmarshallingContext( UnmarshallerImpl _parent, AssociationMap assoc) {
ohair@286 322 this.parent = _parent;
ohair@286 323 this.assoc = assoc;
ohair@286 324 this.root = this.current = new State(null);
ohair@286 325 allocateMoreStates();
ohair@286 326 }
ohair@286 327
ohair@286 328 public void reset(InfosetScanner scanner,boolean isInplaceMode, JaxBeanInfo expectedType, IDResolver idResolver) {
ohair@286 329 this.scanner = scanner;
ohair@286 330 this.isInplaceMode = isInplaceMode;
ohair@286 331 this.expectedType = expectedType;
ohair@286 332 this.idResolver = idResolver;
ohair@286 333 }
ohair@286 334
ohair@286 335 public JAXBContextImpl getJAXBContext() {
ohair@286 336 return parent.context;
ohair@286 337 }
ohair@286 338
ohair@286 339 public State getCurrentState() {
ohair@286 340 return current;
ohair@286 341 }
ohair@286 342
ohair@286 343 /**
ohair@286 344 * On top of {@link JAXBContextImpl#selectRootLoader(State, TagName)},
ohair@286 345 * this method also consults {@link ClassResolver}.
ohair@286 346 *
ohair@286 347 * @throws SAXException
ohair@286 348 * if {@link ValidationEventHandler} reported a failure.
ohair@286 349 */
ohair@286 350 public Loader selectRootLoader(State state, TagName tag) throws SAXException {
ohair@286 351 try {
ohair@286 352 Loader l = getJAXBContext().selectRootLoader(state, tag);
ohair@286 353 if(l!=null) return l;
ohair@286 354
ohair@286 355 if(classResolver!=null) {
ohair@286 356 Class<?> clazz = classResolver.resolveElementName(tag.uri, tag.local);
ohair@286 357 if(clazz!=null) {
ohair@286 358 JAXBContextImpl enhanced = getJAXBContext().createAugmented(clazz);
ohair@286 359 JaxBeanInfo<?> bi = enhanced.getBeanInfo(clazz);
ohair@286 360 return bi.getLoader(enhanced,true);
ohair@286 361 }
ohair@286 362 }
ohair@286 363 } catch (RuntimeException e) {
ohair@286 364 throw e;
ohair@286 365 } catch (Exception e) {
ohair@286 366 handleError(e);
ohair@286 367 }
ohair@286 368
ohair@286 369 return null;
ohair@286 370 }
ohair@286 371
ohair@286 372 /**
ohair@286 373 * Allocates a few more {@link State}s.
ohair@286 374 *
ohair@286 375 * Allocating multiple {@link State}s at once allows those objects
ohair@286 376 * to be allocated near each other, which reduces the working set
ohair@286 377 * of CPU. It improves the chance the relevant data is in the cache.
ohair@286 378 */
ohair@286 379 private void allocateMoreStates() {
ohair@286 380 // this method should be used only when we run out of a state.
ohair@286 381 assert current.next==null;
ohair@286 382
ohair@286 383 State s = current;
ohair@286 384 for( int i=0; i<8; i++ )
ohair@286 385 s = new State(s);
ohair@286 386 }
ohair@286 387
ohair@286 388 public void clearStates() {
ohair@286 389 State last = current;
ohair@286 390 while (last.next != null) last = last.next;
ohair@286 391 while (last.prev != null) {
ohair@286 392 last.loader = null;
ohair@286 393 last.nil = false;
ohair@286 394 last.receiver = null;
ohair@286 395 last.intercepter = null;
ohair@286 396 last.elementDefaultValue = null;
ohair@286 397 last.target = null;
ohair@286 398 last = last.prev;
ohair@286 399 last.next.prev = null;
ohair@286 400 last.next = null;
ohair@286 401 }
ohair@286 402 current = last;
ohair@286 403 }
ohair@286 404
ohair@286 405 /**
ohair@286 406 * User-specified factory methods.
ohair@286 407 */
ohair@286 408 private final Map<Class,Factory> factories = new HashMap<Class, Factory>();
ohair@286 409
ohair@286 410 public void setFactories(Object factoryInstances) {
ohair@286 411 factories.clear();
ohair@286 412 if(factoryInstances==null) {
ohair@286 413 return;
ohair@286 414 }
ohair@286 415 if(factoryInstances instanceof Object[]) {
ohair@286 416 for( Object factory : (Object[])factoryInstances ) {
ohair@286 417 // look for all the public methods inlcuding derived ones
ohair@286 418 addFactory(factory);
ohair@286 419 }
ohair@286 420 } else {
ohair@286 421 addFactory(factoryInstances);
ohair@286 422 }
ohair@286 423 }
ohair@286 424
ohair@286 425 private void addFactory(Object factory) {
ohair@286 426 for( Method m : factory.getClass().getMethods() ) {
ohair@286 427 // look for methods whose signature is T createXXX()
ohair@286 428 if(!m.getName().startsWith("create"))
ohair@286 429 continue;
ohair@286 430 if(m.getParameterTypes().length>0)
ohair@286 431 continue;
ohair@286 432
ohair@286 433 Class type = m.getReturnType();
ohair@286 434
ohair@286 435 factories.put(type,new Factory(factory,m));
ohair@286 436 }
ohair@286 437 }
ohair@286 438
ohair@286 439 public void startDocument(LocatorEx locator, NamespaceContext nsContext) throws SAXException {
ohair@286 440 if(locator!=null)
ohair@286 441 this.locator = locator;
ohair@286 442 this.environmentNamespaceContext = nsContext;
ohair@286 443 // reset the object
ohair@286 444 result = null;
ohair@286 445 current = root;
ohair@286 446
ohair@286 447 patchersLen=0;
ohair@286 448 aborted = false;
ohair@286 449 isUnmarshalInProgress = true;
ohair@286 450 nsLen=0;
ohair@286 451
ohair@286 452 setThreadAffinity();
ohair@286 453
ohair@286 454 if(expectedType!=null)
ohair@286 455 root.loader = EXPECTED_TYPE_ROOT_LOADER;
ohair@286 456 else
ohair@286 457 root.loader = DEFAULT_ROOT_LOADER;
ohair@286 458
ohair@286 459 idResolver.startDocument(this);
ohair@286 460 }
ohair@286 461
ohair@286 462 public void startElement(TagName tagName) throws SAXException {
ohair@286 463 pushCoordinator();
ohair@286 464 try {
ohair@286 465 _startElement(tagName);
ohair@286 466 } finally {
ohair@286 467 popCoordinator();
ohair@286 468 }
ohair@286 469 }
ohair@286 470
ohair@286 471 private void _startElement(TagName tagName) throws SAXException {
ohair@286 472 // remember the current element if we are interested in it.
ohair@286 473 // because the inner peer might not be found while we consume
ohair@286 474 // the enter element token, we need to keep this information
ohair@286 475 // longer than this callback. That's why we assign it to a field.
ohair@286 476 if( assoc!=null )
ohair@286 477 currentElement = scanner.getCurrentElement();
ohair@286 478
ohair@286 479 Loader h = current.loader;
ohair@286 480 current.push();
ohair@286 481
ohair@286 482 // tell the parent about the new child
ohair@286 483 h.childElement(current,tagName);
ohair@286 484 assert current.loader!=null; // the childElement should register this
ohair@286 485 // and tell the new child that you are activated
ohair@286 486 current.loader.startElement(current,tagName);
ohair@286 487 }
ohair@286 488
ohair@286 489 public void text(CharSequence pcdata) throws SAXException {
ohair@286 490 State cur = current;
ohair@286 491 pushCoordinator();
ohair@286 492 try {
ohair@286 493 if(cur.elementDefaultValue!=null) {
ohair@286 494 if(pcdata.length()==0) {
ohair@286 495 // send the default value into the unmarshaller instead
ohair@286 496 pcdata = cur.elementDefaultValue;
ohair@286 497 }
ohair@286 498 }
ohair@286 499 cur.loader.text(cur,pcdata);
ohair@286 500 } finally {
ohair@286 501 popCoordinator();
ohair@286 502 }
ohair@286 503 }
ohair@286 504
ohair@286 505 public final void endElement(TagName tagName) throws SAXException {
ohair@286 506 pushCoordinator();
ohair@286 507 try {
ohair@286 508 State child = current;
ohair@286 509
ohair@286 510 // tell the child that your time is up
ohair@286 511 child.loader.leaveElement(child,tagName);
ohair@286 512
ohair@286 513 // child.pop will erase them so store them now
ohair@286 514 Object target = child.target;
ohair@286 515 Receiver recv = child.receiver;
ohair@286 516 Intercepter intercepter = child.intercepter;
ohair@286 517 child.pop();
ohair@286 518
ohair@286 519 // then let the parent know
ohair@286 520 if(intercepter!=null)
ohair@286 521 target = intercepter.intercept(current,target);
ohair@286 522 if(recv!=null)
ohair@286 523 recv.receive(current,target);
ohair@286 524 } finally {
ohair@286 525 popCoordinator();
ohair@286 526 }
ohair@286 527 }
ohair@286 528
ohair@286 529 public void endDocument() throws SAXException {
ohair@286 530 runPatchers();
ohair@286 531 idResolver.endDocument();
ohair@286 532
ohair@286 533 isUnmarshalInProgress = false;
ohair@286 534 currentElement = null;
ohair@286 535 locator = DUMMY_INSTANCE;
ohair@286 536 environmentNamespaceContext = null;
ohair@286 537
ohair@286 538 // at the successful completion, scope must be all closed
ohair@286 539 assert root==current;
ohair@286 540
ohair@286 541 resetThreadAffinity();
ohair@286 542 }
ohair@286 543
ohair@286 544 /**
ohair@286 545 * You should be always calling this through {@link TextPredictor}.
ohair@286 546 */
ohair@286 547 @Deprecated
ohair@286 548 public boolean expectText() {
ohair@286 549 return current.loader.expectText;
ohair@286 550 }
ohair@286 551
ohair@286 552 /**
ohair@286 553 * You should be always getting {@link TextPredictor} from {@link XmlVisitor}.
ohair@286 554 */
ohair@286 555 @Deprecated
ohair@286 556 public TextPredictor getPredictor() {
ohair@286 557 return this;
ohair@286 558 }
ohair@286 559
ohair@286 560 public UnmarshallingContext getContext() {
ohair@286 561 return this;
ohair@286 562 }
ohair@286 563
ohair@286 564 /**
ohair@286 565 * Gets the result of the unmarshalling
ohair@286 566 */
ohair@286 567 public Object getResult() throws UnmarshalException {
ohair@286 568 if(isUnmarshalInProgress)
ohair@286 569 throw new IllegalStateException();
ohair@286 570
ohair@286 571 if(!aborted) return result;
ohair@286 572
ohair@286 573 // there was an error.
ohair@286 574 throw new UnmarshalException((String)null);
ohair@286 575 }
ohair@286 576
ohair@286 577 void clearResult() {
ohair@286 578 if (isUnmarshalInProgress) {
ohair@286 579 throw new IllegalStateException();
ohair@286 580 }
ohair@286 581 result = null;
ohair@286 582 }
ohair@286 583
ohair@286 584 /**
ohair@286 585 * Creates a new instance of the specified class.
ohair@286 586 * In the unmarshaller, we need to check the user-specified factory class.
ohair@286 587 */
ohair@286 588 public Object createInstance( Class<?> clazz ) throws SAXException {
ohair@286 589 if(!factories.isEmpty()) {
ohair@286 590 Factory factory = factories.get(clazz);
ohair@286 591 if(factory!=null)
ohair@286 592 return factory.createInstance();
ohair@286 593 }
ohair@286 594 return ClassFactory.create(clazz);
ohair@286 595 }
ohair@286 596
ohair@286 597 /**
ohair@286 598 * Creates a new instance of the specified class.
ohair@286 599 * In the unmarshaller, we need to check the user-specified factory class.
ohair@286 600 */
ohair@286 601 public Object createInstance( JaxBeanInfo beanInfo ) throws SAXException {
ohair@286 602 if(!factories.isEmpty()) {
ohair@286 603 Factory factory = factories.get(beanInfo.jaxbType);
ohair@286 604 if(factory!=null)
ohair@286 605 return factory.createInstance();
ohair@286 606 }
ohair@286 607 try {
ohair@286 608 return beanInfo.createInstance(this);
ohair@286 609 } catch (IllegalAccessException e) {
ohair@286 610 Loader.reportError("Unable to create an instance of "+beanInfo.jaxbType.getName(),e,false);
ohair@286 611 } catch (InvocationTargetException e) {
ohair@286 612 Loader.reportError("Unable to create an instance of "+beanInfo.jaxbType.getName(),e,false);
ohair@286 613 } catch (InstantiationException e) {
ohair@286 614 Loader.reportError("Unable to create an instance of "+beanInfo.jaxbType.getName(),e,false);
ohair@286 615 }
ohair@286 616 return null; // can never be here
ohair@286 617 }
ohair@286 618
ohair@286 619
ohair@286 620
ohair@286 621 //
ohair@286 622 //
ohair@286 623 // error handling
ohair@286 624 //
ohair@286 625 //
ohair@286 626
ohair@286 627 /**
ohair@286 628 * Reports an error to the user, and asks if s/he wants
ohair@286 629 * to recover. If the canRecover flag is false, regardless
ohair@286 630 * of the client instruction, an exception will be thrown.
ohair@286 631 *
ohair@286 632 * Only if the flag is true and the user wants to recover from an error,
ohair@286 633 * the method returns normally.
ohair@286 634 *
ohair@286 635 * The thrown exception will be catched by the unmarshaller.
ohair@286 636 */
ohair@286 637 public void handleEvent(ValidationEvent event, boolean canRecover ) throws SAXException {
ohair@286 638 ValidationEventHandler eventHandler = parent.getEventHandler();
ohair@286 639
ohair@286 640 boolean recover = eventHandler.handleEvent(event);
ohair@286 641
ohair@286 642 // if the handler says "abort", we will not return the object
ohair@286 643 // from the unmarshaller.getResult()
ohair@286 644 if(!recover) aborted = true;
ohair@286 645
ohair@286 646 if( !canRecover || !recover )
ohair@286 647 throw new SAXParseException2( event.getMessage(), locator,
ohair@286 648 new UnmarshalException(
ohair@286 649 event.getMessage(),
ohair@286 650 event.getLinkedException() ) );
ohair@286 651 }
ohair@286 652
ohair@286 653 public boolean handleEvent(ValidationEvent event) {
ohair@286 654 try {
ohair@286 655 // if the handler says "abort", we will not return the object.
ohair@286 656 boolean recover = parent.getEventHandler().handleEvent(event);
ohair@286 657 if(!recover) aborted = true;
ohair@286 658 return recover;
ohair@286 659 } catch( RuntimeException re ) {
ohair@286 660 // if client event handler causes a runtime exception, then we
ohair@286 661 // have to return false.
ohair@286 662 return false;
ohair@286 663 }
ohair@286 664 }
ohair@286 665
ohair@286 666 /**
ohair@286 667 * Reports an exception found during the unmarshalling to the user.
ohair@286 668 * This method is a convenience method that calls into
ohair@286 669 * {@link #handleEvent(ValidationEvent, boolean)}
ohair@286 670 */
ohair@286 671 public void handleError(Exception e) throws SAXException {
ohair@286 672 handleError(e,true);
ohair@286 673 }
ohair@286 674
ohair@286 675 public void handleError(Exception e,boolean canRecover) throws SAXException {
ohair@286 676 handleEvent(new ValidationEventImpl(ValidationEvent.ERROR,e.getMessage(),locator.getLocation(),e),canRecover);
ohair@286 677 }
ohair@286 678
ohair@286 679 public void handleError(String msg) {
ohair@286 680 handleEvent(new ValidationEventImpl(ValidationEvent.ERROR,msg,locator.getLocation()));
ohair@286 681 }
ohair@286 682
ohair@286 683 protected ValidationEventLocator getLocation() {
ohair@286 684 return locator.getLocation();
ohair@286 685 }
ohair@286 686
ohair@286 687 /**
ohair@286 688 * Gets the current source location information in SAX {@link Locator}.
ohair@286 689 * <p>
ohair@286 690 * Sometimes the unmarshaller works against a different kind of XML source,
ohair@286 691 * making this information meaningless.
ohair@286 692 */
ohair@286 693 public LocatorEx getLocator() { return locator; }
ohair@286 694
ohair@286 695 /**
ohair@286 696 * Called when there's no corresponding ID value.
ohair@286 697 */
ohair@286 698 public void errorUnresolvedIDREF(Object bean, String idref, LocatorEx loc) throws SAXException {
ohair@286 699 handleEvent( new ValidationEventImpl(
ohair@286 700 ValidationEvent.ERROR,
ohair@286 701 Messages.UNRESOLVED_IDREF.format(idref),
ohair@286 702 loc.getLocation()), true );
ohair@286 703 }
ohair@286 704
ohair@286 705
ohair@286 706 //
ohair@286 707 //
ohair@286 708 // ID/IDREF related code
ohair@286 709 //
ohair@286 710 //
ohair@286 711 /**
ohair@286 712 * Submitted patchers in the order they've submitted.
ohair@286 713 * Many XML vocabulary doesn't use ID/IDREF at all, so we
ohair@286 714 * initialize it with null.
ohair@286 715 */
ohair@286 716 private Patcher[] patchers = null;
ohair@286 717 private int patchersLen = 0;
ohair@286 718
ohair@286 719 /**
ohair@286 720 * Adds a job that will be executed at the last of the unmarshalling.
ohair@286 721 * This method is used to support ID/IDREF feature, but it can be used
ohair@286 722 * for other purposes as well.
ohair@286 723 *
ohair@286 724 * @param job
ohair@286 725 * The run method of this object is called.
ohair@286 726 */
ohair@286 727 public void addPatcher( Patcher job ) {
ohair@286 728 // re-allocate buffer if necessary
ohair@286 729 if( patchers==null )
ohair@286 730 patchers = new Patcher[32];
ohair@286 731 if( patchers.length == patchersLen ) {
ohair@286 732 Patcher[] buf = new Patcher[patchersLen*2];
ohair@286 733 System.arraycopy(patchers,0,buf,0,patchersLen);
ohair@286 734 patchers = buf;
ohair@286 735 }
ohair@286 736 patchers[patchersLen++] = job;
ohair@286 737 }
ohair@286 738
ohair@286 739 /** Executes all the patchers. */
ohair@286 740 private void runPatchers() throws SAXException {
ohair@286 741 if( patchers!=null ) {
ohair@286 742 for( int i=0; i<patchersLen; i++ ) {
ohair@286 743 patchers[i].run();
ohair@286 744 patchers[i] = null; // free memory
ohair@286 745 }
ohair@286 746 }
ohair@286 747 }
ohair@286 748
ohair@286 749 /**
ohair@286 750 * Adds the object which is currently being unmarshalled
ohair@286 751 * to the ID table.
ohair@286 752 *
ohair@286 753 * @return
ohair@286 754 * Returns the value passed as the parameter.
ohair@286 755 * This is a hack, but this makes it easier for ID
ohair@286 756 * transducer to do its job.
ohair@286 757 */
ohair@286 758 // TODO: what shall we do if the ID is already declared?
ohair@286 759 //
ohair@286 760 // throwing an exception is one way. Overwriting the previous one
ohair@286 761 // is another way. The latter allows us to process invalid documents,
ohair@286 762 // while the former makes it impossible to handle them.
ohair@286 763 //
ohair@286 764 // I prefer to be flexible in terms of invalid document handling,
ohair@286 765 // so chose not to throw an exception.
ohair@286 766 //
ohair@286 767 // I believe this is an implementation choice, not the spec issue.
ohair@286 768 // -kk
ohair@286 769 public String addToIdTable( String id ) throws SAXException {
ohair@286 770 // Hmm...
ohair@286 771 // in cases such as when ID is used as an attribute, or as @XmlValue
ohair@286 772 // the target wilil be current.target.
ohair@286 773 // but in some other cases, such as when ID is used as a child element
ohair@286 774 // or a value of JAXBElement, it's current.prev.target.
ohair@286 775 // I don't know if this detection logic is complete
ohair@286 776 Object o = current.target;
ohair@286 777 if(o==null)
ohair@286 778 o = current.prev.target;
ohair@286 779 idResolver.bind(id,o);
ohair@286 780 return id;
ohair@286 781 }
ohair@286 782
ohair@286 783 /**
ohair@286 784 * Looks up the ID table and gets associated object.
ohair@286 785 *
ohair@286 786 * <p>
ohair@286 787 * The exception thrown from {@link Callable#call()} means the unmarshaller should abort
ohair@286 788 * right away.
ohair@286 789 *
ohair@286 790 * @see IDResolver#resolve(String, Class)
ohair@286 791 */
ohair@286 792 public Callable getObjectFromId( String id, Class targetType ) throws SAXException {
ohair@286 793 return idResolver.resolve(id,targetType);
ohair@286 794 }
ohair@286 795
ohair@286 796 //
ohair@286 797 //
ohair@286 798 // namespace binding maintainance
ohair@286 799 //
ohair@286 800 //
ohair@286 801 private String[] nsBind = new String[16];
ohair@286 802 private int nsLen=0;
ohair@286 803
ohair@286 804 public void startPrefixMapping( String prefix, String uri ) {
ohair@286 805 if(nsBind.length==nsLen) {
ohair@286 806 // expand the buffer
ohair@286 807 String[] n = new String[nsLen*2];
ohair@286 808 System.arraycopy(nsBind,0,n,0,nsLen);
ohair@286 809 nsBind=n;
ohair@286 810 }
ohair@286 811 nsBind[nsLen++] = prefix;
ohair@286 812 nsBind[nsLen++] = uri;
ohair@286 813 }
ohair@286 814 public void endPrefixMapping( String prefix ) {
ohair@286 815 nsLen-=2;
ohair@286 816 }
ohair@286 817 private String resolveNamespacePrefix( String prefix ) {
ohair@286 818 if(prefix.equals("xml"))
ohair@286 819 return XMLConstants.XML_NS_URI;
ohair@286 820
ohair@286 821 for( int i=nsLen-2; i>=0; i-=2 ) {
ohair@286 822 if(prefix.equals(nsBind[i]))
ohair@286 823 return nsBind[i+1];
ohair@286 824 }
ohair@286 825
ohair@286 826 if(environmentNamespaceContext!=null)
ohair@286 827 // temporary workaround until Zephyr fixes 6337180
ohair@286 828 return environmentNamespaceContext.getNamespaceURI(prefix.intern());
ohair@286 829
ohair@286 830 // by default, the default ns is bound to "".
ohair@286 831 // but allow environmentNamespaceContext to take precedence
ohair@286 832 if(prefix.equals(""))
ohair@286 833 return "";
ohair@286 834
ohair@286 835 // unresolved. error.
ohair@286 836 return null;
ohair@286 837 }
ohair@286 838
ohair@286 839 /**
ohair@286 840 * Returns a list of prefixes newly declared on the current element.
ohair@286 841 *
ohair@286 842 * @return
ohair@286 843 * A possible zero-length array of prefixes. The default prefix
ohair@286 844 * is represented by the empty string.
ohair@286 845 */
ohair@286 846 public String[] getNewlyDeclaredPrefixes() {
ohair@286 847 return getPrefixList( current.prev.numNsDecl );
ohair@286 848 }
ohair@286 849
ohair@286 850 /**
ohair@286 851 * Returns a list of all in-scope prefixes.
ohair@286 852 *
ohair@286 853 * @return
ohair@286 854 * A possible zero-length array of prefixes. The default prefix
ohair@286 855 * is represented by the empty string.
ohair@286 856 */
ohair@286 857 public String[] getAllDeclaredPrefixes() {
ohair@286 858 return getPrefixList(0);
ohair@286 859 }
ohair@286 860
ohair@286 861 private String[] getPrefixList( int startIndex ) {
ohair@286 862 int size = (current.numNsDecl - startIndex)/2;
ohair@286 863 String[] r = new String[size];
ohair@286 864 for( int i=0; i<r.length; i++ )
ohair@286 865 r[i] = nsBind[startIndex+i*2];
ohair@286 866 return r;
ohair@286 867 }
ohair@286 868
ohair@286 869 // NamespaceContext2 implementation
ohair@286 870 //
ohair@286 871 public Iterator<String> getPrefixes(String uri) {
ohair@286 872 // TODO: could be implemented much faster
ohair@286 873 // wrap it into unmodifiable list so that the remove method
ohair@286 874 // will throw UnsupportedOperationException.
ohair@286 875 return Collections.unmodifiableList(
ohair@286 876 getAllPrefixesInList(uri)).iterator();
ohair@286 877 }
ohair@286 878
ohair@286 879 private List<String> getAllPrefixesInList(String uri) {
ohair@286 880 List<String> a = new ArrayList<String>();
ohair@286 881
ohair@286 882 if( uri==null )
ohair@286 883 throw new IllegalArgumentException();
ohair@286 884 if( uri.equals(XMLConstants.XML_NS_URI) ) {
ohair@286 885 a.add(XMLConstants.XML_NS_PREFIX);
ohair@286 886 return a;
ohair@286 887 }
ohair@286 888 if( uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) ) {
ohair@286 889 a.add(XMLConstants.XMLNS_ATTRIBUTE);
ohair@286 890 return a;
ohair@286 891 }
ohair@286 892
ohair@286 893 for( int i=nsLen-2; i>=0; i-=2 )
ohair@286 894 if(uri.equals(nsBind[i+1]))
ohair@286 895 if( getNamespaceURI(nsBind[i]).equals(nsBind[i+1]) )
ohair@286 896 // make sure that this prefix is still effective.
ohair@286 897 a.add(nsBind[i]);
ohair@286 898
ohair@286 899 return a;
ohair@286 900 }
ohair@286 901
ohair@286 902 public String getPrefix(String uri) {
ohair@286 903 if( uri==null )
ohair@286 904 throw new IllegalArgumentException();
ohair@286 905 if( uri.equals(XMLConstants.XML_NS_URI) )
ohair@286 906 return XMLConstants.XML_NS_PREFIX;
ohair@286 907 if( uri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) )
ohair@286 908 return XMLConstants.XMLNS_ATTRIBUTE;
ohair@286 909
ohair@286 910 for( int i=nsLen-2; i>=0; i-=2 )
ohair@286 911 if(uri.equals(nsBind[i+1]))
ohair@286 912 if( getNamespaceURI(nsBind[i]).equals(nsBind[i+1]) )
ohair@286 913 // make sure that this prefix is still effective.
ohair@286 914 return nsBind[i];
ohair@286 915
ohair@286 916 if(environmentNamespaceContext!=null)
ohair@286 917 return environmentNamespaceContext.getPrefix(uri);
ohair@286 918
ohair@286 919 return null;
ohair@286 920 }
ohair@286 921
ohair@286 922 public String getNamespaceURI(String prefix) {
ohair@286 923 if (prefix == null)
ohair@286 924 throw new IllegalArgumentException();
ohair@286 925 if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE))
ohair@286 926 return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
ohair@286 927
ohair@286 928 return resolveNamespacePrefix(prefix);
ohair@286 929 }
ohair@286 930
ohair@286 931 //
ohair@286 932 //
ohair@286 933 //
ohair@286 934 // scope management
ohair@286 935 //
ohair@286 936 //
ohair@286 937 //
ohair@286 938 private Scope[] scopes = new Scope[16];
ohair@286 939 /**
ohair@286 940 * Points to the top of the scope stack (=size-1).
ohair@286 941 */
ohair@286 942 private int scopeTop=0;
ohair@286 943
ohair@286 944 {
ohair@286 945 for( int i=0; i<scopes.length; i++ )
ohair@286 946 scopes[i] = new Scope(this);
ohair@286 947 }
ohair@286 948
ohair@286 949 /**
ohair@286 950 * Starts a new packing scope.
ohair@286 951 *
ohair@286 952 * <p>
ohair@286 953 * This method allocates a specified number of fresh {@link Scope} objects.
ohair@286 954 * They can be accessed by the {@link #getScope} method until the corresponding
ohair@286 955 * {@link #endScope} method is invoked.
ohair@286 956 *
ohair@286 957 * <p>
ohair@286 958 * A new scope will mask the currently active scope. Only one frame of {@link Scope}s
ohair@286 959 * can be accessed at any given time.
ohair@286 960 *
ohair@286 961 * @param frameSize
ohair@286 962 * The # of slots to be allocated.
ohair@286 963 */
ohair@286 964 public void startScope(int frameSize) {
ohair@286 965 scopeTop += frameSize;
ohair@286 966
ohair@286 967 // reallocation
ohair@286 968 if(scopeTop>=scopes.length) {
ohair@286 969 Scope[] s = new Scope[Math.max(scopeTop+1,scopes.length*2)];
ohair@286 970 System.arraycopy(scopes,0,s,0,scopes.length);
ohair@286 971 for( int i=scopes.length; i<s.length; i++ )
ohair@286 972 s[i] = new Scope(this);
ohair@286 973 scopes = s;
ohair@286 974 }
ohair@286 975 }
ohair@286 976
ohair@286 977 /**
ohair@286 978 * Ends the current packing scope.
ohair@286 979 *
ohair@286 980 * <p>
ohair@286 981 * If any packing in progress will be finalized by this method.
ohair@286 982 *
ohair@286 983 * @param frameSize
ohair@286 984 * The same size that gets passed to the {@link #startScope(int)}
ohair@286 985 * method.
ohair@286 986 */
ohair@286 987 public void endScope(int frameSize) throws SAXException {
ohair@286 988 try {
ohair@286 989 for( ; frameSize>0; frameSize--, scopeTop-- )
ohair@286 990 scopes[scopeTop].finish();
ohair@286 991 } catch (AccessorException e) {
ohair@286 992 handleError(e);
ohair@286 993
ohair@286 994 // the error might have left scopes in inconsistent state,
ohair@286 995 // so replace them by fresh ones
ohair@286 996 for( ; frameSize>0; frameSize-- )
ohair@286 997 scopes[scopeTop--] = new Scope(this);
ohair@286 998 }
ohair@286 999 }
ohair@286 1000
ohair@286 1001 /**
ohair@286 1002 * Gets the currently active {@link Scope}.
ohair@286 1003 *
ohair@286 1004 * @param offset
ohair@286 1005 * a number between [0,frameSize)
ohair@286 1006 *
ohair@286 1007 * @return
ohair@286 1008 * always a valid {@link Scope} object.
ohair@286 1009 */
ohair@286 1010 public Scope getScope(int offset) {
ohair@286 1011 return scopes[scopeTop-offset];
ohair@286 1012 }
ohair@286 1013
ohair@286 1014 //
ohair@286 1015 //
ohair@286 1016 //
ohair@286 1017 //
ohair@286 1018 //
ohair@286 1019 //
ohair@286 1020 //
ohair@286 1021
ohair@286 1022 private static final Loader DEFAULT_ROOT_LOADER = new DefaultRootLoader();
ohair@286 1023 private static final Loader EXPECTED_TYPE_ROOT_LOADER = new ExpectedTypeRootLoader();
ohair@286 1024
ohair@286 1025 /**
ohair@286 1026 * Root loader that uses the tag name and possibly its @xsi:type
ohair@286 1027 * to decide how to start unmarshalling.
ohair@286 1028 */
ohair@286 1029 private static final class DefaultRootLoader extends Loader implements Receiver {
ohair@286 1030 /**
ohair@286 1031 * Receives the root element and determines how to start
ohair@286 1032 * unmarshalling.
ohair@286 1033 */
ohair@286 1034 @Override
ohair@286 1035 public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
ohair@286 1036 Loader loader = state.getContext().selectRootLoader(state,ea);
ohair@286 1037 if(loader!=null) {
ohair@286 1038 state.loader = loader;
ohair@286 1039 state.receiver = this;
ohair@286 1040 return;
ohair@286 1041 }
ohair@286 1042
ohair@286 1043 // the registry doesn't know about this element.
ohair@286 1044 // try its xsi:type
ohair@286 1045 JaxBeanInfo beanInfo = XsiTypeLoader.parseXsiType(state, ea, null);
ohair@286 1046 if(beanInfo==null) {
ohair@286 1047 // we don't even know its xsi:type
ohair@286 1048 reportUnexpectedChildElement(ea,false);
ohair@286 1049 return;
ohair@286 1050 }
ohair@286 1051
ohair@286 1052 state.loader = beanInfo.getLoader(null,false);
ohair@286 1053 state.prev.backup = new JAXBElement<Object>(ea.createQName(),Object.class,null);
ohair@286 1054 state.receiver = this;
ohair@286 1055 }
ohair@286 1056
ohair@286 1057 @Override
ohair@286 1058 public Collection<QName> getExpectedChildElements() {
ohair@286 1059 return getInstance().getJAXBContext().getValidRootNames();
ohair@286 1060 }
ohair@286 1061
ohair@286 1062 public void receive(State state, Object o) {
ohair@286 1063 if(state.backup!=null) {
ohair@286 1064 ((JAXBElement<Object>)state.backup).setValue(o);
ohair@286 1065 o = state.backup;
ohair@286 1066 }
ohair@286 1067 if (state.nil) {
ohair@286 1068 ((JAXBElement<Object>)o).setNil(true);
ohair@286 1069 }
ohair@286 1070 state.getContext().result = o;
ohair@286 1071 }
ohair@286 1072 }
ohair@286 1073
ohair@286 1074 /**
ohair@286 1075 * Root loader that uses {@link UnmarshallingContext#expectedType}
ohair@286 1076 * to decide how to start unmarshalling.
ohair@286 1077 */
ohair@286 1078 private static final class ExpectedTypeRootLoader extends Loader implements Receiver {
ohair@286 1079 /**
ohair@286 1080 * Receives the root element and determines how to start
ohair@286 1081 * unmarshalling.
ohair@286 1082 */
ohair@286 1083 @Override
ohair@286 1084 public void childElement(UnmarshallingContext.State state, TagName ea) {
ohair@286 1085 UnmarshallingContext context = state.getContext();
ohair@286 1086
ohair@286 1087 // unmarshals the specified type
ohair@286 1088 QName qn = new QName(ea.uri,ea.local);
ohair@286 1089 state.prev.target = new JAXBElement(qn,context.expectedType.jaxbType,null,null);
ohair@286 1090 state.receiver = this;
ohair@286 1091 // this is bit wasteful, as in theory we should have each expectedType keep
ohair@286 1092 // nillable version --- but that increases the combination from two to four,
ohair@286 1093 // which adds the resident memory footprint. Since XsiNilLoader is small,
ohair@286 1094 // I intentionally allocate a new instance freshly.
ohair@286 1095 state.loader = new XsiNilLoader(context.expectedType.getLoader(null,true));
ohair@286 1096 }
ohair@286 1097
ohair@286 1098 public void receive(State state, Object o) {
ohair@286 1099 JAXBElement e = (JAXBElement)state.target;
ohair@286 1100 e.setValue(o);
ohair@286 1101 state.getContext().recordOuterPeer(e);
ohair@286 1102 state.getContext().result = e;
ohair@286 1103 }
ohair@286 1104 }
ohair@286 1105
ohair@286 1106 //
ohair@286 1107 // in-place unmarshalling related capabilities
ohair@286 1108 //
ohair@286 1109 /**
ohair@286 1110 * Notifies the context about the inner peer of the current element.
ohair@286 1111 *
ohair@286 1112 * <p>
ohair@286 1113 * If the unmarshalling is building the association, the context
ohair@286 1114 * will use this information. Otherwise it will be just ignored.
ohair@286 1115 */
ohair@286 1116 public void recordInnerPeer(Object innerPeer) {
ohair@286 1117 if(assoc!=null)
ohair@286 1118 assoc.addInner(currentElement,innerPeer);
ohair@286 1119 }
ohair@286 1120
ohair@286 1121 /**
ohair@286 1122 * Gets the inner peer JAXB object associated with the current element.
ohair@286 1123 *
ohair@286 1124 * @return
ohair@286 1125 * null if the current element doesn't have an inner peer,
ohair@286 1126 * or if we are not doing the in-place unmarshalling.
ohair@286 1127 */
ohair@286 1128 public Object getInnerPeer() {
ohair@286 1129 if(assoc!=null && isInplaceMode)
ohair@286 1130 return assoc.getInnerPeer(currentElement);
ohair@286 1131 else
ohair@286 1132 return null;
ohair@286 1133 }
ohair@286 1134
ohair@286 1135 /**
ohair@286 1136 * Notifies the context about the outer peer of the current element.
ohair@286 1137 *
ohair@286 1138 * <p>
ohair@286 1139 * If the unmarshalling is building the association, the context
ohair@286 1140 * will use this information. Otherwise it will be just ignored.
ohair@286 1141 */
ohair@286 1142 public void recordOuterPeer(Object outerPeer) {
ohair@286 1143 if(assoc!=null)
ohair@286 1144 assoc.addOuter(currentElement,outerPeer);
ohair@286 1145 }
ohair@286 1146
ohair@286 1147 /**
ohair@286 1148 * Gets the outer peer JAXB object associated with the current element.
ohair@286 1149 *
ohair@286 1150 * @return
ohair@286 1151 * null if the current element doesn't have an inner peer,
ohair@286 1152 * or if we are not doing the in-place unmarshalling.
ohair@286 1153 */
ohair@286 1154 public Object getOuterPeer() {
ohair@286 1155 if(assoc!=null && isInplaceMode)
ohair@286 1156 return assoc.getOuterPeer(currentElement);
ohair@286 1157 else
ohair@286 1158 return null;
ohair@286 1159 }
ohair@286 1160
ohair@286 1161 /**
ohair@286 1162 * Gets the xmime:contentType value for the current object.
ohair@286 1163 *
ohair@286 1164 * @see JAXBContextImpl#getXMIMEContentType(Object)
ohair@286 1165 */
ohair@286 1166 public String getXMIMEContentType() {
ohair@286 1167 /*
ohair@286 1168 this won't work when the class is like
ohair@286 1169
ohair@286 1170 class Foo {
ohair@286 1171 @XmlValue Image img;
ohair@286 1172 }
ohair@286 1173
ohair@286 1174 because the target will return Foo, not the class enclosing Foo
ohair@286 1175 which will have xmime:contentType
ohair@286 1176 */
ohair@286 1177 Object t = current.target;
ohair@286 1178 if(t==null) return null;
ohair@286 1179 return getJAXBContext().getXMIMEContentType(t);
ohair@286 1180 }
ohair@286 1181
ohair@286 1182 /**
ohair@286 1183 * When called from within the realm of the unmarshaller, this method
ohair@286 1184 * returns the current {@link UnmarshallingContext} in charge.
ohair@286 1185 */
ohair@286 1186 public static UnmarshallingContext getInstance() {
ohair@286 1187 return (UnmarshallingContext) Coordinator._getInstance();
ohair@286 1188 }
ohair@286 1189
ohair@286 1190 /**
ohair@286 1191 * Allows to access elements which are expected in current state.
ohair@286 1192 * Useful for getting elements for current parent.
ohair@286 1193 *
ohair@286 1194 * @return
ohair@286 1195 */
ohair@286 1196 public Collection<QName> getCurrentExpectedElements() {
ohair@286 1197 pushCoordinator();
ohair@286 1198 try {
ohair@286 1199 State s = getCurrentState();
ohair@286 1200 Loader l = s.loader;
ohair@286 1201 return (l != null) ? l.getExpectedChildElements() : null;
ohair@286 1202 } finally {
ohair@286 1203 popCoordinator();
ohair@286 1204 }
ohair@286 1205 }
ohair@286 1206
ohair@286 1207 /**
ohair@286 1208 * Allows to access attributes which are expected in current state.
ohair@286 1209 * Useful for getting attributes for current parent.
ohair@286 1210 *
ohair@286 1211 * @return
ohair@286 1212 */
ohair@286 1213 public Collection<QName> getCurrentExpectedAttributes() {
ohair@286 1214 pushCoordinator();
ohair@286 1215 try {
ohair@286 1216 State s = getCurrentState();
ohair@286 1217 Loader l = s.loader;
ohair@286 1218 return (l != null) ? l.getExpectedAttributes() : null;
ohair@286 1219 } finally {
ohair@286 1220 popCoordinator();
ohair@286 1221 }
ohair@286 1222 }
ohair@286 1223
ohair@286 1224 /**
ohair@286 1225 * Gets StructureLoader if used as loader.
ohair@286 1226 * Useful when determining if element is mixed or not.
ohair@286 1227 *
ohair@286 1228 */
ohair@286 1229 public StructureLoader getStructureLoader() {
ohair@286 1230 if(current.loader instanceof StructureLoader)
ohair@286 1231 return (StructureLoader)current.loader;
ohair@286 1232
ohair@286 1233 return null;
ohair@286 1234 }
ohair@286 1235
ohair@286 1236 }

mercurial