KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > xmlparser > XMLStringBuffer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ====================================================================
17  *
18  * This software consists of voluntary contributions made by many
19  * individuals on behalf of the Apache Software Foundation and was
20  * originally based on software copyright (c) 1999, International
21  * Business Machines, Inc., http://www.apache.org. For more
22  * information on the Apache Software Foundation, please see
23  * <http://www.apache.org/>.
24  */

25
26 package org.apache.jasper.xmlparser;
27
28 /**
29  * XMLString is a structure used to pass character arrays. However,
30  * XMLStringBuffer is a buffer in which characters can be appended
31  * and extends XMLString so that it can be passed to methods
32  * expecting an XMLString object. This is a safe operation because
33  * it is assumed that any callee will <strong>not</strong> modify
34  * the contents of the XMLString structure.
35  * <p>
36  * The contents of the string are managed by the string buffer. As
37  * characters are appended, the string buffer will grow as needed.
38  * <p>
39  * <strong>Note:</strong> Never set the <code>ch</code>,
40  * <code>offset</code>, and <code>length</code> fields directly.
41  * These fields are managed by the string buffer. In order to reset
42  * the buffer, call <code>clear()</code>.
43  *
44  * @author Andy Clark, IBM
45  * @author Eric Ye, IBM
46  *
47  * @version $Id: XMLStringBuffer.java 467222 2006-10-24 03:17:11Z markt $
48  */

49 public class XMLStringBuffer
50     extends XMLString {
51
52     //
53
// Constants
54
//
55

56     /** Default buffer size (32). */
57     public static final int DEFAULT_SIZE = 32;
58
59     //
60
// Constructors
61
//
62

63     /**
64      *
65      */

66     public XMLStringBuffer() {
67         this(DEFAULT_SIZE);
68     } // <init>()
69

70     /**
71      *
72      *
73      * @param size
74      */

75     public XMLStringBuffer(int size) {
76         ch = new char[size];
77     } // <init>(int)
78

79     /** Constructs a string buffer from a char. */
80     public XMLStringBuffer(char c) {
81         this(1);
82         append(c);
83     } // <init>(char)
84

85     /** Constructs a string buffer from a String. */
86     public XMLStringBuffer(String JavaDoc s) {
87         this(s.length());
88         append(s);
89     } // <init>(String)
90

91     /** Constructs a string buffer from the specified character array. */
92     public XMLStringBuffer(char[] ch, int offset, int length) {
93         this(length);
94         append(ch, offset, length);
95     } // <init>(char[],int,int)
96

97     /** Constructs a string buffer from the specified XMLString. */
98     public XMLStringBuffer(XMLString s) {
99         this(s.length);
100         append(s);
101     } // <init>(XMLString)
102

103     //
104
// Public methods
105
//
106

107     /** Clears the string buffer. */
108     public void clear() {
109         offset = 0;
110         length = 0;
111     }
112
113     /**
114      * append
115      *
116      * @param c
117      */

118     public void append(char c) {
119         if (this.length + 1 > this.ch.length) {
120                     int newLength = this.ch.length*2;
121                     if (newLength < this.ch.length + DEFAULT_SIZE)
122                         newLength = this.ch.length + DEFAULT_SIZE;
123                     char[] newch = new char[newLength];
124                     System.arraycopy(this.ch, 0, newch, 0, this.length);
125                     this.ch = newch;
126         }
127         this.ch[this.length] = c;
128         this.length++;
129     } // append(char)
130

131     /**
132      * append
133      *
134      * @param s
135      */

136     public void append(String JavaDoc s) {
137         int length = s.length();
138         if (this.length + length > this.ch.length) {
139             int newLength = this.ch.length*2;
140             if (newLength < this.length + length + DEFAULT_SIZE)
141                 newLength = this.ch.length + length + DEFAULT_SIZE;
142             char[] newch = new char[newLength];
143             System.arraycopy(this.ch, 0, newch, 0, this.length);
144             this.ch = newch;
145         }
146         s.getChars(0, length, this.ch, this.length);
147         this.length += length;
148     } // append(String)
149

150     /**
151      * append
152      *
153      * @param ch
154      * @param offset
155      * @param length
156      */

157     public void append(char[] ch, int offset, int length) {
158         if (this.length + length > this.ch.length) {
159             char[] newch = new char[this.ch.length + length + DEFAULT_SIZE];
160             System.arraycopy(this.ch, 0, newch, 0, this.length);
161             this.ch = newch;
162         }
163         System.arraycopy(ch, offset, this.ch, this.length, length);
164         this.length += length;
165     } // append(char[],int,int)
166

167     /**
168      * append
169      *
170      * @param s
171      */

172     public void append(XMLString s) {
173         append(s.ch, s.offset, s.length);
174     } // append(XMLString)
175

176 } // class XMLStringBuffer
177
Popular Tags