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

changeset 0
373ffda63c9a
child 650
121e938cb9c3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/Scope.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,136 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
    1.30 +
    1.31 +import com.sun.xml.internal.bind.api.AccessorException;
    1.32 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
    1.33 +import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
    1.34 +
    1.35 +import org.xml.sax.SAXException;
    1.36 +
    1.37 +/**
    1.38 + * Holds the information about packing scope.
    1.39 + *
    1.40 + * <p>
    1.41 + * When no packing is started yet, all the fields should be set to null.
    1.42 + *
    1.43 + * @author Kohsuke Kawaguchi
    1.44 + */
    1.45 +public final class Scope<BeanT,PropT,ItemT,PackT> {
    1.46 +
    1.47 +    public final UnmarshallingContext context;
    1.48 +
    1.49 +    private BeanT bean;
    1.50 +    private Accessor<BeanT,PropT> acc;
    1.51 +    private PackT pack;
    1.52 +    private Lister<BeanT,PropT,ItemT,PackT> lister;
    1.53 +
    1.54 +    Scope(UnmarshallingContext context) {
    1.55 +        this.context = context;
    1.56 +    }
    1.57 +
    1.58 +    /**
    1.59 +     * Returns true if this scope object is filled by a packing in progress.
    1.60 +     */
    1.61 +    public boolean hasStarted() {
    1.62 +        return bean!=null;
    1.63 +    }
    1.64 +
    1.65 +    /**
    1.66 +     * Initializes all the fields to null.
    1.67 +     */
    1.68 +    public void reset() {
    1.69 +        if(bean==null) {
    1.70 +            // already initialized
    1.71 +            assert clean();
    1.72 +            return;
    1.73 +        }
    1.74 +
    1.75 +        bean = null;
    1.76 +        acc = null;
    1.77 +        pack = null;
    1.78 +        lister = null;
    1.79 +    }
    1.80 +
    1.81 +    /**
    1.82 +     * Finishes up the current packing in progress (if any) and
    1.83 +     * resets this object.
    1.84 +     */
    1.85 +    public void finish() throws AccessorException {
    1.86 +        if(hasStarted()) {
    1.87 +            lister.endPacking(pack,bean,acc);
    1.88 +            reset();
    1.89 +        }
    1.90 +        assert clean();
    1.91 +    }
    1.92 +
    1.93 +    private boolean clean() {
    1.94 +        return bean==null && acc==null && pack==null && lister==null;
    1.95 +    }
    1.96 +
    1.97 +    /**
    1.98 +     * Adds a new item to this packing scope.
    1.99 +     */
   1.100 +    public void add( Accessor<BeanT,PropT> acc, Lister<BeanT,PropT,ItemT,PackT> lister, ItemT value) throws SAXException{
   1.101 +        try {
   1.102 +            if(!hasStarted()) {
   1.103 +                this.bean = (BeanT)context.getCurrentState().target;
   1.104 +                this.acc = acc;
   1.105 +                this.lister = lister;
   1.106 +                this.pack = lister.startPacking(bean,acc);
   1.107 +            }
   1.108 +
   1.109 +            lister.addToPack(pack,value);
   1.110 +        } catch (AccessorException e) {
   1.111 +            Loader.handleGenericException(e,true);
   1.112 +            // recover from this error by ignoring future items.
   1.113 +            this.lister = Lister.getErrorInstance();
   1.114 +            this.acc = Accessor.getErrorInstance();
   1.115 +        }
   1.116 +    }
   1.117 +
   1.118 +    /**
   1.119 +     * Starts the packing scope, without adding any item.
   1.120 +     *
   1.121 +     * This allows us to return an empty pack, thereby allowing the user
   1.122 +     * to distinguish empty array vs null array.
   1.123 +     */
   1.124 +    public void start( Accessor<BeanT,PropT> acc, Lister<BeanT,PropT,ItemT,PackT> lister) throws SAXException{
   1.125 +        try {
   1.126 +            if(!hasStarted()) {
   1.127 +                this.bean = (BeanT)context.getCurrentState().target;
   1.128 +                this.acc = acc;
   1.129 +                this.lister = lister;
   1.130 +                this.pack = lister.startPacking(bean,acc);
   1.131 +            }
   1.132 +        } catch (AccessorException e) {
   1.133 +            Loader.handleGenericException(e,true);
   1.134 +            // recover from this error by ignoring future items.
   1.135 +            this.lister = Lister.getErrorInstance();
   1.136 +            this.acc = Accessor.getErrorInstance();
   1.137 +        }
   1.138 +    }
   1.139 +}

mercurial