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