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;
038:
039: import java.io.IOException;
040: import java.lang.reflect.Array;
041: import java.util.List;
042:
043: import javax.xml.bind.JAXBException;
044: import javax.xml.bind.ValidationEvent;
045: import javax.xml.bind.helpers.ValidationEventImpl;
046: import javax.xml.stream.XMLStreamException;
047:
048: import com.sun.istack.FinalArrayList;
049: import com.sun.xml.bind.WhiteSpaceProcessor;
050: import com.sun.xml.bind.api.AccessorException;
051: import com.sun.xml.bind.v2.runtime.unmarshaller.Loader;
052: import com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext;
053:
054: import org.xml.sax.SAXException;
055:
056: /**
057: * {@link JaxBeanInfo} implementation that binds T[] to a list of simple types.
058: *
059: * @author Kohsuke Kawaguchi
060: */
061: final class ValueListBeanInfoImpl extends JaxBeanInfo {
062:
063: private final Class itemType;
064: private final Transducer xducer; // for items
065:
066: public ValueListBeanInfoImpl(JAXBContextImpl owner, Class arrayType)
067: throws JAXBException {
068: super (owner, null, arrayType, false, true, false);
069: this .itemType = jaxbType.getComponentType();
070: this .xducer = owner.getBeanInfo(arrayType.getComponentType(),
071: true).getTransducer();
072: assert xducer != null;
073: }
074:
075: private final Loader loader = new Loader(true) {
076: @Override
077: public void text(UnmarshallingContext.State state,
078: CharSequence text) throws SAXException {
079: List<Object> r = new FinalArrayList<Object>();
080:
081: int idx = 0;
082: int len = text.length();
083:
084: while (true) {
085: int p = idx;
086: while (p < len
087: && !WhiteSpaceProcessor.isWhiteSpace(text
088: .charAt(p)))
089: p++;
090:
091: CharSequence token = text.subSequence(idx, p);
092: if (!token.equals(""))
093: try {
094: r.add(xducer.parse(token));
095: } catch (AccessorException e) {
096: handleGenericException(e, true);
097: continue; // move on to next
098: }
099:
100: if (p == len)
101: break; // done
102:
103: while (p < len
104: && WhiteSpaceProcessor.isWhiteSpace(text
105: .charAt(p)))
106: p++;
107: if (p == len)
108: break; // done
109:
110: idx = p;
111: }
112:
113: state.target = toArray(r);
114: }
115: };
116:
117: private Object toArray(List list) {
118: int len = list.size();
119: Object array = Array.newInstance(itemType, len);
120: for (int i = 0; i < len; i++)
121: Array.set(array, i, list.get(i));
122: return array;
123: }
124:
125: public void serializeBody(Object array, XMLSerializer target)
126: throws SAXException, IOException, XMLStreamException {
127: int len = Array.getLength(array);
128: for (int i = 0; i < len; i++) {
129: Object item = Array.get(array, i);
130: try {
131: xducer.writeText(target, item, "arrayItem");
132: } catch (AccessorException e) {
133: target.reportError("arrayItem", e);
134: }
135: }
136: }
137:
138: public final void serializeURIs(Object array, XMLSerializer target)
139: throws SAXException {
140: if (xducer.useNamespace()) {
141: int len = Array.getLength(array);
142: for (int i = 0; i < len; i++) {
143: Object item = Array.get(array, i);
144: try {
145: xducer.declareNamespace(item, target);
146: } catch (AccessorException e) {
147: target.reportError("arrayItem", e);
148: }
149: }
150: }
151: }
152:
153: public final String getElementNamespaceURI(Object array) {
154: throw new UnsupportedOperationException();
155: }
156:
157: public final String getElementLocalName(Object array) {
158: throw new UnsupportedOperationException();
159: }
160:
161: public final Object createInstance(UnmarshallingContext context) {
162: throw new UnsupportedOperationException();
163: }
164:
165: public final boolean reset(Object array,
166: UnmarshallingContext context) {
167: return false;
168: }
169:
170: public final String getId(Object array, XMLSerializer target) {
171: return null;
172: }
173:
174: public final void serializeAttributes(Object array,
175: XMLSerializer target) {
176: // noop
177: }
178:
179: public final void serializeRoot(Object array, XMLSerializer target)
180: throws SAXException {
181: target.reportError(new ValidationEventImpl(
182: ValidationEvent.ERROR,
183: Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(array
184: .getClass().getName()), null, null));
185: }
186:
187: public final Transducer getTransducer() {
188: return null;
189: }
190:
191: public final Loader getLoader(JAXBContextImpl context,
192: boolean typeSubstitutionCapable) {
193: // type substitution impossible
194: return loader;
195: }
196: }
|