KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > xni > XMLString


1 /*
2  * Copyright 2000-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.xni;
18
19 /**
20  * This class is used as a structure to pass text contained in the underlying
21  * character buffer of the scanner. The offset and length fields allow the
22  * buffer to be re-used without creating new character arrays.
23  * <p>
24  * <strong>Note:</strong> Methods that are passed an XMLString structure
25  * should consider the contents read-only and not make any modifications
26  * to the contents of the buffer. The method receiving this structure
27  * should also not modify the offset and length if this structure (or
28  * the values of this structure) are passed to another method.
29  * <p>
30  * <strong>Note:</strong> Methods that are passed an XMLString structure
31  * are required to copy the information out of the buffer if it is to be
32  * saved for use beyond the scope of the method. The contents of the
33  * structure are volatile and the contents of the character buffer cannot
34  * be assured once the method that is passed this structure returns.
35  * Therefore, methods passed this structure should not save any reference
36  * to the structure or the character array contained in the structure.
37  *
38  * @author Eric Ye, IBM
39  * @author Andy Clark, IBM
40  *
41  * @version $Id: XMLString.java,v 1.5 2004/02/24 23:15:54 mrglavas Exp $
42  */

43 public class XMLString {
44
45     //
46
// Data
47
//
48

49     /** The character array. */
50     public char[] ch;
51
52     /** The offset into the character array. */
53     public int offset;
54
55     /** The length of characters from the offset. */
56     public int length;
57
58     //
59
// Constructors
60
//
61

62     /** Default constructor. */
63     public XMLString() {
64     } // <init>()
65

66     /**
67      * Constructs an XMLString structure preset with the specified
68      * values.
69      *
70      * @param ch The character array.
71      * @param offset The offset into the character array.
72      * @param length The length of characters from the offset.
73      */

74     public XMLString(char[] ch, int offset, int length) {
75         setValues(ch, offset, length);
76     } // <init>(char[],int,int)
77

78     /**
79      * Constructs an XMLString structure with copies of the values in
80      * the given structure.
81      * <p>
82      * <strong>Note:</strong> This does not copy the character array;
83      * only the reference to the array is copied.
84      *
85      * @param string The XMLString to copy.
86      */

87     public XMLString(XMLString string) {
88         setValues(string);
89     } // <init>(XMLString)
90

91     //
92
// Public methods
93
//
94

95     /**
96      * Initializes the contents of the XMLString structure with the
97      * specified values.
98      *
99      * @param ch The character array.
100      * @param offset The offset into the character array.
101      * @param length The length of characters from the offset.
102      */

103     public void setValues(char[] ch, int offset, int length) {
104         this.ch = ch;
105         this.offset = offset;
106         this.length = length;
107     } // setValues(char[],int,int)
108

109     /**
110      * Initializes the contents of the XMLString structure with copies
111      * of the given string structure.
112      * <p>
113      * <strong>Note:</strong> This does not copy the character array;
114      * only the reference to the array is copied.
115      *
116      * @param s
117      */

118     public void setValues(XMLString s) {
119         setValues(s.ch, s.offset, s.length);
120     } // setValues(XMLString)
121

122     /** Resets all of the values to their defaults. */
123     public void clear() {
124         this.ch = null;
125         this.offset = 0;
126         this.length = -1;
127     } // clear()
128

129     /**
130      * Returns true if the contents of this XMLString structure and
131      * the specified array are equal.
132      *
133      * @param ch The character array.
134      * @param offset The offset into the character array.
135      * @param length The length of characters from the offset.
136      */

137     public boolean equals(char[] ch, int offset, int length) {
138         if (ch == null) {
139             return false;
140         }
141         if (this.length != length) {
142             return false;
143         }
144
145         for (int i=0; i<length; i++) {
146             if (this.ch[this.offset+i] != ch[offset+i] ) {
147                 return false;
148             }
149         }
150         return true;
151     } // equals(char[],int,int):boolean
152

153     /**
154      * Returns true if the contents of this XMLString structure and
155      * the specified string are equal.
156      *
157      * @param s The string to compare.
158      */

159     public boolean equals(String JavaDoc s) {
160         if (s == null) {
161             return false;
162         }
163         if ( length != s.length() ) {
164             return false;
165         }
166
167         // is this faster than call s.toCharArray first and compare the
168
// two arrays directly, which will possibly involve creating a
169
// new char array object.
170
for (int i=0; i<length; i++) {
171             if (ch[offset+i] != s.charAt(i)) {
172                 return false;
173             }
174         }
175
176         return true;
177     } // equals(String):boolean
178

179     //
180
// Object methods
181
//
182

183     /** Returns a string representation of this object. */
184     public String JavaDoc toString() {
185         return length > 0 ? new String JavaDoc(ch, offset, length) : "";
186     } // toString():String
187

188 } // class XMLString
189
Popular Tags