Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, 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.reflect; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.ref.WeakReference; |
aoqi@0 | 29 | import java.lang.reflect.Array; |
aoqi@0 | 30 | import java.lang.reflect.ParameterizedType; |
aoqi@0 | 31 | import java.lang.reflect.Type; |
aoqi@0 | 32 | import java.util.ArrayList; |
aoqi@0 | 33 | import java.util.Collection; |
aoqi@0 | 34 | import java.util.Collections; |
aoqi@0 | 35 | import java.util.HashMap; |
aoqi@0 | 36 | import java.util.Iterator; |
aoqi@0 | 37 | import java.util.List; |
aoqi@0 | 38 | import java.util.Map; |
aoqi@0 | 39 | import java.util.WeakHashMap; |
aoqi@0 | 40 | import java.util.LinkedList; |
aoqi@0 | 41 | import java.util.HashSet; |
aoqi@0 | 42 | import java.util.TreeSet; |
aoqi@0 | 43 | import java.util.Stack; |
aoqi@0 | 44 | import java.util.concurrent.Callable; |
aoqi@0 | 45 | |
aoqi@0 | 46 | import javax.xml.bind.JAXBException; |
aoqi@0 | 47 | |
aoqi@0 | 48 | import com.sun.istack.internal.SAXException2; |
aoqi@0 | 49 | import com.sun.xml.internal.bind.api.AccessorException; |
aoqi@0 | 50 | import com.sun.xml.internal.bind.v2.ClassFactory; |
aoqi@0 | 51 | import com.sun.xml.internal.bind.v2.TODO; |
aoqi@0 | 52 | import com.sun.xml.internal.bind.v2.model.core.Adapter; |
aoqi@0 | 53 | import com.sun.xml.internal.bind.v2.model.core.ID; |
aoqi@0 | 54 | import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; |
aoqi@0 | 55 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Patcher; |
aoqi@0 | 56 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
aoqi@0 | 57 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LocatorEx; |
aoqi@0 | 58 | |
aoqi@0 | 59 | import org.xml.sax.SAXException; |
aoqi@0 | 60 | |
aoqi@0 | 61 | /** |
aoqi@0 | 62 | * Used to list individual values of a multi-value property, and |
aoqi@0 | 63 | * to pack individual values into a multi-value property. |
aoqi@0 | 64 | * |
aoqi@0 | 65 | * @author Kohsuke Kawaguchi (kk@kohsuke.org) |
aoqi@0 | 66 | */ |
aoqi@0 | 67 | public abstract class Lister<BeanT,PropT,ItemT,PackT> { |
aoqi@0 | 68 | |
aoqi@0 | 69 | protected Lister() {} |
aoqi@0 | 70 | |
aoqi@0 | 71 | /** |
aoqi@0 | 72 | * Iterates values of a multi-value property. |
aoqi@0 | 73 | * |
aoqi@0 | 74 | * @param context |
aoqi@0 | 75 | * This parameter is used to support ID/IDREF handling. |
aoqi@0 | 76 | */ |
aoqi@0 | 77 | public abstract ListIterator<ItemT> iterator(PropT multiValueProp, XMLSerializer context); |
aoqi@0 | 78 | |
aoqi@0 | 79 | /** |
aoqi@0 | 80 | * Setting values to a multi-value property starts by creating |
aoqi@0 | 81 | * a transient object called "pack" from the current field. |
aoqi@0 | 82 | */ |
aoqi@0 | 83 | public abstract PackT startPacking(BeanT bean, Accessor<BeanT, PropT> acc) throws AccessorException; |
aoqi@0 | 84 | |
aoqi@0 | 85 | /** |
aoqi@0 | 86 | * Once the {@link #startPacking} is called, you can |
aoqi@0 | 87 | * add values to the pack by using this method. |
aoqi@0 | 88 | */ |
aoqi@0 | 89 | public abstract void addToPack( PackT pack, ItemT newValue ) throws AccessorException; |
aoqi@0 | 90 | |
aoqi@0 | 91 | /** |
aoqi@0 | 92 | * Finally, call this method to |
aoqi@0 | 93 | * wraps up the {@code pack}. This method may update the field of |
aoqi@0 | 94 | * the given bean. |
aoqi@0 | 95 | */ |
aoqi@0 | 96 | public abstract void endPacking( PackT pack, BeanT bean, Accessor<BeanT,PropT> acc ) throws AccessorException; |
aoqi@0 | 97 | |
aoqi@0 | 98 | /** |
aoqi@0 | 99 | * Clears the values of the property. |
aoqi@0 | 100 | */ |
aoqi@0 | 101 | public abstract void reset(BeanT o,Accessor<BeanT,PropT> acc) throws AccessorException; |
aoqi@0 | 102 | |
aoqi@0 | 103 | |
aoqi@0 | 104 | /** |
aoqi@0 | 105 | * Gets a reference to the appropriate {@link Lister} object |
aoqi@0 | 106 | * if the field is a multi-value field. Otherwise null. |
aoqi@0 | 107 | * |
aoqi@0 | 108 | * @param fieldType |
aoqi@0 | 109 | * the type of the field that stores the collection |
aoqi@0 | 110 | * @param idness |
aoqi@0 | 111 | * ID-ness of the property. |
aoqi@0 | 112 | * @param adapter |
aoqi@0 | 113 | * adapter to be used for individual items. can be null. |
aoqi@0 | 114 | */ |
aoqi@0 | 115 | public static <BeanT,PropT,ItemT,PackT> |
aoqi@0 | 116 | Lister<BeanT,PropT,ItemT,PackT> create(Type fieldType,ID idness, Adapter<Type,Class> adapter) { |
aoqi@0 | 117 | |
aoqi@0 | 118 | Class rawType = (Class) Utils.REFLECTION_NAVIGATOR.erasure(fieldType); |
aoqi@0 | 119 | Class itemType; |
aoqi@0 | 120 | |
aoqi@0 | 121 | Lister l; |
aoqi@0 | 122 | if( rawType.isArray() ) { |
aoqi@0 | 123 | itemType = rawType.getComponentType(); |
aoqi@0 | 124 | l = getArrayLister(itemType); |
aoqi@0 | 125 | } else |
aoqi@0 | 126 | if( Collection.class.isAssignableFrom(rawType) ) { |
aoqi@0 | 127 | Type bt = Utils.REFLECTION_NAVIGATOR.getBaseClass(fieldType,Collection.class); |
aoqi@0 | 128 | if(bt instanceof ParameterizedType) |
aoqi@0 | 129 | itemType = (Class) Utils.REFLECTION_NAVIGATOR.erasure(((ParameterizedType)bt).getActualTypeArguments()[0]); |
aoqi@0 | 130 | else |
aoqi@0 | 131 | itemType = Object.class; |
aoqi@0 | 132 | l = new CollectionLister(getImplClass(rawType)); |
aoqi@0 | 133 | } else |
aoqi@0 | 134 | return null; |
aoqi@0 | 135 | |
aoqi@0 | 136 | if(idness==ID.IDREF) |
aoqi@0 | 137 | l = new IDREFS(l,itemType); |
aoqi@0 | 138 | |
aoqi@0 | 139 | if(adapter!=null) |
aoqi@0 | 140 | l = new AdaptedLister(l,adapter.adapterType); |
aoqi@0 | 141 | |
aoqi@0 | 142 | return l; |
aoqi@0 | 143 | } |
aoqi@0 | 144 | |
aoqi@0 | 145 | private static Class getImplClass(Class<?> fieldType) { |
aoqi@0 | 146 | return ClassFactory.inferImplClass(fieldType,COLLECTION_IMPL_CLASSES); |
aoqi@0 | 147 | } |
aoqi@0 | 148 | |
aoqi@0 | 149 | /** |
aoqi@0 | 150 | * Cache instances of {@link ArrayLister}s. |
aoqi@0 | 151 | */ |
aoqi@0 | 152 | private static final Map<Class,WeakReference<Lister>> arrayListerCache = |
aoqi@0 | 153 | Collections.synchronizedMap(new WeakHashMap<Class,WeakReference<Lister>>()); |
aoqi@0 | 154 | |
aoqi@0 | 155 | /** |
aoqi@0 | 156 | * Creates a lister for array type. |
aoqi@0 | 157 | */ |
aoqi@0 | 158 | private static Lister getArrayLister( Class componentType ) { |
aoqi@0 | 159 | Lister l=null; |
aoqi@0 | 160 | if(componentType.isPrimitive()) |
aoqi@0 | 161 | l = primitiveArrayListers.get(componentType); |
aoqi@0 | 162 | else { |
aoqi@0 | 163 | WeakReference<Lister> wr = arrayListerCache.get(componentType); |
aoqi@0 | 164 | if(wr!=null) |
aoqi@0 | 165 | l = wr.get(); |
aoqi@0 | 166 | if(l==null) { |
aoqi@0 | 167 | l = new ArrayLister(componentType); |
aoqi@0 | 168 | arrayListerCache.put(componentType,new WeakReference<Lister>(l)); |
aoqi@0 | 169 | } |
aoqi@0 | 170 | } |
aoqi@0 | 171 | assert l!=null; |
aoqi@0 | 172 | return l; |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | /** |
aoqi@0 | 176 | * {@link Lister} for an array. |
aoqi@0 | 177 | * |
aoqi@0 | 178 | * <p> |
aoqi@0 | 179 | * Array packing is slower, but we expect this to be used less frequently than |
aoqi@0 | 180 | * the {@link CollectionLister}. |
aoqi@0 | 181 | */ |
aoqi@0 | 182 | private static final class ArrayLister<BeanT,ItemT> extends Lister<BeanT,ItemT[],ItemT,Pack<ItemT>> { |
aoqi@0 | 183 | |
aoqi@0 | 184 | private final Class<ItemT> itemType; |
aoqi@0 | 185 | |
aoqi@0 | 186 | public ArrayLister(Class<ItemT> itemType) { |
aoqi@0 | 187 | this.itemType = itemType; |
aoqi@0 | 188 | } |
aoqi@0 | 189 | |
aoqi@0 | 190 | public ListIterator<ItemT> iterator(final ItemT[] objects, XMLSerializer context) { |
aoqi@0 | 191 | return new ListIterator<ItemT>() { |
aoqi@0 | 192 | int idx=0; |
aoqi@0 | 193 | public boolean hasNext() { |
aoqi@0 | 194 | return idx<objects.length; |
aoqi@0 | 195 | } |
aoqi@0 | 196 | |
aoqi@0 | 197 | public ItemT next() { |
aoqi@0 | 198 | return objects[idx++]; |
aoqi@0 | 199 | } |
aoqi@0 | 200 | }; |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | public Pack startPacking(BeanT current, Accessor<BeanT, ItemT[]> acc) { |
aoqi@0 | 204 | return new Pack<ItemT>(itemType); |
aoqi@0 | 205 | } |
aoqi@0 | 206 | |
aoqi@0 | 207 | public void addToPack(Pack<ItemT> objects, ItemT o) { |
aoqi@0 | 208 | objects.add(o); |
aoqi@0 | 209 | } |
aoqi@0 | 210 | |
aoqi@0 | 211 | public void endPacking( Pack<ItemT> pack, BeanT bean, Accessor<BeanT,ItemT[]> acc ) throws AccessorException { |
aoqi@0 | 212 | acc.set(bean,pack.build()); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | |
aoqi@0 | 215 | public void reset(BeanT o,Accessor<BeanT,ItemT[]> acc) throws AccessorException { |
aoqi@0 | 216 | acc.set(o,(ItemT[])Array.newInstance(itemType,0)); |
aoqi@0 | 217 | } |
aoqi@0 | 218 | |
aoqi@0 | 219 | } |
aoqi@0 | 220 | |
aoqi@0 | 221 | public static final class Pack<ItemT> extends ArrayList<ItemT> { |
aoqi@0 | 222 | private final Class<ItemT> itemType; |
aoqi@0 | 223 | |
aoqi@0 | 224 | public Pack(Class<ItemT> itemType) { |
aoqi@0 | 225 | this.itemType = itemType; |
aoqi@0 | 226 | } |
aoqi@0 | 227 | |
aoqi@0 | 228 | public ItemT[] build() { |
aoqi@0 | 229 | return super.toArray( (ItemT[])Array.newInstance(itemType,size()) ); |
aoqi@0 | 230 | } |
aoqi@0 | 231 | } |
aoqi@0 | 232 | |
aoqi@0 | 233 | /** |
aoqi@0 | 234 | * Listers for the primitive type arrays, keyed by their primitive Class object. |
aoqi@0 | 235 | */ |
aoqi@0 | 236 | /*package*/ static final Map<Class,Lister> primitiveArrayListers = new HashMap<Class,Lister>(); |
aoqi@0 | 237 | |
aoqi@0 | 238 | static { |
aoqi@0 | 239 | // register primitive array listers |
aoqi@0 | 240 | PrimitiveArrayListerBoolean.register(); |
aoqi@0 | 241 | PrimitiveArrayListerByte.register(); |
aoqi@0 | 242 | PrimitiveArrayListerCharacter.register(); |
aoqi@0 | 243 | PrimitiveArrayListerDouble.register(); |
aoqi@0 | 244 | PrimitiveArrayListerFloat.register(); |
aoqi@0 | 245 | PrimitiveArrayListerInteger.register(); |
aoqi@0 | 246 | PrimitiveArrayListerLong.register(); |
aoqi@0 | 247 | PrimitiveArrayListerShort.register(); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | |
aoqi@0 | 250 | /** |
aoqi@0 | 251 | * {@link Lister} for a collection |
aoqi@0 | 252 | */ |
aoqi@0 | 253 | public static final class CollectionLister<BeanT,T extends Collection> extends Lister<BeanT,T,Object,T> { |
aoqi@0 | 254 | |
aoqi@0 | 255 | /** |
aoqi@0 | 256 | * Sometimes we need to create a new instance of a collection. |
aoqi@0 | 257 | * This is such an implementation class. |
aoqi@0 | 258 | */ |
aoqi@0 | 259 | private final Class<? extends T> implClass; |
aoqi@0 | 260 | |
aoqi@0 | 261 | public CollectionLister(Class<? extends T> implClass) { |
aoqi@0 | 262 | this.implClass = implClass; |
aoqi@0 | 263 | } |
aoqi@0 | 264 | |
aoqi@0 | 265 | public ListIterator iterator(T collection, XMLSerializer context) { |
aoqi@0 | 266 | final Iterator itr = collection.iterator(); |
aoqi@0 | 267 | return new ListIterator() { |
aoqi@0 | 268 | public boolean hasNext() { |
aoqi@0 | 269 | return itr.hasNext(); |
aoqi@0 | 270 | } |
aoqi@0 | 271 | public Object next() { |
aoqi@0 | 272 | return itr.next(); |
aoqi@0 | 273 | } |
aoqi@0 | 274 | }; |
aoqi@0 | 275 | } |
aoqi@0 | 276 | |
aoqi@0 | 277 | public T startPacking(BeanT bean, Accessor<BeanT, T> acc) throws AccessorException { |
aoqi@0 | 278 | T collection = acc.get(bean); |
aoqi@0 | 279 | if(collection==null) { |
aoqi@0 | 280 | collection = ClassFactory.create(implClass); |
aoqi@0 | 281 | if(!acc.isAdapted()) |
aoqi@0 | 282 | acc.set(bean,collection); |
aoqi@0 | 283 | } |
aoqi@0 | 284 | collection.clear(); |
aoqi@0 | 285 | return collection; |
aoqi@0 | 286 | } |
aoqi@0 | 287 | |
aoqi@0 | 288 | public void addToPack(T collection, Object o) { |
aoqi@0 | 289 | collection.add(o); |
aoqi@0 | 290 | } |
aoqi@0 | 291 | |
aoqi@0 | 292 | public void endPacking( T collection, BeanT bean, Accessor<BeanT,T> acc ) throws AccessorException { |
aoqi@0 | 293 | // this needs to be done in the endPacking, because |
aoqi@0 | 294 | // sometimes the accessor uses an adapter, and the adapter needs to see |
aoqi@0 | 295 | // the whole thing. |
aoqi@0 | 296 | |
aoqi@0 | 297 | // but always doing so causes a problem when this collection property |
aoqi@0 | 298 | // is getter-only |
aoqi@0 | 299 | |
aoqi@0 | 300 | // invoke set when possible (see Issue 488) |
aoqi@0 | 301 | try { |
aoqi@0 | 302 | if (acc.isAdapted()) { |
aoqi@0 | 303 | acc.set(bean,collection); |
aoqi@0 | 304 | } |
aoqi@0 | 305 | } catch (AccessorException ae) { |
aoqi@0 | 306 | if(acc.isAdapted()) throw ae; |
aoqi@0 | 307 | } |
aoqi@0 | 308 | } |
aoqi@0 | 309 | |
aoqi@0 | 310 | public void reset(BeanT bean, Accessor<BeanT, T> acc) throws AccessorException { |
aoqi@0 | 311 | T collection = acc.get(bean); |
aoqi@0 | 312 | if(collection == null) { |
aoqi@0 | 313 | return; |
aoqi@0 | 314 | } |
aoqi@0 | 315 | collection.clear(); |
aoqi@0 | 316 | } |
aoqi@0 | 317 | } |
aoqi@0 | 318 | |
aoqi@0 | 319 | /** |
aoqi@0 | 320 | * {@link Lister} for IDREFS. |
aoqi@0 | 321 | */ |
aoqi@0 | 322 | private static final class IDREFS<BeanT,PropT> extends Lister<BeanT,PropT,String,IDREFS<BeanT,PropT>.Pack> { |
aoqi@0 | 323 | private final Lister<BeanT,PropT,Object,Object> core; |
aoqi@0 | 324 | /** |
aoqi@0 | 325 | * Expected type to which IDREF resolves to. |
aoqi@0 | 326 | */ |
aoqi@0 | 327 | private final Class itemType; |
aoqi@0 | 328 | |
aoqi@0 | 329 | public IDREFS(Lister core, Class itemType) { |
aoqi@0 | 330 | this.core = core; |
aoqi@0 | 331 | this.itemType = itemType; |
aoqi@0 | 332 | } |
aoqi@0 | 333 | |
aoqi@0 | 334 | public ListIterator<String> iterator(PropT prop, XMLSerializer context) { |
aoqi@0 | 335 | final ListIterator i = core.iterator(prop,context); |
aoqi@0 | 336 | |
aoqi@0 | 337 | return new IDREFSIterator(i, context); |
aoqi@0 | 338 | } |
aoqi@0 | 339 | |
aoqi@0 | 340 | public Pack startPacking(BeanT bean, Accessor<BeanT, PropT> acc) { |
aoqi@0 | 341 | return new Pack(bean,acc); |
aoqi@0 | 342 | } |
aoqi@0 | 343 | |
aoqi@0 | 344 | public void addToPack(Pack pack, String item) { |
aoqi@0 | 345 | pack.add(item); |
aoqi@0 | 346 | } |
aoqi@0 | 347 | |
aoqi@0 | 348 | public void endPacking(Pack pack, BeanT bean, Accessor<BeanT, PropT> acc) { |
aoqi@0 | 349 | } |
aoqi@0 | 350 | |
aoqi@0 | 351 | public void reset(BeanT bean, Accessor<BeanT, PropT> acc) throws AccessorException { |
aoqi@0 | 352 | core.reset(bean,acc); |
aoqi@0 | 353 | } |
aoqi@0 | 354 | |
aoqi@0 | 355 | /** |
aoqi@0 | 356 | * PackT for this lister. |
aoqi@0 | 357 | */ |
aoqi@0 | 358 | private class Pack implements Patcher { |
aoqi@0 | 359 | private final BeanT bean; |
aoqi@0 | 360 | private final List<String> idrefs = new ArrayList<String>(); |
aoqi@0 | 361 | private final UnmarshallingContext context; |
aoqi@0 | 362 | private final Accessor<BeanT,PropT> acc; |
aoqi@0 | 363 | private final LocatorEx location; |
aoqi@0 | 364 | |
aoqi@0 | 365 | public Pack(BeanT bean, Accessor<BeanT,PropT> acc) { |
aoqi@0 | 366 | this.bean = bean; |
aoqi@0 | 367 | this.acc = acc; |
aoqi@0 | 368 | this.context = UnmarshallingContext.getInstance(); |
aoqi@0 | 369 | this.location = new LocatorEx.Snapshot(context.getLocator()); |
aoqi@0 | 370 | context.addPatcher(this); |
aoqi@0 | 371 | } |
aoqi@0 | 372 | |
aoqi@0 | 373 | public void add(String item) { |
aoqi@0 | 374 | idrefs.add(item); |
aoqi@0 | 375 | } |
aoqi@0 | 376 | |
aoqi@0 | 377 | /** |
aoqi@0 | 378 | * Resolves IDREFS and fill in the actual array. |
aoqi@0 | 379 | */ |
aoqi@0 | 380 | public void run() throws SAXException { |
aoqi@0 | 381 | try { |
aoqi@0 | 382 | Object pack = core.startPacking(bean,acc); |
aoqi@0 | 383 | |
aoqi@0 | 384 | for( String id : idrefs ) { |
aoqi@0 | 385 | Callable callable = context.getObjectFromId(id,itemType); |
aoqi@0 | 386 | Object t; |
aoqi@0 | 387 | |
aoqi@0 | 388 | try { |
aoqi@0 | 389 | t = (callable!=null) ? callable.call() : null; |
aoqi@0 | 390 | } catch (SAXException e) { |
aoqi@0 | 391 | throw e; |
aoqi@0 | 392 | } catch (Exception e) { |
aoqi@0 | 393 | throw new SAXException2(e); |
aoqi@0 | 394 | } |
aoqi@0 | 395 | |
aoqi@0 | 396 | if(t==null) { |
aoqi@0 | 397 | context.errorUnresolvedIDREF(bean,id,location); |
aoqi@0 | 398 | } else { |
aoqi@0 | 399 | TODO.prototype(); // TODO: check if the type of t is proper. |
aoqi@0 | 400 | core.addToPack(pack,t); |
aoqi@0 | 401 | } |
aoqi@0 | 402 | } |
aoqi@0 | 403 | |
aoqi@0 | 404 | core.endPacking(pack,bean,acc); |
aoqi@0 | 405 | } catch (AccessorException e) { |
aoqi@0 | 406 | context.handleError(e); |
aoqi@0 | 407 | } |
aoqi@0 | 408 | } |
aoqi@0 | 409 | } |
aoqi@0 | 410 | } |
aoqi@0 | 411 | |
aoqi@0 | 412 | /** |
aoqi@0 | 413 | * {@link Iterator} for IDREFS lister. |
aoqi@0 | 414 | * |
aoqi@0 | 415 | * <p> |
aoqi@0 | 416 | * Only in ArrayElementProperty we need to get the actual |
aoqi@0 | 417 | * referenced object. This is a kind of ugly way to make that work. |
aoqi@0 | 418 | */ |
aoqi@0 | 419 | public static final class IDREFSIterator implements ListIterator<String> { |
aoqi@0 | 420 | private final ListIterator i; |
aoqi@0 | 421 | private final XMLSerializer context; |
aoqi@0 | 422 | private Object last; |
aoqi@0 | 423 | |
aoqi@0 | 424 | private IDREFSIterator(ListIterator i, XMLSerializer context) { |
aoqi@0 | 425 | this.i = i; |
aoqi@0 | 426 | this.context = context; |
aoqi@0 | 427 | } |
aoqi@0 | 428 | |
aoqi@0 | 429 | public boolean hasNext() { |
aoqi@0 | 430 | return i.hasNext(); |
aoqi@0 | 431 | } |
aoqi@0 | 432 | |
aoqi@0 | 433 | /** |
aoqi@0 | 434 | * Returns the last referenced object (not just its ID) |
aoqi@0 | 435 | */ |
aoqi@0 | 436 | public Object last() { |
aoqi@0 | 437 | return last; |
aoqi@0 | 438 | } |
aoqi@0 | 439 | |
aoqi@0 | 440 | public String next() throws SAXException, JAXBException { |
aoqi@0 | 441 | last = i.next(); |
aoqi@0 | 442 | String id = context.grammar.getBeanInfo(last,true).getId(last,context); |
aoqi@0 | 443 | if(id==null) { |
aoqi@0 | 444 | context.errorMissingId(last); |
aoqi@0 | 445 | } |
aoqi@0 | 446 | return id; |
aoqi@0 | 447 | } |
aoqi@0 | 448 | } |
aoqi@0 | 449 | |
aoqi@0 | 450 | /** |
aoqi@0 | 451 | * Gets the special {@link Lister} used to recover from an error. |
aoqi@0 | 452 | */ |
aoqi@0 | 453 | @SuppressWarnings("unchecked") |
aoqi@0 | 454 | public static <A,B,C,D> Lister<A,B,C,D> getErrorInstance() { |
aoqi@0 | 455 | return ERROR; |
aoqi@0 | 456 | } |
aoqi@0 | 457 | |
aoqi@0 | 458 | public static final Lister ERROR = new Lister() { |
aoqi@0 | 459 | public ListIterator iterator(Object o, XMLSerializer context) { |
aoqi@0 | 460 | return EMPTY_ITERATOR; |
aoqi@0 | 461 | } |
aoqi@0 | 462 | |
aoqi@0 | 463 | public Object startPacking(Object o, Accessor accessor) { |
aoqi@0 | 464 | return null; |
aoqi@0 | 465 | } |
aoqi@0 | 466 | |
aoqi@0 | 467 | public void addToPack(Object o, Object o1) { |
aoqi@0 | 468 | } |
aoqi@0 | 469 | |
aoqi@0 | 470 | public void endPacking(Object o, Object o1, Accessor accessor) { |
aoqi@0 | 471 | } |
aoqi@0 | 472 | |
aoqi@0 | 473 | public void reset(Object o, Accessor accessor) { |
aoqi@0 | 474 | } |
aoqi@0 | 475 | }; |
aoqi@0 | 476 | |
aoqi@0 | 477 | private static final ListIterator EMPTY_ITERATOR = new ListIterator() { |
aoqi@0 | 478 | public boolean hasNext() { |
aoqi@0 | 479 | return false; |
aoqi@0 | 480 | } |
aoqi@0 | 481 | |
aoqi@0 | 482 | public Object next() { |
aoqi@0 | 483 | throw new IllegalStateException(); |
aoqi@0 | 484 | } |
aoqi@0 | 485 | }; |
aoqi@0 | 486 | |
aoqi@0 | 487 | private static final Class[] COLLECTION_IMPL_CLASSES = new Class[] { |
aoqi@0 | 488 | ArrayList.class, |
aoqi@0 | 489 | LinkedList.class, |
aoqi@0 | 490 | HashSet.class, |
aoqi@0 | 491 | TreeSet.class, |
aoqi@0 | 492 | Stack.class |
aoqi@0 | 493 | }; |
aoqi@0 | 494 | } |