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.property;
038:
039: import java.io.IOException;
040:
041: import javax.xml.stream.XMLStreamException;
042:
043: import com.sun.xml.bind.api.AccessorException;
044: import com.sun.xml.bind.v2.util.QNameMap;
045: import com.sun.xml.bind.v2.model.core.ElementPropertyInfo;
046: import com.sun.xml.bind.v2.model.core.PropertyKind;
047: import com.sun.xml.bind.v2.model.runtime.RuntimeElementPropertyInfo;
048: import com.sun.xml.bind.v2.model.runtime.RuntimeTypeRef;
049: import com.sun.xml.bind.v2.runtime.JAXBContextImpl;
050: import com.sun.xml.bind.v2.runtime.Name;
051: import com.sun.xml.bind.v2.runtime.Transducer;
052: import com.sun.xml.bind.v2.runtime.XMLSerializer;
053: import com.sun.xml.bind.v2.runtime.reflect.ListTransducedAccessorImpl;
054: import com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor;
055: import com.sun.xml.bind.v2.runtime.unmarshaller.ChildLoader;
056: import com.sun.xml.bind.v2.runtime.unmarshaller.LeafPropertyLoader;
057:
058: import org.xml.sax.SAXException;
059:
060: /**
061: * {@link Property} implementation for {@link ElementPropertyInfo} whose
062: * {@link ElementPropertyInfo#isValueList()} is true.
063: *
064: * @author Kohsuke Kawaguchi
065: */
066: final class ListElementProperty<BeanT, ListT, ItemT> extends
067: ArrayProperty<BeanT, ListT, ItemT> {
068:
069: private final Name tagName;
070: /**
071: * Converts all the values to a list and back.
072: */
073: private final TransducedAccessor<BeanT> xacc;
074:
075: public ListElementProperty(JAXBContextImpl grammar,
076: RuntimeElementPropertyInfo prop) {
077: super (grammar, prop);
078:
079: assert prop.isValueList();
080: assert prop.getTypes().size() == 1; // required by the contract of isValueList
081: RuntimeTypeRef ref = prop.getTypes().get(0);
082:
083: tagName = grammar.nameBuilder.createElementName(ref
084: .getTagName());
085:
086: // transducer for each item
087: Transducer xducer = ref.getTransducer();
088: // transduced accessor for the whole thing
089: xacc = new ListTransducedAccessorImpl(xducer, acc, lister);
090: }
091:
092: public PropertyKind getKind() {
093: return PropertyKind.ELEMENT;
094: }
095:
096: public void buildChildElementUnmarshallers(UnmarshallerChain chain,
097: QNameMap<ChildLoader> handlers) {
098: handlers.put(tagName, new ChildLoader(new LeafPropertyLoader(
099: xacc), null));
100: }
101:
102: public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer)
103: throws SAXException, AccessorException, IOException,
104: XMLStreamException {
105: ListT list = acc.get(o);
106:
107: if (list != null) {
108: if (xacc.useNamespace()) {
109: w.startElement(tagName, null);
110: xacc.declareNamespace(o, w);
111: w.endNamespaceDecls(list);
112: w.endAttributes();
113: xacc.writeText(w, o, fieldName);
114: w.endElement();
115: } else {
116: xacc.writeLeafElement(w, tagName, o, fieldName);
117: }
118: }
119: }
120: }
|