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
1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.bind.v2.runtime;
28 import java.io.IOException;
29 import java.lang.reflect.Array;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.List;
35 import javax.xml.bind.ValidationEvent;
36 import javax.xml.bind.helpers.ValidationEventImpl;
37 import javax.xml.namespace.QName;
38 import javax.xml.stream.XMLStreamException;
40 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeArrayInfo;
41 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
42 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver;
43 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName;
44 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
46 import org.xml.sax.SAXException;
48 /**
49 * {@link JaxBeanInfo} implementation that binds T[] to a complex type
50 * with an element for each item.
51 *
52 * @author Kohsuke Kawaguchi
53 */
54 final class ArrayBeanInfoImpl extends JaxBeanInfo {
56 private final Class itemType;
57 private final JaxBeanInfo itemBeanInfo;
58 private Loader loader;
60 public ArrayBeanInfoImpl(JAXBContextImpl owner, RuntimeArrayInfo rai) {
61 super(owner,rai,rai.getType(), rai.getTypeName(), false, true, false);
62 this.itemType = jaxbType.getComponentType();
63 this.itemBeanInfo = owner.getOrCreate(rai.getItemType());
64 }
66 @Override
67 protected void link(JAXBContextImpl grammar) {
68 getLoader(grammar,false);
69 super.link(grammar);
70 }
72 private final class ArrayLoader extends Loader implements Receiver {
73 public ArrayLoader(JAXBContextImpl owner) {
74 super(false);
75 itemLoader = itemBeanInfo.getLoader(owner,true);
76 }
78 private final Loader itemLoader;
80 @Override
81 public void startElement(UnmarshallingContext.State state, TagName ea) {
82 state.setTarget(new ArrayList());
83 }
85 @Override
86 public void leaveElement(UnmarshallingContext.State state, TagName ea) {
87 state.setTarget(toArray((List)state.getTarget()));
88 }
90 @Override
91 public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
92 if(ea.matches("","item")) {
93 state.setLoader(itemLoader);
94 state.setReceiver(this);
95 } else {
96 super.childElement(state,ea);
97 }
98 }
100 @Override
101 public Collection<QName> getExpectedChildElements() {
102 return Collections.singleton(new QName("","item"));
103 }
105 public void receive(UnmarshallingContext.State state, Object o) {
106 ((List)state.getTarget()).add(o);
107 }
108 }
110 protected Object toArray( List list ) {
111 int len = list.size();
112 Object array = Array.newInstance(itemType,len);
113 for( int i=0; i<len; i++ )
114 Array.set(array,i,list.get(i));
115 return array;
116 }
118 public void serializeBody(Object array, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
119 int len = Array.getLength(array);
120 for( int i=0; i<len; i++ ) {
121 Object item = Array.get(array,i);
122 // TODO: check the namespace URI.
123 target.startElement("","item",null,null);
124 if(item==null) {
125 target.writeXsiNilTrue();
126 } else {
127 target.childAsXsiType(item,"arrayItem",itemBeanInfo, false);
128 }
129 target.endElement();
130 }
131 }
133 public final String getElementNamespaceURI(Object array) {
134 throw new UnsupportedOperationException();
135 }
137 public final String getElementLocalName(Object array) {
138 throw new UnsupportedOperationException();
139 }
141 public final Object createInstance(UnmarshallingContext context) {
142 // we first create a List and then later convert it to an array
143 return new ArrayList();
144 }
146 public final boolean reset(Object array, UnmarshallingContext context) {
147 return false;
148 }
150 public final String getId(Object array, XMLSerializer target) {
151 return null;
152 }
154 public final void serializeAttributes(Object array, XMLSerializer target) {
155 // noop
156 }
158 public final void serializeRoot(Object array, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
159 target.reportError(
160 new ValidationEventImpl(
161 ValidationEvent.ERROR,
162 Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(array.getClass().getName()),
163 null,
164 null));
165 }
167 public final void serializeURIs(Object array, XMLSerializer target) {
168 // noop
169 }
171 public final Transducer getTransducer() {
172 return null;
173 }
175 public final Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) {
176 if(loader==null)
177 loader = new ArrayLoader(context);
179 // type substitution not possible
180 return loader;
181 }
182 }