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.ArrayList;
042: import java.util.Collection;
043: import java.util.Collections;
044: import java.util.List;
045:
046: import javax.xml.bind.ValidationEvent;
047: import javax.xml.bind.helpers.ValidationEventImpl;
048: import javax.xml.namespace.QName;
049: import javax.xml.stream.XMLStreamException;
050:
051: import com.sun.xml.bind.v2.model.runtime.RuntimeArrayInfo;
052: import com.sun.xml.bind.v2.runtime.unmarshaller.Loader;
053: import com.sun.xml.bind.v2.runtime.unmarshaller.Receiver;
054: import com.sun.xml.bind.v2.runtime.unmarshaller.TagName;
055: import com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext;
056:
057: import org.xml.sax.SAXException;
058:
059: /**
060: * {@link JaxBeanInfo} implementation that binds T[] to a complex type
061: * with an element for each item.
062: *
063: * @author Kohsuke Kawaguchi
064: */
065: final class ArrayBeanInfoImpl extends JaxBeanInfo {
066:
067: private final Class itemType;
068: private final JaxBeanInfo itemBeanInfo;
069: private Loader loader;
070:
071: public ArrayBeanInfoImpl(JAXBContextImpl owner, RuntimeArrayInfo rai) {
072: super (owner, rai, rai.getType(), rai.getTypeName(), false,
073: true, false);
074: this .itemType = jaxbType.getComponentType();
075: this .itemBeanInfo = owner.getOrCreate(rai.getItemType());
076: }
077:
078: @Override
079: protected void link(JAXBContextImpl grammar) {
080: getLoader(grammar, false);
081: super .link(grammar);
082: }
083:
084: private final class ArrayLoader extends Loader implements Receiver {
085: public ArrayLoader(JAXBContextImpl owner) {
086: super (false);
087: itemLoader = itemBeanInfo.getLoader(owner, true);
088: }
089:
090: private final Loader itemLoader;
091:
092: @Override
093: public void startElement(UnmarshallingContext.State state,
094: TagName ea) {
095: state.target = new ArrayList();
096: }
097:
098: @Override
099: public void leaveElement(UnmarshallingContext.State state,
100: TagName ea) {
101: state.target = toArray((List) state.target);
102: }
103:
104: @Override
105: public void childElement(UnmarshallingContext.State state,
106: TagName ea) throws SAXException {
107: if (ea.matches("", "item")) {
108: state.loader = itemLoader;
109: state.receiver = this ;
110: } else {
111: super .childElement(state, ea);
112: }
113: }
114:
115: @Override
116: public Collection<QName> getExpectedChildElements() {
117: return Collections.singleton(new QName("", "item"));
118: }
119:
120: public void receive(UnmarshallingContext.State state, Object o) {
121: ((List) state.target).add(o);
122: }
123: };
124:
125: protected Object toArray(List list) {
126: int len = list.size();
127: Object array = Array.newInstance(itemType, len);
128: for (int i = 0; i < len; i++)
129: Array.set(array, i, list.get(i));
130: return array;
131: }
132:
133: public void serializeBody(Object array, XMLSerializer target)
134: throws SAXException, IOException, XMLStreamException {
135: int len = Array.getLength(array);
136: for (int i = 0; i < len; i++) {
137: Object item = Array.get(array, i);
138: // TODO: check the namespace URI.
139: target.startElement("", "item", null, null);
140: if (item == null) {
141: target.writeXsiNilTrue();
142: } else {
143: target.childAsXsiType(item, "arrayItem", itemBeanInfo);
144: }
145: target.endElement();
146: }
147: }
148:
149: public final String getElementNamespaceURI(Object array) {
150: throw new UnsupportedOperationException();
151: }
152:
153: public final String getElementLocalName(Object array) {
154: throw new UnsupportedOperationException();
155: }
156:
157: public final Object createInstance(UnmarshallingContext context) {
158: // we first create a List and then later convert it to an array
159: return new ArrayList();
160: }
161:
162: public final boolean reset(Object array,
163: UnmarshallingContext context) {
164: return false;
165: }
166:
167: public final String getId(Object array, XMLSerializer target) {
168: return null;
169: }
170:
171: public final void serializeAttributes(Object array,
172: XMLSerializer target) {
173: // noop
174: }
175:
176: public final void serializeRoot(Object array, XMLSerializer target)
177: throws SAXException, IOException, XMLStreamException {
178: target.reportError(new ValidationEventImpl(
179: ValidationEvent.ERROR,
180: Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(array
181: .getClass().getName()), null, null));
182: }
183:
184: public final void serializeURIs(Object array, XMLSerializer target) {
185: // noop
186: }
187:
188: public final Transducer getTransducer() {
189: return null;
190: }
191:
192: public final Loader getLoader(JAXBContextImpl context,
193: boolean typeSubstitutionCapable) {
194: if (loader == null)
195: loader = new ArrayLoader(context);
196:
197: // type substitution not possible
198: return loader;
199: }
200: }
|