KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > EscapedReader


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.util;
28 import soot.*;
29
30 import java.io.*;
31
32 /** A FilterReader which catches escaped characters (<code>\\unnnn</code>) in the
33  * input and de-escapes them. Used in the Jimple Parser. */

34 public class EscapedReader extends FilterReader
35 {
36     /** Constructs an EscapedReader around the given Reader. */
37     public EscapedReader(Reader fos)
38     {
39         super(fos);
40     }
41
42     private StringBuffer JavaDoc mini = new StringBuffer JavaDoc();
43
44     boolean nextF;
45     int nextch = 0;
46
47     /** Reads a character from the input. */
48     public int read() throws IOException
49     {
50         /* if you already read the char, just return it */
51         if (nextF)
52         {
53             nextF = false;
54             return nextch;
55         }
56
57         int ch = super.read();
58         
59         if (ch != '\\')
60             return ch;
61
62         /* we may have an escape sequence here ..*/
63         mini = new StringBuffer JavaDoc();
64
65         ch = super.read();
66         if (ch != 'u')
67           {
68             nextF = true; nextch = ch;
69             return '\\';
70           }
71         
72         mini.append("\\u");
73         while (mini.length() < 6)
74         {
75             ch = super.read();
76             mini.append((char)ch);
77         }
78
79         // G.v().out.println(mini.toString());
80
ch = Integer.parseInt(mini.substring(2).toString(), 16);
81
82         return ch;
83     }
84 }
85
Popular Tags