1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.xni;
  58. /**
  59. * This class is used as a structure to pass text contained in the underlying
  60. * character buffer of the scanner. The offset and length fields allow the
  61. * buffer to be re-used without creating new character arrays.
  62. * <p>
  63. * <strong>Note:</strong> Methods that are passed an XMLString structure
  64. * should consider the contents read-only and not make any modifications
  65. * to the contents of the buffer. The method receiving this structure
  66. * should also not modify the offset and length if this structure (or
  67. * the values of this structure) are passed to another method.
  68. * <p>
  69. * <strong>Note:</strong> Methods that are passed an XMLString structure
  70. * are required to copy the information out of the buffer if it is to be
  71. * saved for use beyond the scope of the method. The contents of the
  72. * structure are volatile and the contents of the character buffer cannot
  73. * be assured once the method that is passed this structure returns.
  74. * Therefore, methods passed this structure should not save any reference
  75. * to the structure or the character array contained in the structure.
  76. *
  77. * @author Eric Ye, IBM
  78. * @author Andy Clark, IBM
  79. *
  80. * @version $Id: XMLString.java,v 1.4 2002/01/29 01:15:19 lehors Exp $
  81. */
  82. public class XMLString {
  83. //
  84. // Data
  85. //
  86. /** The character array. */
  87. public char[] ch;
  88. /** The offset into the character array. */
  89. public int offset;
  90. /** The length of characters from the offset. */
  91. public int length;
  92. //
  93. // Constructors
  94. //
  95. /** Default constructor. */
  96. public XMLString() {
  97. } // <init>()
  98. /**
  99. * Constructs an XMLString structure preset with the specified
  100. * 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 XMLString(char[] ch, int offset, int length) {
  107. setValues(ch, offset, length);
  108. } // <init>(char[],int,int)
  109. /**
  110. * Constructs an XMLString structure with copies of the values in
  111. * the given 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 string The XMLString to copy.
  117. */
  118. public XMLString(XMLString string) {
  119. setValues(string);
  120. } // <init>(XMLString)
  121. //
  122. // Public methods
  123. //
  124. /**
  125. * Initializes the contents of the XMLString structure with the
  126. * specified values.
  127. *
  128. * @param ch The character array.
  129. * @param offset The offset into the character array.
  130. * @param length The length of characters from the offset.
  131. */
  132. public void setValues(char[] ch, int offset, int length) {
  133. this.ch = ch;
  134. this.offset = offset;
  135. this.length = length;
  136. } // setValues(char[],int,int)
  137. /**
  138. * Initializes the contents of the XMLString structure with copies
  139. * of the given string structure.
  140. * <p>
  141. * <strong>Note:</strong> This does not copy the character array;
  142. * only the reference to the array is copied.
  143. *
  144. * @param s
  145. */
  146. public void setValues(XMLString s) {
  147. setValues(s.ch, s.offset, s.length);
  148. } // setValues(XMLString)
  149. /** Resets all of the values to their defaults. */
  150. public void clear() {
  151. this.ch = null;
  152. this.offset = 0;
  153. this.length = -1;
  154. } // clear()
  155. /**
  156. * Returns true if the contents of this XMLString structure and
  157. * the specified array are equal.
  158. *
  159. * @param ch The character array.
  160. * @param offset The offset into the character array.
  161. * @param length The length of characters from the offset.
  162. */
  163. public boolean equals(char[] ch, int offset, int length) {
  164. if (ch == null) {
  165. return false;
  166. }
  167. if (this.length != length) {
  168. return false;
  169. }
  170. for (int i=0; i<length; i++) {
  171. if (this.ch[this.offset+i] != ch[offset+i] ) {
  172. return false;
  173. }
  174. }
  175. return true;
  176. } // equals(char[],int,int):boolean
  177. /**
  178. * Returns true if the contents of this XMLString structure and
  179. * the specified string are equal.
  180. *
  181. * @param s The string to compare.
  182. */
  183. public boolean equals(String s) {
  184. if (s == null) {
  185. return false;
  186. }
  187. if ( length != s.length() ) {
  188. return false;
  189. }
  190. // is this faster than call s.toCharArray first and compare the
  191. // two arrays directly, which will possibly involve creating a
  192. // new char array object.
  193. for (int i=0; i<length; i++) {
  194. if (ch[offset+i] != s.charAt(i)) {
  195. return false;
  196. }
  197. }
  198. return true;
  199. } // equals(String):boolean
  200. //
  201. // Object methods
  202. //
  203. /** Returns a string representation of this object. */
  204. public String toString() {
  205. return length > 0 ? new String(ch, offset, length) : "";
  206. } // toString():String
  207. } // class XMLString