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