001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime.property;
027:
028: import java.io.IOException;
029:
030: import javax.xml.stream.XMLStreamException;
031:
032: import com.sun.xml.internal.bind.api.AccessorException;
033: import com.sun.xml.internal.bind.v2.util.QNameMap;
034: import com.sun.xml.internal.bind.v2.model.core.ElementPropertyInfo;
035: import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
036: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo;
037: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeRef;
038: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
039: import com.sun.xml.internal.bind.v2.runtime.Name;
040: import com.sun.xml.internal.bind.v2.runtime.Transducer;
041: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
042: import com.sun.xml.internal.bind.v2.runtime.reflect.ListTransducedAccessorImpl;
043: import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor;
044: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
045: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LeafPropertyLoader;
046:
047: import org.xml.sax.SAXException;
048:
049: /**
050: * {@link Property} implementation for {@link ElementPropertyInfo} whose
051: * {@link ElementPropertyInfo#isValueList()} is true.
052: *
053: * @author Kohsuke Kawaguchi
054: */
055: final class ListElementProperty<BeanT, ListT, ItemT> extends
056: ArrayProperty<BeanT, ListT, ItemT> {
057:
058: private final Name tagName;
059: /**
060: * Converts all the values to a list and back.
061: */
062: private final TransducedAccessor<BeanT> xacc;
063:
064: public ListElementProperty(JAXBContextImpl grammar,
065: RuntimeElementPropertyInfo prop) {
066: super (grammar, prop);
067:
068: assert prop.isValueList();
069: assert prop.getTypes().size() == 1; // required by the contract of isValueList
070: RuntimeTypeRef ref = prop.getTypes().get(0);
071:
072: tagName = grammar.nameBuilder.createElementName(ref
073: .getTagName());
074:
075: // transducer for each item
076: Transducer xducer = ref.getTransducer();
077: // transduced accessor for the whole thing
078: xacc = new ListTransducedAccessorImpl(xducer, acc, lister);
079: }
080:
081: public PropertyKind getKind() {
082: return PropertyKind.ELEMENT;
083: }
084:
085: public void buildChildElementUnmarshallers(UnmarshallerChain chain,
086: QNameMap<ChildLoader> handlers) {
087: handlers.put(tagName, new ChildLoader(new LeafPropertyLoader(
088: xacc), null));
089: }
090:
091: public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer)
092: throws SAXException, AccessorException, IOException,
093: XMLStreamException {
094: ListT list = acc.get(o);
095:
096: if (list != null) {
097: if (xacc.useNamespace()) {
098: w.startElement(tagName, null);
099: xacc.declareNamespace(o, w);
100: w.endNamespaceDecls(list);
101: w.endAttributes();
102: xacc.writeText(w, o, fieldName);
103: w.endElement();
104: } else {
105: xacc.writeLeafElement(w, tagName, o, fieldName);
106: }
107: }
108: }
109: }
|