ohair@286: /* ohair@286: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.bind.v2.runtime; ohair@286: ohair@286: import java.io.IOException; ohair@286: import java.io.InputStream; ohair@286: import java.io.OutputStream; ohair@286: ohair@286: import javax.xml.bind.JAXBException; ohair@286: import javax.xml.bind.MarshalException; ohair@286: import javax.xml.bind.Marshaller; ohair@286: import javax.xml.bind.UnmarshalException; ohair@286: import javax.xml.bind.Unmarshaller; ohair@286: import javax.xml.bind.annotation.adapters.XmlAdapter; ohair@286: import javax.xml.namespace.NamespaceContext; ohair@286: import javax.xml.stream.XMLStreamException; ohair@286: import javax.xml.stream.XMLStreamReader; ohair@286: import javax.xml.stream.XMLStreamWriter; ohair@286: import javax.xml.transform.Result; ohair@286: import javax.xml.transform.Source; ohair@286: ohair@286: import com.sun.istack.internal.NotNull; ohair@286: import com.sun.xml.internal.bind.api.Bridge; ohair@286: import com.sun.xml.internal.bind.api.TypeReference; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl; ohair@286: ohair@286: import org.w3c.dom.Node; ohair@286: import org.xml.sax.ContentHandler; ohair@286: import org.xml.sax.SAXException; ohair@286: ohair@286: /** ohair@286: * {@link Bridge} decorator for {@link XmlAdapter}. ohair@286: * ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ ohair@286: final class BridgeAdapter extends InternalBridge { ohair@286: private final InternalBridge core; ohair@286: private final Class> adapter; ohair@286: ohair@286: public BridgeAdapter(InternalBridge core, Class> adapter) { ohair@286: super(core.getContext()); ohair@286: this.core = core; ohair@286: this.adapter = adapter; ohair@286: } ohair@286: ohair@286: public void marshal(Marshaller m, InMemory inMemory, XMLStreamWriter output) throws JAXBException { ohair@286: core.marshal(m,adaptM(m,inMemory),output); ohair@286: } ohair@286: ohair@286: public void marshal(Marshaller m, InMemory inMemory, OutputStream output, NamespaceContext nsc) throws JAXBException { ohair@286: core.marshal(m,adaptM(m,inMemory),output,nsc); ohair@286: } ohair@286: ohair@286: public void marshal(Marshaller m, InMemory inMemory, Node output) throws JAXBException { ohair@286: core.marshal(m,adaptM(m,inMemory),output); ohair@286: } ohair@286: ohair@286: public void marshal(Marshaller context, InMemory inMemory, ContentHandler contentHandler) throws JAXBException { ohair@286: core.marshal(context,adaptM(context,inMemory),contentHandler); ohair@286: } ohair@286: ohair@286: public void marshal(Marshaller context, InMemory inMemory, Result result) throws JAXBException { ohair@286: core.marshal(context,adaptM(context,inMemory),result); ohair@286: } ohair@286: ohair@286: private OnWire adaptM(Marshaller m,InMemory v) throws JAXBException { ohair@286: XMLSerializer serializer = ((MarshallerImpl)m).serializer; ohair@286: serializer.pushCoordinator(); ohair@286: try { ohair@286: return _adaptM(serializer, v); ohair@286: } finally { ohair@286: serializer.popCoordinator(); ohair@286: } ohair@286: } ohair@286: ohair@286: private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException { ohair@286: XmlAdapter a = serializer.getAdapter(adapter); ohair@286: try { ohair@286: return a.marshal(v); ohair@286: } catch (Exception e) { ohair@286: serializer.handleError(e,v,null); ohair@286: throw new MarshalException(e); ohair@286: } ohair@286: } ohair@286: ohair@286: ohair@286: public @NotNull InMemory unmarshal(Unmarshaller u, XMLStreamReader in) throws JAXBException { ohair@286: return adaptU(u, core.unmarshal(u,in)); ohair@286: } ohair@286: ohair@286: public @NotNull InMemory unmarshal(Unmarshaller u, Source in) throws JAXBException { ohair@286: return adaptU(u, core.unmarshal(u,in)); ohair@286: } ohair@286: ohair@286: public @NotNull InMemory unmarshal(Unmarshaller u, InputStream in) throws JAXBException { ohair@286: return adaptU(u, core.unmarshal(u,in)); ohair@286: } ohair@286: ohair@286: public @NotNull InMemory unmarshal(Unmarshaller u, Node n) throws JAXBException { ohair@286: return adaptU(u, core.unmarshal(u,n)); ohair@286: } ohair@286: ohair@286: public TypeReference getTypeReference() { ohair@286: return core.getTypeReference(); ohair@286: } ohair@286: ohair@286: private @NotNull InMemory adaptU(Unmarshaller _u, OnWire v) throws JAXBException { ohair@286: UnmarshallerImpl u = (UnmarshallerImpl) _u; ohair@286: XmlAdapter a = u.coordinator.getAdapter(adapter); ohair@286: u.coordinator.pushCoordinator(); ohair@286: try { ohair@286: return a.unmarshal(v); ohair@286: } catch (Exception e) { ohair@286: throw new UnmarshalException(e); ohair@286: } finally { ohair@286: u.coordinator.popCoordinator(); ohair@286: } ohair@286: } ohair@286: ohair@286: void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException { ohair@286: try { ohair@286: core.marshal(_adaptM( XMLSerializer.getInstance(), o ), out ); ohair@286: } catch (MarshalException e) { ohair@286: // recover from error by not marshalling this element. ohair@286: } ohair@286: } ohair@286: }