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 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 DoubleArrayProperty extends ArrayProperty {
056: private static final L10N L = new L10N(DoubleArrayProperty.class);
057:
058: public static final DoubleArrayProperty PROPERTY = new DoubleArrayProperty();
059:
060: public DoubleArrayProperty() {
061: super (DoubleProperty.PRIMITIVE_PROPERTY);
062: }
063:
064: public Object read(Unmarshaller u, XMLStreamReader in,
065: Object previous) throws IOException, XMLStreamException,
066: JAXBException {
067: double[] array = (double[]) previous;
068:
069: if (array == null)
070: array = new double[1];
071: else {
072: double[] newArray = new double[array.length + 1];
073: System.arraycopy(array, 0, newArray, 0, array.length);
074:
075: array = newArray;
076: }
077:
078: array[array.length - 1] = ((Double) _componentProperty.read(u,
079: in, null)).doubleValue();
080:
081: return array;
082: }
083:
084: public Object bindFrom(BinderImpl binder, NodeIterator node,
085: Object previous) throws IOException, JAXBException {
086: double[] array = (double[]) previous;
087:
088: if (array == null)
089: array = new double[1];
090: else {
091: double[] newArray = new double[array.length + 1];
092: System.arraycopy(array, 0, newArray, 0, array.length);
093:
094: array = newArray;
095: }
096:
097: Double b = (Double) _componentProperty.bindFrom(binder, node,
098: null);
099:
100: array[array.length - 1] = b.doubleValue();
101:
102: return array;
103: }
104:
105: public void write(Marshaller m, XMLStreamWriter out, Object value,
106: Namer namer) throws IOException, XMLStreamException,
107: JAXBException {
108: if (value != null) {
109: double[] array = (double[]) value;
110:
111: for (int i = 0; i < array.length; i++)
112: DoubleProperty.PRIMITIVE_PROPERTY.write(m, out,
113: 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: double[] array = (double[]) value;
130:
131: for (int i = 0; i < array.length; i++) {
132: Node child = JAXBUtil.elementFromQName(qname, doc);
133: node.appendChild(DoubleProperty.PRIMITIVE_PROPERTY
134: .bindTo(binder, child, array[i], namer));
135: }
136: }
137:
138: return node;
139: }
140: }
|