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