1 /*
   2  * Copyright (c) 2014, 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.ws.api.message.saaj;
  27 
  28 import java.util.Arrays;
  29 import java.util.Iterator;
  30 
  31 import javax.xml.namespace.NamespaceContext;
  32 import javax.xml.namespace.QName;
  33 import javax.xml.soap.SOAPElement;
  34 import javax.xml.soap.SOAPException;
  35 import javax.xml.soap.SOAPMessage;
  36 import javax.xml.stream.XMLStreamException;
  37 import javax.xml.stream.XMLStreamWriter;
  38 
  39 import org.w3c.dom.Comment;
  40 import org.w3c.dom.Node;
  41 
  42 /**
  43  * SaajStaxWriter builds a SAAJ SOAPMessage by using XMLStreamWriter interface.
  44  *
  45  * @deprecated use com.sun.xml.internal.org.jvnet.staxex.util.SaajStaxWriter
  46  *
  47  * @author shih-chang.chen@oracle.com
  48  */
  49 public class SaajStaxWriter implements XMLStreamWriter {
  50 
  51     protected SOAPMessage soap;
  52     protected String envURI;
  53     protected SOAPElement currentElement;
  54 
  55     static final protected String Envelope = "Envelope";
  56     static final protected String Header = "Header";
  57     static final protected String Body = "Body";
  58     static final protected String xmlns = "xmlns";
  59 
  60     public SaajStaxWriter(final SOAPMessage msg) throws SOAPException {
  61         soap = msg;
  62         currentElement = soap.getSOAPPart().getEnvelope();
  63         envURI = currentElement.getNamespaceURI();
  64     }
  65 
  66     public SOAPMessage getSOAPMessage() {
  67         return soap;
  68     }
  69 
  70     @Override
  71     public void writeStartElement(final String localName) throws XMLStreamException {
  72         try {
  73             currentElement = currentElement.addChildElement(localName);
  74         } catch (SOAPException e) {
  75             throw new XMLStreamException(e);
  76         }
  77     }
  78 
  79     @Override
  80     public void writeStartElement(final String ns, final String ln) throws XMLStreamException {
  81         writeStartElement(null, ln, ns);
  82     }
  83 
  84     @Override
  85     public void writeStartElement(final String prefix, final String ln, final String ns) throws XMLStreamException {
  86         try {
  87             if (envURI.equals(ns)) {
  88                 if (Envelope.equals(ln)) {
  89                     currentElement = soap.getSOAPPart().getEnvelope();
  90                     fixPrefix(prefix);
  91                     return;
  92                 } else if (Header.equals(ln)) {
  93                     currentElement = soap.getSOAPHeader();
  94                     fixPrefix(prefix);
  95                     return;
  96                 } else if (Body.equals(ln)) {
  97                     currentElement = soap.getSOAPBody();
  98                     fixPrefix(prefix);
  99                     return;
 100                 }
 101             }
 102             currentElement = (prefix == null) ?
 103                     currentElement.addChildElement(new QName(ns, ln)) :
 104                     currentElement.addChildElement(ln, prefix, ns);
 105         } catch (SOAPException e) {
 106             throw new XMLStreamException(e);
 107         }
 108     }
 109 
 110     private void fixPrefix(final String prfx) throws XMLStreamException {
 111         String oldPrfx = currentElement.getPrefix();
 112         if (prfx != null && !prfx.equals(oldPrfx)) {
 113             currentElement.setPrefix(prfx);
 114         }
 115     }
 116 
 117     @Override
 118     public void writeEmptyElement(final String uri, final String ln) throws XMLStreamException {
 119         writeStartElement(null, ln, uri);
 120     }
 121 
 122     @Override
 123     public void writeEmptyElement(final String prefix, final String ln, final String uri) throws XMLStreamException {
 124         writeStartElement(prefix, ln, uri);
 125     }
 126 
 127     @Override
 128     public void writeEmptyElement(final String ln) throws XMLStreamException {
 129         writeStartElement(null, ln, null);
 130     }
 131 
 132     @Override
 133     public void writeEndElement() throws XMLStreamException {
 134         if (currentElement != null) currentElement = currentElement.getParentElement();
 135     }
 136 
 137     @Override
 138     public void writeEndDocument() throws XMLStreamException {
 139     }
 140 
 141     @Override
 142     public void close() throws XMLStreamException {
 143     }
 144 
 145     @Override
 146     public void flush() throws XMLStreamException {
 147     }
 148 
 149     @Override
 150     public void writeAttribute(final String ln, final String val) throws XMLStreamException {
 151         writeAttribute(null, null, ln, val);
 152     }
 153 
 154     @Override
 155     public void writeAttribute(final String prefix, final String ns, final String ln, final String value) throws XMLStreamException {
 156         try {
 157             if (ns == null) {
 158                 if (prefix == null && xmlns.equals(ln)) {
 159                     currentElement.addNamespaceDeclaration("", value);
 160                 } else {
 161                     currentElement.setAttributeNS("", ln, value);
 162                 }
 163             } else {
 164                 QName name = (prefix == null) ? new QName(ns, ln) : new QName(ns, ln, prefix);
 165                 currentElement.addAttribute(name, value);
 166             }
 167         } catch (SOAPException e) {
 168             throw new XMLStreamException(e);
 169         }
 170     }
 171 
 172     @Override
 173     public void writeAttribute(final String ns, final String ln, final String val) throws XMLStreamException {
 174         writeAttribute(null, ns, ln, val);
 175     }
 176 
 177     @Override
 178     public void writeNamespace(String prefix, final String uri) throws XMLStreamException {
 179 
 180         // make prefix default if null or "xmlns" (according to javadoc)
 181         if (prefix == null || "xmlns".equals(prefix)) {
 182             prefix = "";
 183         }
 184 
 185         try {
 186             currentElement.addNamespaceDeclaration(prefix, uri);
 187         } catch (SOAPException e) {
 188             throw new XMLStreamException(e);
 189         }
 190     }
 191 
 192     @Override
 193     public void writeDefaultNamespace(final String uri) throws XMLStreamException {
 194         writeNamespace("", uri);
 195     }
 196 
 197     @Override
 198     public void writeComment(final String data) throws XMLStreamException {
 199         Comment c = soap.getSOAPPart().createComment(data);
 200         currentElement.appendChild(c);
 201     }
 202 
 203     @Override
 204     public void writeProcessingInstruction(final String target) throws XMLStreamException {
 205         Node n = soap.getSOAPPart().createProcessingInstruction(target, "");
 206         currentElement.appendChild(n);
 207     }
 208 
 209     @Override
 210     public void writeProcessingInstruction(final String target, final String data) throws XMLStreamException {
 211         Node n = soap.getSOAPPart().createProcessingInstruction(target, data);
 212         currentElement.appendChild(n);
 213     }
 214 
 215     @Override
 216     public void writeCData(final String data) throws XMLStreamException {
 217         Node n = soap.getSOAPPart().createCDATASection(data);
 218         currentElement.appendChild(n);
 219     }
 220 
 221     @Override
 222     public void writeDTD(final String dtd) throws XMLStreamException {
 223         //TODO ... Don't do anything here
 224     }
 225 
 226     @Override
 227     public void writeEntityRef(final String name) throws XMLStreamException {
 228         Node n = soap.getSOAPPart().createEntityReference(name);
 229         currentElement.appendChild(n);
 230     }
 231 
 232     @Override
 233     public void writeStartDocument() throws XMLStreamException {
 234     }
 235 
 236     @Override
 237     public void writeStartDocument(final String version) throws XMLStreamException {
 238         if (version != null) soap.getSOAPPart().setXmlVersion(version);
 239     }
 240 
 241     @Override
 242     public void writeStartDocument(final String encoding, final String version) throws XMLStreamException {
 243         if (version != null) soap.getSOAPPart().setXmlVersion(version);
 244         if (encoding != null) {
 245             try {
 246                 soap.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, encoding);
 247             } catch (SOAPException e) {
 248                 throw new XMLStreamException(e);
 249             }
 250         }
 251     }
 252 
 253     @Override
 254     public void writeCharacters(final String text) throws XMLStreamException {
 255         try {
 256             currentElement.addTextNode(text);
 257         } catch (SOAPException e) {
 258             throw new XMLStreamException(e);
 259         }
 260     }
 261 
 262     @Override
 263     public void writeCharacters(final char[] text, final int start, final int len) throws XMLStreamException {
 264         char[] chr = (start == 0 && len == text.length) ? text : Arrays.copyOfRange(text, start, start + len);
 265         try {
 266             currentElement.addTextNode(new String(chr));
 267         } catch (SOAPException e) {
 268             throw new XMLStreamException(e);
 269         }
 270     }
 271 
 272     @Override
 273     public String getPrefix(final String uri) throws XMLStreamException {
 274         return currentElement.lookupPrefix(uri);
 275     }
 276 
 277     @Override
 278     public void setPrefix(final String prefix, final String uri) throws XMLStreamException {
 279         try {
 280             this.currentElement.addNamespaceDeclaration(prefix, uri);
 281         } catch (SOAPException e) {
 282             throw new XMLStreamException(e);
 283         }
 284     }
 285 
 286     @Override
 287     public void setDefaultNamespace(final String uri) throws XMLStreamException {
 288         setPrefix("", uri);
 289     }
 290 
 291     @Override
 292     public void setNamespaceContext(final NamespaceContext context)throws XMLStreamException {
 293         throw new UnsupportedOperationException();
 294     }
 295 
 296     @Override
 297     public Object getProperty(final String name) throws IllegalArgumentException {
 298         //TODO the following line is to make eclipselink happy ... they are aware of this problem -
 299         if (javax.xml.stream.XMLOutputFactory.IS_REPAIRING_NAMESPACES.equals(name)) return Boolean.FALSE;
 300         return null;
 301     }
 302 
 303     @Override
 304     public NamespaceContext getNamespaceContext() {
 305         return new NamespaceContext() {
 306             public String getNamespaceURI(final String prefix) {
 307                 return currentElement.getNamespaceURI(prefix);
 308             }
 309             public String getPrefix(final String namespaceURI) {
 310                 return currentElement.lookupPrefix(namespaceURI);
 311             }
 312             public Iterator getPrefixes(final String namespaceURI) {
 313                 return new Iterator() {
 314                     String prefix = getPrefix(namespaceURI);
 315                     public boolean hasNext() {
 316                         return (prefix != null);
 317                     }
 318                     public Object next() {
 319                         if (!hasNext()) throw new java.util.NoSuchElementException();
 320                         String next = prefix;
 321                         prefix = null;
 322                         return next;
 323                     }
 324                     public void remove() {}
 325                 };
 326             }
 327         };
 328     }
 329 }