001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package com.sun.xml.stream.xerces.util;
059:
060: import com.sun.xml.stream.xerces.xni.XMLString;
061:
062: /**
063: * XMLString is a structure used to pass character arrays. However,
064: * XMLStringBuffer is a buffer in which characters can be appended
065: * and extends XMLString so that it can be passed to methods
066: * expecting an XMLString object. This is a safe operation because
067: * it is assumed that any callee will <strong>not</strong> modify
068: * the contents of the XMLString structure.
069: * <p>
070: * The contents of the string are managed by the string buffer. As
071: * characters are appended, the string buffer will grow as needed.
072: * <p>
073: * <strong>Note:</strong> Never set the <code>ch</code>,
074: * <code>offset</code>, and <code>length</code> fields directly.
075: * These fields are managed by the string buffer. In order to reset
076: * the buffer, call <code>clear()</code>.
077: *
078: * @author Andy Clark, IBM
079: * @author Eric Ye, IBM
080: *
081: * @version $Id: XMLStringBuffer.java,v 1.2 2006/04/01 06:01:40 jeffsuttor Exp $
082: */
083: public class XMLStringBuffer extends XMLString {
084:
085: //
086: // Constants
087: //
088:
089: /** Default buffer size (32). */
090: public static final int DEFAULT_SIZE = 32;
091:
092: //
093: // Data
094: //
095:
096: //
097: // Constructors
098: //
099:
100: /**
101: *
102: */
103: public XMLStringBuffer() {
104: this (DEFAULT_SIZE);
105: } // <init>()
106:
107: /**
108: *
109: *
110: * @param size
111: */
112: public XMLStringBuffer(int size) {
113: ch = new char[size];
114: } // <init>(int)
115:
116: /** Constructs a string buffer from a char. */
117: public XMLStringBuffer(char c) {
118: this (1);
119: append(c);
120: } // <init>(char)
121:
122: /** Constructs a string buffer from a String. */
123: public XMLStringBuffer(String s) {
124: this (s.length());
125: append(s);
126: } // <init>(String)
127:
128: /** Constructs a string buffer from the specified character array. */
129: public XMLStringBuffer(char[] ch, int offset, int length) {
130: this (length);
131: append(ch, offset, length);
132: } // <init>(char[],int,int)
133:
134: /** Constructs a string buffer from the specified XMLString. */
135: public XMLStringBuffer(XMLString s) {
136: this (s.length);
137: append(s);
138: } // <init>(XMLString)
139:
140: //
141: // Public methods
142: //
143:
144: /** Clears the string buffer. */
145: public void clear() {
146: offset = 0;
147: length = 0;
148: }
149:
150: /**
151: * append
152: *
153: * @param c
154: */
155: public void append(char c) {
156: if (this .length + 1 > this .ch.length) {
157: int newLength = this .ch.length * 2;
158: if (newLength < this .ch.length + DEFAULT_SIZE) {
159: newLength = this .ch.length + DEFAULT_SIZE;
160: }
161: char[] tmp = new char[newLength];
162: System.arraycopy(this .ch, 0, tmp, 0, this .length);
163: this .ch = tmp;
164: }
165: this .ch[this .length] = c;
166: this .length++;
167: } // append(char)
168:
169: /**
170: * append
171: *
172: * @param s
173: */
174: public void append(String s) {
175: int length = s.length();
176: if (this .length + length > this .ch.length) {
177: int newLength = this .ch.length * 2;
178: if (newLength < this .ch.length + length + DEFAULT_SIZE) {
179: newLength = this .ch.length + length + DEFAULT_SIZE;
180: }
181:
182: char[] newch = new char[newLength];
183: System.arraycopy(this .ch, 0, newch, 0, this .length);
184: this .ch = newch;
185: }
186: s.getChars(0, length, this .ch, this .length);
187: this .length += length;
188: } // append(String)
189:
190: /**
191: * append
192: *
193: * @param ch
194: * @param offset
195: * @param length
196: */
197: public void append(char[] ch, int offset, int length) {
198: if (this .length + length > this .ch.length) {
199: int newLength = this .ch.length * 2;
200: if (newLength < this .ch.length + length + DEFAULT_SIZE) {
201: newLength = this .ch.length + length + DEFAULT_SIZE;
202: }
203: char[] newch = new char[newLength];
204: System.arraycopy(this .ch, 0, newch, 0, this .length);
205: this .ch = newch;
206: }
207: //making the code more robust as it would handle null or 0 length data,
208: //add the data only when it contains some thing
209: if (ch != null && length > 0) {
210: System.arraycopy(ch, offset, this .ch, this .length, length);
211: this .length += length;
212: }
213: } // append(char[],int,int)
214:
215: /**
216: * append
217: *
218: * @param s
219: */
220: public void append(XMLString s) {
221: append(s.ch, s.offset, s.length);
222: } // append(XMLString)
223:
224: } // class XMLStringBuffer
|