com.sun.xml.ws.message.DOMHeader Maven / Gradle / Ivy
/*
* Copyright (c) 1997, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.ws.message;
import com.sun.xml.bind.api.Bridge;
import com.sun.xml.bind.unmarshaller.DOMScanner;
import com.sun.xml.ws.streaming.DOMStreamReader;
import com.sun.xml.ws.util.DOMUtil;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
/**
* {@link com.sun.xml.ws.api.message.Header} implementation for a DOM.
*
* @author Kohsuke Kawaguchi
*/
public class DOMHeader extends AbstractHeaderImpl {
protected final N node;
private final String nsUri;
private final String localName;
public DOMHeader(N node) {
assert node!=null;
this.node = node;
this.nsUri = fixNull(node.getNamespaceURI());
this.localName = node.getLocalName();
}
public String getNamespaceURI() {
return nsUri;
}
public String getLocalPart() {
return localName;
}
public XMLStreamReader readHeader() throws XMLStreamException {
DOMStreamReader r = new DOMStreamReader(node);
r.nextTag(); // move ahead to the start tag
return r;
}
public T readAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
return (T) unmarshaller.unmarshal(node);
}
/** @deprecated */
public T readAsJAXB(Bridge bridge) throws JAXBException {
return bridge.unmarshal(node);
}
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
DOMUtil.serializeNode(node, w);
}
private static String fixNull(String s) {
if(s!=null) return s;
else return "";
}
public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
DOMScanner ds = new DOMScanner();
ds.setContentHandler(contentHandler);
ds.scan(node);
}
public String getAttribute(String nsUri, String localName) {
if(nsUri.length()==0) nsUri=null; // DOM wants null, not "".
return node.getAttributeNS(nsUri,localName);
}
public void writeTo(SOAPMessage saaj) throws SOAPException {
SOAPHeader header = saaj.getSOAPHeader();
if(header == null)
header = saaj.getSOAPPart().getEnvelope().addHeader();
Node clone = header.getOwnerDocument().importNode(node,true);
header.appendChild(clone);
}
@Override
public String getStringContent() {
return node.getTextContent();
}
public N getWrappedNode() {
return node;
}
@Override
public int hashCode() {
return getWrappedNode().hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof DOMHeader) {
return getWrappedNode().equals(((DOMHeader) obj).getWrappedNode());
} else {
return false;
}
}
}