FileDocCategorySizeDatePackage
StringEscaper.javaAPI DocGlassfish v2 API6225Fri May 04 22:31:06 BST 2007com.sun.appserv.management.util.misc

StringEscaper

public final class StringEscaper extends Object
Escapes/unescapes strings

Fields Summary
public static final char
BACKSLASH
public static final char
NEWLINE
public static final char
RETURN
public static final char
TAB
public static final char
SPACE
public static final char
ESCAPE_CHAR
public static final char
UNICODE_START
final char
mEscapeChar
final char[]
mCharsToEscape
StringCharacterIterator
mCharIter
Constructors Summary
public StringEscaper()

	
		
	
	
		this( "\n\r\t" );
	
public StringEscaper(String charsToEscape)

		this( ESCAPE_CHAR, charsToEscape );
	
public StringEscaper(char escapeChar, String charsToEscape)

		mCharIter	= null;
		
		mEscapeChar	= escapeChar;
		
		mCharsToEscape	= new char[ 1 + charsToEscape.length() ];
		
		mCharsToEscape[ 0 ]	= ESCAPE_CHAR;
		final int	length	= charsToEscape.length();
		for( int i = 0; i < length; ++i )
		{
			mCharsToEscape[ i + 1 ]	= charsToEscape.charAt( i );
		}
	
Methods Summary
public java.lang.Stringescape(java.lang.String s)

		final StringBuffer	buf	= new StringBuffer();
		
		final int length	= s.length();
		for( int i = 0; i < length; ++i )
		{
			final char	c	= s.charAt( i );
			
			if ( shouldEscape( c ) )
			{
				buf.append( getEscapeSequence( c ) );
			}
			else
			{
				buf.append( c );
			}
		}
		
		return( buf.toString() );
	
charescapeSequenceToChar()

		final char	c	= nextChar();
		char	result	= 0;
		
		if ( c == mEscapeChar )
		{
			result	= mEscapeChar;
		}
		else if ( c == 'n" )
		{
			result	= NEWLINE;
		}
		else if ( c == 'r" )
		{
			result	= RETURN;
		}
		else if ( c == 't" )
		{
			result	= TAB;
		}
		else if ( c == 's" )
		{
			result	= SPACE;
		}
		else if ( c == UNICODE_START )
		{
			final String	unicodeSequence	= "" + nextChar() + nextChar() + nextChar() + nextChar();
			final int		intValue	= Integer.parseInt( unicodeSequence, 16 );
			
			result	= (char)intValue;
		}
		else
		{
			throw new IllegalArgumentException( "Illegal escape sequence" );
		}
		
		return( result );
	
java.lang.StringgetEscapeSequence(char c)

		String	sequence	= null;
		
		if ( c == mEscapeChar )
		{
			sequence	= "" + mEscapeChar + mEscapeChar;
		}
		else if ( c == NEWLINE )
		{
			sequence	= mEscapeChar + "n";
		}
		else if ( c == RETURN )
		{
			sequence	= mEscapeChar + "r";
		}
		else if ( c == TAB )
		{
			sequence	= mEscapeChar + "t";
		}
		else if ( c == SPACE )
		{
			sequence	= mEscapeChar + "s";
		}
		else
		{
			final int	numericValue	= (int)c;
			
			String	valueString	= "" + Integer.toHexString( numericValue );
			// make sure it's 4 digits by prepending leading zeroes
			while ( valueString.length() != 4 )
			{
				valueString	= "0" + valueString;
			}
			
			// careful not to append char to char
			sequence	= mEscapeChar + (UNICODE_START + valueString);
		}
		
		return( sequence );
	
booleanhasMoreChars()

		return( mCharIter.current() != mCharIter.DONE );
	
charnextChar()

		final char	theChar	= mCharIter.current();
		mCharIter.next();
		
		if ( theChar == mCharIter.DONE )
		{
			throw new ArrayIndexOutOfBoundsException();
		}
		
		return( theChar );
	
charpeekNextChar()

		return( mCharIter.current() );
	
booleanshouldEscape(char c)

		boolean	shouldEscape	= false;
		
		for( int i = 0; i < mCharsToEscape.length; ++i )
		{
			if ( c == mCharsToEscape[ i ] )
			{
				shouldEscape	= true;
				break;
			}
		}
		
		return( shouldEscape );
	
public java.lang.Stringunescape(java.lang.String s)

		final StringBuffer	buf	= new StringBuffer();
		
		mCharIter	= new StringCharacterIterator( s );
		
		while ( hasMoreChars() )
		{
			final char	c	= (char)nextChar();
			assert ( c != mCharIter.DONE );
			
			if ( c == mEscapeChar )
			{
				final char	newChar	= escapeSequenceToChar();
				buf.append( newChar );
			}
			else
			{
				buf.append( c );
			}
		}
		
		mCharIter	= null;
		
		return( buf.toString() );