src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/ArrayBeanInfoImpl.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 650
121e938cb9c3
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aefimov@650 2 * Copyright (c) 1997, 2014, 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.bind.v2.runtime;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.lang.reflect.Array;
aoqi@0 30 import java.util.ArrayList;
aoqi@0 31 import java.util.Collection;
aoqi@0 32 import java.util.Collections;
aoqi@0 33 import java.util.List;
aoqi@0 34
aoqi@0 35 import javax.xml.bind.ValidationEvent;
aoqi@0 36 import javax.xml.bind.helpers.ValidationEventImpl;
aoqi@0 37 import javax.xml.namespace.QName;
aoqi@0 38 import javax.xml.stream.XMLStreamException;
aoqi@0 39
aoqi@0 40 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeArrayInfo;
aoqi@0 41 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
aoqi@0 42 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver;
aoqi@0 43 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName;
aoqi@0 44 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
aoqi@0 45
aoqi@0 46 import org.xml.sax.SAXException;
aoqi@0 47
aoqi@0 48 /**
aoqi@0 49 * {@link JaxBeanInfo} implementation that binds T[] to a complex type
aoqi@0 50 * with an element for each item.
aoqi@0 51 *
aoqi@0 52 * @author Kohsuke Kawaguchi
aoqi@0 53 */
aoqi@0 54 final class ArrayBeanInfoImpl extends JaxBeanInfo {
aoqi@0 55
aoqi@0 56 private final Class itemType;
aoqi@0 57 private final JaxBeanInfo itemBeanInfo;
aoqi@0 58 private Loader loader;
aoqi@0 59
aoqi@0 60 public ArrayBeanInfoImpl(JAXBContextImpl owner, RuntimeArrayInfo rai) {
aoqi@0 61 super(owner,rai,rai.getType(), rai.getTypeName(), false, true, false);
aoqi@0 62 this.itemType = jaxbType.getComponentType();
aoqi@0 63 this.itemBeanInfo = owner.getOrCreate(rai.getItemType());
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 @Override
aoqi@0 67 protected void link(JAXBContextImpl grammar) {
aoqi@0 68 getLoader(grammar,false);
aoqi@0 69 super.link(grammar);
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 private final class ArrayLoader extends Loader implements Receiver {
aoqi@0 73 public ArrayLoader(JAXBContextImpl owner) {
aoqi@0 74 super(false);
aoqi@0 75 itemLoader = itemBeanInfo.getLoader(owner,true);
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 private final Loader itemLoader;
aoqi@0 79
aoqi@0 80 @Override
aoqi@0 81 public void startElement(UnmarshallingContext.State state, TagName ea) {
aefimov@650 82 state.setTarget(new ArrayList());
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 @Override
aoqi@0 86 public void leaveElement(UnmarshallingContext.State state, TagName ea) {
aefimov@650 87 state.setTarget(toArray((List)state.getTarget()));
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 @Override
aoqi@0 91 public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
aoqi@0 92 if(ea.matches("","item")) {
aefimov@650 93 state.setLoader(itemLoader);
aefimov@650 94 state.setReceiver(this);
aoqi@0 95 } else {
aoqi@0 96 super.childElement(state,ea);
aoqi@0 97 }
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 @Override
aoqi@0 101 public Collection<QName> getExpectedChildElements() {
aoqi@0 102 return Collections.singleton(new QName("","item"));
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 public void receive(UnmarshallingContext.State state, Object o) {
aefimov@650 106 ((List)state.getTarget()).add(o);
aoqi@0 107 }
aefimov@650 108 }
aoqi@0 109
aoqi@0 110 protected Object toArray( List list ) {
aoqi@0 111 int len = list.size();
aoqi@0 112 Object array = Array.newInstance(itemType,len);
aoqi@0 113 for( int i=0; i<len; i++ )
aoqi@0 114 Array.set(array,i,list.get(i));
aoqi@0 115 return array;
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 public void serializeBody(Object array, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
aoqi@0 119 int len = Array.getLength(array);
aoqi@0 120 for( int i=0; i<len; i++ ) {
aoqi@0 121 Object item = Array.get(array,i);
aoqi@0 122 // TODO: check the namespace URI.
aoqi@0 123 target.startElement("","item",null,null);
aoqi@0 124 if(item==null) {
aoqi@0 125 target.writeXsiNilTrue();
aoqi@0 126 } else {
aoqi@0 127 target.childAsXsiType(item,"arrayItem",itemBeanInfo, false);
aoqi@0 128 }
aoqi@0 129 target.endElement();
aoqi@0 130 }
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 public final String getElementNamespaceURI(Object array) {
aoqi@0 134 throw new UnsupportedOperationException();
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 public final String getElementLocalName(Object array) {
aoqi@0 138 throw new UnsupportedOperationException();
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 public final Object createInstance(UnmarshallingContext context) {
aoqi@0 142 // we first create a List and then later convert it to an array
aoqi@0 143 return new ArrayList();
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 public final boolean reset(Object array, UnmarshallingContext context) {
aoqi@0 147 return false;
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 public final String getId(Object array, XMLSerializer target) {
aoqi@0 151 return null;
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 public final void serializeAttributes(Object array, XMLSerializer target) {
aoqi@0 155 // noop
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 public final void serializeRoot(Object array, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
aoqi@0 159 target.reportError(
aoqi@0 160 new ValidationEventImpl(
aoqi@0 161 ValidationEvent.ERROR,
aoqi@0 162 Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(array.getClass().getName()),
aoqi@0 163 null,
aoqi@0 164 null));
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 public final void serializeURIs(Object array, XMLSerializer target) {
aoqi@0 168 // noop
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 public final Transducer getTransducer() {
aoqi@0 172 return null;
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 public final Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) {
aoqi@0 176 if(loader==null)
aoqi@0 177 loader = new ArrayLoader(context);
aoqi@0 178
aoqi@0 179 // type substitution not possible
aoqi@0 180 return loader;
aoqi@0 181 }
aoqi@0 182 }

mercurial