001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: XStringForChars.java,v 1.9 2004/08/17 19:25:40 jycli Exp $
018: */
019: package org.apache.xpath.objects;
020:
021: import org.apache.xalan.res.XSLMessages;
022: import org.apache.xml.utils.FastStringBuffer;
023: import org.apache.xpath.res.XPATHErrorResources;
024:
025: /**
026: * This class will wrap a FastStringBuffer and allow for
027: */
028: public class XStringForChars extends XString {
029: static final long serialVersionUID = -2235248887220850467L;
030: /** The start position in the fsb. */
031: int m_start;
032:
033: /** The length of the string. */
034: int m_length;
035:
036: protected String m_strCache = null;
037:
038: /**
039: * Construct a XNodeSet object.
040: *
041: * @param val FastStringBuffer object this will wrap, must be non-null.
042: * @param start The start position in the array.
043: * @param length The number of characters to read from the array.
044: */
045: public XStringForChars(char[] val, int start, int length) {
046: super (val);
047: m_start = start;
048: m_length = length;
049: if (null == val)
050: throw new IllegalArgumentException(
051: XSLMessages
052: .createXPATHMessage(
053: XPATHErrorResources.ER_FASTSTRINGBUFFER_CANNOT_BE_NULL,
054: null)); //"The FastStringBuffer argument can not be null!!");
055: }
056:
057: /**
058: * Construct a XNodeSet object.
059: *
060: * @param val String object this will wrap.
061: */
062: private XStringForChars(String val) {
063: super (val);
064: throw new IllegalArgumentException(
065: XSLMessages
066: .createXPATHMessage(
067: XPATHErrorResources.ER_XSTRINGFORCHARS_CANNOT_TAKE_STRING,
068: null)); //"XStringForChars can not take a string for an argument!");
069: }
070:
071: /**
072: * Cast result object to a string.
073: *
074: * @return The string this wraps or the empty string if null
075: */
076: public FastStringBuffer fsb() {
077: throw new RuntimeException(
078: XSLMessages
079: .createXPATHMessage(
080: XPATHErrorResources.ER_FSB_NOT_SUPPORTED_XSTRINGFORCHARS,
081: null)); //"fsb() not supported for XStringForChars!");
082: }
083:
084: /**
085: * Cast result object to a string.
086: *
087: * @return The string this wraps or the empty string if null
088: */
089: public void appendToFsb(org.apache.xml.utils.FastStringBuffer fsb) {
090: fsb.append((char[]) m_obj, m_start, m_length);
091: }
092:
093: /**
094: * Tell if this object contains a java String object.
095: *
096: * @return true if this XMLString can return a string without creating one.
097: */
098: public boolean hasString() {
099: return (null != m_strCache);
100: }
101:
102: /**
103: * Cast result object to a string.
104: *
105: * @return The string this wraps or the empty string if null
106: */
107: public String str() {
108: if (null == m_strCache)
109: m_strCache = new String((char[]) m_obj, m_start, m_length);
110:
111: return m_strCache;
112: }
113:
114: /**
115: * Since this object is incomplete without the length and the offset, we
116: * have to convert to a string when this function is called.
117: *
118: * @return The java String representation of this object.
119: */
120: public Object object() {
121: return str();
122: }
123:
124: /**
125: * Directly call the
126: * characters method on the passed ContentHandler for the
127: * string-value. Multiple calls to the
128: * ContentHandler's characters methods may well occur for a single call to
129: * this method.
130: *
131: * @param ch A non-null reference to a ContentHandler.
132: *
133: * @throws org.xml.sax.SAXException
134: */
135: public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch)
136: throws org.xml.sax.SAXException {
137: ch.characters((char[]) m_obj, m_start, m_length);
138: }
139:
140: /**
141: * Directly call the
142: * comment method on the passed LexicalHandler for the
143: * string-value.
144: *
145: * @param lh A non-null reference to a LexicalHandler.
146: *
147: * @throws org.xml.sax.SAXException
148: */
149: public void dispatchAsComment(org.xml.sax.ext.LexicalHandler lh)
150: throws org.xml.sax.SAXException {
151: lh.comment((char[]) m_obj, m_start, m_length);
152: }
153:
154: /**
155: * Returns the length of this string.
156: *
157: * @return the length of the sequence of characters represented by this
158: * object.
159: */
160: public int length() {
161: return m_length;
162: }
163:
164: /**
165: * Returns the character at the specified index. An index ranges
166: * from <code>0</code> to <code>length() - 1</code>. The first character
167: * of the sequence is at index <code>0</code>, the next at index
168: * <code>1</code>, and so on, as for array indexing.
169: *
170: * @param index the index of the character.
171: * @return the character at the specified index of this string.
172: * The first character is at index <code>0</code>.
173: * @exception IndexOutOfBoundsException if the <code>index</code>
174: * argument is negative or not less than the length of this
175: * string.
176: */
177: public char charAt(int index) {
178: return ((char[]) m_obj)[index + m_start];
179: }
180:
181: /**
182: * Copies characters from this string into the destination character
183: * array.
184: *
185: * @param srcBegin index of the first character in the string
186: * to copy.
187: * @param srcEnd index after the last character in the string
188: * to copy.
189: * @param dst the destination array.
190: * @param dstBegin the start offset in the destination array.
191: * @exception IndexOutOfBoundsException If any of the following
192: * is true:
193: * <ul><li><code>srcBegin</code> is negative.
194: * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
195: * <li><code>srcEnd</code> is greater than the length of this
196: * string
197: * <li><code>dstBegin</code> is negative
198: * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
199: * <code>dst.length</code></ul>
200: * @exception NullPointerException if <code>dst</code> is <code>null</code>
201: */
202: public void getChars(int srcBegin, int srcEnd, char dst[],
203: int dstBegin) {
204: System.arraycopy((char[]) m_obj, m_start + srcBegin, dst,
205: dstBegin, srcEnd);
206: }
207:
208: }
|