Fri, 24 Oct 2014 15:02:28 +0200
8054367: More references for endpoints
Summary: fix also reviewed by Iaroslav.Savytskyi@oracle.com, Alexander.Fomin@oracle.com
Reviewed-by: mullan, skoivu
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.spi.db; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.HashSet; |
aoqi@0 | 29 | import java.util.Iterator; |
aoqi@0 | 30 | import java.util.Collection; |
aoqi@0 | 31 | import java.util.List; |
aoqi@0 | 32 | import java.util.Set; |
aoqi@0 | 33 | import java.io.InputStream; |
aoqi@0 | 34 | import java.io.OutputStream; |
aoqi@0 | 35 | import java.util.NoSuchElementException; |
aoqi@0 | 36 | |
aoqi@0 | 37 | import javax.xml.bind.JAXBException; |
aoqi@0 | 38 | import javax.xml.bind.attachment.AttachmentMarshaller; |
aoqi@0 | 39 | import javax.xml.bind.attachment.AttachmentUnmarshaller; |
aoqi@0 | 40 | import javax.xml.namespace.NamespaceContext; |
aoqi@0 | 41 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 42 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 43 | import javax.xml.transform.Result; |
aoqi@0 | 44 | import javax.xml.transform.Source; |
aoqi@0 | 45 | |
aoqi@0 | 46 | import org.w3c.dom.Node; |
aoqi@0 | 47 | import org.xml.sax.ContentHandler; |
aoqi@0 | 48 | |
aoqi@0 | 49 | /** |
aoqi@0 | 50 | * RepeatedElementBridge |
aoqi@0 | 51 | * |
aoqi@0 | 52 | * @author shih-chang.chen@oracle.com |
aoqi@0 | 53 | */ |
aoqi@0 | 54 | @SuppressWarnings({"rawtypes", "unchecked"}) |
aoqi@0 | 55 | public class RepeatedElementBridge<T> implements XMLBridge<T> { |
aoqi@0 | 56 | |
aoqi@0 | 57 | XMLBridge<T> delegate; |
aoqi@0 | 58 | CollectionHandler collectionHandler; |
aoqi@0 | 59 | |
aoqi@0 | 60 | public RepeatedElementBridge(TypeInfo typeInfo, XMLBridge xb) { |
aoqi@0 | 61 | delegate = xb; |
aoqi@0 | 62 | collectionHandler = create(typeInfo); |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | public CollectionHandler collectionHandler() { |
aoqi@0 | 66 | return collectionHandler; |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | @Override |
aoqi@0 | 70 | public BindingContext context() { |
aoqi@0 | 71 | return delegate.context(); |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | @Override |
aoqi@0 | 75 | public void marshal(T object, XMLStreamWriter output, AttachmentMarshaller am) throws JAXBException { |
aoqi@0 | 76 | delegate.marshal(object, output, am); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | |
aoqi@0 | 79 | @Override |
aoqi@0 | 80 | public void marshal(T object, OutputStream output, NamespaceContext nsContext, AttachmentMarshaller am) throws JAXBException { |
aoqi@0 | 81 | delegate.marshal(object, output, nsContext, am); |
aoqi@0 | 82 | } |
aoqi@0 | 83 | |
aoqi@0 | 84 | @Override |
aoqi@0 | 85 | public void marshal(T object, Node output) throws JAXBException { |
aoqi@0 | 86 | delegate.marshal(object, output); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | |
aoqi@0 | 89 | @Override |
aoqi@0 | 90 | public void marshal(T object, ContentHandler contentHandler, AttachmentMarshaller am) throws JAXBException { |
aoqi@0 | 91 | delegate.marshal(object, contentHandler, am); |
aoqi@0 | 92 | } |
aoqi@0 | 93 | |
aoqi@0 | 94 | @Override |
aoqi@0 | 95 | public void marshal(T object, Result result) throws JAXBException { |
aoqi@0 | 96 | delegate.marshal(object, result); |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | @Override |
aoqi@0 | 100 | public T unmarshal(XMLStreamReader in, AttachmentUnmarshaller au) throws JAXBException { |
aoqi@0 | 101 | return delegate.unmarshal(in, au); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | |
aoqi@0 | 104 | @Override |
aoqi@0 | 105 | public T unmarshal(Source in, AttachmentUnmarshaller au) throws JAXBException { |
aoqi@0 | 106 | return delegate.unmarshal(in, au); |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | @Override |
aoqi@0 | 110 | public T unmarshal(InputStream in) throws JAXBException { |
aoqi@0 | 111 | return delegate.unmarshal(in); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | @Override |
aoqi@0 | 115 | public T unmarshal(Node n, AttachmentUnmarshaller au) throws JAXBException { |
aoqi@0 | 116 | return delegate.unmarshal(n, au); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | @Override |
aoqi@0 | 120 | public TypeInfo getTypeInfo() { |
aoqi@0 | 121 | return delegate.getTypeInfo(); |
aoqi@0 | 122 | } |
aoqi@0 | 123 | |
aoqi@0 | 124 | @Override |
aoqi@0 | 125 | public boolean supportOutputStream() { |
aoqi@0 | 126 | return delegate.supportOutputStream(); |
aoqi@0 | 127 | } |
aoqi@0 | 128 | |
aoqi@0 | 129 | static public interface CollectionHandler { |
aoqi@0 | 130 | int getSize(Object c); |
aoqi@0 | 131 | Iterator iterator(Object c); |
aoqi@0 | 132 | Object convert(List list); |
aoqi@0 | 133 | } |
aoqi@0 | 134 | |
aoqi@0 | 135 | static class BaseCollectionHandler implements CollectionHandler { |
aoqi@0 | 136 | Class type; |
aoqi@0 | 137 | BaseCollectionHandler(Class c) {type = c;} |
aoqi@0 | 138 | @Override |
aoqi@0 | 139 | public int getSize(Object c) { return ((Collection) c).size(); } |
aoqi@0 | 140 | @Override |
aoqi@0 | 141 | public Object convert(List list) { |
aoqi@0 | 142 | try { |
aoqi@0 | 143 | Object o = type.newInstance(); |
aoqi@0 | 144 | ((Collection)o).addAll(list); |
aoqi@0 | 145 | return o; |
aoqi@0 | 146 | } catch (Exception e) { |
aoqi@0 | 147 | e.printStackTrace(); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | return list; |
aoqi@0 | 150 | } |
aoqi@0 | 151 | @Override |
aoqi@0 | 152 | public Iterator iterator(Object c) {return ((Collection)c).iterator();} |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | static final CollectionHandler ListHandler = new BaseCollectionHandler(List.class) { |
aoqi@0 | 156 | @Override |
aoqi@0 | 157 | public Object convert(List list) {return list;} |
aoqi@0 | 158 | }; |
aoqi@0 | 159 | |
aoqi@0 | 160 | static final CollectionHandler HashSetHandler = new BaseCollectionHandler(HashSet.class) { |
aoqi@0 | 161 | @Override |
aoqi@0 | 162 | public Object convert(List list) { return new HashSet(list);} |
aoqi@0 | 163 | }; |
aoqi@0 | 164 | |
aoqi@0 | 165 | static public CollectionHandler create(TypeInfo ti) { |
aoqi@0 | 166 | Class javaClass = (Class) ti.type; |
aoqi@0 | 167 | if (javaClass.isArray()) { |
aoqi@0 | 168 | return new ArrayHandler((Class) ti.getItemType().type); |
aoqi@0 | 169 | } else if (List.class.equals(javaClass) || Collection.class.equals(javaClass)) { |
aoqi@0 | 170 | return ListHandler; |
aoqi@0 | 171 | } else if (Set.class.equals(javaClass) || HashSet.class.equals(javaClass)) { |
aoqi@0 | 172 | return HashSetHandler; |
aoqi@0 | 173 | } else { |
aoqi@0 | 174 | return new BaseCollectionHandler(javaClass); |
aoqi@0 | 175 | } |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | static class ArrayHandler implements CollectionHandler { |
aoqi@0 | 179 | Class componentClass; |
aoqi@0 | 180 | public ArrayHandler(Class component) { |
aoqi@0 | 181 | componentClass = component; |
aoqi@0 | 182 | } |
aoqi@0 | 183 | @Override |
aoqi@0 | 184 | public int getSize(Object c) { |
aoqi@0 | 185 | return java.lang.reflect.Array.getLength(c); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | @Override |
aoqi@0 | 188 | public Object convert(List list) { |
aoqi@0 | 189 | Object array = java.lang.reflect.Array.newInstance(componentClass, list.size()); |
aoqi@0 | 190 | for (int i = 0; i < list.size(); i++) { |
aoqi@0 | 191 | java.lang.reflect.Array.set(array, i, list.get(i)); |
aoqi@0 | 192 | } |
aoqi@0 | 193 | return array; |
aoqi@0 | 194 | } |
aoqi@0 | 195 | @Override |
aoqi@0 | 196 | public Iterator iterator(final Object c) { |
aoqi@0 | 197 | return new Iterator() { |
aoqi@0 | 198 | int index = 0; |
aoqi@0 | 199 | @Override |
aoqi@0 | 200 | public boolean hasNext() { |
aoqi@0 | 201 | if (c == null || java.lang.reflect.Array.getLength(c) == 0) { |
aoqi@0 | 202 | return false; |
aoqi@0 | 203 | } |
aoqi@0 | 204 | return (index != java.lang.reflect.Array.getLength(c)); |
aoqi@0 | 205 | } |
aoqi@0 | 206 | @Override |
aoqi@0 | 207 | public Object next() throws NoSuchElementException { |
aoqi@0 | 208 | Object retVal = null; |
aoqi@0 | 209 | try { |
aoqi@0 | 210 | retVal = java.lang.reflect.Array.get(c, index++); |
aoqi@0 | 211 | } catch (ArrayIndexOutOfBoundsException ex) { |
aoqi@0 | 212 | throw new NoSuchElementException(); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | return retVal; |
aoqi@0 | 215 | } |
aoqi@0 | 216 | @Override |
aoqi@0 | 217 | public void remove() {} |
aoqi@0 | 218 | }; |
aoqi@0 | 219 | } |
aoqi@0 | 220 | } |
aoqi@0 | 221 | } |