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