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: package org.apache.xerces.util;
019:
020: import org.apache.xerces.xni.XMLString;
021:
022: /**
023: * XMLString is a structure used to pass character arrays. However,
024: * XMLStringBuffer is a buffer in which characters can be appended
025: * and extends XMLString so that it can be passed to methods
026: * expecting an XMLString object. This is a safe operation because
027: * it is assumed that any callee will <strong>not</strong> modify
028: * the contents of the XMLString structure.
029: * <p>
030: * The contents of the string are managed by the string buffer. As
031: * characters are appended, the string buffer will grow as needed.
032: * <p>
033: * <strong>Note:</strong> Never set the <code>ch</code>,
034: * <code>offset</code>, and <code>length</code> fields directly.
035: * These fields are managed by the string buffer. In order to reset
036: * the buffer, call <code>clear()</code>.
037: *
038: * @author Andy Clark, IBM
039: * @author Eric Ye, IBM
040: *
041: * @version $Id: XMLStringBuffer.java 447241 2006-09-18 05:12:57Z mrglavas $
042: */
043: public class XMLStringBuffer extends XMLString {
044:
045: //
046: // Constants
047: //
048:
049: /** Default buffer size (32). */
050: public static final int DEFAULT_SIZE = 32;
051:
052: //
053: // Constructors
054: //
055:
056: /**
057: *
058: */
059: public XMLStringBuffer() {
060: this (DEFAULT_SIZE);
061: } // <init>()
062:
063: /**
064: *
065: *
066: * @param size
067: */
068: public XMLStringBuffer(int size) {
069: ch = new char[size];
070: } // <init>(int)
071:
072: /** Constructs a string buffer from a char. */
073: public XMLStringBuffer(char c) {
074: this (1);
075: append(c);
076: } // <init>(char)
077:
078: /** Constructs a string buffer from a String. */
079: public XMLStringBuffer(String s) {
080: this (s.length());
081: append(s);
082: } // <init>(String)
083:
084: /** Constructs a string buffer from the specified character array. */
085: public XMLStringBuffer(char[] ch, int offset, int length) {
086: this (length);
087: append(ch, offset, length);
088: } // <init>(char[],int,int)
089:
090: /** Constructs a string buffer from the specified XMLString. */
091: public XMLStringBuffer(XMLString s) {
092: this (s.length);
093: append(s);
094: } // <init>(XMLString)
095:
096: //
097: // Public methods
098: //
099:
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 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
153: + DEFAULT_SIZE];
154: System.arraycopy(this .ch, 0, newch, 0, this .length);
155: this .ch = newch;
156: }
157: System.arraycopy(ch, offset, this .ch, this .length, length);
158: this .length += length;
159: } // append(char[],int,int)
160:
161: /**
162: * append
163: *
164: * @param s
165: */
166: public void append(XMLString s) {
167: append(s.ch, s.offset, s.length);
168: } // append(XMLString)
169:
170: } // class XMLStringBuffer
|