KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > StringEscaper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin-core/mbeanapi/src/java/com/sun/appserv/management/util/misc/StringEscaper.java,v 1.3 2005/12/25 03:51:52 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:51:52 $
28  */

29  
30 package com.sun.appserv.management.util.misc;
31
32 import java.text.StringCharacterIterator JavaDoc;
33
34
35 /**
36     Escapes/unescapes strings
37  */

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 JavaDoc mCharIter;
51     
52         public
53     StringEscaper()
54     {
55         this( "\n\r\t" );
56     }
57     
58         public
59     StringEscaper( String JavaDoc charsToEscape )
60     {
61         this( ESCAPE_CHAR, charsToEscape );
62     }
63     
64     
65         public
66     StringEscaper( char escapeChar, String JavaDoc 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     /*
100         Get an escape sequence for the character.
101      */

102         String JavaDoc
103     getEscapeSequence( char c )
104     {
105         String JavaDoc 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 JavaDoc valueString = "" + Integer.toHexString( numericValue );
132             // make sure it's 4 digits by prepending leading zeroes
133
while ( valueString.length() != 4 )
134             {
135                 valueString = "0" + valueString;
136             }
137             
138             // careful not to append char to char
139
sequence = mEscapeChar + (UNICODE_START + valueString);
140         }
141         
142         return( sequence );
143     }
144     
145         public String JavaDoc
146     escape( String JavaDoc s)
147     {
148         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc();
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 JavaDoc 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 JavaDoc( "Illegal escape sequence" );
230         }
231         
232         return( result );
233     }
234     
235     
236         public String JavaDoc
237     unescape( String JavaDoc s )
238     {
239         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
240         
241         mCharIter = new StringCharacterIterator JavaDoc( 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