1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/ArrayBeanInfoImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,182 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, 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.bind.v2.runtime; 1.30 + 1.31 +import java.io.IOException; 1.32 +import java.lang.reflect.Array; 1.33 +import java.util.ArrayList; 1.34 +import java.util.Collection; 1.35 +import java.util.Collections; 1.36 +import java.util.List; 1.37 + 1.38 +import javax.xml.bind.ValidationEvent; 1.39 +import javax.xml.bind.helpers.ValidationEventImpl; 1.40 +import javax.xml.namespace.QName; 1.41 +import javax.xml.stream.XMLStreamException; 1.42 + 1.43 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeArrayInfo; 1.44 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; 1.45 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver; 1.46 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName; 1.47 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; 1.48 + 1.49 +import org.xml.sax.SAXException; 1.50 + 1.51 +/** 1.52 + * {@link JaxBeanInfo} implementation that binds T[] to a complex type 1.53 + * with an element for each item. 1.54 + * 1.55 + * @author Kohsuke Kawaguchi 1.56 + */ 1.57 +final class ArrayBeanInfoImpl extends JaxBeanInfo { 1.58 + 1.59 + private final Class itemType; 1.60 + private final JaxBeanInfo itemBeanInfo; 1.61 + private Loader loader; 1.62 + 1.63 + public ArrayBeanInfoImpl(JAXBContextImpl owner, RuntimeArrayInfo rai) { 1.64 + super(owner,rai,rai.getType(), rai.getTypeName(), false, true, false); 1.65 + this.itemType = jaxbType.getComponentType(); 1.66 + this.itemBeanInfo = owner.getOrCreate(rai.getItemType()); 1.67 + } 1.68 + 1.69 + @Override 1.70 + protected void link(JAXBContextImpl grammar) { 1.71 + getLoader(grammar,false); 1.72 + super.link(grammar); 1.73 + } 1.74 + 1.75 + private final class ArrayLoader extends Loader implements Receiver { 1.76 + public ArrayLoader(JAXBContextImpl owner) { 1.77 + super(false); 1.78 + itemLoader = itemBeanInfo.getLoader(owner,true); 1.79 + } 1.80 + 1.81 + private final Loader itemLoader; 1.82 + 1.83 + @Override 1.84 + public void startElement(UnmarshallingContext.State state, TagName ea) { 1.85 + state.target = new ArrayList(); 1.86 + } 1.87 + 1.88 + @Override 1.89 + public void leaveElement(UnmarshallingContext.State state, TagName ea) { 1.90 + state.target = toArray((List)state.target); 1.91 + } 1.92 + 1.93 + @Override 1.94 + public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.95 + if(ea.matches("","item")) { 1.96 + state.loader = itemLoader; 1.97 + state.receiver = this; 1.98 + } else { 1.99 + super.childElement(state,ea); 1.100 + } 1.101 + } 1.102 + 1.103 + @Override 1.104 + public Collection<QName> getExpectedChildElements() { 1.105 + return Collections.singleton(new QName("","item")); 1.106 + } 1.107 + 1.108 + public void receive(UnmarshallingContext.State state, Object o) { 1.109 + ((List)state.target).add(o); 1.110 + } 1.111 + }; 1.112 + 1.113 + protected Object toArray( List list ) { 1.114 + int len = list.size(); 1.115 + Object array = Array.newInstance(itemType,len); 1.116 + for( int i=0; i<len; i++ ) 1.117 + Array.set(array,i,list.get(i)); 1.118 + return array; 1.119 + } 1.120 + 1.121 + public void serializeBody(Object array, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.122 + int len = Array.getLength(array); 1.123 + for( int i=0; i<len; i++ ) { 1.124 + Object item = Array.get(array,i); 1.125 + // TODO: check the namespace URI. 1.126 + target.startElement("","item",null,null); 1.127 + if(item==null) { 1.128 + target.writeXsiNilTrue(); 1.129 + } else { 1.130 + target.childAsXsiType(item,"arrayItem",itemBeanInfo, false); 1.131 + } 1.132 + target.endElement(); 1.133 + } 1.134 + } 1.135 + 1.136 + public final String getElementNamespaceURI(Object array) { 1.137 + throw new UnsupportedOperationException(); 1.138 + } 1.139 + 1.140 + public final String getElementLocalName(Object array) { 1.141 + throw new UnsupportedOperationException(); 1.142 + } 1.143 + 1.144 + public final Object createInstance(UnmarshallingContext context) { 1.145 + // we first create a List and then later convert it to an array 1.146 + return new ArrayList(); 1.147 + } 1.148 + 1.149 + public final boolean reset(Object array, UnmarshallingContext context) { 1.150 + return false; 1.151 + } 1.152 + 1.153 + public final String getId(Object array, XMLSerializer target) { 1.154 + return null; 1.155 + } 1.156 + 1.157 + public final void serializeAttributes(Object array, XMLSerializer target) { 1.158 + // noop 1.159 + } 1.160 + 1.161 + public final void serializeRoot(Object array, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.162 + target.reportError( 1.163 + new ValidationEventImpl( 1.164 + ValidationEvent.ERROR, 1.165 + Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(array.getClass().getName()), 1.166 + null, 1.167 + null)); 1.168 + } 1.169 + 1.170 + public final void serializeURIs(Object array, XMLSerializer target) { 1.171 + // noop 1.172 + } 1.173 + 1.174 + public final Transducer getTransducer() { 1.175 + return null; 1.176 + } 1.177 + 1.178 + public final Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { 1.179 + if(loader==null) 1.180 + loader = new ArrayLoader(context); 1.181 + 1.182 + // type substitution not possible 1.183 + return loader; 1.184 + } 1.185 +}