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: package org.apache.xerces.xni;
019:
020: /**
021: * This class is used as a structure to pass text contained in the underlying
022: * character buffer of the scanner. The offset and length fields allow the
023: * buffer to be re-used without creating new character arrays.
024: * <p>
025: * <strong>Note:</strong> Methods that are passed an XMLString structure
026: * should consider the contents read-only and not make any modifications
027: * to the contents of the buffer. The method receiving this structure
028: * should also not modify the offset and length if this structure (or
029: * the values of this structure) are passed to another method.
030: * <p>
031: * <strong>Note:</strong> Methods that are passed an XMLString structure
032: * are required to copy the information out of the buffer if it is to be
033: * saved for use beyond the scope of the method. The contents of the
034: * structure are volatile and the contents of the character buffer cannot
035: * be assured once the method that is passed this structure returns.
036: * Therefore, methods passed this structure should not save any reference
037: * to the structure or the character array contained in the structure.
038: *
039: * @author Eric Ye, IBM
040: * @author Andy Clark, IBM
041: *
042: * @version $Id: XMLString.java 447247 2006-09-18 05:23:52Z mrglavas $
043: */
044: public class XMLString {
045:
046: //
047: // Data
048: //
049:
050: /** The character array. */
051: public char[] ch;
052:
053: /** The offset into the character array. */
054: public int offset;
055:
056: /** The length of characters from the offset. */
057: public int length;
058:
059: //
060: // Constructors
061: //
062:
063: /** Default constructor. */
064: public XMLString() {
065: } // <init>()
066:
067: /**
068: * Constructs an XMLString structure preset with the specified
069: * values.
070: *
071: * @param ch The character array.
072: * @param offset The offset into the character array.
073: * @param length The length of characters from the offset.
074: */
075: public XMLString(char[] ch, int offset, int length) {
076: setValues(ch, offset, length);
077: } // <init>(char[],int,int)
078:
079: /**
080: * Constructs an XMLString structure with copies of the values in
081: * the given structure.
082: * <p>
083: * <strong>Note:</strong> This does not copy the character array;
084: * only the reference to the array is copied.
085: *
086: * @param string The XMLString to copy.
087: */
088: public XMLString(XMLString string) {
089: setValues(string);
090: } // <init>(XMLString)
091:
092: //
093: // Public methods
094: //
095:
096: /**
097: * Initializes the contents of the XMLString structure with the
098: * specified values.
099: *
100: * @param ch The character array.
101: * @param offset The offset into the character array.
102: * @param length The length of characters from the offset.
103: */
104: public void setValues(char[] ch, int offset, int length) {
105: this .ch = ch;
106: this .offset = offset;
107: this .length = length;
108: } // setValues(char[],int,int)
109:
110: /**
111: * Initializes the contents of the XMLString structure with copies
112: * of the given string structure.
113: * <p>
114: * <strong>Note:</strong> This does not copy the character array;
115: * only the reference to the array is copied.
116: *
117: * @param s
118: */
119: public void setValues(XMLString s) {
120: setValues(s.ch, s.offset, s.length);
121: } // setValues(XMLString)
122:
123: /** Resets all of the values to their defaults. */
124: public void clear() {
125: this .ch = null;
126: this .offset = 0;
127: this .length = -1;
128: } // clear()
129:
130: /**
131: * Returns true if the contents of this XMLString structure and
132: * the specified array are equal.
133: *
134: * @param ch The character array.
135: * @param offset The offset into the character array.
136: * @param length The length of characters from the offset.
137: */
138: public boolean equals(char[] ch, int offset, int length) {
139: if (ch == null) {
140: return false;
141: }
142: if (this .length != length) {
143: return false;
144: }
145:
146: for (int i = 0; i < length; i++) {
147: if (this .ch[this .offset + i] != ch[offset + i]) {
148: return false;
149: }
150: }
151: return true;
152: } // equals(char[],int,int):boolean
153:
154: /**
155: * Returns true if the contents of this XMLString structure and
156: * the specified string are equal.
157: *
158: * @param s The string to compare.
159: */
160: public boolean equals(String s) {
161: if (s == null) {
162: return false;
163: }
164: if (length != s.length()) {
165: return false;
166: }
167:
168: // is this faster than call s.toCharArray first and compare the
169: // two arrays directly, which will possibly involve creating a
170: // new char array object.
171: for (int i = 0; i < length; i++) {
172: if (ch[offset + i] != s.charAt(i)) {
173: return false;
174: }
175: }
176:
177: return true;
178: } // equals(String):boolean
179:
180: //
181: // Object methods
182: //
183:
184: /** Returns a string representation of this object. */
185: public String toString() {
186: return length > 0 ? new String(ch, offset, length) : "";
187: } // toString():String
188:
189: } // class XMLString
|