Source for gnu.xml.dom.DomText

   1: /* DomText.java --
   2:    Copyright (C) 1999, 2000, 2001, 2004 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 gnu.java.lang.CPStringBuilder;
  41: 
  42: import org.w3c.dom.DOMException;
  43: import org.w3c.dom.Text;
  44: 
  45: /**
  46:  * <p> "Text" implementation.  </p>
  47:  *
  48:  * @author David Brownell
  49:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  50:  */
  51: public class DomText
  52:   extends DomCharacterData
  53:   implements Text
  54: {
  55: 
  56:   // NOTE:  deleted unused per-instance "isIgnorable"
  57:   // support to reclaim its space.
  58: 
  59:   /**
  60:    * Constructs a text node associated with the specified
  61:    * document and holding the specified data.
  62:    *
  63:    * <p>This constructor should only be invoked by a Document object
  64:    * as part of its createTextNode functionality, or through a subclass
  65:    * which is similarly used in a "Sub-DOM" style layer.
  66:    */
  67:   protected DomText(DomDocument owner, String value)
  68:   {
  69:     super(TEXT_NODE, owner, value);
  70:   }
  71: 
  72:   protected DomText(DomDocument owner, char[] buf, int off, int len)
  73:   {
  74:     super(TEXT_NODE, owner, buf, off, len);
  75:   }
  76: 
  77:   // Used by DomCDATA
  78:   DomText(short nodeType, DomDocument owner, String value)
  79:   {
  80:     super(nodeType, owner, value);
  81:   }
  82: 
  83:   DomText(short nodeType, DomDocument owner, char[] buf, int off, int len)
  84:   {
  85:     super(nodeType, owner, buf, off, len);
  86:   }
  87: 
  88:   /**
  89:    * <b>DOM L1</b>
  90:    * Returns the string "#text".
  91:    */
  92:   // can't be 'final' with CDATA subclassing
  93:   public String getNodeName()
  94:   {
  95:     return "#text";
  96:   }
  97: 
  98:   /**
  99:    * <b>DOM L1</b>
 100:    * Splits this text node in two parts at the offset, returning
 101:    * the new text node (the sibling with the second part).
 102:    */
 103:   public Text splitText(int offset)
 104:   {
 105:     if (isReadonly())
 106:       {
 107:         throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
 108:       }
 109:     try
 110:       {
 111:         String text = getNodeValue();
 112:         String before = text.substring(0, offset);
 113:         String after = text.substring(offset);
 114:         Text next;
 115: 
 116:         if (getNodeType() == TEXT_NODE)
 117:           {
 118:             next = owner.createTextNode(after);
 119:           }
 120:         else // CDATA_SECTION_NODE
 121:           {
 122:             next = owner.createCDATASection(after);
 123:           }
 124: 
 125:         if (this.next != null)
 126:           {
 127:             parent.insertBefore(next, this.next);
 128:           }
 129:         else
 130:           {
 131:             parent.appendChild(next);
 132:           }
 133:         setNodeValue(before);
 134:         return next;
 135: 
 136:       }
 137:     catch (IndexOutOfBoundsException x)
 138:       {
 139:         throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
 140:       }
 141:   }
 142: 
 143:   // DOM Level 3
 144: 
 145:   public boolean isElementContentWhitespace()
 146:   {
 147:     if (parent != null)
 148:       {
 149:         DomDoctype doctype = (DomDoctype) owner.getDoctype();
 150:         if (doctype != null)
 151:           {
 152:             DTDElementTypeInfo info =
 153:               doctype.getElementTypeInfo(parent.getNodeName());
 154:             if (info != null)
 155:               {
 156:                 if (info.model == null && info.model.indexOf("#PCDATA") != -1)
 157:                   {
 158:                     return false;
 159:                   }
 160:                 return getNodeValue().trim().length() == 0;
 161:               }
 162:           }
 163:       }
 164:     return false;
 165:   }
 166: 
 167:   public String getWholeText()
 168:   {
 169:     DomNode ref = this;
 170:     DomNode ctx;
 171:     for (ctx = previous; ctx != null &&
 172:          (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
 173:          ctx = ctx.previous)
 174:       {
 175:         ref = ctx;
 176:       }
 177:     CPStringBuilder buf = new CPStringBuilder(ref.getNodeValue());
 178:     for (ctx = ref.next; ctx != null &&
 179:          (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
 180:          ctx = ctx.next)
 181:       {
 182:         buf.append(ctx.getNodeValue());
 183:       }
 184:     return buf.toString ();
 185:   }
 186: 
 187:   public Text replaceWholeText(String content)
 188:     throws DOMException
 189:   {
 190:     boolean isEmpty = (content == null || content.length () == 0);
 191:     if (!isEmpty)
 192:       {
 193:         setNodeValue(content);
 194:       }
 195: 
 196:     DomNode ref = this;
 197:     DomNode ctx;
 198:     for (ctx = previous; ctx != null &&
 199:          (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
 200:          ctx = ctx.previous)
 201:       {
 202:         ref = ctx;
 203:       }
 204:     ctx = ref.next;
 205:     if ((isEmpty || ref != this) && parent != null)
 206:       {
 207:         parent.removeChild(ref);
 208:       }
 209:     for (; ctx != null &&
 210:          (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
 211:          ctx = ref)
 212:       {
 213:         ref = ctx.next;
 214:         if ((isEmpty || ctx != this) && parent != null)
 215:           {
 216:             parent.removeChild(ctx);
 217:           }
 218:       }
 219:     return (isEmpty) ? null : this;
 220:   }
 221: 
 222: }