1. /*
  2. * Copyright 1999-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. * $Id: XStringForChars.java,v 1.8 2004/02/17 04:34:38 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.objects;
  20. import com.sun.org.apache.xalan.internal.res.XSLMessages;
  21. import com.sun.org.apache.xml.internal.utils.FastStringBuffer;
  22. import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  23. /**
  24. * This class will wrap a FastStringBuffer and allow for
  25. */
  26. public class XStringForChars extends XString
  27. {
  28. /** The start position in the fsb. */
  29. int m_start;
  30. /** The length of the string. */
  31. int m_length;
  32. protected String m_strCache = null;
  33. /**
  34. * Construct a XNodeSet object.
  35. *
  36. * @param val FastStringBuffer object this will wrap, must be non-null.
  37. * @param start The start position in the array.
  38. * @param length The number of characters to read from the array.
  39. */
  40. public XStringForChars(char[] val, int start, int length)
  41. {
  42. super(val);
  43. m_start = start;
  44. m_length = length;
  45. if(null == val)
  46. throw new IllegalArgumentException(
  47. XSLMessages.createXPATHMessage(XPATHErrorResources.ER_FASTSTRINGBUFFER_CANNOT_BE_NULL, null)); //"The FastStringBuffer argument can not be null!!");
  48. }
  49. /**
  50. * Construct a XNodeSet object.
  51. *
  52. * @param val String object this will wrap.
  53. */
  54. private XStringForChars(String val)
  55. {
  56. super(val);
  57. throw new IllegalArgumentException(
  58. XSLMessages.createXPATHMessage(XPATHErrorResources.ER_XSTRINGFORCHARS_CANNOT_TAKE_STRING, null)); //"XStringForChars can not take a string for an argument!");
  59. }
  60. /**
  61. * Cast result object to a string.
  62. *
  63. * @return The string this wraps or the empty string if null
  64. */
  65. public FastStringBuffer fsb()
  66. {
  67. throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_FSB_NOT_SUPPORTED_XSTRINGFORCHARS, null)); //"fsb() not supported for XStringForChars!");
  68. }
  69. /**
  70. * Cast result object to a string.
  71. *
  72. * @return The string this wraps or the empty string if null
  73. */
  74. public void appendToFsb(com.sun.org.apache.xml.internal.utils.FastStringBuffer fsb)
  75. {
  76. fsb.append((char[])m_obj, m_start, m_length);
  77. }
  78. /**
  79. * Tell if this object contains a java String object.
  80. *
  81. * @return true if this XMLString can return a string without creating one.
  82. */
  83. public boolean hasString()
  84. {
  85. return (null != m_strCache);
  86. }
  87. /**
  88. * Cast result object to a string.
  89. *
  90. * @return The string this wraps or the empty string if null
  91. */
  92. public String str()
  93. {
  94. if(null == m_strCache)
  95. m_strCache = new String((char[])m_obj, m_start, m_length);
  96. return m_strCache;
  97. }
  98. /**
  99. * Since this object is incomplete without the length and the offset, we
  100. * have to convert to a string when this function is called.
  101. *
  102. * @return The java String representation of this object.
  103. */
  104. public Object object()
  105. {
  106. return str();
  107. }
  108. /**
  109. * Directly call the
  110. * characters method on the passed ContentHandler for the
  111. * string-value. Multiple calls to the
  112. * ContentHandler's characters methods may well occur for a single call to
  113. * this method.
  114. *
  115. * @param ch A non-null reference to a ContentHandler.
  116. *
  117. * @throws org.xml.sax.SAXException
  118. */
  119. public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch)
  120. throws org.xml.sax.SAXException
  121. {
  122. ch.characters((char[])m_obj, m_start, m_length);
  123. }
  124. /**
  125. * Directly call the
  126. * comment method on the passed LexicalHandler for the
  127. * string-value.
  128. *
  129. * @param lh A non-null reference to a LexicalHandler.
  130. *
  131. * @throws org.xml.sax.SAXException
  132. */
  133. public void dispatchAsComment(org.xml.sax.ext.LexicalHandler lh)
  134. throws org.xml.sax.SAXException
  135. {
  136. lh.comment((char[])m_obj, m_start, m_length);
  137. }
  138. /**
  139. * Returns the length of this string.
  140. *
  141. * @return the length of the sequence of characters represented by this
  142. * object.
  143. */
  144. public int length()
  145. {
  146. return m_length;
  147. }
  148. /**
  149. * Returns the character at the specified index. An index ranges
  150. * from <code>0</code> to <code>length() - 1</code>. The first character
  151. * of the sequence is at index <code>0</code>, the next at index
  152. * <code>1</code>, and so on, as for array indexing.
  153. *
  154. * @param index the index of the character.
  155. * @return the character at the specified index of this string.
  156. * The first character is at index <code>0</code>.
  157. * @exception IndexOutOfBoundsException if the <code>index</code>
  158. * argument is negative or not less than the length of this
  159. * string.
  160. */
  161. public char charAt(int index)
  162. {
  163. return ((char[])m_obj)[index+m_start];
  164. }
  165. /**
  166. * Copies characters from this string into the destination character
  167. * array.
  168. *
  169. * @param srcBegin index of the first character in the string
  170. * to copy.
  171. * @param srcEnd index after the last character in the string
  172. * to copy.
  173. * @param dst the destination array.
  174. * @param dstBegin the start offset in the destination array.
  175. * @exception IndexOutOfBoundsException If any of the following
  176. * is true:
  177. * <ul><li><code>srcBegin</code> is negative.
  178. * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
  179. * <li><code>srcEnd</code> is greater than the length of this
  180. * string
  181. * <li><code>dstBegin</code> is negative
  182. * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
  183. * <code>dst.length</code></ul>
  184. * @exception NullPointerException if <code>dst</code> is <code>null</code>
  185. */
  186. public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
  187. {
  188. System.arraycopy((char[])m_obj, m_start+srcBegin, dst, dstBegin, srcEnd);
  189. }
  190. }