View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   * ====================================================================
17   *
18   * This software consists of voluntary contributions made by many
19   * individuals on behalf of the Apache Software Foundation and was
20   * originally based on software copyright (c) 1999, International
21   * Business Machines, Inc., http://www.apache.org.  For more
22   * information on the Apache Software Foundation, please see
23   * <http://www.apache.org/>.
24   */
25  
26  package org.apache.struts2.jasper.xmlparser;
27  
28  /***
29   * XMLString is a structure used to pass character arrays. However,
30   * XMLStringBuffer is a buffer in which characters can be appended
31   * and extends XMLString so that it can be passed to methods
32   * expecting an XMLString object. This is a safe operation because
33   * it is assumed that any callee will <strong>not</strong> modify
34   * the contents of the XMLString structure.
35   * <p/>
36   * The contents of the string are managed by the string buffer. As
37   * characters are appended, the string buffer will grow as needed.
38   * <p/>
39   * <strong>Note:</strong> Never set the <code>ch</code>,
40   * <code>offset</code>, and <code>length</code> fields directly.
41   * These fields are managed by the string buffer. In order to reset
42   * the buffer, call <code>clear()</code>.
43   *
44   * @author Andy Clark, IBM
45   * @author Eric Ye, IBM
46   * @version $Id: XMLStringBuffer.java 466606 2006-10-21 23:07:12Z markt $
47   */
48  public class XMLStringBuffer
49          extends XMLString {
50  
51      //
52      // Constants
53      //
54  
55      /***
56       * Default buffer size (32).
57       */
58      public static final int DEFAULT_SIZE = 32;
59  
60      //
61      // Constructors
62      //
63  
64      /***
65       *
66       */
67      public XMLStringBuffer() {
68          this(DEFAULT_SIZE);
69      } // <init>()
70  
71      /***
72       * @param size
73       */
74      public XMLStringBuffer(int size) {
75          ch = new char[size];
76      } // <init>(int)
77  
78      /***
79       * Constructs a string buffer from a char.
80       */
81      public XMLStringBuffer(char c) {
82          this(1);
83          append(c);
84      } // <init>(char)
85  
86      /***
87       * Constructs a string buffer from a String.
88       */
89      public XMLStringBuffer(String s) {
90          this(s.length());
91          append(s);
92      } // <init>(String)
93  
94      /***
95       * Constructs a string buffer from the specified character array.
96       */
97      public XMLStringBuffer(char[] ch, int offset, int length) {
98          this(length);
99          append(ch, offset, length);
100     } // <init>(char[],int,int)
101 
102     /***
103      * Constructs a string buffer from the specified XMLString.
104      */
105     public XMLStringBuffer(XMLString s) {
106         this(s.length);
107         append(s);
108     } // <init>(XMLString)
109 
110     //
111     // Public methods
112     //
113 
114     /***
115      * Clears the string buffer.
116      */
117     public void clear() {
118         offset = 0;
119         length = 0;
120     }
121 
122     /***
123      * append
124      *
125      * @param c
126      */
127     public void append(char c) {
128         if (this.length + 1 > this.ch.length) {
129             int newLength = this.ch.length * 2;
130             if (newLength < this.ch.length + DEFAULT_SIZE)
131                 newLength = this.ch.length + DEFAULT_SIZE;
132             char[] newch = new char[newLength];
133             System.arraycopy(this.ch, 0, newch, 0, this.length);
134             this.ch = newch;
135         }
136         this.ch[this.length] = c;
137         this.length++;
138     } // append(char)
139 
140     /***
141      * append
142      *
143      * @param s
144      */
145     public void append(String s) {
146         int length = s.length();
147         if (this.length + length > this.ch.length) {
148             int newLength = this.ch.length * 2;
149             if (newLength < this.length + length + DEFAULT_SIZE)
150                 newLength = this.ch.length + length + DEFAULT_SIZE;
151             char[] newch = new char[newLength];
152             System.arraycopy(this.ch, 0, newch, 0, this.length);
153             this.ch = newch;
154         }
155         s.getChars(0, length, this.ch, this.length);
156         this.length += length;
157     } // append(String)
158 
159     /***
160      * append
161      *
162      * @param ch
163      * @param offset
164      * @param length
165      */
166     public void append(char[] ch, int offset, int length) {
167         if (this.length + length > this.ch.length) {
168             char[] newch = new char[this.ch.length + length + DEFAULT_SIZE];
169             System.arraycopy(this.ch, 0, newch, 0, this.length);
170             this.ch = newch;
171         }
172         System.arraycopy(ch, offset, this.ch, this.length, length);
173         this.length += length;
174     } // append(char[],int,int)
175 
176     /***
177      * append
178      *
179      * @param s
180      */
181     public void append(XMLString s) {
182         append(s.ch, s.offset, s.length);
183     } // append(XMLString)
184 
185 } // class XMLStringBuffer