001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: * ====================================================================
017: *
018: * This software consists of voluntary contributions made by many
019: * individuals on behalf of the Apache Software Foundation and was
020: * originally based on software copyright (c) 1999, International
021: * Business Machines, Inc., http://www.apache.org. For more
022: * information on the Apache Software Foundation, please see
023: * <http://www.apache.org/>.
024: */
025:
026: package org.apache.jasper.xmlparser;
027:
028: /**
029: * XMLString is a structure used to pass character arrays. However,
030: * XMLStringBuffer is a buffer in which characters can be appended
031: * and extends XMLString so that it can be passed to methods
032: * expecting an XMLString object. This is a safe operation because
033: * it is assumed that any callee will <strong>not</strong> modify
034: * the contents of the XMLString structure.
035: * <p>
036: * The contents of the string are managed by the string buffer. As
037: * characters are appended, the string buffer will grow as needed.
038: * <p>
039: * <strong>Note:</strong> Never set the <code>ch</code>,
040: * <code>offset</code>, and <code>length</code> fields directly.
041: * These fields are managed by the string buffer. In order to reset
042: * the buffer, call <code>clear()</code>.
043: *
044: * @author Andy Clark, IBM
045: * @author Eric Ye, IBM
046: *
047: * @version $Id: XMLStringBuffer.java 467222 2006-10-24 03:17:11Z markt $
048: */
049: public class XMLStringBuffer extends XMLString {
050:
051: //
052: // Constants
053: //
054:
055: /** Default buffer size (32). */
056: public static final int DEFAULT_SIZE = 32;
057:
058: //
059: // Constructors
060: //
061:
062: /**
063: *
064: */
065: public XMLStringBuffer() {
066: this (DEFAULT_SIZE);
067: } // <init>()
068:
069: /**
070: *
071: *
072: * @param size
073: */
074: public XMLStringBuffer(int size) {
075: ch = new char[size];
076: } // <init>(int)
077:
078: /** Constructs a string buffer from a char. */
079: public XMLStringBuffer(char c) {
080: this (1);
081: append(c);
082: } // <init>(char)
083:
084: /** Constructs a string buffer from a String. */
085: public XMLStringBuffer(String s) {
086: this (s.length());
087: append(s);
088: } // <init>(String)
089:
090: /** Constructs a string buffer from the specified character array. */
091: public XMLStringBuffer(char[] ch, int offset, int length) {
092: this (length);
093: append(ch, offset, length);
094: } // <init>(char[],int,int)
095:
096: /** Constructs a string buffer from the specified XMLString. */
097: public XMLStringBuffer(XMLString s) {
098: this (s.length);
099: append(s);
100: } // <init>(XMLString)
101:
102: //
103: // Public methods
104: //
105:
106: /** Clears the string buffer. */
107: public void clear() {
108: offset = 0;
109: length = 0;
110: }
111:
112: /**
113: * append
114: *
115: * @param c
116: */
117: public void append(char c) {
118: if (this .length + 1 > this .ch.length) {
119: int newLength = this .ch.length * 2;
120: if (newLength < this .ch.length + DEFAULT_SIZE)
121: newLength = this .ch.length + DEFAULT_SIZE;
122: char[] newch = new char[newLength];
123: System.arraycopy(this .ch, 0, newch, 0, this .length);
124: this .ch = newch;
125: }
126: this .ch[this .length] = c;
127: this .length++;
128: } // append(char)
129:
130: /**
131: * append
132: *
133: * @param s
134: */
135: public void append(String s) {
136: int length = s.length();
137: if (this .length + length > this .ch.length) {
138: int newLength = this .ch.length * 2;
139: if (newLength < this .length + length + DEFAULT_SIZE)
140: newLength = this .ch.length + length + DEFAULT_SIZE;
141: char[] newch = new char[newLength];
142: System.arraycopy(this .ch, 0, newch, 0, this .length);
143: this .ch = newch;
144: }
145: s.getChars(0, length, this .ch, this .length);
146: this .length += length;
147: } // append(String)
148:
149: /**
150: * append
151: *
152: * @param ch
153: * @param offset
154: * @param length
155: */
156: public void append(char[] ch, int offset, int length) {
157: if (this .length + length > this .ch.length) {
158: char[] newch = new char[this .ch.length + length
159: + 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
|