KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > xmlparser > XMLString


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.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  *
50  * @version $Id: XMLString.java 467222 2006-10-24 03:17:11Z markt $
51  */

52 public class XMLString {
53
54     //
55
// Data
56
//
57

58     /** The character array. */
59     public char[] ch;
60
61     /** The offset into the character array. */
62     public int offset;
63
64     /** The length of characters from the offset. */
65     public int length;
66
67     //
68
// Constructors
69
//
70

71     /** Default constructor. */
72     public XMLString() {
73     } // <init>()
74

75     /**
76      * Constructs an XMLString structure preset with the specified
77      * values.
78      *
79      * @param ch The character array.
80      * @param offset The offset into the character array.
81      * @param length The length of characters from the offset.
82      */

83     public XMLString(char[] ch, int offset, int length) {
84         setValues(ch, offset, length);
85     } // <init>(char[],int,int)
86

87     /**
88      * Constructs an XMLString structure with copies of the values in
89      * the given structure.
90      * <p>
91      * <strong>Note:</strong> This does not copy the character array;
92      * only the reference to the array is copied.
93      *
94      * @param string The XMLString to copy.
95      */

96     public XMLString(XMLString string) {
97         setValues(string);
98     } // <init>(XMLString)
99

100     //
101
// Public methods
102
//
103

104     /**
105      * Initializes the contents of the XMLString structure with the
106      * specified values.
107      *
108      * @param ch The character array.
109      * @param offset The offset into the character array.
110      * @param length The length of characters from the offset.
111      */

112     public void setValues(char[] ch, int offset, int length) {
113         this.ch = ch;
114         this.offset = offset;
115         this.length = length;
116     } // setValues(char[],int,int)
117

118     /**
119      * Initializes the contents of the XMLString structure with copies
120      * of the given string structure.
121      * <p>
122      * <strong>Note:</strong> This does not copy the character array;
123      * only the reference to the array is copied.
124      *
125      * @param s
126      */

127     public void setValues(XMLString s) {
128         setValues(s.ch, s.offset, s.length);
129     } // setValues(XMLString)
130

131     /** Resets all of the values to their defaults. */
132     public void clear() {
133         this.ch = null;
134         this.offset = 0;
135         this.length = -1;
136     } // clear()
137

138     /**
139      * Returns true if the contents of this XMLString structure and
140      * the specified array are equal.
141      *
142      * @param ch The character array.
143      * @param offset The offset into the character array.
144      * @param length The length of characters from the offset.
145      */

146     public boolean equals(char[] ch, int offset, int length) {
147         if (ch == null) {
148             return false;
149         }
150         if (this.length != length) {
151             return false;
152         }
153
154         for (int i=0; i<length; i++) {
155             if (this.ch[this.offset+i] != ch[offset+i] ) {
156                 return false;
157             }
158         }
159         return true;
160     } // equals(char[],int,int):boolean
161

162     /**
163      * Returns true if the contents of this XMLString structure and
164      * the specified string are equal.
165      *
166      * @param s The string to compare.
167      */

168     public boolean equals(String JavaDoc s) {
169         if (s == null) {
170             return false;
171         }
172         if ( length != s.length() ) {
173             return false;
174         }
175
176         // is this faster than call s.toCharArray first and compare the
177
// two arrays directly, which will possibly involve creating a
178
// new char array object.
179
for (int i=0; i<length; i++) {
180             if (ch[offset+i] != s.charAt(i)) {
181                 return false;
182             }
183         }
184
185         return true;
186     } // equals(String):boolean
187

188     //
189
// Object methods
190
//
191

192     /** Returns a string representation of this object. */
193     public String JavaDoc toString() {
194         return length > 0 ? new String JavaDoc(ch, offset, length) : "";
195     } // toString():String
196

197 } // class XMLString
198
Popular Tags