1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.util;
  58. import com.sun.org.apache.xerces.internal.xni.XMLString;
  59. /**
  60. * XMLString is a structure used to pass character arrays. However,
  61. * XMLStringBuffer is a buffer in which characters can be appended
  62. * and extends XMLString so that it can be passed to methods
  63. * expecting an XMLString object. This is a safe operation because
  64. * it is assumed that any callee will <strong>not</strong> modify
  65. * the contents of the XMLString structure.
  66. * <p>
  67. * The contents of the string are managed by the string buffer. As
  68. * characters are appended, the string buffer will grow as needed.
  69. * <p>
  70. * <strong>Note:</strong> Never set the <code>ch</code>,
  71. * <code>offset</code>, and <code>length</code> fields directly.
  72. * These fields are managed by the string buffer. In order to reset
  73. * the buffer, call <code>clear()</code>.
  74. *
  75. * @author Andy Clark, IBM
  76. * @author Eric Ye, IBM
  77. *
  78. * @version $Id: XMLStringBuffer.java,v 1.5 2003/07/28 23:27:36 elena Exp $
  79. */
  80. public class XMLStringBuffer
  81. extends XMLString {
  82. //
  83. // Constants
  84. //
  85. /** Default buffer size (32). */
  86. public static final int DEFAULT_SIZE = 32;
  87. //
  88. // Constructors
  89. //
  90. /**
  91. *
  92. */
  93. public XMLStringBuffer() {
  94. this(DEFAULT_SIZE);
  95. } // <init>()
  96. /**
  97. *
  98. *
  99. * @param size
  100. */
  101. public XMLStringBuffer(int size) {
  102. ch = new char[size];
  103. } // <init>(int)
  104. /** Constructs a string buffer from a char. */
  105. public XMLStringBuffer(char c) {
  106. this(1);
  107. append(c);
  108. } // <init>(char)
  109. /** Constructs a string buffer from a String. */
  110. public XMLStringBuffer(String s) {
  111. this(s.length());
  112. append(s);
  113. } // <init>(String)
  114. /** Constructs a string buffer from the specified character array. */
  115. public XMLStringBuffer(char[] ch, int offset, int length) {
  116. this(length);
  117. append(ch, offset, length);
  118. } // <init>(char[],int,int)
  119. /** Constructs a string buffer from the specified XMLString. */
  120. public XMLStringBuffer(XMLString s) {
  121. this(s.length);
  122. append(s);
  123. } // <init>(XMLString)
  124. //
  125. // Public methods
  126. //
  127. /** Clears the string buffer. */
  128. public void clear() {
  129. offset = 0;
  130. length = 0;
  131. }
  132. /**
  133. * append
  134. *
  135. * @param c
  136. */
  137. public void append(char c) {
  138. if (this.length + 1 > this.ch.length) {
  139. int newLength = this.ch.length*2;
  140. if (newLength < this.ch.length + DEFAULT_SIZE)
  141. newLength = this.ch.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. this.ch[this.length] = c;
  147. this.length++;
  148. } // append(char)
  149. /**
  150. * append
  151. *
  152. * @param s
  153. */
  154. public void append(String s) {
  155. int length = s.length();
  156. if (this.length + length > this.ch.length) {
  157. int newLength = this.ch.length*2;
  158. if (newLength < this.length + length + DEFAULT_SIZE)
  159. newLength = this.ch.length + length + DEFAULT_SIZE;
  160. char[] newch = new char[newLength];
  161. System.arraycopy(this.ch, 0, newch, 0, this.length);
  162. this.ch = newch;
  163. }
  164. s.getChars(0, length, this.ch, this.length);
  165. this.length += length;
  166. } // append(String)
  167. /**
  168. * append
  169. *
  170. * @param ch
  171. * @param offset
  172. * @param length
  173. */
  174. public void append(char[] ch, int offset, int length) {
  175. if (this.length + length > this.ch.length) {
  176. char[] newch = new char[this.ch.length + length + DEFAULT_SIZE];
  177. System.arraycopy(this.ch, 0, newch, 0, this.length);
  178. this.ch = newch;
  179. }
  180. System.arraycopy(ch, offset, this.ch, this.length, length);
  181. this.length += length;
  182. } // append(char[],int,int)
  183. /**
  184. * append
  185. *
  186. * @param s
  187. */
  188. public void append(XMLString s) {
  189. append(s.ch, s.offset, s.length);
  190. } // append(XMLString)
  191. } // class XMLStringBuffer