001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.dom;
059:
060: import org.w3c.dom.CharacterData;
061: import org.w3c.dom.DOMException;
062: import org.w3c.dom.Node;
063: import org.w3c.dom.Text;
064:
065: /**
066: * Text nodes hold the non-markup, non-Entity content of
067: * an Element or Attribute.
068: * <P>
069: * When a document is first made available to the DOM, there is only
070: * one Text object for each block of adjacent plain-text. Users (ie,
071: * applications) may create multiple adjacent Texts during editing --
072: * see {@link Element#normalize} for discussion.
073: * <P>
074: * Note that CDATASection is a subclass of Text. This is conceptually
075: * valid, since they're really just two different ways of quoting
076: * characters when they're written out as part of an XML stream.
077: *
078: * @version
079: * @since PR-DOM-Level-1-19980818.
080: */
081: public class TextImpl extends CharacterDataImpl implements
082: CharacterData, Text {
083:
084: //
085: // Constants
086: //
087:
088: /** Serialization version. */
089: static final long serialVersionUID = -5294980852957403469L;
090:
091: //
092: // Constructors
093: //
094:
095: /** Factory constructor. */
096: public TextImpl(CoreDocumentImpl ownerDoc, String data) {
097: super (ownerDoc, data);
098: }
099:
100: //
101: // Node methods
102: //
103:
104: /**
105: * A short integer indicating what type of node this is. The named
106: * constants for this value are defined in the org.w3c.dom.Node interface.
107: */
108: public short getNodeType() {
109: return Node.TEXT_NODE;
110: }
111:
112: /** Returns the node name. */
113: public String getNodeName() {
114: return "#text";
115: }
116:
117: /**
118: * NON-DOM: Set whether this Text is ignorable whitespace.
119: */
120: public void setIgnorableWhitespace(boolean ignore) {
121:
122: if (needsSyncData()) {
123: synchronizeData();
124: }
125: isIgnorableWhitespace(ignore);
126:
127: } // setIgnorableWhitespace(boolean)
128:
129: /**
130: * NON-DOM: Returns whether this Text is ignorable whitespace.
131: */
132: public boolean isIgnorableWhitespace() {
133:
134: if (needsSyncData()) {
135: synchronizeData();
136: }
137: return internalIsIgnorableWhitespace();
138:
139: } // isIgnorableWhitespace():boolean
140:
141: //
142: // Text methods
143: //
144:
145: /**
146: * Break a text node into two sibling nodes. (Note that if the
147: * current node has no parent, they won't wind up as "siblings" --
148: * they'll both be orphans.)
149: *
150: * @param offset The offset at which to split. If offset is at the
151: * end of the available data, the second node will be empty.
152: *
153: * @returns A reference to the new node (containing data after the
154: * offset point). The original node will contain data up to that
155: * point.
156: *
157: * @throws DOMException(INDEX_SIZE_ERR) if offset is <0 or >length.
158: *
159: * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if node is read-only.
160: */
161: public Text splitText(int offset) throws DOMException {
162:
163: if (isReadOnly()) {
164: throw new DOMException(
165: DOMException.NO_MODIFICATION_ALLOWED_ERR,
166: "DOM001 Modification not allowed");
167: }
168:
169: if (needsSyncData()) {
170: synchronizeData();
171: }
172: if (offset < 0 || offset > data.length()) {
173: throw new DOMException(DOMException.INDEX_SIZE_ERR,
174: "DOM004 Index out of bounds");
175: }
176:
177: // split text into two separate nodes
178: Text newText = getOwnerDocument().createTextNode(
179: data.substring(offset));
180: setNodeValue(data.substring(0, offset));
181:
182: // insert new text node
183: Node parentNode = getParentNode();
184: if (parentNode != null) {
185: parentNode.insertBefore(newText, nextSibling);
186: }
187:
188: return newText;
189:
190: } // splitText(int):Text
191:
192: } // class TextImpl
|