1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/spi/db/RepeatedElementBridge.java Tue Apr 09 14:51:13 2013 +0100 1.3 @@ -0,0 +1,221 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.spi.db; 1.30 + 1.31 +import java.util.HashSet; 1.32 +import java.util.Iterator; 1.33 +import java.util.Collection; 1.34 +import java.util.List; 1.35 +import java.util.Set; 1.36 +import java.io.InputStream; 1.37 +import java.io.OutputStream; 1.38 +import java.util.NoSuchElementException; 1.39 + 1.40 +import javax.xml.bind.JAXBException; 1.41 +import javax.xml.bind.attachment.AttachmentMarshaller; 1.42 +import javax.xml.bind.attachment.AttachmentUnmarshaller; 1.43 +import javax.xml.namespace.NamespaceContext; 1.44 +import javax.xml.stream.XMLStreamReader; 1.45 +import javax.xml.stream.XMLStreamWriter; 1.46 +import javax.xml.transform.Result; 1.47 +import javax.xml.transform.Source; 1.48 + 1.49 +import org.w3c.dom.Node; 1.50 +import org.xml.sax.ContentHandler; 1.51 + 1.52 +/** 1.53 + * RepeatedElementBridge 1.54 + * 1.55 + * @author shih-chang.chen@oracle.com 1.56 + */ 1.57 +@SuppressWarnings({"rawtypes", "unchecked"}) 1.58 +public class RepeatedElementBridge<T> implements XMLBridge<T> { 1.59 + 1.60 + XMLBridge<T> delegate; 1.61 + CollectionHandler collectionHandler; 1.62 + 1.63 + public RepeatedElementBridge(TypeInfo typeInfo, XMLBridge xb) { 1.64 + delegate = xb; 1.65 + collectionHandler = create(typeInfo); 1.66 + } 1.67 + 1.68 + public CollectionHandler collectionHandler() { 1.69 + return collectionHandler; 1.70 + } 1.71 + 1.72 + @Override 1.73 + public BindingContext context() { 1.74 + return delegate.context(); 1.75 + } 1.76 + 1.77 + @Override 1.78 + public void marshal(T object, XMLStreamWriter output, AttachmentMarshaller am) throws JAXBException { 1.79 + delegate.marshal(object, output, am); 1.80 + } 1.81 + 1.82 + @Override 1.83 + public void marshal(T object, OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException { 1.84 + delegate.marshal(object, output, nsContext, am); 1.85 + } 1.86 + 1.87 + @Override 1.88 + public void marshal(T object, Node output) throws JAXBException { 1.89 + delegate.marshal(object, output); 1.90 + } 1.91 + 1.92 + @Override 1.93 + public void marshal(T object, ContentHandler contentHandler, AttachmentMarshaller am) throws JAXBException { 1.94 + delegate.marshal(object, contentHandler, am); 1.95 + } 1.96 + 1.97 + @Override 1.98 + public void marshal(T object, Result result) throws JAXBException { 1.99 + delegate.marshal(object, result); 1.100 + } 1.101 + 1.102 + @Override 1.103 + public T unmarshal(XMLStreamReader in, AttachmentUnmarshaller au) throws JAXBException { 1.104 + return delegate.unmarshal(in, au); 1.105 + } 1.106 + 1.107 + @Override 1.108 + public T unmarshal(Source in, AttachmentUnmarshaller au) throws JAXBException { 1.109 + return delegate.unmarshal(in, au); 1.110 + } 1.111 + 1.112 + @Override 1.113 + public T unmarshal(InputStream in) throws JAXBException { 1.114 + return delegate.unmarshal(in); 1.115 + } 1.116 + 1.117 + @Override 1.118 + public T unmarshal(Node n, AttachmentUnmarshaller au) throws JAXBException { 1.119 + return delegate.unmarshal(n, au); 1.120 + } 1.121 + 1.122 + @Override 1.123 + public TypeInfo getTypeInfo() { 1.124 + return delegate.getTypeInfo(); 1.125 + } 1.126 + 1.127 + @Override 1.128 + public boolean supportOutputStream() { 1.129 + return delegate.supportOutputStream(); 1.130 + } 1.131 + 1.132 + static public interface CollectionHandler { 1.133 + int getSize(Object c); 1.134 + Iterator iterator(Object c); 1.135 + Object convert(List list); 1.136 + } 1.137 + 1.138 + static class BaseCollectionHandler implements CollectionHandler { 1.139 + Class type; 1.140 + BaseCollectionHandler(Class c) {type = c;} 1.141 + @Override 1.142 + public int getSize(Object c) { return ((Collection) c).size(); } 1.143 + @Override 1.144 + public Object convert(List list) { 1.145 + try { 1.146 + Object o = type.newInstance(); 1.147 + ((Collection)o).addAll(list); 1.148 + return o; 1.149 + } catch (Exception e) { 1.150 + e.printStackTrace(); 1.151 + } 1.152 + return list; 1.153 + } 1.154 + @Override 1.155 + public Iterator iterator(Object c) {return ((Collection)c).iterator();} 1.156 + } 1.157 + 1.158 + static final CollectionHandler ListHandler = new BaseCollectionHandler(List.class) { 1.159 + @Override 1.160 + public Object convert(List list) {return list;} 1.161 + }; 1.162 + 1.163 + static final CollectionHandler HashSetHandler = new BaseCollectionHandler(HashSet.class) { 1.164 + @Override 1.165 + public Object convert(List list) { return new HashSet(list);} 1.166 + }; 1.167 + 1.168 + static public CollectionHandler create(TypeInfo ti) { 1.169 + Class javaClass = (Class) ti.type; 1.170 + if (javaClass.isArray()) { 1.171 + return new ArrayHandler((Class) ti.getItemType().type); 1.172 + } else if (List.class.equals(javaClass) || Collection.class.equals(javaClass)) { 1.173 + return ListHandler; 1.174 + } else if (Set.class.equals(javaClass) || HashSet.class.equals(javaClass)) { 1.175 + return HashSetHandler; 1.176 + } else { 1.177 + return new BaseCollectionHandler(javaClass); 1.178 + } 1.179 + } 1.180 + 1.181 + static class ArrayHandler implements CollectionHandler { 1.182 + Class componentClass; 1.183 + public ArrayHandler(Class component) { 1.184 + componentClass = component; 1.185 + } 1.186 + @Override 1.187 + public int getSize(Object c) { 1.188 + return java.lang.reflect.Array.getLength(c); 1.189 + } 1.190 + @Override 1.191 + public Object convert(List list) { 1.192 + Object array = java.lang.reflect.Array.newInstance(componentClass, list.size()); 1.193 + for (int i = 0; i < list.size(); i++) { 1.194 + java.lang.reflect.Array.set(array, i, list.get(i)); 1.195 + } 1.196 + return array; 1.197 + } 1.198 + @Override 1.199 + public Iterator iterator(final Object c) { 1.200 + return new Iterator() { 1.201 + int index = 0; 1.202 + @Override 1.203 + public boolean hasNext() { 1.204 + if (c == null || java.lang.reflect.Array.getLength(c) == 0) { 1.205 + return false; 1.206 + } 1.207 + return (index != java.lang.reflect.Array.getLength(c)); 1.208 + } 1.209 + @Override 1.210 + public Object next() throws NoSuchElementException { 1.211 + Object retVal = null; 1.212 + try { 1.213 + retVal = java.lang.reflect.Array.get(c, index++); 1.214 + } catch (ArrayIndexOutOfBoundsException ex) { 1.215 + throw new NoSuchElementException(); 1.216 + } 1.217 + return retVal; 1.218 + } 1.219 + @Override 1.220 + public void remove() {} 1.221 + }; 1.222 + } 1.223 + } 1.224 +}