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.DatatypeConverter;
033: import javax.xml.bind.JAXBException;
034: import javax.xml.bind.Marshaller;
035: import javax.xml.namespace.QName;
036: import javax.xml.stream.XMLStreamException;
037: import javax.xml.stream.XMLStreamWriter;
038:
039: import java.io.IOException;
040:
041: import java.lang.reflect.Array;
042:
043: import java.util.Collection;
044:
045: public class XmlListArrayProperty<T> extends CDataProperty {
046: private final CDataProperty _componentProperty;
047: private final Class<T> _type;
048:
049: protected XmlListArrayProperty(CDataProperty componentProperty) {
050: _componentProperty = componentProperty;
051: _type = null;
052: }
053:
054: private XmlListArrayProperty(CDataProperty componentProperty,
055: Class<T> type) {
056: _componentProperty = componentProperty;
057: _type = type;
058: }
059:
060: public static XmlListArrayProperty createXmlListArrayProperty(
061: CDataProperty componentProperty, Class type)
062: throws JAXBException {
063: if (boolean.class.equals(type))
064: return new XmlListBooleanArrayProperty();
065: else if (byte.class.equals(type))
066: throw new JAXBException(
067: L
068: .l("@XmlList applied to byte[] valued fields or properties"));
069: else if (char.class.equals(type))
070: return new XmlListCharacterArrayProperty();
071: else if (double.class.equals(type))
072: return new XmlListDoubleArrayProperty();
073: else if (float.class.equals(type))
074: return new XmlListFloatArrayProperty();
075: else if (int.class.equals(type))
076: return new XmlListIntegerArrayProperty();
077: else if (long.class.equals(type))
078: return new XmlListLongArrayProperty();
079: else if (short.class.equals(type))
080: return new XmlListShortArrayProperty();
081: else
082: return new XmlListArrayProperty(componentProperty, type);
083: }
084:
085: public String write(Object in) throws IOException, JAXBException {
086: if (in == null)
087: return "";
088:
089: StringBuilder sb = new StringBuilder();
090:
091: int length = Array.getLength(in);
092:
093: for (int i = 0; i < length; i++) {
094: sb.append(_componentProperty.write(Array.get(in, i)));
095:
096: if (i != length - 1)
097: sb.append(' ');
098: }
099:
100: return sb.toString();
101: }
102:
103: protected Object read(String in) throws IOException, JAXBException {
104: String[] tokens = in.split("\\s+");
105:
106: T[] array = (T[]) Array.newInstance(_type, tokens.length);
107:
108: for (int i = 0; i < tokens.length; i++)
109: array[i] = (T) _componentProperty.read(tokens[i]);
110:
111: return array;
112: }
113:
114: public QName getSchemaType() {
115: return _componentProperty.getSchemaType();
116: }
117: }
|