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.reflect;
038:
039: import javax.xml.bind.JAXBException;
040:
041: import com.sun.xml.bind.WhiteSpaceProcessor;
042: import com.sun.xml.bind.api.AccessorException;
043: import com.sun.xml.bind.v2.runtime.Transducer;
044: import com.sun.xml.bind.v2.runtime.XMLSerializer;
045:
046: import org.xml.sax.SAXException;
047:
048: /**
049: * {@link TransducedAccessor} for a list simple type.
050: *
051: * @author Kohsuke Kawaguchi
052: */
053: public final class ListTransducedAccessorImpl<BeanT, ListT, ItemT, PackT>
054: extends DefaultTransducedAccessor<BeanT> {
055: /**
056: * {@link Transducer} for each item type.
057: */
058: private final Transducer<ItemT> xducer;
059: /**
060: * {@link Lister} for handling list of tokens.
061: */
062: private final Lister<BeanT, ListT, ItemT, PackT> lister;
063: /**
064: * {@link Accessor} to get/set the list.
065: */
066: private final Accessor<BeanT, ListT> acc;
067:
068: public ListTransducedAccessorImpl(Transducer<ItemT> xducer,
069: Accessor<BeanT, ListT> acc,
070: Lister<BeanT, ListT, ItemT, PackT> lister) {
071: this .xducer = xducer;
072: this .lister = lister;
073: this .acc = acc;
074: }
075:
076: public boolean useNamespace() {
077: return xducer.useNamespace();
078: }
079:
080: public void declareNamespace(BeanT bean, XMLSerializer w)
081: throws AccessorException, SAXException {
082: ListT list = acc.get(bean);
083:
084: if (list != null) {
085: ListIterator<ItemT> itr = lister.iterator(list, w);
086:
087: while (itr.hasNext()) {
088: try {
089: ItemT item = itr.next();
090: if (item != null) {
091: xducer.declareNamespace(item, w);
092: }
093: } catch (JAXBException e) {
094: w.reportError(null, e);
095: }
096: }
097: }
098: }
099:
100: // TODO: this is inefficient, consider a redesign
101: // perhaps we should directly write to XMLSerializer,
102: // or maybe add more methods like writeLeafElement.
103: public String print(BeanT o) throws AccessorException, SAXException {
104: ListT list = acc.get(o);
105:
106: if (list == null)
107: return null;
108:
109: StringBuilder buf = new StringBuilder();
110: XMLSerializer w = XMLSerializer.getInstance();
111: ListIterator<ItemT> itr = lister.iterator(list, w);
112:
113: while (itr.hasNext()) {
114: try {
115: ItemT item = itr.next();
116: if (item != null) {
117: if (buf.length() > 0)
118: buf.append(' ');
119: buf.append(xducer.print(item));
120: }
121: } catch (JAXBException e) {
122: w.reportError(null, e);
123: }
124: }
125: return buf.toString();
126: }
127:
128: private void processValue(BeanT bean, CharSequence s)
129: throws AccessorException, SAXException {
130: PackT pack = lister.startPacking(bean, acc);
131:
132: int idx = 0;
133: int len = s.length();
134:
135: while (true) {
136: int p = idx;
137: while (p < len
138: && !WhiteSpaceProcessor.isWhiteSpace(s.charAt(p)))
139: p++;
140:
141: CharSequence token = s.subSequence(idx, p);
142: if (!token.equals(""))
143: lister.addToPack(pack, xducer.parse(token));
144:
145: if (p == len)
146: break; // done
147:
148: while (p < len
149: && WhiteSpaceProcessor.isWhiteSpace(s.charAt(p)))
150: p++;
151: if (p == len)
152: break; // done
153:
154: idx = p;
155: }
156:
157: lister.endPacking(pack, bean, acc);
158: }
159:
160: public void parse(BeanT bean, CharSequence lexical)
161: throws AccessorException, SAXException {
162: processValue(bean, lexical);
163: }
164:
165: public boolean hasValue(BeanT bean) throws AccessorException {
166: return acc.get(bean) != null;
167: }
168: }
|