1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xpath.internal.objects;
23
24import com.sun.org.apache.xalan.internal.res.XSLMessages;
25import com.sun.org.apache.xml.internal.utils.FastStringBuffer;
26import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
27
28
29/**
30 * This class will wrap a FastStringBuffer and allow for
31 */
32public class XStringForChars extends XString
33{
34    static final long serialVersionUID = -2235248887220850467L;
35  /** The start position in the fsb. */
36  int m_start;
37
38  /** The length of the string. */
39  int m_length;
40
41  protected String m_strCache = null;
42
43  /**
44   * Construct a XNodeSet object.
45   *
46   * @param val FastStringBuffer object this will wrap, must be non-null.
47   * @param start The start position in the array.
48   * @param length The number of characters to read from the array.
49   */
50  public XStringForChars(char[] val, int start, int length)
51  {
52    super(val);
53    m_start = start;
54    m_length = length;
55    if(null == val)
56      throw new IllegalArgumentException(
57                          XSLMessages.createXPATHMessage(XPATHErrorResources.ER_FASTSTRINGBUFFER_CANNOT_BE_NULL, null)); //"The FastStringBuffer argument can not be null!!");
58  }
59
60
61  /**
62   * Construct a XNodeSet object.
63   *
64   * @param val String object this will wrap.
65   */
66  private XStringForChars(String val)
67  {
68    super(val);
69    throw new IllegalArgumentException(
70                      XSLMessages.createXPATHMessage(XPATHErrorResources.ER_XSTRINGFORCHARS_CANNOT_TAKE_STRING, null)); //"XStringForChars can not take a string for an argument!");
71  }
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 FastStringBuffer fsb()
79  {
80    throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_FSB_NOT_SUPPORTED_XSTRINGFORCHARS, null)); //"fsb() not supported for XStringForChars!");
81  }
82
83  /**
84   * Cast result object to a string.
85   *
86   * @return The string this wraps or the empty string if null
87   */
88  public void appendToFsb(com.sun.org.apache.xml.internal.utils.FastStringBuffer fsb)
89  {
90    fsb.append((char[])m_obj, m_start, m_length);
91  }
92
93
94  /**
95   * Tell if this object contains a java String object.
96   *
97   * @return true if this XMLString can return a string without creating one.
98   */
99  public boolean hasString()
100  {
101    return (null != m_strCache);
102  }
103
104
105  /**
106   * Cast result object to a string.
107   *
108   * @return The string this wraps or the empty string if null
109   */
110  public String str()
111  {
112    if(null == m_strCache)
113      m_strCache = new String((char[])m_obj, m_start, m_length);
114
115    return m_strCache;
116  }
117
118
119  /**
120   * Since this object is incomplete without the length and the offset, we
121   * have to convert to a string when this function is called.
122   *
123   * @return The java String representation of this object.
124   */
125  public Object object()
126  {
127    return str();
128  }
129
130  /**
131   * Directly call the
132   * characters method on the passed ContentHandler for the
133   * string-value. Multiple calls to the
134   * ContentHandler's characters methods may well occur for a single call to
135   * this method.
136   *
137   * @param ch A non-null reference to a ContentHandler.
138   *
139   * @throws org.xml.sax.SAXException
140   */
141  public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch)
142      throws org.xml.sax.SAXException
143  {
144    ch.characters((char[])m_obj, m_start, m_length);
145  }
146
147  /**
148   * Directly call the
149   * comment method on the passed LexicalHandler for the
150   * string-value.
151   *
152   * @param lh A non-null reference to a LexicalHandler.
153   *
154   * @throws org.xml.sax.SAXException
155   */
156  public void dispatchAsComment(org.xml.sax.ext.LexicalHandler lh)
157      throws org.xml.sax.SAXException
158  {
159    lh.comment((char[])m_obj, m_start, m_length);
160  }
161
162  /**
163   * Returns the length of this string.
164   *
165   * @return  the length of the sequence of characters represented by this
166   *          object.
167   */
168  public int length()
169  {
170    return m_length;
171  }
172
173  /**
174   * Returns the character at the specified index. An index ranges
175   * from <code>0</code> to <code>length() - 1</code>. The first character
176   * of the sequence is at index <code>0</code>, the next at index
177   * <code>1</code>, and so on, as for array indexing.
178   *
179   * @param      index   the index of the character.
180   * @return     the character at the specified index of this string.
181   *             The first character is at index <code>0</code>.
182   * @exception  IndexOutOfBoundsException  if the <code>index</code>
183   *             argument is negative or not less than the length of this
184   *             string.
185   */
186  public char charAt(int index)
187  {
188    return ((char[])m_obj)[index+m_start];
189  }
190
191  /**
192   * Copies characters from this string into the destination character
193   * array.
194   *
195   * @param      srcBegin   index of the first character in the string
196   *                        to copy.
197   * @param      srcEnd     index after the last character in the string
198   *                        to copy.
199   * @param      dst        the destination array.
200   * @param      dstBegin   the start offset in the destination array.
201   * @exception IndexOutOfBoundsException If any of the following
202   *            is true:
203   *            <ul><li><code>srcBegin</code> is negative.
204   *            <li><code>srcBegin</code> is greater than <code>srcEnd</code>
205   *            <li><code>srcEnd</code> is greater than the length of this
206   *                string
207   *            <li><code>dstBegin</code> is negative
208   *            <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
209   *                <code>dst.length</code></ul>
210   * @exception NullPointerException if <code>dst</code> is <code>null</code>
211   */
212  public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
213  {
214    System.arraycopy((char[])m_obj, m_start+srcBegin, dst, dstBegin, srcEnd);
215  }
216
217}
218