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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     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.reflect;
    28 import javax.xml.bind.JAXBException;
    30 import com.sun.xml.internal.bind.WhiteSpaceProcessor;
    31 import com.sun.xml.internal.bind.api.AccessorException;
    32 import com.sun.xml.internal.bind.v2.runtime.Transducer;
    33 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
    35 import org.xml.sax.SAXException;
    37 /**
    38  * {@link TransducedAccessor} for a list simple type.
    39  *
    40  * @author Kohsuke Kawaguchi
    41  */
    42 public final class ListTransducedAccessorImpl<BeanT,ListT,ItemT,PackT> extends DefaultTransducedAccessor<BeanT> {
    43     /**
    44      * {@link Transducer} for each item type.
    45      */
    46     private final Transducer<ItemT> xducer;
    47     /**
    48      * {@link Lister} for handling list of tokens.
    49      */
    50     private final Lister<BeanT,ListT,ItemT,PackT> lister;
    51     /**
    52      * {@link Accessor} to get/set the list.
    53      */
    54     private final Accessor<BeanT,ListT> acc;
    56     public ListTransducedAccessorImpl(Transducer<ItemT> xducer, Accessor<BeanT,ListT> acc, Lister<BeanT,ListT,ItemT,PackT> lister) {
    57         this.xducer = xducer;
    58         this.lister = lister;
    59         this.acc = acc;
    60     }
    62     public boolean useNamespace() {
    63         return xducer.useNamespace();
    64     }
    66     public void declareNamespace(BeanT bean, XMLSerializer w) throws AccessorException, SAXException {
    67         ListT list = acc.get(bean);
    69         if(list!=null) {
    70            ListIterator<ItemT> itr = lister.iterator(list, w);
    72             while(itr.hasNext()) {
    73                 try {
    74                     ItemT item = itr.next();
    75                     if (item != null) {
    76                         xducer.declareNamespace(item,w);
    77                     }
    78                 } catch (JAXBException e) {
    79                     w.reportError(null,e);
    80                 }
    81             }
    82         }
    83     }
    85     // TODO: this is inefficient, consider a redesign
    86     // perhaps we should directly write to XMLSerializer,
    87     // or maybe add more methods like writeLeafElement.
    88     public String print(BeanT o) throws AccessorException, SAXException {
    89         ListT list = acc.get(o);
    91         if(list==null)
    92             return null;
    94         StringBuilder buf = new StringBuilder();
    95         XMLSerializer w = XMLSerializer.getInstance();
    96         ListIterator<ItemT> itr = lister.iterator(list, w);
    98         while(itr.hasNext()) {
    99             try {
   100                 ItemT item = itr.next();
   101                 if (item != null) {
   102                     if(buf.length()>0)  buf.append(' ');
   103                     buf.append(xducer.print(item));
   104                 }
   105             } catch (JAXBException e) {
   106                 w.reportError(null,e);
   107             }
   108         }
   109         return buf.toString();
   110     }
   112     private void processValue(BeanT bean, CharSequence s) throws AccessorException, SAXException {
   113         PackT pack = lister.startPacking(bean,acc);
   115         int idx = 0;
   116         int len = s.length();
   118         while(true) {
   119             int p = idx;
   120             while( p<len && !WhiteSpaceProcessor.isWhiteSpace(s.charAt(p)) )
   121                 p++;
   123             CharSequence token = s.subSequence(idx,p);
   124             if (!token.equals(""))
   125                 lister.addToPack(pack,xducer.parse(token));
   127             if(p==len)      break;  // done
   129             while( p<len && WhiteSpaceProcessor.isWhiteSpace(s.charAt(p)) )
   130                 p++;
   131             if(p==len)      break;  // done
   133             idx = p;
   134         }
   136         lister.endPacking(pack,bean,acc);
   137     }
   139     public void parse(BeanT bean, CharSequence lexical) throws AccessorException, SAXException {
   140         processValue(bean,lexical);
   141     }
   143     public boolean hasValue(BeanT bean) throws AccessorException {
   144         return acc.get(bean)!=null;
   145     }
   146 }

mercurial