|
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.reflect; |
|
27 |
|
28 import javax.xml.bind.annotation.adapters.XmlAdapter; |
|
29 import javax.xml.bind.JAXBException; |
|
30 |
|
31 import com.sun.xml.internal.bind.v2.runtime.Coordinator; |
|
32 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; |
|
33 import com.sun.xml.internal.bind.api.AccessorException; |
|
34 |
|
35 import org.xml.sax.SAXException; |
|
36 |
|
37 /** |
|
38 * {@link Lister} that adapts individual item types. |
|
39 */ |
|
40 final class AdaptedLister<BeanT,PropT,InMemItemT,OnWireItemT,PackT> extends Lister<BeanT,PropT,OnWireItemT,PackT> { |
|
41 private final Lister<BeanT,PropT,InMemItemT,PackT> core; |
|
42 private final Class<? extends XmlAdapter<OnWireItemT,InMemItemT>> adapter; |
|
43 |
|
44 /*package*/ AdaptedLister( |
|
45 Lister<BeanT,PropT,InMemItemT,PackT> core, |
|
46 Class<? extends XmlAdapter<OnWireItemT,InMemItemT>> adapter) { |
|
47 |
|
48 this.core = core; |
|
49 this.adapter = adapter; |
|
50 } |
|
51 |
|
52 private XmlAdapter<OnWireItemT,InMemItemT> getAdapter() { |
|
53 return Coordinator._getInstance().getAdapter(adapter); |
|
54 } |
|
55 |
|
56 public ListIterator<OnWireItemT> iterator(PropT prop, XMLSerializer context) { |
|
57 return new ListIteratorImpl( core.iterator(prop,context), context ); |
|
58 } |
|
59 |
|
60 public PackT startPacking(BeanT bean, Accessor<BeanT, PropT> accessor) throws AccessorException { |
|
61 return core.startPacking(bean,accessor); |
|
62 } |
|
63 |
|
64 public void addToPack(PackT pack, OnWireItemT item) throws AccessorException { |
|
65 InMemItemT r; |
|
66 try { |
|
67 r = getAdapter().unmarshal(item); |
|
68 } catch (Exception e) { |
|
69 throw new AccessorException(e); |
|
70 } |
|
71 core.addToPack(pack,r); |
|
72 } |
|
73 |
|
74 public void endPacking(PackT pack, BeanT bean, Accessor<BeanT,PropT> accessor) throws AccessorException { |
|
75 core.endPacking(pack,bean,accessor); |
|
76 } |
|
77 |
|
78 public void reset(BeanT bean, Accessor<BeanT, PropT> accessor) throws AccessorException { |
|
79 core.reset(bean,accessor); |
|
80 } |
|
81 |
|
82 private final class ListIteratorImpl implements ListIterator<OnWireItemT> { |
|
83 private final ListIterator<InMemItemT> core; |
|
84 private final XMLSerializer serializer; |
|
85 |
|
86 public ListIteratorImpl(ListIterator<InMemItemT> core,XMLSerializer serializer) { |
|
87 this.core = core; |
|
88 this.serializer = serializer; |
|
89 } |
|
90 |
|
91 public boolean hasNext() { |
|
92 return core.hasNext(); |
|
93 } |
|
94 |
|
95 public OnWireItemT next() throws SAXException, JAXBException { |
|
96 InMemItemT next = core.next(); |
|
97 try { |
|
98 return getAdapter().marshal(next); |
|
99 } catch (Exception e) { |
|
100 serializer.reportError(null,e); |
|
101 return null; // recover this error by returning null |
|
102 } |
|
103 } |
|
104 } |
|
105 } |