Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
28 import com.sun.xml.internal.bind.api.AccessorException;
29 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
30 import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
32 import org.xml.sax.SAXException;
34 /**
35 * Holds the information about packing scope.
36 *
37 * <p>
38 * When no packing is started yet, all the fields should be set to null.
39 *
40 * @author Kohsuke Kawaguchi
41 */
42 public final class Scope<BeanT,PropT,ItemT,PackT> {
44 public final UnmarshallingContext context;
46 private BeanT bean;
47 private Accessor<BeanT,PropT> acc;
48 private PackT pack;
49 private Lister<BeanT,PropT,ItemT,PackT> lister;
51 Scope(UnmarshallingContext context) {
52 this.context = context;
53 }
55 /**
56 * Returns true if this scope object is filled by a packing in progress.
57 */
58 public boolean hasStarted() {
59 return bean!=null;
60 }
62 /**
63 * Initializes all the fields to null.
64 */
65 public void reset() {
66 if(bean==null) {
67 // already initialized
68 assert clean();
69 return;
70 }
72 bean = null;
73 acc = null;
74 pack = null;
75 lister = null;
76 }
78 /**
79 * Finishes up the current packing in progress (if any) and
80 * resets this object.
81 */
82 public void finish() throws AccessorException {
83 if(hasStarted()) {
84 lister.endPacking(pack,bean,acc);
85 reset();
86 }
87 assert clean();
88 }
90 private boolean clean() {
91 return bean==null && acc==null && pack==null && lister==null;
92 }
94 /**
95 * Adds a new item to this packing scope.
96 */
97 public void add( Accessor<BeanT,PropT> acc, Lister<BeanT,PropT,ItemT,PackT> lister, ItemT value) throws SAXException{
98 try {
99 if(!hasStarted()) {
100 this.bean = (BeanT)context.getCurrentState().target;
101 this.acc = acc;
102 this.lister = lister;
103 this.pack = lister.startPacking(bean,acc);
104 }
106 lister.addToPack(pack,value);
107 } catch (AccessorException e) {
108 Loader.handleGenericException(e,true);
109 // recover from this error by ignoring future items.
110 this.lister = Lister.getErrorInstance();
111 this.acc = Accessor.getErrorInstance();
112 }
113 }
115 /**
116 * Starts the packing scope, without adding any item.
117 *
118 * This allows us to return an empty pack, thereby allowing the user
119 * to distinguish empty array vs null array.
120 */
121 public void start( Accessor<BeanT,PropT> acc, Lister<BeanT,PropT,ItemT,PackT> lister) throws SAXException{
122 try {
123 if(!hasStarted()) {
124 this.bean = (BeanT)context.getCurrentState().target;
125 this.acc = acc;
126 this.lister = lister;
127 this.pack = lister.startPacking(bean,acc);
128 }
129 } catch (AccessorException e) {
130 Loader.handleGenericException(e,true);
131 // recover from this error by ignoring future items.
132 this.lister = Lister.getErrorInstance();
133 this.acc = Accessor.getErrorInstance();
134 }
135 }
136 }