001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: * ====================================================================
017: *
018: * This software consists of voluntary contributions made by many
019: * individuals on behalf of the Apache Software Foundation and was
020: * originally based on software copyright (c) 1999, International
021: * Business Machines, Inc., http://www.apache.org. For more
022: * information on the Apache Software Foundation, please see
023: * <http://www.apache.org/>.
024: */
025:
026: package org.apache.jasper.xmlparser;
027:
028: /**
029: * This class is used as a structure to pass text contained in the underlying
030: * character buffer of the scanner. The offset and length fields allow the
031: * buffer to be re-used without creating new character arrays.
032: * <p>
033: * <strong>Note:</strong> Methods that are passed an XMLString structure
034: * should consider the contents read-only and not make any modifications
035: * to the contents of the buffer. The method receiving this structure
036: * should also not modify the offset and length if this structure (or
037: * the values of this structure) are passed to another method.
038: * <p>
039: * <strong>Note:</strong> Methods that are passed an XMLString structure
040: * are required to copy the information out of the buffer if it is to be
041: * saved for use beyond the scope of the method. The contents of the
042: * structure are volatile and the contents of the character buffer cannot
043: * be assured once the method that is passed this structure returns.
044: * Therefore, methods passed this structure should not save any reference
045: * to the structure or the character array contained in the structure.
046: *
047: * @author Eric Ye, IBM
048: * @author Andy Clark, IBM
049: *
050: * @version $Id: XMLString.java 467222 2006-10-24 03:17:11Z markt $
051: */
052: public class XMLString {
053:
054: //
055: // Data
056: //
057:
058: /** The character array. */
059: public char[] ch;
060:
061: /** The offset into the character array. */
062: public int offset;
063:
064: /** The length of characters from the offset. */
065: public int length;
066:
067: //
068: // Constructors
069: //
070:
071: /** Default constructor. */
072: public XMLString() {
073: } // <init>()
074:
075: /**
076: * Constructs an XMLString structure preset with the specified
077: * values.
078: *
079: * @param ch The character array.
080: * @param offset The offset into the character array.
081: * @param length The length of characters from the offset.
082: */
083: public XMLString(char[] ch, int offset, int length) {
084: setValues(ch, offset, length);
085: } // <init>(char[],int,int)
086:
087: /**
088: * Constructs an XMLString structure with copies of the values in
089: * the given structure.
090: * <p>
091: * <strong>Note:</strong> This does not copy the character array;
092: * only the reference to the array is copied.
093: *
094: * @param string The XMLString to copy.
095: */
096: public XMLString(XMLString string) {
097: setValues(string);
098: } // <init>(XMLString)
099:
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 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 toString() {
194: return length > 0 ? new String(ch, offset, length) : "";
195: } // toString():String
196:
197: } // class XMLString
|