1/*
2 * Copyright (c) 1997, 2013, 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
26package com.sun.xml.internal.ws.message;
27
28import com.sun.xml.internal.bind.api.Bridge;
29import com.sun.xml.internal.bind.unmarshaller.DOMScanner;
30import com.sun.xml.internal.ws.streaming.DOMStreamReader;
31import com.sun.xml.internal.ws.util.DOMUtil;
32import org.w3c.dom.Element;
33import org.w3c.dom.Node;
34import org.xml.sax.ContentHandler;
35import org.xml.sax.ErrorHandler;
36import org.xml.sax.SAXException;
37
38import javax.xml.bind.JAXBException;
39import javax.xml.bind.Unmarshaller;
40import javax.xml.soap.SOAPException;
41import javax.xml.soap.SOAPHeader;
42import javax.xml.soap.SOAPMessage;
43import javax.xml.stream.XMLStreamException;
44import javax.xml.stream.XMLStreamReader;
45import javax.xml.stream.XMLStreamWriter;
46
47/**
48 * {@link com.sun.xml.internal.ws.api.message.Header} implementation for a DOM.
49 *
50 * @author Kohsuke Kawaguchi
51 */
52public class DOMHeader<N extends Element> extends AbstractHeaderImpl {
53    protected final N node;
54
55    private final String nsUri;
56    private final String localName;
57
58    public DOMHeader(N node) {
59        assert node!=null;
60        this.node = node;
61
62        this.nsUri = fixNull(node.getNamespaceURI());
63        this.localName = node.getLocalName();
64    }
65
66
67    public String getNamespaceURI() {
68        return nsUri;
69    }
70
71    public String getLocalPart() {
72        return localName;
73    }
74
75    public XMLStreamReader readHeader() throws XMLStreamException {
76        DOMStreamReader r = new DOMStreamReader(node);
77        r.nextTag();    // move ahead to the start tag
78        return r;
79    }
80
81    public <T> T readAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
82        return (T) unmarshaller.unmarshal(node);
83    }
84    /** @deprecated */
85    public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException {
86        return bridge.unmarshal(node);
87    }
88
89    public void writeTo(XMLStreamWriter w) throws XMLStreamException {
90        DOMUtil.serializeNode(node, w);
91    }
92
93    private static String fixNull(String s) {
94        if(s!=null)     return s;
95        else            return "";
96    }
97
98    public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
99        DOMScanner ds = new DOMScanner();
100        ds.setContentHandler(contentHandler);
101        ds.scan(node);
102    }
103
104    public String getAttribute(String nsUri, String localName) {
105        if(nsUri.length()==0)   nsUri=null; // DOM wants null, not "".
106        return node.getAttributeNS(nsUri,localName);
107    }
108
109    public void writeTo(SOAPMessage saaj) throws SOAPException {
110        SOAPHeader header = saaj.getSOAPHeader();
111        if(header == null)
112            header = saaj.getSOAPPart().getEnvelope().addHeader();
113        Node clone = header.getOwnerDocument().importNode(node,true);
114        header.appendChild(clone);
115    }
116
117    @Override
118    public String getStringContent() {
119        return node.getTextContent();
120    }
121
122    public N getWrappedNode() {
123        return node;
124    }
125
126
127    @Override
128    public int hashCode() {
129        return getWrappedNode().hashCode();
130    }
131
132
133    @Override
134    public boolean equals(Object obj) {
135        if (obj instanceof DOMHeader) {
136            return getWrappedNode().equals(((DOMHeader) obj).getWrappedNode());
137        } else {
138            return false;
139        }
140    }
141
142
143}
144