Source for gnu.xml.dom.DomCharacterData

   1: /* DomCharacterData.java --
   2:    Copyright (C) 1999,2000,2001,2004,2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.xml.dom;
  39: 
  40: import org.w3c.dom.CharacterData;
  41: import org.w3c.dom.DOMException;
  42: import org.w3c.dom.Node;
  43: import org.w3c.dom.NodeList;
  44: import org.w3c.dom.events.MutationEvent;
  45: 
  46: 
  47: /**
  48:  * <p> Abstract "CharacterData" implementation.  This
  49:  * facilitates reusing code in classes implementing subtypes of that DOM
  50:  * interface (Text, Comment, CDATASection).  </p>
  51:  *
  52:  * @author David Brownell
  53:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  54:  */
  55: public abstract class DomCharacterData
  56:   extends DomNode
  57:   implements CharacterData
  58: {
  59: 
  60:   /**
  61:    * Empty node list representing the children of character data nodes.
  62:    */
  63:   static class EmptyNodeList
  64:     implements NodeList
  65:   {
  66: 
  67:     public int getLength()
  68:     {
  69:       return 0;
  70:     }
  71: 
  72:     public Node item(int index)
  73:     {
  74:       return null;
  75:     }
  76: 
  77:   }
  78: 
  79:   /**
  80:    * Singleton empty node list for character data nodes.
  81:    */
  82:   static final NodeList CHILD_NODES = new EmptyNodeList();
  83: 
  84:   private String text;
  85: 
  86:   // package private
  87:   DomCharacterData(short nodeType, DomDocument doc, String value)
  88:   {
  89:     super(nodeType, doc);
  90:     text = (value == null) ? "" : value;
  91:   }
  92: 
  93:   // package private
  94:   DomCharacterData(short nodeType, DomDocument doc,
  95:                    char[] buf, int offset, int length)
  96:   {
  97:     super(nodeType, doc);
  98:     text = (buf == null) ? "" : new String(buf, offset, length);
  99:   }
 100: 
 101:   /**
 102:    * <b>DOM L1</b>
 103:    * Appends the specified data to the value of this node.
 104:    * Causes a DOMCharacterDataModified mutation event to be reported.
 105:    */
 106:   public void appendData(String arg)
 107:   {
 108:     if (isReadonly())
 109:       {
 110:         throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
 111:       }
 112:     String value = text + arg;
 113:     mutating(value);
 114:     text = value;
 115:   }
 116: 
 117:   /**
 118:    * <b>DOM L1</b>
 119:    * Modifies the value of this node.
 120:    * Causes a DOMCharacterDataModified mutation event to be reported.
 121:    */
 122:   public void deleteData(int offset, int count)
 123:   {
 124:     if (isReadonly())
 125:       {
 126:         throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
 127:       }
 128:     char[] raw = text.toCharArray();
 129:     if (offset < 0 || count < 0 || offset > raw.length)
 130:       {
 131:         throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
 132:       }
 133:     if ((offset + count) > raw.length)
 134:       {
 135:         count = raw.length - offset;
 136:       }
 137:     if (count == 0)
 138:       {
 139:         return;
 140:       }
 141:     try
 142:       {
 143:         char[] buf = new char[raw.length - count];
 144:         System.arraycopy(raw, 0, buf, 0, offset);
 145:         System.arraycopy(raw, offset + count, buf, offset,
 146:                          raw.length - (offset + count));
 147:         String value = new String(buf);
 148:         mutating(value);
 149:         text = value;
 150:       }
 151:     catch (IndexOutOfBoundsException x)
 152:       {
 153:         throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
 154:       }
 155:   }
 156: 
 157:   /**
 158:    * <b>DOM L1</b>
 159:    * Returns the value of this node.
 160:    */
 161:   public String getNodeValue()
 162:   {
 163:     return text;
 164:   }
 165: 
 166:   /**
 167:    * <b>DOM L1</b>
 168:    * Returns the value of this node; same as getNodeValue.
 169:    */
 170:   public final String getData()
 171:   {
 172:     return text;
 173:   }
 174: 
 175:   /**
 176:    * <b>DOM L1</b>
 177:    * Returns the length of the data.
 178:    */
 179:   public int getLength()
 180:   {
 181:     return text.length();
 182:   }
 183: 
 184:   /**
 185:    * <b>DOM L1</b>
 186:    * Modifies the value of this node.
 187:    */
 188:   public void insertData(int offset, String arg)
 189:   {
 190:     if (isReadonly())
 191:       {
 192:       throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
 193:       }
 194:     char[] raw = text.toCharArray();
 195:     char[] tmp = arg.toCharArray ();
 196:     char[] buf = new char[raw.length + tmp.length];
 197: 
 198:     try
 199:       {
 200:         System.arraycopy(raw, 0, buf, 0, offset);
 201:         System.arraycopy(tmp, 0, buf, offset, tmp.length);
 202:         System.arraycopy(raw, offset, buf, offset + tmp.length,
 203:                          raw.length - offset);
 204:         String value = new String(buf);
 205:         mutating(value);
 206:         text = value;
 207:       }
 208:     catch (IndexOutOfBoundsException x)
 209:       {
 210:         throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
 211:       }
 212:   }
 213: 
 214:   /**
 215:    * <b>DOM L1</b>
 216:    * Modifies the value of this node.  Causes DOMCharacterDataModified
 217:    * mutation events to be reported (at least one).
 218:    */
 219:   public void replaceData(int offset, int count, String arg)
 220:   {
 221:     if (readonly)
 222:       {
 223:         throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
 224:       }
 225:     char[] raw = text.toCharArray();
 226: 
 227:     // deleteData
 228:     if (offset < 0 || count < 0 || offset > raw.length)
 229:       {
 230:         throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
 231:       }
 232:     if ((offset + count) > raw.length)
 233:       {
 234:         count = raw.length - offset;
 235:       }
 236:     try
 237:       {
 238:         char[] buf = new char[raw.length - count];
 239:         System.arraycopy(raw, 0, buf, 0, offset);
 240:         System.arraycopy(raw, offset + count, buf, offset,
 241:                          raw.length - (offset + count));
 242: 
 243:         // insertData
 244:         char[] tmp = arg.toCharArray ();
 245:         char[] buf2 = new char[buf.length + tmp.length];
 246:         System.arraycopy(raw, 0, buf, 0, offset);
 247:         System.arraycopy(tmp, 0, buf, offset, tmp.length);
 248:         System.arraycopy(raw, offset, buf, offset + tmp.length,
 249:                          raw.length - offset);
 250:         String value = new String(buf);
 251:         mutating(value);
 252:         text = value;
 253:       }
 254:     catch (IndexOutOfBoundsException x)
 255:       {
 256:         throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
 257:       }
 258:   }
 259: 
 260:   /**
 261:    * <b>DOM L1</b>
 262:    * Assigns the value of this node.
 263:    * Causes a DOMCharacterDataModified mutation event to be reported.
 264:    */
 265:   public void setNodeValue(String value)
 266:   {
 267:     if (isReadonly())
 268:       {
 269:         throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
 270:       }
 271:     if (value == null)
 272:       {
 273:         value = "";
 274:       }
 275:     mutating(value);
 276:     text = value;
 277:   }
 278: 
 279:   /**
 280:    * <b>DOM L1</b>
 281:    * Assigns the value of this node; same as setNodeValue.
 282:    */
 283:   final public void setData(String data)
 284:   {
 285:     setNodeValue(data);
 286:   }
 287: 
 288:   /**
 289:    * <b>DOM L1</b>
 290:    * Returns the specified substring.
 291:    */
 292:   public String substringData(int offset, int count)
 293:   {
 294:     try
 295:       {
 296:         return text.substring(offset, count);
 297:       }
 298:     catch (StringIndexOutOfBoundsException e)
 299:       {
 300:         if (offset >= 0 && count >= 0 && offset < text.length())
 301:           {
 302:             return text.substring(offset);
 303:           }
 304:         throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
 305:       }
 306:   }
 307: 
 308:   /**
 309:    * Returns an empty node list.
 310:    * Character data nodes do not have children.
 311:    */
 312:   public NodeList getChildNodes()
 313:   {
 314:     return CHILD_NODES;
 315:   }
 316: 
 317:   /**
 318:    * The base URI for character data is <code>null</code>.
 319:    * @since DOM Level 3 Core
 320:    */
 321:   public final String getBaseURI()
 322:   {
 323:     return null;
 324:   }
 325: 
 326:   private void mutating(String newValue)
 327:   {
 328:     if (!reportMutations)
 329:       {
 330:         return;
 331:       }
 332: 
 333:     // EVENT:  DOMCharacterDataModified, target = this,
 334:     //  prev/new values provided
 335:     MutationEvent  event;
 336: 
 337:     event = (MutationEvent) createEvent("MutationEvents");
 338:     event.initMutationEvent("DOMCharacterDataModified",
 339:                             true /* bubbles */, false /* nocancel */,
 340:                             null, text, newValue, null, (short) 0);
 341:     dispatchEvent(event);
 342:   }
 343: 
 344: }