/Users/lyon/j4p/src/futils/ProtoType.java

1    package futils; 
2     
3    import java.awt.*; 
4    import java.io.*; 
5     
6     
7    public class ProtoType { 
8        PrintWriter pw = null; 
9        FileOutputStream fos = null; 
10       File dir; 
11       boolean classFlag = false; 
12       boolean firstClassInFile = true; 
13       boolean openParenthesis = false; 
14    
15       public String[] ls(String wc) { 
16           WildFilter wf = new WildFilter(wc); 
17           dir = getDir(); 
18           return dir.list(wf); 
19       } 
20    
21       public String[] lsAbs(String wc) { 
22           String files[] = ls(wc); 
23           for (int i = 0; i < files.length; i++) 
24               files[i] = dir.getAbsolutePath() + files[i]; 
25           return files; 
26       } 
27    
28       public File getDir() { 
29           FileDialog fd = new FileDialog(new Frame(), "Open file"); 
30           fd.setVisible(true); 
31           fd.setVisible(false); 
32    
33           String fn = fd.getFile(); 
34           if (fn == null) return null; 
35    
36           return new File(fd.getDirectory()); 
37       } 
38    
39       public String getReadFileName() { 
40           FileDialog fd = new FileDialog(new Frame(), "Open file"); 
41           fd.setVisible(true); 
42           fd.setVisible(false); 
43           String fn = fd.getFile(); 
44           if (fn == null) return null; 
45           return fd.getDirectory() + fn; 
46       } 
47    
48       public PrintWriter getPrintWriter() throws IOException { 
49           fos = new FileOutputStream(getWriteFileName()); 
50           return new PrintWriter(fos); 
51       } 
52    
53       public String getWriteFileName() { 
54           FileDialog fd = new FileDialog(new Frame(), "Open file", FileDialog.SAVE); 
55           fd.setVisible(true); 
56           fd.setVisible(false); 
57           String fn = fd.getFile(); 
58           if (fn == null) return null; 
59           return fd.getDirectory() + fn; 
60       } 
61    
62       public void processLine(StreamTokenizer st) throws IOException { 
63           //System.out.println("Hi1"); 
64           boolean privateFlag = false, functionFlag = false; 
65           String ss = st.sval; //contains public or private 
66    
67           StringBuffer sb = new StringBuffer(40); 
68    
69           if (ss.equals("private")) 
70               privateFlag = true; 
71    
72           int ft = st.nextToken(); 
73           if (st.sval.equals("class")) { 
74               if (firstClassInFile == true) 
75                   firstClassInFile = false; 
76               else 
77                   pw.println('}'); 
78               classFlag = true; 
79           } 
80           sb.append(ss); 
81    
82           while (ft != st.TT_EOF && st.ttype != '{' && st.ttype != ';') { 
83               if (ft == st.TT_WORD) { 
84                   if (openParenthesis == true) 
85                       openParenthesis = false; 
86                   else 
87                       sb.append(" "); 
88                   sb.append(st.sval); 
89               } else if (ft == st.TT_NUMBER) { 
90                   sb.append(" " + st.nval); 
91               } else { 
92                   if (st.ttype == '(') { 
93                       openParenthesis = true; 
94                       //functionFlag = true; 
95                   } else 
96                       openParenthesis = false; 
97                   sb.append((char) st.ttype); 
98               } 
99               ft = st.nextToken(); 
100          } 
101   
102          if (st.ttype == '{') 
103              if (classFlag == true) { 
104                  sb.append('{'); 
105                  classFlag = false; 
106                  functionFlag = true; 
107              } else { 
108                  sb.append(';'); 
109              } 
110          //System.out.println(sb); 
111   
112          if (privateFlag == true && functionFlag == false) 
113              return; 
114          pw.println(sb); 
115          sb = null; 
116      } 
117   
118      public void process(InputStream is) throws IOException { 
119          InputStreamReader isr = new InputStreamReader(is); 
120          BufferedReader br = new BufferedReader(isr); 
121          StreamTokenizer st = new StreamTokenizer(br); 
122          int ft = 0; 
123          st.eolIsSignificant(true); 
124          while ((ft = st.nextToken()) != st.TT_EOF) 
125              if (ft == st.TT_WORD) { 
126                  //System.out.println(st.sval); 
127                  if (st.sval.equals("public") || st.sval.equals("private")) //||st.sval.equals("class")) 
128                      processLine(st); 
129              } 
130      } 
131   
132      public void process(String fn) { 
133          try { 
134              firstClassInFile = true; 
135              FileInputStream fis = new FileInputStream(fn); 
136              process(fis); 
137              fis.close(); 
138          } catch (Exception e) { 
139          } 
140      } 
141   
142      public void process() { 
143          String fn[] = lsAbs(".java"); 
144          try { 
145              pw = getPrintWriter(); 
146          } catch (Exception e) { 
147          } 
148   
149  //      int i = 0; 
150          for (int i = 0; i < fn.length; i++) { 
151              System.out.println(fn[i]); 
152              pw.println(fn[i] + ":-"); 
153              process(fn[i]); 
154              pw.println('}'); 
155              pw.println(); 
156          } 
157          try { 
158              fos.close(); 
159          } catch (Exception e) { 
160          } 
161      } 
162   
163      public static void main(String args[]) { 
164          ProtoType p = new ProtoType(); 
165          p.process(); 
166      } 
167  } 
168