View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2000-2002,2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package com.sun.org.apache.xerces.internal.xni;
22  
23  /**
24   * This class is used as a structure to pass text contained in the underlying
25   * character buffer of the scanner. The offset and length fields allow the
26   * buffer to be re-used without creating new character arrays.
27   * <p>
28   * <strong>Note:</strong> Methods that are passed an XMLString structure
29   * should consider the contents read-only and not make any modifications
30   * to the contents of the buffer. The method receiving this structure
31   * should also not modify the offset and length if this structure (or
32   * the values of this structure) are passed to another method.
33   * <p>
34   * <strong>Note:</strong> Methods that are passed an XMLString structure
35   * are required to copy the information out of the buffer if it is to be
36   * saved for use beyond the scope of the method. The contents of the
37   * structure are volatile and the contents of the character buffer cannot
38   * be assured once the method that is passed this structure returns.
39   * Therefore, methods passed this structure should not save any reference
40   * to the structure or the character array contained in the structure.
41   *
42   * @author Eric Ye, IBM
43   * @author Andy Clark, IBM
44   *
45   */
46  public class XMLString {
47  
48      //
49      // Data
50      //
51  
52      /** The character array. */
53      public char[] ch;
54  
55      /** The offset into the character array. */
56      public int offset;
57  
58      /** The length of characters from the offset. */
59      public int length;
60  
61      //
62      // Constructors
63      //
64  
65      /** Default constructor. */
66      public XMLString() {
67      } // <init>()
68  
69      /**
70       * Constructs an XMLString structure preset with the specified
71       * values.
72       *
73       * @param ch     The character array.
74       * @param offset The offset into the character array.
75       * @param length The length of characters from the offset.
76       */
77      public XMLString(char[] ch, int offset, int length) {
78          setValues(ch, offset, length);
79      } // <init>(char[],int,int)
80  
81      /**
82       * Constructs an XMLString structure with copies of the values in
83       * the given structure.
84       * <p>
85       * <strong>Note:</strong> This does not copy the character array;
86       * only the reference to the array is copied.
87       *
88       * @param string The XMLString to copy.
89       */
90      public XMLString(XMLString string) {
91          setValues(string);
92      } // <init>(XMLString)
93  
94      //
95      // Public methods
96      //
97  
98      /**
99       * Initializes the contents of the XMLString structure with the
100      * specified values.
101      *
102      * @param ch     The character array.
103      * @param offset The offset into the character array.
104      * @param length The length of characters from the offset.
105      */
106     public void setValues(char[] ch, int offset, int length) {
107         this.ch = ch;
108         this.offset = offset;
109         this.length = length;
110     } // setValues(char[],int,int)
111 
112     /**
113      * Initializes the contents of the XMLString structure with copies
114      * of the given string structure.
115      * <p>
116      * <strong>Note:</strong> This does not copy the character array;
117      * only the reference to the array is copied.
118      *
119      * @param s
120      */
121     public void setValues(XMLString s) {
122         setValues(s.ch, s.offset, s.length);
123     } // setValues(XMLString)
124 
125     /** Resets all of the values to their defaults. */
126     public void clear() {
127         this.ch = null;
128         this.offset = 0;
129         this.length = -1;
130     } // clear()
131 
132     /**
133      * Returns true if the contents of this XMLString structure and
134      * the specified array are equal.
135      *
136      * @param ch     The character array.
137      * @param offset The offset into the character array.
138      * @param length The length of characters from the offset.
139      */
140     public boolean equals(char[] ch, int offset, int length) {
141         if (ch == null) {
142             return false;
143         }
144         if (this.length != length) {
145             return false;
146         }
147 
148         for (int i=0; i<length; i++) {
149             if (this.ch[this.offset+i] != ch[offset+i] ) {
150                 return false;
151             }
152         }
153         return true;
154     } // equals(char[],int,int):boolean
155 
156     /**
157      * Returns true if the contents of this XMLString structure and
158      * the specified string are equal.
159      *
160      * @param s The string to compare.
161      */
162     public boolean equals(String s) {
163         if (s == null) {
164             return false;
165         }
166         if ( length != s.length() ) {
167             return false;
168         }
169 
170         // is this faster than call s.toCharArray first and compare the
171         // two arrays directly, which will possibly involve creating a
172         // new char array object.
173         for (int i=0; i<length; i++) {
174             if (ch[offset+i] != s.charAt(i)) {
175                 return false;
176             }
177         }
178 
179         return true;
180     } // equals(String):boolean
181 
182     //
183     // Object methods
184     //
185 
186     /** Returns a string representation of this object. */
187     public String toString() {
188         return length > 0 ? new String(ch, offset, length) : "";
189     } // toString():String
190 
191 } // class XMLString