xstringforfsb.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,081 行 · 第 1/3 页
JAVA
1,081 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xpath.objects;import org.apache.xml.utils.FastStringBuffer;import org.apache.xml.utils.XMLString;import org.apache.xml.utils.XMLStringFactory;import org.apache.xml.utils.XMLCharacterRecognizer;import org.apache.xpath.res.XPATHErrorResources;import org.apache.xalan.res.XSLMessages;import java.util.Locale;/** * This class will wrap a FastStringBuffer and allow for */public class XStringForFSB extends XString{ /** The start position in the fsb. */ int m_start; /** The length of the string. */ int m_length; /** If the str() function is called, the string will be cached here. */ protected String m_strCache = null; /** cached hash code */ protected int m_hash = 0; /** * Construct a XNodeSet object. * * @param val FastStringBuffer object this will wrap, must be non-null. * @param start The start position in the array. * @param length The number of characters to read from the array. */ public XStringForFSB(FastStringBuffer val, int start, int length) { super(val); m_start = start; m_length = length; if (null == val) throw new IllegalArgumentException( XSLMessages.createXPATHMessage(XPATHErrorResources.ER_FASTSTRINGBUFFER_CANNOT_BE_NULL, null)); } /** * Construct a XNodeSet object. * * @param val String object this will wrap. */ private XStringForFSB(String val) { super(val); throw new IllegalArgumentException( XSLMessages.createXPATHMessage(XPATHErrorResources.ER_FSB_CANNOT_TAKE_STRING, null)); // "XStringForFSB can not take a string for an argument!"); } /** * Cast result object to a string. * * @return The string this wraps or the empty string if null */ public FastStringBuffer fsb() { return ((FastStringBuffer) m_obj); } /** * Cast result object to a string. * * @return The string this wraps or the empty string if null */ public void appendToFsb(org.apache.xml.utils.FastStringBuffer fsb) { // %OPT% !!! FSB has to be updated to take partial fsb's for append. fsb.append(str()); } /** * Tell if this object contains a java String object. * * @return true if this XMLString can return a string without creating one. */ public boolean hasString() { return (null != m_strCache); }// /** NEEDSDOC Field strCount */// public static int strCount = 0;//// /** NEEDSDOC Field xtable */// static java.util.Hashtable xtable = new java.util.Hashtable(); /** * Since this object is incomplete without the length and the offset, we * have to convert to a string when this function is called. * * @return The java String representation of this object. */ public Object object() { return str(); } /** * Cast result object to a string. * * @return The string this wraps or the empty string if null */ public String str() { if (null == m_strCache) { m_strCache = fsb().getString(m_start, m_length);// strCount++;//// RuntimeException e = new RuntimeException("Bad! Bad!");// java.io.CharArrayWriter writer = new java.io.CharArrayWriter();// java.io.PrintWriter pw = new java.io.PrintWriter(writer);//// e.printStackTrace(pw);//// String str = writer.toString();//// str = str.substring(0, 600);//// if (null == xtable.get(str))// {// xtable.put(str, str);// System.out.println(str);// }// System.out.println("strCount: " + strCount);// throw e;// e.printStackTrace(); // System.exit(-1); } return m_strCache; } /** * Directly call the * characters method on the passed ContentHandler for the * string-value. Multiple calls to the * ContentHandler's characters methods may well occur for a single call to * this method. * * @param ch A non-null reference to a ContentHandler. * * @throws org.xml.sax.SAXException */ public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch) throws org.xml.sax.SAXException { fsb().sendSAXcharacters(ch, m_start, m_length); } /** * Directly call the * comment method on the passed LexicalHandler for the * string-value. * * @param lh A non-null reference to a LexicalHandler. * * @throws org.xml.sax.SAXException */ public void dispatchAsComment(org.xml.sax.ext.LexicalHandler lh) throws org.xml.sax.SAXException { fsb().sendSAXComment(lh, m_start, m_length); } /** * Returns the length of this string. * * @return the length of the sequence of characters represented by this * object. */ public int length() { return m_length; } /** * Returns the character at the specified index. An index ranges * from <code>0</code> to <code>length() - 1</code>. The first character * of the sequence is at index <code>0</code>, the next at index * <code>1</code>, and so on, as for array indexing. * * @param index the index of the character. * @return the character at the specified index of this string. * The first character is at index <code>0</code>. * @exception IndexOutOfBoundsException if the <code>index</code> * argument is negative or not less than the length of this * string. */ public char charAt(int index) { return fsb().charAt(m_start + index); } /** * Copies characters from this string into the destination character * array. * * @param srcBegin index of the first character in the string * to copy. * @param srcEnd index after the last character in the string * to copy. * @param dst the destination array. * @param dstBegin the start offset in the destination array. * @exception IndexOutOfBoundsException If any of the following * is true: * <ul><li><code>srcBegin</code> is negative. * <li><code>srcBegin</code> is greater than <code>srcEnd</code> * <li><code>srcEnd</code> is greater than the length of this * string * <li><code>dstBegin</code> is negative * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than * <code>dst.length</code></ul> * @exception NullPointerException if <code>dst</code> is <code>null</code> */ public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { // %OPT% Need to call this on FSB when it is implemented. // %UNTESTED% (I don't think anyone calls this yet?) int n = srcEnd - srcBegin; if (n > m_length) n = m_length; if (n > (dst.length - dstBegin)) n = (dst.length - dstBegin); int end = srcBegin + m_start + n; int d = dstBegin; FastStringBuffer fsb = fsb(); for (int i = srcBegin + m_start; i < end; i++) { dst[d++] = fsb.charAt(i); } } /** * Compares this string to the specified object. * The result is <code>true</code> if and only if the argument is not * <code>null</code> and is a <code>String</code> object that represents * the same sequence of characters as this object. * * @param anObject the object to compare this <code>String</code> * against. * * NEEDSDOC @param obj2 * @return <code>true</code> if the <code>String </code>are equal; * <code>false</code> otherwise. * @see java.lang.String#compareTo(java.lang.String) * @see java.lang.String#equalsIgnoreCase(java.lang.String) */ public boolean equals(XMLString obj2) { if (this == obj2) { return true; } int n = m_length; if (n == obj2.length()) { FastStringBuffer fsb = fsb(); int i = m_start; int j = 0; while (n-- != 0) { if (fsb.charAt(i) != obj2.charAt(j)) { return false; } i++; j++; } return true; } return false; } /**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?