KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jasmin > ScannerUtils


1 /* --- Copyright Jonathan Meyer 1997. All rights reserved. -----------------
2  > File: jasmin/src/jasmin/ScannerUtils.java
3  > Purpose: Various static methods utilized to breakdown strings
4  > Author: Jonathan Meyer, 8 Feb 1997
5  
6  */

7
8  /**
9     Modifications Copyright (C) 1997, 1998 Raja Vallee-Rai (kor@sable.mcgill.ca)
10     All rights reserved.
11    
12     Changes:
13         - Changed the grammar so that Floats are produced only when an "F" is encountered.
14         (putting a 'L' after a number forces it to be a long)
15         
16 */

17
18 package jasmin;
19
20 abstract class ScannerUtils {
21
22     //
23
// Converts a string of a given radix to an int or a long
24
// (uses smallest format that will hold the number)
25
//
26
public static Number JavaDoc convertInt(String JavaDoc str, int radix)
27                 throws NumberFormatException JavaDoc
28     {
29         boolean forceLong = false;
30         
31         if(str.endsWith("L"))
32         {
33             forceLong = true;
34             str = str.substring(0, str.length() - 1);
35         }
36                     
37         long x = Long.parseLong(str, radix);
38         
39         if (x <= (long)Integer.MAX_VALUE && x >= (long)Integer.MIN_VALUE && !forceLong) {
40             return new Integer JavaDoc((int)x);
41         }
42         return new Long JavaDoc(x);
43     }
44
45     //
46
// Converts a string to a number (int, float, long, or double).
47
// (uses smallest format that will hold the number)
48
//
49
public static Number JavaDoc convertNumber(String JavaDoc str)
50                 throws NumberFormatException JavaDoc
51     {
52     // System.out.println("Converting:" + str);
53

54         if (str.startsWith("0x")) {
55             // base 16 integer
56
return (convertInt(str.substring(2), 16));
57         } else if (str.indexOf('.') != -1)
58         {
59             boolean isFloat = false;
60             
61             if(str.endsWith("F"))
62             {
63                 isFloat = true;
64                 str = str.substring(0, str.length() - 1);
65             }
66             
67             double x = (new Double JavaDoc(str)).doubleValue();
68             
69             if(isFloat)
70                 return new Float JavaDoc((float)x);
71             else
72                 return new Double JavaDoc(x);
73         } else {
74             // assume long or int in base 10
75
return (convertInt(str, 10));
76         }
77     }
78
79     //
80
// Maps '.' characters to '/' characters in a string
81
//
82
public static String JavaDoc convertDots(String JavaDoc orig_name)
83     {
84         return convertChars(orig_name, ".", '/');
85     }
86
87     //
88
// Maps chars to toChar in a given String
89
//
90
public static String JavaDoc convertChars(String JavaDoc orig_name,
91                                       String JavaDoc chars, char toChar)
92     {
93         StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(orig_name);
94         int i;
95         for (i = 0; i < tmp.length(); i++) {
96             if (chars.indexOf(tmp.charAt(i)) != -1) {
97                 tmp.setCharAt(i, toChar);
98             }
99         }
100         return new String JavaDoc(tmp);
101     }
102
103     //
104
// Splits a string like:
105
// "a/b/c/d(xyz)v"
106
// into three strings:
107
// "a/b/c", "d", "(xyz)v"
108
//
109
public static String JavaDoc[] splitClassMethodSignature(String JavaDoc name)
110     {
111         String JavaDoc result[] = new String JavaDoc[3];
112         int i, pos = 0, sigpos = 0;
113         for (i = 0; i < name.length(); i++) {
114             char c = name.charAt(i);
115             if (c == '.' || c == '/') pos = i;
116             else if (c == '(') {sigpos = i; break; }
117         }
118         result[0] = convertDots(name.substring(0, pos));
119         result[1] = name.substring(pos + 1, sigpos);
120         result[2] = convertDots(name.substring(sigpos));
121
122         return result;
123     }
124
125     //
126
// Splits a string like:
127
// "java/lang/System/out"
128
// into two strings:
129
// "java/lang/System" and "out"
130
//
131
public static String JavaDoc[] splitClassField(String JavaDoc name)
132     {
133         String JavaDoc result[] = new String JavaDoc[2];
134         int i, pos = -1, sigpos = 0;
135         for (i = 0; i < name.length(); i++) {
136             char c = name.charAt(i);
137             if (c == '.' || c == '/') pos = i;
138         }
139         if (pos == -1) { // no '/' in string
140
result[0] = null;
141             result[1] = name;
142         } else {
143             result[0] = convertDots(name.substring(0, pos));
144             result[1] = name.substring(pos + 1);
145         }
146
147         return result;
148     }
149
150     // Splits a string like:
151
// "main(Ljava/lang/String;)V
152
// into two strings:
153
// "main" and "(Ljava/lang/String;)V"
154
//
155
public static String JavaDoc[] splitMethodSignature(String JavaDoc name)
156     {
157         String JavaDoc result[] = new String JavaDoc[2];
158         int i, sigpos = 0;
159         for (i = 0; i < name.length(); i++) {
160             char c = name.charAt(i);
161             if (c == '(') {sigpos = i; break; }
162         }
163         result[0] = name.substring(0, sigpos);
164         result[1] = convertDots(name.substring(sigpos));
165
166         return result;
167     }
168 }
169
Popular Tags