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.Constructor;
042: import java.lang.reflect.Modifier;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.LinkedHashSet;
047: import java.util.LinkedList;
048: import java.util.List;
049: import java.util.Queue;
050: import java.util.Set;
051:
052: public class XmlListCollectionProperty extends CDataProperty {
053: private final CDataProperty _componentProperty;
054: private final Constructor _collectionConstructor;
055:
056: public XmlListCollectionProperty(CDataProperty componentProperty,
057: Class collectionType) throws JAXBException {
058: _componentProperty = componentProperty;
059:
060: try {
061: if (collectionType.isInterface()
062: || Modifier.isAbstract(collectionType
063: .getModifiers())) {
064:
065: if (List.class.isAssignableFrom(collectionType))
066: _collectionConstructor = ArrayList.class
067: .getConstructor();
068:
069: else if (Set.class.isAssignableFrom(collectionType))
070: _collectionConstructor = LinkedHashSet.class
071: .getConstructor();
072:
073: else if (Queue.class.isAssignableFrom(collectionType))
074: _collectionConstructor = LinkedList.class
075: .getConstructor();
076:
077: else if (Collection.class
078: .isAssignableFrom(collectionType))
079: _collectionConstructor = ArrayList.class
080: .getConstructor();
081:
082: else
083: _collectionConstructor = null;
084: } else
085: _collectionConstructor = collectionType
086: .getConstructor();
087: } catch (Exception e) {
088: throw new JAXBException(e);
089: }
090:
091: if (_collectionConstructor == null)
092: throw new JAXBException(
093: L
094: .l(
095: "Cannot instantiate interface or abstract class: {0}",
096: collectionType));
097: }
098:
099: public String write(Object in) throws IOException, JAXBException {
100: if (in == null)
101: return "";
102:
103: StringBuilder sb = new StringBuilder();
104:
105: for (Object o : (Collection) in) {
106: sb.append(_componentProperty.write(o));
107: sb.append(' ');
108: }
109:
110: if (sb.length() > 0)
111: sb.deleteCharAt(sb.length() - 1);
112:
113: return sb.toString();
114: }
115:
116: protected Object read(String in) throws IOException, JAXBException {
117: String[] tokens = in.split("\\s+");
118:
119: try {
120: Collection collection = (Collection) _collectionConstructor
121: .newInstance();
122:
123: for (int i = 0; i < tokens.length; i++)
124: collection.add(_componentProperty.read(tokens[i]));
125:
126: return collection;
127: } catch (JAXBException e) {
128: throw e;
129: } catch (Exception e) {
130: throw new JAXBException(e);
131: }
132: }
133:
134: public QName getSchemaType() {
135: return _componentProperty.getSchemaType();
136: }
137: }
|