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