001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: DOMCDATA.java,v 1.4 2001/06/20 18:59:23 jstrachan Exp $ 008 */ 009 010package org.dom4j.dom; 011 012import org.dom4j.CDATA; 013import org.dom4j.Element; 014import org.dom4j.QName; 015import org.dom4j.Text; 016import org.dom4j.tree.DefaultCDATA; 017 018import org.w3c.dom.Document; 019import org.w3c.dom.DOMException; 020import org.w3c.dom.NamedNodeMap; 021import org.w3c.dom.NodeList; 022 023/** <p><code>DOMCDATA</code> implements a CDATA Section which 024 * supports the W3C DOM API.</p> 025 * 026 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 027 * @version $Revision: 1.4 $ 028 */ 029public class DOMCDATA extends DefaultCDATA implements org.w3c.dom.CDATASection { 030 031 public DOMCDATA(String text) { 032 super(text); 033 } 034 035 public DOMCDATA(Element parent,String text) { 036 super(parent, text); 037 } 038 039 040 041 // org.w3c.dom.Node interface 042 //------------------------------------------------------------------------- 043 public String getNamespaceURI() { 044 return DOMNodeHelper.getNamespaceURI(this); 045 } 046 047 public String getPrefix() { 048 return DOMNodeHelper.getPrefix(this); 049 } 050 051 public void setPrefix(String prefix) throws DOMException { 052 DOMNodeHelper.setPrefix(this, prefix); 053 } 054 055 public String getLocalName() { 056 return DOMNodeHelper.getLocalName(this); 057 } 058 059 public String getNodeName() { 060 return getName(); 061 } 062 063 //already part of API 064 // 065 //public short getNodeType(); 066 067 068 069 public String getNodeValue() throws DOMException { 070 return DOMNodeHelper.getNodeValue(this); 071 } 072 073 public void setNodeValue(String nodeValue) throws DOMException { 074 DOMNodeHelper.setNodeValue(this, nodeValue); 075 } 076 077 078 public org.w3c.dom.Node getParentNode() { 079 return DOMNodeHelper.getParentNode(this); 080 } 081 082 public NodeList getChildNodes() { 083 return DOMNodeHelper.getChildNodes(this); 084 } 085 086 public org.w3c.dom.Node getFirstChild() { 087 return DOMNodeHelper.getFirstChild(this); 088 } 089 090 public org.w3c.dom.Node getLastChild() { 091 return DOMNodeHelper.getLastChild(this); 092 } 093 094 public org.w3c.dom.Node getPreviousSibling() { 095 return DOMNodeHelper.getPreviousSibling(this); 096 } 097 098 public org.w3c.dom.Node getNextSibling() { 099 return DOMNodeHelper.getNextSibling(this); 100 } 101 102 public NamedNodeMap getAttributes() { 103 return DOMNodeHelper.getAttributes(this); 104 } 105 106 public Document getOwnerDocument() { 107 return DOMNodeHelper.getOwnerDocument(this); 108 } 109 110 public org.w3c.dom.Node insertBefore( 111 org.w3c.dom.Node newChild, 112 org.w3c.dom.Node refChild 113 ) throws DOMException { 114 return DOMNodeHelper.insertBefore(this, newChild, refChild); 115 } 116 117 public org.w3c.dom.Node replaceChild( 118 org.w3c.dom.Node newChild, 119 org.w3c.dom.Node oldChild 120 ) throws DOMException { 121 return DOMNodeHelper.replaceChild(this, newChild, oldChild); 122 } 123 124 public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild) throws DOMException { 125 return DOMNodeHelper.removeChild(this, oldChild); 126 } 127 128 public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild) throws DOMException { 129 return DOMNodeHelper.appendChild(this, newChild); 130 } 131 132 public boolean hasChildNodes() { 133 return DOMNodeHelper.hasChildNodes(this); 134 } 135 136 public org.w3c.dom.Node cloneNode(boolean deep) { 137 return DOMNodeHelper.cloneNode(this, deep); 138 } 139 140 public void normalize() { 141 DOMNodeHelper.normalize(this); 142 } 143 144 public boolean isSupported(String feature, String version) { 145 return DOMNodeHelper.isSupported(this, feature, version); 146 } 147 148 public boolean hasAttributes() { 149 return DOMNodeHelper.hasAttributes(this); 150 } 151 152 // org.w3c.dom.CharacterData interface 153 //------------------------------------------------------------------------- 154 public String getData() throws DOMException { 155 return DOMNodeHelper.getData(this); 156 } 157 158 public void setData(String data) throws DOMException { 159 DOMNodeHelper.setData(this, data); 160 } 161 162 public int getLength() { 163 return DOMNodeHelper.getLength(this); 164 } 165 166 public String substringData( int offset, int count) throws DOMException { 167 return DOMNodeHelper.substringData(this, offset, count); 168 } 169 170 public void appendData(String arg) throws DOMException { 171 DOMNodeHelper.appendData(this, arg); 172 } 173 174 public void insertData(int offset, String arg) throws DOMException { 175 DOMNodeHelper.insertData(this, offset, arg); 176 } 177 178 public void deleteData(int offset, int count) throws DOMException { 179 DOMNodeHelper.deleteData(this, offset, count); 180 } 181 182 public void replaceData( 183 int offset, int count, String arg 184 ) throws DOMException { 185 DOMNodeHelper.replaceData(this, offset, count, arg); 186 } 187 188 // org.w3c.dom.Text interface 189 //------------------------------------------------------------------------- 190 public org.w3c.dom.Text splitText(int offset) throws DOMException { 191 if ( isReadOnly() ) { 192 throw new DOMException( 193 DOMException.NO_MODIFICATION_ALLOWED_ERR, 194 "CharacterData node is read only: " + this 195 ); 196 } 197 else { 198 String text = getText(); 199 int length = (text != null) ? text.length() : 0; 200 if ( offset < 0 || offset >= length ) { 201 throw new DOMException( 202 DOMException.INDEX_SIZE_ERR, 203 "No text at offset: " + offset 204 ); 205 } 206 else { 207 String start = text.substring(0, offset); 208 String rest = text.substring(offset); 209 setText(start); 210 Element parent = getParent(); 211 CDATA newText = createCDATA(rest); 212 if ( parent != null ) { 213 parent.add( newText ); 214 } 215 return DOMNodeHelper.asDOMText( newText ); 216 } 217 } 218 } 219 220 // Implementation methods 221 //------------------------------------------------------------------------- 222 protected CDATA createCDATA(String text) { 223 return new DOMCDATA( text ); 224 } 225} 226 227 228 229 230/* 231 * Redistribution and use of this software and associated documentation 232 * ("Software"), with or without modification, are permitted provided 233 * that the following conditions are met: 234 * 235 * 1. Redistributions of source code must retain copyright 236 * statements and notices. Redistributions must also contain a 237 * copy of this document. 238 * 239 * 2. Redistributions in binary form must reproduce the 240 * above copyright notice, this list of conditions and the 241 * following disclaimer in the documentation and/or other 242 * materials provided with the distribution. 243 * 244 * 3. The name "DOM4J" must not be used to endorse or promote 245 * products derived from this Software without prior written 246 * permission of MetaStuff, Ltd. For written permission, 247 * please contact dom4j-info@metastuff.com. 248 * 249 * 4. Products derived from this Software may not be called "DOM4J" 250 * nor may "DOM4J" appear in their names without prior written 251 * permission of MetaStuff, Ltd. DOM4J is a registered 252 * trademark of MetaStuff, Ltd. 253 * 254 * 5. Due credit should be given to the DOM4J Project 255 * (http://dom4j.org/). 256 * 257 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 258 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 259 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 260 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 261 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 262 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 263 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 264 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 265 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 266 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 267 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 268 * OF THE POSSIBILITY OF SUCH DAMAGE. 269 * 270 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 271 * 272 * $Id: DOMCDATA.java,v 1.4 2001/06/20 18:59:23 jstrachan Exp $ 273 */