001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime.unmarshaller;
038:
039: import com.sun.xml.bind.api.AccessorException;
040: import com.sun.xml.bind.v2.runtime.reflect.Accessor;
041: import com.sun.xml.bind.v2.runtime.reflect.Lister;
042:
043: import org.xml.sax.SAXException;
044:
045: /**
046: * Holds the information about packing scope.
047: *
048: * <p>
049: * When no packing is started yet, all the fields should be set to null.
050: *
051: * @author Kohsuke Kawaguchi
052: */
053: public final class Scope<BeanT, PropT, ItemT, PackT> {
054:
055: public final UnmarshallingContext context;
056:
057: private BeanT bean;
058: private Accessor<BeanT, PropT> acc;
059: private PackT pack;
060: private Lister<BeanT, PropT, ItemT, PackT> lister;
061:
062: Scope(UnmarshallingContext context) {
063: this .context = context;
064: }
065:
066: /**
067: * Returns true if this scope object is filled by a packing in progress.
068: */
069: public boolean hasStarted() {
070: return bean != null;
071: }
072:
073: /**
074: * Initializes all the fields to null.
075: */
076: public void reset() {
077: if (bean == null) {
078: // already initialized
079: assert clean();
080: return;
081: }
082:
083: bean = null;
084: acc = null;
085: pack = null;
086: lister = null;
087: }
088:
089: /**
090: * Finishes up the current packing in progress (if any) and
091: * resets this object.
092: */
093: public void finish() throws AccessorException {
094: if (hasStarted()) {
095: lister.endPacking(pack, bean, acc);
096: reset();
097: }
098: assert clean();
099: }
100:
101: private boolean clean() {
102: return bean == null && acc == null && pack == null
103: && lister == null;
104: }
105:
106: /**
107: * Adds a new item to this packing scope.
108: */
109: public void add(Accessor<BeanT, PropT> acc,
110: Lister<BeanT, PropT, ItemT, PackT> lister, ItemT value)
111: throws SAXException {
112: try {
113: if (!hasStarted()) {
114: this .bean = (BeanT) context.getCurrentState().target;
115: this .acc = acc;
116: this .lister = lister;
117: this .pack = lister.startPacking(bean, acc);
118: }
119:
120: lister.addToPack(pack, value);
121: } catch (AccessorException e) {
122: Loader.handleGenericException(e, true);
123: // recover from this error by ignoring future items.
124: this .lister = Lister.getErrorInstance();
125: this .acc = Accessor.getErrorInstance();
126: }
127: }
128:
129: /**
130: * Starts the packing scope, without adding any item.
131: *
132: * This allows us to return an empty pack, thereby allowing the user
133: * to distinguish empty array vs null array.
134: */
135: public void start(Accessor<BeanT, PropT> acc,
136: Lister<BeanT, PropT, ItemT, PackT> lister)
137: throws SAXException {
138: try {
139: if (!hasStarted()) {
140: this .bean = (BeanT) context.getCurrentState().target;
141: this .acc = acc;
142: this .lister = lister;
143: this .pack = lister.startPacking(bean, acc);
144: }
145: } catch (AccessorException e) {
146: Loader.handleGenericException(e, true);
147: // recover from this error by ignoring future items.
148: this.lister = Lister.getErrorInstance();
149: this.acc = Accessor.getErrorInstance();
150: }
151: }
152: }
|