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

Mon, 28 May 2018 10:36:45 +0800

author
aoqi
date
Mon, 28 May 2018 10:36:45 +0800
changeset 1546
dc8316632248
parent 760
e530533619ec
permissions
-rw-r--r--

Merge

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

mercurial