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   * This class is used as a structure to pass text contained in the underlying
30   * character buffer of the scanner. The offset and length fields allow the
31   * buffer to be re-used without creating new character arrays.
32   * <p/>
33   * <strong>Note:</strong> Methods that are passed an XMLString structure
34   * should consider the contents read-only and not make any modifications
35   * to the contents of the buffer. The method receiving this structure
36   * should also not modify the offset and length if this structure (or
37   * the values of this structure) are passed to another method.
38   * <p/>
39   * <strong>Note:</strong> Methods that are passed an XMLString structure
40   * are required to copy the information out of the buffer if it is to be
41   * saved for use beyond the scope of the method. The contents of the
42   * structure are volatile and the contents of the character buffer cannot
43   * be assured once the method that is passed this structure returns.
44   * Therefore, methods passed this structure should not save any reference
45   * to the structure or the character array contained in the structure.
46   *
47   * @author Eric Ye, IBM
48   * @author Andy Clark, IBM
49   * @version $Id: XMLString.java 466606 2006-10-21 23:07:12Z markt $
50   */
51  public class XMLString {
52  
53      //
54      // Data
55      //
56  
57      /***
58       * The character array.
59       */
60      public char[] ch;
61  
62      /***
63       * The offset into the character array.
64       */
65      public int offset;
66  
67      /***
68       * The length of characters from the offset.
69       */
70      public int length;
71  
72      //
73      // Constructors
74      //
75  
76      /***
77       * Default constructor.
78       */
79      public XMLString() {
80      } // <init>()
81  
82      /***
83       * Constructs an XMLString structure preset with the specified
84       * values.
85       *
86       * @param ch     The character array.
87       * @param offset The offset into the character array.
88       * @param length The length of characters from the offset.
89       */
90      public XMLString(char[] ch, int offset, int length) {
91          setValues(ch, offset, length);
92      } // <init>(char[],int,int)
93  
94      /***
95       * Constructs an XMLString structure with copies of the values in
96       * the given structure.
97       * <p/>
98       * <strong>Note:</strong> This does not copy the character array;
99       * only the reference to the array is copied.
100      *
101      * @param string The XMLString to copy.
102      */
103     public XMLString(XMLString string) {
104         setValues(string);
105     } // <init>(XMLString)
106 
107     //
108     // Public methods
109     //
110 
111     /***
112      * Initializes the contents of the XMLString structure with the
113      * specified values.
114      *
115      * @param ch     The character array.
116      * @param offset The offset into the character array.
117      * @param length The length of characters from the offset.
118      */
119     public void setValues(char[] ch, int offset, int length) {
120         this.ch = ch;
121         this.offset = offset;
122         this.length = length;
123     } // setValues(char[],int,int)
124 
125     /***
126      * Initializes the contents of the XMLString structure with copies
127      * of the given string structure.
128      * <p/>
129      * <strong>Note:</strong> This does not copy the character array;
130      * only the reference to the array is copied.
131      *
132      * @param s
133      */
134     public void setValues(XMLString s) {
135         setValues(s.ch, s.offset, s.length);
136     } // setValues(XMLString)
137 
138     /***
139      * Resets all of the values to their defaults.
140      */
141     public void clear() {
142         this.ch = null;
143         this.offset = 0;
144         this.length = -1;
145     } // clear()
146 
147     /***
148      * Returns true if the contents of this XMLString structure and
149      * the specified array are equal.
150      *
151      * @param ch     The character array.
152      * @param offset The offset into the character array.
153      * @param length The length of characters from the offset.
154      */
155     public boolean equals(char[] ch, int offset, int length) {
156         if (ch == null) {
157             return false;
158         }
159         if (this.length != length) {
160             return false;
161         }
162 
163         for (int i = 0; i < length; i++) {
164             if (this.ch[this.offset + i] != ch[offset + i]) {
165                 return false;
166             }
167         }
168         return true;
169     } // equals(char[],int,int):boolean
170 
171     /***
172      * Returns true if the contents of this XMLString structure and
173      * the specified string are equal.
174      *
175      * @param s The string to compare.
176      */
177     public boolean equals(String s) {
178         if (s == null) {
179             return false;
180         }
181         if (length != s.length()) {
182             return false;
183         }
184 
185         // is this faster than call s.toCharArray first and compare the 
186         // two arrays directly, which will possibly involve creating a
187         // new char array object.
188         for (int i = 0; i < length; i++) {
189             if (ch[offset + i] != s.charAt(i)) {
190                 return false;
191             }
192         }
193 
194         return true;
195     } // equals(String):boolean
196 
197     //
198     // Object methods
199     //
200 
201     /***
202      * Returns a string representation of this object.
203      */
204     public String toString() {
205         return length > 0 ? new String(ch, offset, length) : "";
206     } // toString():String
207 
208 } // class XMLString