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: * aint 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 Emil Ong
028: */
029:
030: package com.caucho.jaxb.property;
031:
032: import javax.xml.bind.JAXBException;
033: import javax.xml.bind.Marshaller;
034: import javax.xml.bind.Unmarshaller;
035: import javax.xml.namespace.QName;
036: import javax.xml.stream.XMLStreamException;
037: import javax.xml.stream.XMLStreamReader;
038: import javax.xml.stream.XMLStreamWriter;
039: import java.io.IOException;
040: import java.lang.reflect.Array;
041: import java.util.ArrayList;
042:
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Node;
045:
046: import com.caucho.jaxb.BinderImpl;
047: import com.caucho.jaxb.JAXBUtil;
048: import com.caucho.jaxb.NodeIterator;
049: import com.caucho.jaxb.mapping.Namer;
050: import com.caucho.util.L10N;
051:
052: /**
053: * a property for serializing/deserializing arrays
054: */
055: public class ObjectArrayProperty<T> extends ArrayProperty {
056: private static final L10N L = new L10N(ObjectArrayProperty.class);
057:
058: private Class<T> _type;
059:
060: public ObjectArrayProperty(Property componentProperty, Class<T> type) {
061: super (componentProperty);
062: _type = type;
063: }
064:
065: public Object read(Unmarshaller u, XMLStreamReader in,
066: Object previous) throws IOException, XMLStreamException,
067: JAXBException {
068: T[] array = (T[]) previous;
069:
070: if (array == null)
071: array = (T[]) Array.newInstance(_type, 1);
072: else {
073: T[] newArray = (T[]) Array.newInstance(_type,
074: array.length + 1);
075: System.arraycopy(array, 0, newArray, 0, array.length);
076:
077: array = newArray;
078: }
079:
080: array[array.length - 1] = (T) _componentProperty.read(u, in,
081: null);
082:
083: return array;
084: }
085:
086: public Object bindFrom(BinderImpl binder, NodeIterator node,
087: Object previous) throws IOException, JAXBException {
088: T[] array = (T[]) previous;
089:
090: if (array == null)
091: array = (T[]) Array.newInstance(_type, 1);
092: else {
093: T[] newArray = (T[]) Array.newInstance(_type,
094: array.length + 1);
095: System.arraycopy(array, 0, newArray, 0, array.length);
096:
097: array = newArray;
098: }
099:
100: array[array.length - 1] = (T) _componentProperty.bindFrom(
101: binder, node, null);
102:
103: return array;
104: }
105:
106: public void write(Marshaller m, XMLStreamWriter out, Object value,
107: Namer namer) throws IOException, XMLStreamException,
108: JAXBException {
109: if (value != null) {
110: T[] array = (T[]) value;
111:
112: for (int i = 0; i < array.length; i++)
113: _componentProperty.write(m, out, array[i], namer);
114: }
115: }
116:
117: public Node bindTo(BinderImpl binder, Node node, Object value,
118: Namer namer) throws IOException, JAXBException {
119: QName qname = namer.getQName(value);
120: QName name = JAXBUtil.qnameFromNode(node);
121: Document doc = node.getOwnerDocument();
122:
123: if (!name.equals(qname))
124: node = JAXBUtil.elementFromQName(qname, doc);
125:
126: binder.bind(value, node);
127:
128: if (value != null) {
129: T[] array = (T[]) value;
130:
131: for (int i = 0; i < array.length; i++) {
132: Node child = JAXBUtil.elementFromQName(qname, doc);
133: node.appendChild(_componentProperty.bindTo(binder,
134: child, array[i], namer));
135: }
136: }
137:
138: return node;
139: }
140: }
|