src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/reflect/ListTransducedAccessorImpl.java

changeset 0
373ffda63c9a
     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/reflect/ListTransducedAccessorImpl.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,146 @@
     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.reflect;
    1.30 +
    1.31 +import javax.xml.bind.JAXBException;
    1.32 +
    1.33 +import com.sun.xml.internal.bind.WhiteSpaceProcessor;
    1.34 +import com.sun.xml.internal.bind.api.AccessorException;
    1.35 +import com.sun.xml.internal.bind.v2.runtime.Transducer;
    1.36 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
    1.37 +
    1.38 +import org.xml.sax.SAXException;
    1.39 +
    1.40 +/**
    1.41 + * {@link TransducedAccessor} for a list simple type.
    1.42 + *
    1.43 + * @author Kohsuke Kawaguchi
    1.44 + */
    1.45 +public final class ListTransducedAccessorImpl<BeanT,ListT,ItemT,PackT> extends DefaultTransducedAccessor<BeanT> {
    1.46 +    /**
    1.47 +     * {@link Transducer} for each item type.
    1.48 +     */
    1.49 +    private final Transducer<ItemT> xducer;
    1.50 +    /**
    1.51 +     * {@link Lister} for handling list of tokens.
    1.52 +     */
    1.53 +    private final Lister<BeanT,ListT,ItemT,PackT> lister;
    1.54 +    /**
    1.55 +     * {@link Accessor} to get/set the list.
    1.56 +     */
    1.57 +    private final Accessor<BeanT,ListT> acc;
    1.58 +
    1.59 +    public ListTransducedAccessorImpl(Transducer<ItemT> xducer, Accessor<BeanT,ListT> acc, Lister<BeanT,ListT,ItemT,PackT> lister) {
    1.60 +        this.xducer = xducer;
    1.61 +        this.lister = lister;
    1.62 +        this.acc = acc;
    1.63 +    }
    1.64 +
    1.65 +    public boolean useNamespace() {
    1.66 +        return xducer.useNamespace();
    1.67 +    }
    1.68 +
    1.69 +    public void declareNamespace(BeanT bean, XMLSerializer w) throws AccessorException, SAXException {
    1.70 +        ListT list = acc.get(bean);
    1.71 +
    1.72 +        if(list!=null) {
    1.73 +           ListIterator<ItemT> itr = lister.iterator(list, w);
    1.74 +
    1.75 +            while(itr.hasNext()) {
    1.76 +                try {
    1.77 +                    ItemT item = itr.next();
    1.78 +                    if (item != null) {
    1.79 +                        xducer.declareNamespace(item,w);
    1.80 +                    }
    1.81 +                } catch (JAXBException e) {
    1.82 +                    w.reportError(null,e);
    1.83 +                }
    1.84 +            }
    1.85 +        }
    1.86 +    }
    1.87 +
    1.88 +    // TODO: this is inefficient, consider a redesign
    1.89 +    // perhaps we should directly write to XMLSerializer,
    1.90 +    // or maybe add more methods like writeLeafElement.
    1.91 +    public String print(BeanT o) throws AccessorException, SAXException {
    1.92 +        ListT list = acc.get(o);
    1.93 +
    1.94 +        if(list==null)
    1.95 +            return null;
    1.96 +
    1.97 +        StringBuilder buf = new StringBuilder();
    1.98 +        XMLSerializer w = XMLSerializer.getInstance();
    1.99 +        ListIterator<ItemT> itr = lister.iterator(list, w);
   1.100 +
   1.101 +        while(itr.hasNext()) {
   1.102 +            try {
   1.103 +                ItemT item = itr.next();
   1.104 +                if (item != null) {
   1.105 +                    if(buf.length()>0)  buf.append(' ');
   1.106 +                    buf.append(xducer.print(item));
   1.107 +                }
   1.108 +            } catch (JAXBException e) {
   1.109 +                w.reportError(null,e);
   1.110 +            }
   1.111 +        }
   1.112 +        return buf.toString();
   1.113 +    }
   1.114 +
   1.115 +    private void processValue(BeanT bean, CharSequence s) throws AccessorException, SAXException {
   1.116 +        PackT pack = lister.startPacking(bean,acc);
   1.117 +
   1.118 +        int idx = 0;
   1.119 +        int len = s.length();
   1.120 +
   1.121 +        while(true) {
   1.122 +            int p = idx;
   1.123 +            while( p<len && !WhiteSpaceProcessor.isWhiteSpace(s.charAt(p)) )
   1.124 +                p++;
   1.125 +
   1.126 +            CharSequence token = s.subSequence(idx,p);
   1.127 +            if (!token.equals(""))
   1.128 +                lister.addToPack(pack,xducer.parse(token));
   1.129 +
   1.130 +            if(p==len)      break;  // done
   1.131 +
   1.132 +            while( p<len && WhiteSpaceProcessor.isWhiteSpace(s.charAt(p)) )
   1.133 +                p++;
   1.134 +            if(p==len)      break;  // done
   1.135 +
   1.136 +            idx = p;
   1.137 +        }
   1.138 +
   1.139 +        lister.endPacking(pack,bean,acc);
   1.140 +    }
   1.141 +
   1.142 +    public void parse(BeanT bean, CharSequence lexical) throws AccessorException, SAXException {
   1.143 +        processValue(bean,lexical);
   1.144 +    }
   1.145 +
   1.146 +    public boolean hasValue(BeanT bean) throws AccessorException {
   1.147 +        return acc.get(bean)!=null;
   1.148 +    }
   1.149 +}

mercurial