|
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.io.InputStream; |
|
30 import java.io.OutputStream; |
|
31 |
|
32 import javax.xml.bind.JAXBException; |
|
33 import javax.xml.bind.MarshalException; |
|
34 import javax.xml.bind.Marshaller; |
|
35 import javax.xml.bind.UnmarshalException; |
|
36 import javax.xml.bind.Unmarshaller; |
|
37 import javax.xml.bind.annotation.adapters.XmlAdapter; |
|
38 import javax.xml.namespace.NamespaceContext; |
|
39 import javax.xml.stream.XMLStreamException; |
|
40 import javax.xml.stream.XMLStreamReader; |
|
41 import javax.xml.stream.XMLStreamWriter; |
|
42 import javax.xml.transform.Result; |
|
43 import javax.xml.transform.Source; |
|
44 |
|
45 import com.sun.istack.internal.NotNull; |
|
46 import com.sun.xml.internal.bind.api.Bridge; |
|
47 import com.sun.xml.internal.bind.api.TypeReference; |
|
48 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl; |
|
49 |
|
50 import org.w3c.dom.Node; |
|
51 import org.xml.sax.ContentHandler; |
|
52 import org.xml.sax.SAXException; |
|
53 |
|
54 /** |
|
55 * {@link Bridge} decorator for {@link XmlAdapter}. |
|
56 * |
|
57 * @author Kohsuke Kawaguchi |
|
58 */ |
|
59 final class BridgeAdapter<OnWire,InMemory> extends InternalBridge<InMemory> { |
|
60 private final InternalBridge<OnWire> core; |
|
61 private final Class<? extends XmlAdapter<OnWire,InMemory>> adapter; |
|
62 |
|
63 public BridgeAdapter(InternalBridge<OnWire> core, Class<? extends XmlAdapter<OnWire,InMemory>> adapter) { |
|
64 super(core.getContext()); |
|
65 this.core = core; |
|
66 this.adapter = adapter; |
|
67 } |
|
68 |
|
69 public void marshal(Marshaller m, InMemory inMemory, XMLStreamWriter output) throws JAXBException { |
|
70 core.marshal(m,adaptM(m,inMemory),output); |
|
71 } |
|
72 |
|
73 public void marshal(Marshaller m, InMemory inMemory, OutputStream output, NamespaceContext nsc) throws JAXBException { |
|
74 core.marshal(m,adaptM(m,inMemory),output,nsc); |
|
75 } |
|
76 |
|
77 public void marshal(Marshaller m, InMemory inMemory, Node output) throws JAXBException { |
|
78 core.marshal(m,adaptM(m,inMemory),output); |
|
79 } |
|
80 |
|
81 public void marshal(Marshaller context, InMemory inMemory, ContentHandler contentHandler) throws JAXBException { |
|
82 core.marshal(context,adaptM(context,inMemory),contentHandler); |
|
83 } |
|
84 |
|
85 public void marshal(Marshaller context, InMemory inMemory, Result result) throws JAXBException { |
|
86 core.marshal(context,adaptM(context,inMemory),result); |
|
87 } |
|
88 |
|
89 private OnWire adaptM(Marshaller m,InMemory v) throws JAXBException { |
|
90 XMLSerializer serializer = ((MarshallerImpl)m).serializer; |
|
91 serializer.pushCoordinator(); |
|
92 try { |
|
93 return _adaptM(serializer, v); |
|
94 } finally { |
|
95 serializer.popCoordinator(); |
|
96 } |
|
97 } |
|
98 |
|
99 private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException { |
|
100 XmlAdapter<OnWire,InMemory> a = serializer.getAdapter(adapter); |
|
101 try { |
|
102 return a.marshal(v); |
|
103 } catch (Exception e) { |
|
104 serializer.handleError(e,v,null); |
|
105 throw new MarshalException(e); |
|
106 } |
|
107 } |
|
108 |
|
109 |
|
110 public @NotNull InMemory unmarshal(Unmarshaller u, XMLStreamReader in) throws JAXBException { |
|
111 return adaptU(u, core.unmarshal(u,in)); |
|
112 } |
|
113 |
|
114 public @NotNull InMemory unmarshal(Unmarshaller u, Source in) throws JAXBException { |
|
115 return adaptU(u, core.unmarshal(u,in)); |
|
116 } |
|
117 |
|
118 public @NotNull InMemory unmarshal(Unmarshaller u, InputStream in) throws JAXBException { |
|
119 return adaptU(u, core.unmarshal(u,in)); |
|
120 } |
|
121 |
|
122 public @NotNull InMemory unmarshal(Unmarshaller u, Node n) throws JAXBException { |
|
123 return adaptU(u, core.unmarshal(u,n)); |
|
124 } |
|
125 |
|
126 public TypeReference getTypeReference() { |
|
127 return core.getTypeReference(); |
|
128 } |
|
129 |
|
130 private @NotNull InMemory adaptU(Unmarshaller _u, OnWire v) throws JAXBException { |
|
131 UnmarshallerImpl u = (UnmarshallerImpl) _u; |
|
132 XmlAdapter<OnWire,InMemory> a = u.coordinator.getAdapter(adapter); |
|
133 u.coordinator.pushCoordinator(); |
|
134 try { |
|
135 return a.unmarshal(v); |
|
136 } catch (Exception e) { |
|
137 throw new UnmarshalException(e); |
|
138 } finally { |
|
139 u.coordinator.popCoordinator(); |
|
140 } |
|
141 } |
|
142 |
|
143 void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException { |
|
144 try { |
|
145 core.marshal(_adaptM( XMLSerializer.getInstance(), o ), out ); |
|
146 } catch (MarshalException e) { |
|
147 // recover from error by not marshalling this element. |
|
148 } |
|
149 } |
|
150 } |