001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.aegis.type.collection;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Set;
026: import java.util.Vector;
027:
028: import org.apache.cxf.aegis.Context;
029: import org.apache.cxf.aegis.DatabindingException;
030: import org.apache.cxf.aegis.type.Type;
031: import org.apache.cxf.aegis.type.basic.ArrayType;
032: import org.apache.cxf.aegis.xml.MessageReader;
033: import org.apache.cxf.aegis.xml.MessageWriter;
034:
035: public class CollectionType extends ArrayType {
036: private Type componentType;
037:
038: public CollectionType(Type componentType) {
039: super ();
040:
041: this .componentType = componentType;
042: }
043:
044: @Override
045: public Object readObject(MessageReader reader, Context context)
046: throws DatabindingException {
047: try {
048: return readCollection(reader, context);
049: } catch (IllegalArgumentException e) {
050: throw new DatabindingException("Illegal argument.", e);
051: }
052: }
053:
054: @Override
055: @SuppressWarnings("unchecked")
056: protected Collection<Object> createCollection() {
057: Collection values = null;
058:
059: if (getTypeClass().isAssignableFrom(List.class)) {
060: values = new ArrayList();
061: } else if (getTypeClass().isAssignableFrom(Set.class)) {
062: values = new HashSet();
063: } else if (getTypeClass().isAssignableFrom(Vector.class)) {
064: values = new Vector();
065: } else if (getTypeClass().isInterface()) {
066: values = new ArrayList();
067: } else {
068: try {
069: values = (Collection<Object>) getTypeClass()
070: .newInstance();
071: } catch (Exception e) {
072: throw new DatabindingException(
073: "Could not create map implementation: "
074: + getTypeClass().getName(), e);
075: }
076: }
077:
078: return values;
079: }
080:
081: @Override
082: public void writeObject(Object object, MessageWriter writer,
083: Context context) throws DatabindingException {
084: if (object == null) {
085: return;
086: }
087:
088: try {
089: Collection list = (Collection) object;
090:
091: Type type = getComponentType();
092:
093: if (type == null) {
094: throw new DatabindingException(
095: "Couldn't find type for " + type.getTypeClass()
096: + ".");
097: }
098:
099: for (Iterator itr = list.iterator(); itr.hasNext();) {
100: String ns = null;
101: if (type.isAbstract()) {
102: ns = getSchemaType().getNamespaceURI();
103: } else {
104: ns = type.getSchemaType().getNamespaceURI();
105: }
106:
107: writeValue(itr.next(), writer, context, type, type
108: .getSchemaType().getLocalPart(), ns);
109: }
110: } catch (IllegalArgumentException e) {
111: throw new DatabindingException("Illegal argument.", e);
112: }
113: }
114:
115: @Override
116: public Type getComponentType() {
117: return componentType;
118: }
119: }
|