001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Adam Megacz
028: */
029:
030: package com.caucho.jaxb.property;
031:
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Node;
034:
035: import com.caucho.jaxb.BinderImpl;
036: import com.caucho.jaxb.JAXBUtil;
037: import com.caucho.jaxb.NodeIterator;
038: import com.caucho.jaxb.mapping.Namer;
039: import com.caucho.util.L10N;
040:
041: import javax.xml.bind.JAXBException;
042: import javax.xml.bind.Marshaller;
043: import javax.xml.bind.Unmarshaller;
044: import javax.xml.namespace.QName;
045: import javax.xml.stream.XMLStreamException;
046: import javax.xml.stream.XMLStreamReader;
047: import javax.xml.stream.XMLStreamWriter;
048:
049: import java.io.IOException;
050:
051: import java.util.ArrayList;
052: import java.util.Collection;
053:
054: /**
055: * a Collection Property
056: */
057: public class CollectionProperty extends IterableProperty {
058: public static final L10N L = new L10N(CollectionProperty.class);
059:
060: public CollectionProperty(Property componentProperty) {
061: _componentProperty = componentProperty;
062: }
063:
064: public Object read(Unmarshaller u, XMLStreamReader in,
065: Object previous) throws IOException, XMLStreamException,
066: JAXBException {
067: Collection collection = (Collection) previous;
068:
069: if (collection == null)
070: collection = createCollection();
071:
072: collection.add(_componentProperty.read(u, in, null));
073:
074: return collection;
075: }
076:
077: public Object bindFrom(BinderImpl binder, NodeIterator node,
078: Object previous) throws IOException, JAXBException {
079: Node child = node.getNode();
080:
081: Collection collection = (Collection) previous;
082:
083: collection.add(_componentProperty.bindFrom(binder, node, null));
084:
085: return collection;
086: }
087:
088: public void write(Marshaller m, XMLStreamWriter out, Object value,
089: Namer namer) throws IOException, XMLStreamException,
090: JAXBException {
091: if (value != null) {
092: validateType(value);
093:
094: Collection collection = (Collection) value;
095:
096: for (Object o : collection)
097: _componentProperty.write(m, out, o, namer);
098: }
099: }
100:
101: public Node bindTo(BinderImpl binder, Node node, Object value,
102: Namer namer) throws IOException, JAXBException {
103: if (value != null) {
104: validateType(value);
105:
106: Collection collection = (Collection) value;
107:
108: Node child = node.getFirstChild();
109: for (Object o : collection) {
110: if (child != null) {
111: // try to reuse as many of the child nodes as possible
112: Node newNode = _componentProperty.bindTo(binder,
113: child, o, namer);
114:
115: if (newNode != child) {
116: node.replaceChild(child, newNode);
117: binder.invalidate(child);
118: }
119:
120: child = child.getNextSibling();
121: node = JAXBUtil.skipIgnorableNodes(node);
122: } else {
123: QName qname = namer.getQName(value);
124: Node newNode = JAXBUtil.elementFromQName(qname,
125: node);
126: newNode = _componentProperty.bindTo(binder,
127: newNode, o, namer);
128:
129: node.appendChild(newNode);
130: }
131: }
132: }
133:
134: return node;
135: }
136:
137: public QName getSchemaType() {
138: return _componentProperty.getSchemaType();
139: }
140:
141: public boolean isXmlPrimitiveType() {
142: return getComponentProperty().isXmlPrimitiveType();
143: }
144:
145: public String getMaxOccurs() {
146: return "unbounded";
147: }
148:
149: public boolean isNullable() {
150: return true;
151: }
152:
153: protected Collection createCollection() {
154: return new ArrayList();
155: }
156:
157: protected void validateType(Object obj) {
158: if (!(obj instanceof Collection))
159: throw new ClassCastException(L.l(
160: "Argument is not a Collection: {0}", obj));
161: }
162: }
|