|
Java example source code file (XStringForFSB.java)
The XStringForFSB.java Java example source code/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: XStringForFSB.java,v 1.2.4.2 2005/09/14 20:46:27 jeffsuttor Exp $
*/
package com.sun.org.apache.xpath.internal.objects;
import com.sun.org.apache.xalan.internal.res.XSLMessages;
import com.sun.org.apache.xml.internal.utils.FastStringBuffer;
import com.sun.org.apache.xml.internal.utils.XMLCharacterRecognizer;
import com.sun.org.apache.xml.internal.utils.XMLString;
import com.sun.org.apache.xml.internal.utils.XMLStringFactory;
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
/**
* This class will wrap a FastStringBuffer and allow for
*/
public class XStringForFSB extends XString
{
static final long serialVersionUID = -1533039186550674548L;
/** 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(com.sun.org.apache.xml.internal.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 to srcBegin is negative.
* <li>srcBegin is greater than srcEnd
* <li>srcEnd is greater than the length of this
* string
* <li>dstBegin is negative
* <li>dstBegin+(srcEnd-srcBegin) is larger than
* <code>dst.length
* @exception NullPointerException if <code>dst is null
*/
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 if and only if the argument is not
* <code>null and is a String object that represents
* the same sequence of characters as this object.
*
* @param obj2 the object to compare this <code>String
* against.
*
* @return <code>true if the String are equal;
* <code>false 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;
}
/**
* Tell if two objects are functionally equal.
*
* @param obj2 Object to compare this to
*
* @return true if the two objects are equal
*
* @throws javax.xml.transform.TransformerException
*/
public boolean equals(XObject obj2)
{
if (this == obj2)
{
return true;
}
if(obj2.getType() == XObject.CLASS_NUMBER)
return obj2.equals(this);
String str = obj2.str();
int n = m_length;
if (n == str.length())
{
FastStringBuffer fsb = fsb();
int i = m_start;
int j = 0;
while (n-- != 0)
{
if (fsb.charAt(i) != str.charAt(j))
{
return false;
}
i++;
j++;
}
return true;
}
return false;
}
/**
* Tell if two objects are functionally equal.
*
* @param anotherString Object to compare this to
*
* @return true if the two objects are equal
*
* @throws javax.xml.transform.TransformerException
*/
public boolean equals(String anotherString)
{
int n = m_length;
if (n == anotherString.length())
{
FastStringBuffer fsb = fsb();
int i = m_start;
int j = 0;
while (n-- != 0)
{
if (fsb.charAt(i) != anotherString.charAt(j))
{
return false;
}
i++;
j++;
}
return true;
}
return false;
}
/**
* Compares this string to the specified object.
* The result is <code>true if and only if the argument is not
* <code>null and is a String object that represents
* the same sequence of characters as this object.
*
* @param obj2 the object to compare this <code>String
* against.
*
* @return <code>true if the String are equal;
* <code>false otherwise.
* @see java.lang.String#compareTo(java.lang.String)
* @see java.lang.String#equalsIgnoreCase(java.lang.String)
*/
public boolean equals(Object obj2)
{
if (null == obj2)
return false;
if(obj2 instanceof XNumber)
return obj2.equals(this);
// In order to handle the 'all' semantics of
// nodeset comparisons, we always call the
// nodeset function.
else if (obj2 instanceof XNodeSet)
return obj2.equals(this);
else if (obj2 instanceof XStringForFSB)
return equals((XMLString) obj2);
else
return equals(obj2.toString());
}
/**
* Compares this <code>String to another String ,
* ignoring case considerations. Two strings are considered equal
* ignoring case if they are of the same length, and corresponding
* characters in the two strings are equal ignoring case.
*
* @param anotherString the <code>String to compare this
* <code>String against.
* @return <code>true if the argument is not null
* and the <code>Strings are equal,
* ignoring case; <code>false otherwise.
* @see #equals(Object)
* @see java.lang.Character#toLowerCase(char)
* @see java.lang.Character#toUpperCase(char)
*/
public boolean equalsIgnoreCase(String anotherString)
{
return (m_length == anotherString.length())
? str().equalsIgnoreCase(anotherString) : false;
}
/**
* Compares two strings lexicographically.
*
* @param xstr the <code>String to be compared.
*
* @return the value <code>0 if the argument string is equal to
* this string; a value less than <code>0 if this string
* is lexicographically less than the string argument; and a
* value greater than <code>0 if this string is
* lexicographically greater than the string argument.
* @exception java.lang.NullPointerException if <code>anotherString
* is <code>null.
*/
public int compareTo(XMLString xstr)
{
int len1 = m_length;
int len2 = xstr.length();
int n = Math.min(len1, len2);
FastStringBuffer fsb = fsb();
int i = m_start;
int j = 0;
while (n-- != 0)
{
char c1 = fsb.charAt(i);
char c2 = xstr.charAt(j);
if (c1 != c2)
{
return c1 - c2;
}
i++;
j++;
}
return len1 - len2;
}
/**
* Compares two strings lexicographically, ignoring case considerations.
* This method returns an integer whose sign is that of
* <code>this.toUpperCase().toLowerCase().compareTo(
* str.toUpperCase().toLowerCase())</code>.
* <p>
* Note that this method does <em>not take locale into account,
* and will result in an unsatisfactory ordering for certain locales.
* The java.text package provides <em>collators to allow
* locale-sensitive ordering.
*
* @param xstr the <code>String to be compared.
*
* @return a negative integer, zero, or a positive integer as the
* the specified String is greater than, equal to, or less
* than this String, ignoring case considerations.
* @see java.text.Collator#compare(String, String)
* @since 1.2
*/
public int compareToIgnoreCase(XMLString xstr)
{
int len1 = m_length;
int len2 = xstr.length();
int n = Math.min(len1, len2);
FastStringBuffer fsb = fsb();
int i = m_start;
int j = 0;
while (n-- != 0)
{
char c1 = Character.toLowerCase(fsb.charAt(i));
char c2 = Character.toLowerCase(xstr.charAt(j));
if (c1 != c2)
{
return c1 - c2;
}
i++;
j++;
}
return len1 - len2;
}
/**
* Returns a hashcode for this string. The hashcode for a
* <code>String object is computed as
* <blockquote>* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] * </pre> * using <code>int arithmetic, where |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.