1 /*
   2  * Copyright (c) 2017, 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.messaging.saaj.soap.impl;
  27 
  28 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
  29 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
  30 import java.util.logging.Logger;
  31 import javax.xml.soap.SOAPElement;
  32 import javax.xml.soap.SOAPException;
  33 import javax.xml.soap.Text;
  34 import org.w3c.dom.CharacterData;
  35 import org.w3c.dom.DOMException;
  36 import org.w3c.dom.Document;
  37 import org.w3c.dom.NamedNodeMap;
  38 import org.w3c.dom.Node;
  39 import org.w3c.dom.NodeList;
  40 import org.w3c.dom.UserDataHandler;
  41 
  42 /**
  43  *
  44  * @author lukas
  45  * @param <T>
  46  */
  47 public abstract class TextImpl<T extends CharacterData> implements Text, CharacterData {
  48 
  49     protected static final Logger log
  50             = Logger.getLogger(LogDomainConstants.SOAP_IMPL_DOMAIN,
  51                     "com.sun.xml.internal.messaging.saaj.soap.impl.LocalStrings");
  52     private final T domNode;
  53 
  54     protected TextImpl(SOAPDocumentImpl ownerDoc, String text) {
  55         domNode = createN(ownerDoc, text);
  56         ownerDoc.register(this);
  57     }
  58 
  59     protected abstract T createN(SOAPDocumentImpl ownerDoc, String text);
  60 
  61     public T getDomElement() {
  62         return domNode;
  63     }
  64 
  65     @Override
  66     public String getValue() {
  67         String nodeValue = getNodeValue();
  68         return (nodeValue.equals("") ? null : nodeValue);
  69     }
  70 
  71     @Override
  72     public void setValue(String text) {
  73         setNodeValue(text);
  74     }
  75 
  76     @Override
  77     public void setParentElement(SOAPElement parent) throws SOAPException {
  78         if (parent == null) {
  79             log.severe("SAAJ0112.impl.no.null.to.parent.elem");
  80             throw new SOAPException("Cannot pass NULL to setParentElement");
  81         }
  82         ((ElementImpl) parent).addNode(this);
  83     }
  84 
  85     @Override
  86     public SOAPElement getParentElement() {
  87         return (SOAPElement) getParentNode();
  88     }
  89 
  90     @Override
  91     public void detachNode() {
  92         org.w3c.dom.Node parent = getParentNode();
  93         if (parent != null) {
  94              parent.removeChild(getDomElement());
  95         }
  96     }
  97 
  98     @Override
  99     public void recycleNode() {
 100         detachNode();
 101         // TBD
 102         //  - add this to the factory so subsequent
 103         //    creations can reuse this object.
 104     }
 105 
 106     @Override
 107     public String getNodeName() {
 108         return domNode.getNodeName();
 109     }
 110 
 111     @Override
 112     public String getNodeValue() throws DOMException {
 113         return domNode.getNodeValue();
 114     }
 115 
 116     @Override
 117     public void setNodeValue(String nodeValue) throws DOMException {
 118         domNode.setNodeValue(nodeValue);
 119     }
 120 
 121     @Override
 122     public short getNodeType() {
 123         return domNode.getNodeType();
 124     }
 125 
 126     @Override
 127     public Node getParentNode() {
 128         return domNode.getParentNode();
 129     }
 130 
 131     @Override
 132     public NodeList getChildNodes() {
 133         return domNode.getChildNodes();
 134     }
 135 
 136     @Override
 137     public Node getFirstChild() {
 138         return domNode.getFirstChild();
 139     }
 140 
 141     @Override
 142     public Node getLastChild() {
 143         return domNode.getLastChild();
 144     }
 145 
 146     @Override
 147     public Node getPreviousSibling() {
 148         return domNode.getPreviousSibling();
 149     }
 150 
 151     @Override
 152     public Node getNextSibling() {
 153         return domNode.getNextSibling();
 154     }
 155 
 156     @Override
 157     public NamedNodeMap getAttributes() {
 158         return domNode.getAttributes();
 159     }
 160 
 161     @Override
 162     public Document getOwnerDocument() {
 163         return domNode.getOwnerDocument();
 164     }
 165 
 166     @Override
 167     public Node insertBefore(Node newChild, Node refChild) throws DOMException {
 168         return domNode.insertBefore(newChild, refChild);
 169     }
 170 
 171     @Override
 172     public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
 173         return domNode.replaceChild(newChild, oldChild);
 174     }
 175 
 176     @Override
 177     public Node removeChild(Node oldChild) throws DOMException {
 178         return domNode.removeChild(oldChild);
 179     }
 180 
 181     @Override
 182     public Node appendChild(Node newChild) throws DOMException {
 183         return domNode.appendChild(newChild);
 184     }
 185 
 186     @Override
 187     public boolean hasChildNodes() {
 188         return domNode.hasChildNodes();
 189     }
 190 
 191     @Override
 192     public Node cloneNode(boolean deep) {
 193         return domNode.cloneNode(deep);
 194     }
 195 
 196     @Override
 197     public void normalize() {
 198         domNode.normalize();
 199     }
 200 
 201     @Override
 202     public boolean isSupported(String feature, String version) {
 203         return domNode.isSupported(feature, version);
 204     }
 205 
 206     @Override
 207     public String getNamespaceURI() {
 208         return domNode.getNamespaceURI();
 209     }
 210 
 211     @Override
 212     public String getPrefix() {
 213         return domNode.getPrefix();
 214     }
 215 
 216     @Override
 217     public void setPrefix(String prefix) throws DOMException {
 218         domNode.setPrefix(prefix);
 219     }
 220 
 221     @Override
 222     public String getLocalName() {
 223         return domNode.getLocalName();
 224     }
 225 
 226     @Override
 227     public boolean hasAttributes() {
 228         return domNode.hasAttributes();
 229     }
 230 
 231     @Override
 232     public String getBaseURI() {
 233         return domNode.getBaseURI();
 234     }
 235 
 236     @Override
 237     public short compareDocumentPosition(Node other) throws DOMException {
 238         return domNode.compareDocumentPosition(other);
 239     }
 240 
 241     @Override
 242     public String getTextContent() throws DOMException {
 243         return domNode.getTextContent();
 244     }
 245 
 246     @Override
 247     public void setTextContent(String textContent) throws DOMException {
 248         domNode.setTextContent(textContent);;
 249     }
 250 
 251     @Override
 252     public boolean isSameNode(Node other) {
 253         return domNode.isSameNode(other);
 254     }
 255 
 256     @Override
 257     public String lookupPrefix(String namespaceURI) {
 258         return domNode.lookupPrefix(namespaceURI);
 259     }
 260 
 261     @Override
 262     public boolean isDefaultNamespace(String namespaceURI) {
 263         return domNode.isDefaultNamespace(namespaceURI);
 264     }
 265 
 266     @Override
 267     public String lookupNamespaceURI(String prefix) {
 268         return domNode.lookupNamespaceURI(prefix);
 269     }
 270 
 271     @Override
 272     public boolean isEqualNode(Node arg) {
 273         return domNode.isEqualNode(arg);
 274     }
 275 
 276     @Override
 277     public Object getFeature(String feature, String version) {
 278         return domNode.getFeature(feature, version);
 279     }
 280 
 281     @Override
 282     public Object setUserData(String key, Object data, UserDataHandler handler) {
 283         return domNode.setUserData(key, data, handler);
 284     }
 285 
 286     @Override
 287     public Object getUserData(String key) {
 288         return domNode.getUserData(key);
 289     }
 290 
 291     @Override
 292     public String getData() throws DOMException {
 293         return domNode.getData();
 294     }
 295 
 296     @Override
 297     public void setData(String data) throws DOMException {
 298         domNode.setData(data);
 299     }
 300 
 301     @Override
 302     public int getLength() {
 303         return domNode.getLength();
 304     }
 305 
 306     @Override
 307     public String substringData(int offset, int count) throws DOMException {
 308         return domNode.substringData(offset, count);
 309     }
 310 
 311     @Override
 312     public void appendData(String arg) throws DOMException {
 313         domNode.appendData(arg);
 314     }
 315 
 316     @Override
 317     public void insertData(int offset, String arg) throws DOMException {
 318         domNode.insertData(offset, arg);
 319     }
 320 
 321     @Override
 322     public void deleteData(int offset, int count) throws DOMException {
 323         domNode.deleteData(offset, count);
 324     }
 325 
 326     @Override
 327     public void replaceData(int offset, int count, String arg) throws DOMException {
 328         domNode.replaceData(offset, count, arg);
 329     }
 330 
 331 }