KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom > CharacterDataImpl


1 /**
2  * org/ozone-db/xml/dom/CharacterData.java
3  *
4  * The contents of this file are subject to the OpenXML Public
5  * License Version 1.0; you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at
7  * http://www.openxml.org/license.html
8  *
9  * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
10  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
11  * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
12  * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
13  * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
14  * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
15  *
16  * The Initial Developer of this code under the License is Assaf Arkin.
17  * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
18  * All Rights Reserved.
19  */

20
21 /**
22  * Changes for Persistent DOM running with ozone are
23  * Copyright 1999 by SMB GmbH. All rights reserved.
24  */

25
26 package org.ozoneDB.xml.dom;
27
28 import org.w3c.dom.*;
29
30
31 /**
32  * Abstract data class has methods for interacting directly with data contained
33  * in it. Derived classes {@link org.w3c.dom.Text}, {@link org.w3c.dom.Comment}
34  * and {@link org.w3c.dom.CDATASection} provide full implementation of this class.
35  * <P>
36  * The initial data is guaranteed to be a zero length string, not null. Setting
37  * the data to null will always return an empty string.
38  * <P>
39  * Notes:
40  * <OL>
41  * <LI>Node does not support childern
42  * </OL>
43  *
44  *
45  * @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:14 $
46  * @author <a HREF="mailto:arkin@trendline.co.il">Assaf Arkin</a>
47  * @see org.w3c.dom.CharacterData
48  * @see NodeImpl
49  */

50 public abstract class CharacterDataImpl extends NodeImpl implements CharacterDataProxy {
51     
52     final static long serialVersionUID = 1;
53     
54     
55     public final String JavaDoc getData() {
56         // Same as calling getNodeValue().
57
return getNodeValue();
58     }
59     
60     
61     public final void setData( String JavaDoc value ) throws DOMException {
62         setNodeValue( value == null ? "" : value );
63     }
64     
65     
66     public final int getLength() {
67         return getNodeValue().length();
68     }
69     
70     
71     public synchronized final String JavaDoc substringData( int start, int count ) throws DOMException {
72         // Make sure that start and start + count are not outside the range of
73
// the data.
74
if (start < 0 || start >= getLength()) {
75             throw new DOMExceptionImpl( DOMException.INDEX_SIZE_ERR, "'start' out of data size." );
76         }
77         if (start + count > getLength()) {
78             throw new DOMExceptionImpl( DOMException.INDEX_SIZE_ERR, "'start + count' out of data size." );
79         }
80         // Cut the data and return.
81
return getNodeValue().substring( start, count );
82     }
83     
84     
85     public synchronized final void appendData( String JavaDoc value ) {
86         if (isReadOnly()) {
87             throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR );
88         }
89         if (value == null) {
90             value = "";
91         }
92         setNodeValue( getNodeValue() + value );
93     }
94     
95     
96     public synchronized final void insertData( int offset, String JavaDoc value ) throws DOMException {
97         if (isReadOnly()) {
98             throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR );
99         }
100         if (value == null) {
101             value = "";
102         }
103         // Make sure that offest is not outside the range of the data.
104
if (offset < 0 || offset > getLength()) {
105             throw new DOMExceptionImpl( DOMException.INDEX_SIZE_ERR, "'start' out of data size." );
106         }
107         // If offset is zero, prepend value to data. If offest is size
108
// of data, append value to data.
109
if (offset == 0) {
110             setNodeValue( value + getNodeValue() );
111         } else if (offset == getLength()) {
112             setNodeValue( getNodeValue() + value );
113         } else {
114             // Cut data in the middle and combine all three parts.
115
setNodeValue( getNodeValue().substring( 0, offset ) + value + getNodeValue().substring( offset ) );
116         }
117     }
118     
119     
120     public synchronized final void deleteData( int offset, int count ) 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( DOMException.NO_MODIFICATION_ALLOWED_ERR );
125         }
126         if (offset < 0 || offset >= getLength()) {
127             throw new DOMExceptionImpl( DOMException.INDEX_SIZE_ERR, "'start' out of data size." );
128         }
129         if (offset + count > getLength()) {
130             throw new DOMExceptionImpl( DOMException.INDEX_SIZE_ERR, "'start + count' out of data size." );
131         }
132         if (count == 0) {
133             return;
134         }
135         
136         // If offest + count reach end of data, it's easier to cut end of data.
137
// If offest is zero, it's easier to cut beginning of data.
138
if (offset + count == getLength()) {
139             setNodeValue( getNodeValue().substring( 0, offset ) );
140         } else if (offset == 0) {
141             setNodeValue( getNodeValue().substring( count ) );
142         } else {
143             // Cut data in the middle and combine the two parts.
144
setNodeValue( getNodeValue().substring( 0, offset ) + getNodeValue().substring( offset + count ) );
145         }
146     }
147     
148     
149     public synchronized final void replaceData( int offset, int count, String JavaDoc value ) throws DOMException {
150         if (isReadOnly()) {
151             throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR );
152         }
153         if (value == null) {
154             value = "";
155         }
156         // Cheap implementation performs deletion and insertion. Both methods
157
// are synchronized, but replace() must also be synchronized to prevent
158
// mid-call changes.
159
deleteData( offset, count );
160         insertData( offset, value );
161     }
162     
163     
164     protected final boolean supportsChildern() {
165         return false;
166     }
167     
168     
169     /**
170      * Constructor for derived classes.
171      *
172      * @param owner The owner of this document
173      * @param name The name of this node type
174      * @param value Initial value or empty string
175      */

176     protected CharacterDataImpl( DocumentImpl owner, String JavaDoc name, String JavaDoc value ) {
177         super();
178         init( owner, name, value );
179     }
180     
181     
182     public CharacterDataImpl() {
183         super();
184     }
185     
186     
187     public void init( DocumentProxy owner, String JavaDoc name, String JavaDoc value ) {
188         super.init( owner, name, value, false );
189     }
190     
191 }
192
Popular Tags