1 23 24 29 30 package com.sun.appserv.management.util.misc; 31 32 import java.text.StringCharacterIterator ; 33 34 35 38 public final class StringEscaper 39 { 40 static final public char BACKSLASH = '\\'; 41 static final public char NEWLINE = '\n'; 42 static final public char RETURN = '\r'; 43 static final public char TAB = '\t'; 44 static final public char SPACE = ' '; 45 46 static final public char ESCAPE_CHAR = BACKSLASH; 47 static final public char UNICODE_START = 'u'; 48 final char mEscapeChar; 49 final char[] mCharsToEscape; 50 StringCharacterIterator mCharIter; 51 52 public 53 StringEscaper() 54 { 55 this( "\n\r\t" ); 56 } 57 58 public 59 StringEscaper( String charsToEscape ) 60 { 61 this( ESCAPE_CHAR, charsToEscape ); 62 } 63 64 65 public 66 StringEscaper( char escapeChar, String charsToEscape ) 67 { 68 mCharIter = null; 69 70 mEscapeChar = escapeChar; 71 72 mCharsToEscape = new char[ 1 + charsToEscape.length() ]; 73 74 mCharsToEscape[ 0 ] = ESCAPE_CHAR; 75 final int length = charsToEscape.length(); 76 for( int i = 0; i < length; ++i ) 77 { 78 mCharsToEscape[ i + 1 ] = charsToEscape.charAt( i ); 79 } 80 } 81 82 boolean 83 shouldEscape( final char c ) 84 { 85 boolean shouldEscape = false; 86 87 for( int i = 0; i < mCharsToEscape.length; ++i ) 88 { 89 if ( c == mCharsToEscape[ i ] ) 90 { 91 shouldEscape = true; 92 break; 93 } 94 } 95 96 return( shouldEscape ); 97 } 98 99 102 String 103 getEscapeSequence( char c ) 104 { 105 String sequence = null; 106 107 if ( c == mEscapeChar ) 108 { 109 sequence = "" + mEscapeChar + mEscapeChar; 110 } 111 else if ( c == NEWLINE ) 112 { 113 sequence = mEscapeChar + "n"; 114 } 115 else if ( c == RETURN ) 116 { 117 sequence = mEscapeChar + "r"; 118 } 119 else if ( c == TAB ) 120 { 121 sequence = mEscapeChar + "t"; 122 } 123 else if ( c == SPACE ) 124 { 125 sequence = mEscapeChar + "s"; 126 } 127 else 128 { 129 final int numericValue = (int)c; 130 131 String valueString = "" + Integer.toHexString( numericValue ); 132 while ( valueString.length() != 4 ) 134 { 135 valueString = "0" + valueString; 136 } 137 138 sequence = mEscapeChar + (UNICODE_START + valueString); 140 } 141 142 return( sequence ); 143 } 144 145 public String 146 escape( String s) 147 { 148 final StringBuffer buf = new StringBuffer (); 149 150 final int length = s.length(); 151 for( int i = 0; i < length; ++i ) 152 { 153 final char c = s.charAt( i ); 154 155 if ( shouldEscape( c ) ) 156 { 157 buf.append( getEscapeSequence( c ) ); 158 } 159 else 160 { 161 buf.append( c ); 162 } 163 } 164 165 return( buf.toString() ); 166 } 167 168 boolean 169 hasMoreChars() 170 { 171 return( mCharIter.current() != mCharIter.DONE ); 172 } 173 174 char 175 peekNextChar() 176 { 177 return( mCharIter.current() ); 178 } 179 180 char 181 nextChar() 182 { 183 final char theChar = mCharIter.current(); 184 mCharIter.next(); 185 186 if ( theChar == mCharIter.DONE ) 187 { 188 throw new ArrayIndexOutOfBoundsException (); 189 } 190 191 return( theChar ); 192 } 193 194 char 195 escapeSequenceToChar() 196 { 197 final char c = nextChar(); 198 char result = 0; 199 200 if ( c == mEscapeChar ) 201 { 202 result = mEscapeChar; 203 } 204 else if ( c == 'n' ) 205 { 206 result = NEWLINE; 207 } 208 else if ( c == 'r' ) 209 { 210 result = RETURN; 211 } 212 else if ( c == 't' ) 213 { 214 result = TAB; 215 } 216 else if ( c == 's' ) 217 { 218 result = SPACE; 219 } 220 else if ( c == UNICODE_START ) 221 { 222 final String unicodeSequence = "" + nextChar() + nextChar() + nextChar() + nextChar(); 223 final int intValue = Integer.parseInt( unicodeSequence, 16 ); 224 225 result = (char)intValue; 226 } 227 else 228 { 229 throw new IllegalArgumentException ( "Illegal escape sequence" ); 230 } 231 232 return( result ); 233 } 234 235 236 public String 237 unescape( String s ) 238 { 239 final StringBuffer buf = new StringBuffer (); 240 241 mCharIter = new StringCharacterIterator ( s ); 242 243 while ( hasMoreChars() ) 244 { 245 final char c = (char)nextChar(); 246 assert ( c != mCharIter.DONE ); 247 248 if ( c == mEscapeChar ) 249 { 250 final char newChar = escapeSequenceToChar(); 251 buf.append( newChar ); 252 } 253 else 254 { 255 buf.append( c ); 256 } 257 } 258 259 mCharIter = null; 260 261 return( buf.toString() ); 262 } 263 } 264 265 | Popular Tags |