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