aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.spi.db; aoqi@0: aoqi@0: import java.util.HashSet; aoqi@0: import java.util.Iterator; aoqi@0: import java.util.Collection; aoqi@0: import java.util.List; aoqi@0: import java.util.Set; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.OutputStream; aoqi@0: import java.util.NoSuchElementException; aoqi@0: aoqi@0: import javax.xml.bind.JAXBException; aoqi@0: import javax.xml.bind.attachment.AttachmentMarshaller; aoqi@0: import javax.xml.bind.attachment.AttachmentUnmarshaller; aoqi@0: import javax.xml.namespace.NamespaceContext; aoqi@0: import javax.xml.stream.XMLStreamReader; aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: import javax.xml.transform.Result; aoqi@0: import javax.xml.transform.Source; aoqi@0: aoqi@0: import org.w3c.dom.Node; aoqi@0: import org.xml.sax.ContentHandler; aoqi@0: aoqi@0: /** aoqi@0: * RepeatedElementBridge aoqi@0: * aoqi@0: * @author shih-chang.chen@oracle.com aoqi@0: */ aoqi@0: @SuppressWarnings({"rawtypes", "unchecked"}) aoqi@0: public class RepeatedElementBridge implements XMLBridge { aoqi@0: aoqi@0: XMLBridge delegate; aoqi@0: CollectionHandler collectionHandler; aoqi@0: aoqi@0: public RepeatedElementBridge(TypeInfo typeInfo, XMLBridge xb) { aoqi@0: delegate = xb; aoqi@0: collectionHandler = create(typeInfo); aoqi@0: } aoqi@0: aoqi@0: public CollectionHandler collectionHandler() { aoqi@0: return collectionHandler; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public BindingContext context() { aoqi@0: return delegate.context(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void marshal(T object, XMLStreamWriter output, AttachmentMarshaller am) throws JAXBException { aoqi@0: delegate.marshal(object, output, am); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void marshal(T object, OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException { aoqi@0: delegate.marshal(object, output, nsContext, am); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void marshal(T object, Node output) throws JAXBException { aoqi@0: delegate.marshal(object, output); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void marshal(T object, ContentHandler contentHandler, AttachmentMarshaller am) throws JAXBException { aoqi@0: delegate.marshal(object, contentHandler, am); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void marshal(T object, Result result) throws JAXBException { aoqi@0: delegate.marshal(object, result); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public T unmarshal(XMLStreamReader in, AttachmentUnmarshaller au) throws JAXBException { aoqi@0: return delegate.unmarshal(in, au); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public T unmarshal(Source in, AttachmentUnmarshaller au) throws JAXBException { aoqi@0: return delegate.unmarshal(in, au); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public T unmarshal(InputStream in) throws JAXBException { aoqi@0: return delegate.unmarshal(in); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public T unmarshal(Node n, AttachmentUnmarshaller au) throws JAXBException { aoqi@0: return delegate.unmarshal(n, au); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public TypeInfo getTypeInfo() { aoqi@0: return delegate.getTypeInfo(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean supportOutputStream() { aoqi@0: return delegate.supportOutputStream(); aoqi@0: } aoqi@0: aoqi@0: static public interface CollectionHandler { aoqi@0: int getSize(Object c); aoqi@0: Iterator iterator(Object c); aoqi@0: Object convert(List list); aoqi@0: } aoqi@0: aoqi@0: static class BaseCollectionHandler implements CollectionHandler { aoqi@0: Class type; aoqi@0: BaseCollectionHandler(Class c) {type = c;} aoqi@0: @Override aoqi@0: public int getSize(Object c) { return ((Collection) c).size(); } aoqi@0: @Override aoqi@0: public Object convert(List list) { aoqi@0: try { aoqi@0: Object o = type.newInstance(); aoqi@0: ((Collection)o).addAll(list); aoqi@0: return o; aoqi@0: } catch (Exception e) { aoqi@0: e.printStackTrace(); aoqi@0: } aoqi@0: return list; aoqi@0: } aoqi@0: @Override aoqi@0: public Iterator iterator(Object c) {return ((Collection)c).iterator();} aoqi@0: } aoqi@0: aoqi@0: static final CollectionHandler ListHandler = new BaseCollectionHandler(List.class) { aoqi@0: @Override aoqi@0: public Object convert(List list) {return list;} aoqi@0: }; aoqi@0: aoqi@0: static final CollectionHandler HashSetHandler = new BaseCollectionHandler(HashSet.class) { aoqi@0: @Override aoqi@0: public Object convert(List list) { return new HashSet(list);} aoqi@0: }; aoqi@0: aoqi@0: static public CollectionHandler create(TypeInfo ti) { aoqi@0: Class javaClass = (Class) ti.type; aoqi@0: if (javaClass.isArray()) { aoqi@0: return new ArrayHandler((Class) ti.getItemType().type); aoqi@0: } else if (List.class.equals(javaClass) || Collection.class.equals(javaClass)) { aoqi@0: return ListHandler; aoqi@0: } else if (Set.class.equals(javaClass) || HashSet.class.equals(javaClass)) { aoqi@0: return HashSetHandler; aoqi@0: } else { aoqi@0: return new BaseCollectionHandler(javaClass); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static class ArrayHandler implements CollectionHandler { aoqi@0: Class componentClass; aoqi@0: public ArrayHandler(Class component) { aoqi@0: componentClass = component; aoqi@0: } aoqi@0: @Override aoqi@0: public int getSize(Object c) { aoqi@0: return java.lang.reflect.Array.getLength(c); aoqi@0: } aoqi@0: @Override aoqi@0: public Object convert(List list) { aoqi@0: Object array = java.lang.reflect.Array.newInstance(componentClass, list.size()); aoqi@0: for (int i = 0; i < list.size(); i++) { aoqi@0: java.lang.reflect.Array.set(array, i, list.get(i)); aoqi@0: } aoqi@0: return array; aoqi@0: } aoqi@0: @Override aoqi@0: public Iterator iterator(final Object c) { aoqi@0: return new Iterator() { aoqi@0: int index = 0; aoqi@0: @Override aoqi@0: public boolean hasNext() { aoqi@0: if (c == null || java.lang.reflect.Array.getLength(c) == 0) { aoqi@0: return false; aoqi@0: } aoqi@0: return (index != java.lang.reflect.Array.getLength(c)); aoqi@0: } aoqi@0: @Override aoqi@0: public Object next() throws NoSuchElementException { aoqi@0: Object retVal = null; aoqi@0: try { aoqi@0: retVal = java.lang.reflect.Array.get(c, index++); aoqi@0: } catch (ArrayIndexOutOfBoundsException ex) { aoqi@0: throw new NoSuchElementException(); aoqi@0: } aoqi@0: return retVal; aoqi@0: } aoqi@0: @Override aoqi@0: public void remove() {} aoqi@0: }; aoqi@0: } aoqi@0: } aoqi@0: }