001: /**
002: * org/ozone-db/xml/dom/CharacterData.java
003: *
004: * The contents of this file are subject to the OpenXML Public
005: * License Version 1.0; you may not use this file except in compliance
006: * with the License. You may obtain a copy of the License at
007: * http://www.openxml.org/license.html
008: *
009: * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
010: * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
011: * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
012: * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
013: * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
014: * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
015: *
016: * The Initial Developer of this code under the License is Assaf Arkin.
017: * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
018: * All Rights Reserved.
019: */
020:
021: /**
022: * Changes for Persistent DOM running with ozone are
023: * Copyright 1999 by SMB GmbH. All rights reserved.
024: */package org.ozoneDB.xml.dom;
025:
026: import org.w3c.dom.*;
027:
028: /**
029: * Abstract data class has methods for interacting directly with data contained
030: * in it. Derived classes {@link org.w3c.dom.Text}, {@link org.w3c.dom.Comment}
031: * and {@link org.w3c.dom.CDATASection} provide full implementation of this class.
032: * <P>
033: * The initial data is guaranteed to be a zero length string, not null. Setting
034: * the data to null will always return an empty string.
035: * <P>
036: * Notes:
037: * <OL>
038: * <LI>Node does not support childern
039: * </OL>
040: *
041: *
042: * @version $Revision: 1.1 $ $Date: 2001/12/18 11:03:24 $
043: * @author <a href="mailto:arkin@trendline.co.il">Assaf Arkin</a>
044: * @see org.w3c.dom.CharacterData
045: * @see NodeImpl
046: */
047: public abstract class CharacterDataImpl extends NodeImpl implements
048: CharacterDataProxy {
049:
050: final static long serialVersionUID = 1;
051:
052: public final String getData() {
053: // Same as calling getNodeValue().
054: return getNodeValue();
055: }
056:
057: public final void setData(String value) throws DOMException {
058: setNodeValue(value == null ? "" : value);
059: }
060:
061: public final int getLength() {
062: return getNodeValue().length();
063: }
064:
065: public synchronized final String substringData(int start, int count)
066: throws DOMException {
067: // Make sure that start and start + count are not outside the range of
068: // the data.
069: if (start < 0 || start >= getLength()) {
070: throw new DOMExceptionImpl(DOMException.INDEX_SIZE_ERR,
071: "'start' out of data size.");
072: }
073: if (start + count > getLength()) {
074: throw new DOMExceptionImpl(DOMException.INDEX_SIZE_ERR,
075: "'start + count' out of data size.");
076: }
077: // Cut the data and return.
078: return getNodeValue().substring(start, count);
079: }
080:
081: public synchronized final void appendData(String value) {
082: if (isReadOnly()) {
083: throw new DOMExceptionImpl(
084: DOMException.NO_MODIFICATION_ALLOWED_ERR);
085: }
086: if (value == null) {
087: value = "";
088: }
089: setNodeValue(getNodeValue() + value);
090: }
091:
092: public synchronized final void insertData(int offset, String value)
093: throws DOMException {
094: if (isReadOnly()) {
095: throw new DOMExceptionImpl(
096: DOMException.NO_MODIFICATION_ALLOWED_ERR);
097: }
098: if (value == null) {
099: value = "";
100: }
101: // Make sure that offest is not outside the range of the data.
102: if (offset < 0 || offset > getLength()) {
103: throw new DOMExceptionImpl(DOMException.INDEX_SIZE_ERR,
104: "'start' out of data size.");
105: }
106: // If offset is zero, prepend value to data. If offest is size
107: // of data, append value to data.
108: if (offset == 0) {
109: setNodeValue(value + getNodeValue());
110: } else if (offset == getLength()) {
111: setNodeValue(getNodeValue() + value);
112: } else {
113: // Cut data in the middle and combine all three parts.
114: setNodeValue(getNodeValue().substring(0, offset) + value
115: + getNodeValue().substring(offset));
116: }
117: }
118:
119: public synchronized final void deleteData(int offset, int count)
120: throws DOMException {
121: // Make sure that offest and offset + count are not outside
122: // the range of the data.
123: if (isReadOnly()) {
124: throw new DOMExceptionImpl(
125: DOMException.NO_MODIFICATION_ALLOWED_ERR);
126: }
127: if (offset < 0 || offset >= getLength()) {
128: throw new DOMExceptionImpl(DOMException.INDEX_SIZE_ERR,
129: "'start' out of data size.");
130: }
131: if (offset + count > getLength()) {
132: throw new DOMExceptionImpl(DOMException.INDEX_SIZE_ERR,
133: "'start + count' out of data size.");
134: }
135: if (count == 0) {
136: return;
137: }
138:
139: // If offest + count reach end of data, it's easier to cut end of data.
140: // If offest is zero, it's easier to cut beginning of data.
141: if (offset + count == getLength()) {
142: setNodeValue(getNodeValue().substring(0, offset));
143: } else if (offset == 0) {
144: setNodeValue(getNodeValue().substring(count));
145: } else {
146: // Cut data in the middle and combine the two parts.
147: setNodeValue(getNodeValue().substring(0, offset)
148: + getNodeValue().substring(offset + count));
149: }
150: }
151:
152: public synchronized final void replaceData(int offset, int count,
153: String value) throws DOMException {
154: if (isReadOnly()) {
155: throw new DOMExceptionImpl(
156: DOMException.NO_MODIFICATION_ALLOWED_ERR);
157: }
158: if (value == null) {
159: value = "";
160: }
161: // Cheap implementation performs deletion and insertion. Both methods
162: // are synchronized, but replace() must also be synchronized to prevent
163: // mid-call changes.
164: deleteData(offset, count);
165: insertData(offset, value);
166: }
167:
168: protected final boolean supportsChildern() {
169: return false;
170: }
171:
172: /**
173: * Constructor for derived classes.
174: *
175: * @param owner The owner of this document
176: * @param name The name of this node type
177: * @param value Initial value or empty string
178: */
179: protected CharacterDataImpl(DocumentImpl owner, String name,
180: String value) {
181: super ();
182: init(owner, name, value);
183: }
184:
185: public CharacterDataImpl() {
186: super ();
187: }
188:
189: public void init(DocumentProxy owner, String name, String value) {
190: super .init(owner, name, value, false);
191: }
192:
193: }
|