|
1 /* |
|
2 * Copyright (c) 1997, 2011, 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 */ |
|
25 |
|
26 package com.sun.xml.internal.bind.v2.runtime; |
|
27 |
|
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; |
|
34 |
|
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; |
|
39 |
|
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; |
|
45 |
|
46 import org.xml.sax.SAXException; |
|
47 |
|
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 { |
|
55 |
|
56 private final Class itemType; |
|
57 private final JaxBeanInfo itemBeanInfo; |
|
58 private Loader loader; |
|
59 |
|
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 } |
|
65 |
|
66 @Override |
|
67 protected void link(JAXBContextImpl grammar) { |
|
68 getLoader(grammar,false); |
|
69 super.link(grammar); |
|
70 } |
|
71 |
|
72 private final class ArrayLoader extends Loader implements Receiver { |
|
73 public ArrayLoader(JAXBContextImpl owner) { |
|
74 super(false); |
|
75 itemLoader = itemBeanInfo.getLoader(owner,true); |
|
76 } |
|
77 |
|
78 private final Loader itemLoader; |
|
79 |
|
80 @Override |
|
81 public void startElement(UnmarshallingContext.State state, TagName ea) { |
|
82 state.target = new ArrayList(); |
|
83 } |
|
84 |
|
85 @Override |
|
86 public void leaveElement(UnmarshallingContext.State state, TagName ea) { |
|
87 state.target = toArray((List)state.target); |
|
88 } |
|
89 |
|
90 @Override |
|
91 public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { |
|
92 if(ea.matches("","item")) { |
|
93 state.loader = itemLoader; |
|
94 state.receiver = this; |
|
95 } else { |
|
96 super.childElement(state,ea); |
|
97 } |
|
98 } |
|
99 |
|
100 @Override |
|
101 public Collection<QName> getExpectedChildElements() { |
|
102 return Collections.singleton(new QName("","item")); |
|
103 } |
|
104 |
|
105 public void receive(UnmarshallingContext.State state, Object o) { |
|
106 ((List)state.target).add(o); |
|
107 } |
|
108 }; |
|
109 |
|
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 } |
|
117 |
|
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 } |
|
132 |
|
133 public final String getElementNamespaceURI(Object array) { |
|
134 throw new UnsupportedOperationException(); |
|
135 } |
|
136 |
|
137 public final String getElementLocalName(Object array) { |
|
138 throw new UnsupportedOperationException(); |
|
139 } |
|
140 |
|
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 } |
|
145 |
|
146 public final boolean reset(Object array, UnmarshallingContext context) { |
|
147 return false; |
|
148 } |
|
149 |
|
150 public final String getId(Object array, XMLSerializer target) { |
|
151 return null; |
|
152 } |
|
153 |
|
154 public final void serializeAttributes(Object array, XMLSerializer target) { |
|
155 // noop |
|
156 } |
|
157 |
|
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 } |
|
166 |
|
167 public final void serializeURIs(Object array, XMLSerializer target) { |
|
168 // noop |
|
169 } |
|
170 |
|
171 public final Transducer getTransducer() { |
|
172 return null; |
|
173 } |
|
174 |
|
175 public final Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { |
|
176 if(loader==null) |
|
177 loader = new ArrayLoader(context); |
|
178 |
|
179 // type substitution not possible |
|
180 return loader; |
|
181 } |
|
182 } |