~ubuntu-branches/ubuntu/karmic/libxerces2-java/karmic

« back to all changes in this revision

Viewing changes to src/org/apache/xerces/dom/CharacterDataImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-12-04 17:37:55 UTC
  • mfrom: (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20061204173755-hb6ybrrrk097zhx7
Tags: 2.8.1-1ubuntu1
* Merge with Debian unstable; remaining changes:
  - Build -gcj package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * The Apache Software License, Version 1.1
3
 
 *
4
 
 *
5
 
 * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights 
6
 
 * reserved.
7
 
 *
8
 
 * Redistribution and use in source and binary forms, with or without
9
 
 * modification, are permitted provided that the following conditions
10
 
 * are met:
11
 
 *
12
 
 * 1. Redistributions of source code must retain the above copyright
13
 
 *    notice, this list of conditions and the following disclaimer. 
14
 
 *
15
 
 * 2. Redistributions in binary form must reproduce the above copyright
16
 
 *    notice, this list of conditions and the following disclaimer in
17
 
 *    the documentation and/or other materials provided with the
18
 
 *    distribution.
19
 
 *
20
 
 * 3. The end-user documentation included with the redistribution,
21
 
 *    if any, must include the following acknowledgment:  
22
 
 *       "This product includes software developed by the
23
 
 *        Apache Software Foundation (http://www.apache.org/)."
24
 
 *    Alternately, this acknowledgment may appear in the software itself,
25
 
 *    if and wherever such third-party acknowledgments normally appear.
26
 
 *
27
 
 * 4. The names "Xerces" and "Apache Software Foundation" must
28
 
 *    not be used to endorse or promote products derived from this
29
 
 *    software without prior written permission. For written 
30
 
 *    permission, please contact apache@apache.org.
31
 
 *
32
 
 * 5. Products derived from this software may not be called "Apache",
33
 
 *    nor may "Apache" appear in their name, without prior written
34
 
 *    permission of the Apache Software Foundation.
35
 
 *
36
 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37
 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38
 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39
 
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40
 
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41
 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42
 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43
 
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44
 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45
 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46
 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47
 
 * SUCH DAMAGE.
48
 
 * ====================================================================
49
 
 *
50
 
 * This software consists of voluntary contributions made by many
51
 
 * individuals on behalf of the Apache Software Foundation and was
52
 
 * originally based on software copyright (c) 1999, International
53
 
 * Business Machines, Inc., http://www.apache.org.  For more
54
 
 * information on the Apache Software Foundation, please see
55
 
 * <http://www.apache.org/>.
 
2
 * Copyright 1999-2002,2004 The Apache Software Foundation.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
56
15
 */
57
16
 
58
17
package org.apache.xerces.dom;
72
31
 * <p>
73
32
 * This class doesn't directly support mutation events, however, it notifies
74
33
 * the document when mutations are performed so that the document class do so.
 
34
 * 
 
35
 * @xerces.internal 
75
36
 *
76
 
 * @version $Id: CharacterDataImpl.java,v 1.20 2003/05/08 19:52:40 elena Exp $
 
37
 * @version $Id: CharacterDataImpl.java 320126 2004-11-04 20:42:20Z mrglavas $
77
38
 * @since  PR-DOM-Level-1-19980818.
78
39
 */
79
40
public abstract class CharacterDataImpl
129
90
        return data;
130
91
    }
131
92
 
 
93
   /** Convenience wrapper for calling setNodeValueInternal when 
 
94
     * we are not performing a replacement operation  
 
95
     */
 
96
    protected void setNodeValueInternal (String value) {
 
97
        setNodeValueInternal(value, false);
 
98
    }
 
99
    
132
100
    /** This function added so that we can distinguish whether
133
101
     *  setNodeValue has been called from some other DOM functions.
134
102
     *  or by the client.<p>
136
104
     *  from the high-level functions in CharacterData, and another
137
105
     *  type if the client simply calls setNodeValue(value).
138
106
     */
139
 
    protected void setNodeValueInternal(String value) {
140
 
        if (isReadOnly()) {
 
107
    protected void setNodeValueInternal(String value, boolean replace) {
 
108
        
 
109
        CoreDocumentImpl ownerDocument = ownerDocument();
 
110
        
 
111
        if (ownerDocument.errorChecking && isReadOnly()) {
141
112
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
142
113
            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
143
114
        }
 
115
        
144
116
        // revisit: may want to set the value in ownerDocument.
145
 
        // Default behavior, overridden in some subclasses
 
117
        // Default behavior, overridden in some subclasses
146
118
        if (needsSyncData()) {
147
119
            synchronizeData();
148
120
        }
149
 
 
 
121
        
150
122
        // keep old value for document notification
151
123
        String oldvalue = this.data;
152
 
 
153
 
        CoreDocumentImpl ownerDocument = ownerDocument();
154
 
 
155
 
        // notify document
156
 
        ownerDocument.modifyingCharacterData(this);
157
 
 
158
 
        this.data = value;
159
 
 
160
 
        // notify document
161
 
        ownerDocument.modifiedCharacterData(this, oldvalue, value);
 
124
        
 
125
        // notify document
 
126
        ownerDocument.modifyingCharacterData(this, replace);
 
127
        
 
128
        this.data = value;
 
129
        
 
130
        // notify document
 
131
        ownerDocument.modifiedCharacterData(this, oldvalue, value, replace);
162
132
    }
163
133
 
164
134
    /**
242
212
     */
243
213
    public void deleteData(int offset, int count) 
244
214
        throws DOMException {
245
 
 
246
 
        if (isReadOnly()) {
247
 
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
248
 
            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
249
 
        }
250
 
 
251
 
        if (count < 0) {
252
 
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null);
253
 
            throw new DOMException(DOMException.INDEX_SIZE_ERR, msg);
254
 
        }
255
 
 
 
215
        
 
216
        internalDeleteData(offset, count, false);
 
217
    } // deleteData(int,int)
 
218
 
 
219
    
 
220
    /** NON-DOM INTERNAL: Within DOM actions, we sometimes need to be able
 
221
     * to control which mutation events are spawned. This version of the
 
222
     * deleteData operation allows us to do so. It is not intended
 
223
     * for use by application programs.
 
224
     */
 
225
    void internalDeleteData (int offset, int count, boolean replace)
 
226
    throws DOMException {
 
227
        
 
228
        CoreDocumentImpl ownerDocument = ownerDocument();
 
229
        if (ownerDocument.errorChecking) {
 
230
            if (isReadOnly()) {
 
231
                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
 
232
                throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
 
233
            }
 
234
            
 
235
            if (count < 0) {
 
236
                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null);
 
237
                throw new DOMException(DOMException.INDEX_SIZE_ERR, msg);
 
238
            }
 
239
        }
 
240
        
256
241
        if (needsSyncData()) {
257
242
            synchronizeData();
258
243
        }
259
244
        int tailLength = Math.max(data.length() - count - offset, 0);
260
245
        try {
261
246
            String value = data.substring(0, offset) +
262
 
                (tailLength > 0
263
 
                 ? data.substring(offset + count, offset + count + tailLength) 
264
 
                 : "");
265
 
 
266
 
            setNodeValueInternal(value);
267
 
 
 
247
            (tailLength > 0 ? data.substring(offset + count, offset + count + tailLength) : "");            
 
248
            
 
249
            setNodeValueInternal(value, replace);      
 
250
            
268
251
            // notify document
269
 
            ownerDocument().deletedText(this, offset, count);
 
252
            ownerDocument.deletedText(this, offset, count);
270
253
        }
271
254
        catch (StringIndexOutOfBoundsException e) {
272
255
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null);
273
256
            throw new DOMException(DOMException.INDEX_SIZE_ERR, msg);
274
257
        }
275
 
 
276
 
    } // deleteData(int,int)
 
258
        
 
259
    } // internalDeleteData(int,int,boolean)
277
260
 
278
261
    /**
279
262
     * Insert additional characters into the data stored in this node,
287
270
    public void insertData(int offset, String data) 
288
271
        throws DOMException {
289
272
 
290
 
        if (isReadOnly()) {
 
273
        internalInsertData(offset, data, false);
 
274
        
 
275
    } // insertData(int,int)
 
276
    
 
277
    
 
278
    
 
279
    /** NON-DOM INTERNAL: Within DOM actions, we sometimes need to be able
 
280
     * to control which mutation events are spawned. This version of the
 
281
     * insertData operation allows us to do so. It is not intended
 
282
     * for use by application programs.
 
283
     */
 
284
    void internalInsertData (int offset, String data, boolean replace)
 
285
    throws DOMException {
 
286
        
 
287
        CoreDocumentImpl ownerDocument = ownerDocument();
 
288
        
 
289
        if (ownerDocument.errorChecking && isReadOnly()) {
291
290
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
292
291
            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
293
292
        }
294
 
 
 
293
        
295
294
        if (needsSyncData()) {
296
295
            synchronizeData();
297
296
        }
298
297
        try {
299
298
            String value =
300
299
                new StringBuffer(this.data).insert(offset, data).toString();
301
 
 
302
 
            setNodeValueInternal(value);
303
 
 
 
300
            
 
301
            
 
302
            setNodeValueInternal(value, replace);
 
303
            
304
304
            // notify document
305
 
            ownerDocument().insertedText(this, offset, data.length());
 
305
            ownerDocument.insertedText(this, offset, data.length());
306
306
        }
307
307
        catch (StringIndexOutOfBoundsException e) {
308
308
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null);
309
309
            throw new DOMException(DOMException.INDEX_SIZE_ERR, msg);
310
310
        }
311
 
 
312
 
    } // insertData(int,int)
313
 
 
 
311
        
 
312
    } // internalInsertData(int,String,boolean)
 
313
 
 
314
    
 
315
    
314
316
    /**
315
317
     * Replace a series of characters at the specified (zero-based)
316
318
     * offset with a new string, NOT necessarily of the same
336
338
     * readonly.  
337
339
     */
338
340
    public void replaceData(int offset, int count, String data) 
339
 
        throws DOMException {
340
 
 
 
341
    throws DOMException {
 
342
        
 
343
        CoreDocumentImpl ownerDocument = ownerDocument();
 
344
        
341
345
        // The read-only check is done by deleteData()
342
346
        // ***** This could be more efficient w/r/t Mutation Events,
343
347
        // specifically by aggregating DOMAttrModified and
344
348
        // DOMSubtreeModified. But mutation events are 
345
349
        // underspecified; I don't feel compelled
346
350
        // to deal with it right now.
347
 
        deleteData(offset, count);
348
 
        insertData(offset, data);
349
 
 
 
351
        if (ownerDocument.errorChecking && isReadOnly()) {
 
352
            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
 
353
            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
 
354
        }
 
355
        
 
356
        if (needsSyncData()) {
 
357
            synchronizeData();
 
358
        }
 
359
        
 
360
        //notify document
 
361
        ownerDocument.replacingData(this);
 
362
        
 
363
        // keep old value for document notification
 
364
        String oldvalue = this.data;
 
365
        
 
366
        internalDeleteData(offset, count, true);
 
367
        internalInsertData(offset, data, true);
 
368
        
 
369
        ownerDocument.replacedCharacterData(this, oldvalue, this.data);
 
370
        
350
371
    } // replaceData(int,int,String)
351
372
 
352
373
    /**