KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > XMLStringBuffer


1 /*
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.
15  */

16
17 package org.apache.xerces.util;
18
19 import org.apache.xerces.xni.XMLString;
20
21 /**
22  * XMLString is a structure used to pass character arrays. However,
23  * XMLStringBuffer is a buffer in which characters can be appended
24  * and extends XMLString so that it can be passed to methods
25  * expecting an XMLString object. This is a safe operation because
26  * it is assumed that any callee will <strong>not</strong> modify
27  * the contents of the XMLString structure.
28  * <p>
29  * The contents of the string are managed by the string buffer. As
30  * characters are appended, the string buffer will grow as needed.
31  * <p>
32  * <strong>Note:</strong> Never set the <code>ch</code>,
33  * <code>offset</code>, and <code>length</code> fields directly.
34  * These fields are managed by the string buffer. In order to reset
35  * the buffer, call <code>clear()</code>.
36  *
37  * @author Andy Clark, IBM
38  * @author Eric Ye, IBM
39  *
40  * @version $Id: XMLStringBuffer.java,v 1.6 2004/02/24 23:15:53 mrglavas Exp $
41  */

42 public class XMLStringBuffer
43     extends XMLString {
44
45     //
46
// Constants
47
//
48

49     /** Default buffer size (32). */
50     public static final int DEFAULT_SIZE = 32;
51
52     //
53
// Constructors
54
//
55

56     /**
57      *
58      */

59     public XMLStringBuffer() {
60         this(DEFAULT_SIZE);
61     } // <init>()
62

63     /**
64      *
65      *
66      * @param size
67      */

68     public XMLStringBuffer(int size) {
69         ch = new char[size];
70     } // <init>(int)
71

72     /** Constructs a string buffer from a char. */
73     public XMLStringBuffer(char c) {
74         this(1);
75         append(c);
76     } // <init>(char)
77

78     /** Constructs a string buffer from a String. */
79     public XMLStringBuffer(String JavaDoc s) {
80         this(s.length());
81         append(s);
82     } // <init>(String)
83

84     /** Constructs a string buffer from the specified character array. */
85     public XMLStringBuffer(char[] ch, int offset, int length) {
86         this(length);
87         append(ch, offset, length);
88     } // <init>(char[],int,int)
89

90     /** Constructs a string buffer from the specified XMLString. */
91     public XMLStringBuffer(XMLString s) {
92         this(s.length);
93         append(s);
94     } // <init>(XMLString)
95

96     //
97
// Public methods
98
//
99

100     /** Clears the string buffer. */
101     public void clear() {
102         offset = 0;
103         length = 0;
104     }
105
106     /**
107      * append
108      *
109      * @param c
110      */

111     public void append(char c) {
112         if (this.length + 1 > this.ch.length) {
113                     int newLength = this.ch.length*2;
114                     if (newLength < this.ch.length + DEFAULT_SIZE)
115                         newLength = this.ch.length + DEFAULT_SIZE;
116                     char[] newch = new char[newLength];
117                     System.arraycopy(this.ch, 0, newch, 0, this.length);
118                     this.ch = newch;
119         }
120         this.ch[this.length] = c;
121         this.length++;
122     } // append(char)
123

124     /**
125      * append
126      *
127      * @param s
128      */

129     public void append(String JavaDoc s) {
130         int length = s.length();
131         if (this.length + length > this.ch.length) {
132             int newLength = this.ch.length*2;
133             if (newLength < this.length + length + DEFAULT_SIZE)
134                 newLength = this.ch.length + length + DEFAULT_SIZE;
135             char[] newch = new char[newLength];
136             System.arraycopy(this.ch, 0, newch, 0, this.length);
137             this.ch = newch;
138         }
139         s.getChars(0, length, this.ch, this.length);
140         this.length += length;
141     } // append(String)
142

143     /**
144      * append
145      *
146      * @param ch
147      * @param offset
148      * @param length
149      */

150     public void append(char[] ch, int offset, int length) {
151         if (this.length + length > this.ch.length) {
152             char[] newch = new char[this.ch.length + length + DEFAULT_SIZE];
153             System.arraycopy(this.ch, 0, newch, 0, this.length);
154             this.ch = newch;
155         }
156         System.arraycopy(ch, offset, this.ch, this.length, length);
157         this.length += length;
158     } // append(char[],int,int)
159

160     /**
161      * append
162      *
163      * @param s
164      */

165     public void append(XMLString s) {
166         append(s.ch, s.offset, s.length);
167     } // append(XMLString)
168

169 } // class XMLStringBuffer
170
Popular Tags