KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > objects > XStringForChars


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 org.apache.xpath.objects;
20
21 import org.apache.xalan.res.XSLMessages;
22 import org.apache.xml.utils.FastStringBuffer;
23 import org.apache.xpath.res.XPATHErrorResources;
24
25
26 /**
27  * This class will wrap a FastStringBuffer and allow for
28  */

29 public class XStringForChars extends XString
30 {
31   /** The start position in the fsb. */
32   int m_start;
33   
34   /** The length of the string. */
35   int m_length;
36   
37   protected String JavaDoc m_strCache = null;
38   
39   /**
40    * Construct a XNodeSet object.
41    *
42    * @param val FastStringBuffer object this will wrap, must be non-null.
43    * @param start The start position in the array.
44    * @param length The number of characters to read from the array.
45    */

46   public XStringForChars(char[] val, int start, int length)
47   {
48     super(val);
49     m_start = start;
50     m_length = length;
51     if(null == val)
52       throw new IllegalArgumentException JavaDoc(
53                           XSLMessages.createXPATHMessage(XPATHErrorResources.ER_FASTSTRINGBUFFER_CANNOT_BE_NULL, null)); //"The FastStringBuffer argument can not be null!!");
54
}
55
56
57   /**
58    * Construct a XNodeSet object.
59    *
60    * @param val String object this will wrap.
61    */

62   private XStringForChars(String JavaDoc val)
63   {
64     super(val);
65     throw new IllegalArgumentException JavaDoc(
66                       XSLMessages.createXPATHMessage(XPATHErrorResources.ER_XSTRINGFORCHARS_CANNOT_TAKE_STRING, null)); //"XStringForChars can not take a string for an argument!");
67
}
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 FastStringBuffer fsb()
75   {
76     throw new RuntimeException JavaDoc(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_FSB_NOT_SUPPORTED_XSTRINGFORCHARS, null)); //"fsb() not supported for XStringForChars!");
77
}
78   
79   /**
80    * Cast result object to a string.
81    *
82    * @return The string this wraps or the empty string if null
83    */

84   public void appendToFsb(org.apache.xml.utils.FastStringBuffer fsb)
85   {
86     fsb.append((char[])m_obj, m_start, m_length);
87   }
88
89   
90   /**
91    * Tell if this object contains a java String object.
92    *
93    * @return true if this XMLString can return a string without creating one.
94    */

95   public boolean hasString()
96   {
97     return (null != m_strCache);
98   }
99
100   
101   /**
102    * Cast result object to a string.
103    *
104    * @return The string this wraps or the empty string if null
105    */

106   public String JavaDoc str()
107   {
108     if(null == m_strCache)
109       m_strCache = new String JavaDoc((char[])m_obj, m_start, m_length);
110     
111     return m_strCache;
112   }
113   
114
115   /**
116    * Since this object is incomplete without the length and the offset, we
117    * have to convert to a string when this function is called.
118    *
119    * @return The java String representation of this object.
120    */

121   public Object JavaDoc object()
122   {
123     return str();
124   }
125
126   /**
127    * Directly call the
128    * characters method on the passed ContentHandler for the
129    * string-value. Multiple calls to the
130    * ContentHandler's characters methods may well occur for a single call to
131    * this method.
132    *
133    * @param ch A non-null reference to a ContentHandler.
134    *
135    * @throws org.xml.sax.SAXException
136    */

137   public void dispatchCharactersEvents(org.xml.sax.ContentHandler JavaDoc ch)
138       throws org.xml.sax.SAXException JavaDoc
139   {
140     ch.characters((char[])m_obj, m_start, m_length);
141   }
142       
143   /**
144    * Directly call the
145    * comment method on the passed LexicalHandler for the
146    * string-value.
147    *
148    * @param lh A non-null reference to a LexicalHandler.
149    *
150    * @throws org.xml.sax.SAXException
151    */

152   public void dispatchAsComment(org.xml.sax.ext.LexicalHandler JavaDoc lh)
153       throws org.xml.sax.SAXException JavaDoc
154   {
155     lh.comment((char[])m_obj, m_start, m_length);
156   }
157   
158   /**
159    * Returns the length of this string.
160    *
161    * @return the length of the sequence of characters represented by this
162    * object.
163    */

164   public int length()
165   {
166     return m_length;
167   }
168
169   /**
170    * Returns the character at the specified index. An index ranges
171    * from <code>0</code> to <code>length() - 1</code>. The first character
172    * of the sequence is at index <code>0</code>, the next at index
173    * <code>1</code>, and so on, as for array indexing.
174    *
175    * @param index the index of the character.
176    * @return the character at the specified index of this string.
177    * The first character is at index <code>0</code>.
178    * @exception IndexOutOfBoundsException if the <code>index</code>
179    * argument is negative or not less than the length of this
180    * string.
181    */

182   public char charAt(int index)
183   {
184     return ((char[])m_obj)[index+m_start];
185   }
186
187   /**
188    * Copies characters from this string into the destination character
189    * array.
190    *
191    * @param srcBegin index of the first character in the string
192    * to copy.
193    * @param srcEnd index after the last character in the string
194    * to copy.
195    * @param dst the destination array.
196    * @param dstBegin the start offset in the destination array.
197    * @exception IndexOutOfBoundsException If any of the following
198    * is true:
199    * <ul><li><code>srcBegin</code> is negative.
200    * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
201    * <li><code>srcEnd</code> is greater than the length of this
202    * string
203    * <li><code>dstBegin</code> is negative
204    * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
205    * <code>dst.length</code></ul>
206    * @exception NullPointerException if <code>dst</code> is <code>null</code>
207    */

208   public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
209   {
210     System.arraycopy((char[])m_obj, m_start+srcBegin, dst, dstBegin, srcEnd);
211   }
212   
213 }
214
Popular Tags