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

1    package futils; 
2     
3    // futils.ReaderUtil 
4     
5    import utils.BooleanException; 
6    import utils.StringUtils; 
7    import xml.adbk.SimpleAddress; 
8    import xml.adbk.SimpleAddressBook; 
9     
10   import javax.swing.JFileChooser; 
11   import javax.swing.JFrame; 
12   import java.awt.FileDialog; 
13   import java.awt.Frame; 
14   import java.io.BufferedReader; 
15   import java.io.ByteArrayOutputStream; 
16   import java.io.File; 
17   import java.io.FileNotFoundException; 
18   import java.io.FileReader; 
19   import java.io.IOException; 
20   import java.io.InputStreamReader; 
21   import java.io.StreamTokenizer; 
22   import java.util.StringTokenizer; 
23   import java.util.Vector; 
24    
25   public class ReaderUtil { 
26       private static BufferedReader br = 
27               new BufferedReader(new InputStreamReader(System.in)); 
28    
29       public static void main(String args[]) { 
30           SimpleAddressBook sab = getAddressBook(); 
31           System.out.println(sab); 
32       } 
33    
34       public static void setReader(BufferedReader _br) { 
35           br = _br; 
36       } 
37    
38       public static BufferedReader getReader() { 
39           return br; 
40       } 
41    
42       public static String getString(String prompt) { 
43           if (prompt != null) 
44               System.out.print(prompt); 
45           try { 
46               return br.readLine(); 
47           } catch (IOException e) { 
48           } 
49           return null; 
50       } 
51    
52       public static byte[] getBytes(int i) throws IOException { 
53           byte newLine[] = "\n".getBytes(); 
54           ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
55           baos.write(Integer.toString(i).getBytes()); 
56           baos.write(newLine); 
57           return baos.toByteArray(); 
58       } 
59    
60       public static int[] getInt(byte b[]) { 
61           int ia[] = new int[b.length]; 
62           for (int i = 0; i < b.length; i++) 
63               ia[i] = b[i]; 
64           return ia; 
65       } 
66    
67       public static void testGetInts() { 
68           int ia[] = getInts(0.111f); 
69           print(ia); 
70       } 
71    
72       public static int[] getInts(float f) { 
73           byte newLine[] = "\n".getBytes(); 
74           byte fb[] = Float.toString(f).getBytes(); 
75           int ia[] = new int[newLine.length + fb.length]; 
76           for (int i = 0; i < fb.length; i++) 
77               ia[i] = fb[i]; 
78           ia[fb.length] = newLine[0]; 
79           return ia; 
80       } 
81    
82       public static byte[] getBytes(String o[]) throws IOException { 
83           byte newLine[] = "\n".getBytes(); 
84           ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
85           for (int i = 0; i < o.length; i++) { 
86               baos.write(o[i].toString().getBytes()); 
87               baos.write(newLine); 
88           } 
89           return baos.toByteArray(); 
90       } 
91    
92       public static void print(Object o[]) { 
93           for (int i = 0; i < o.length; i++) 
94               System.out.println(o[i]); 
95       } 
96    
97       public static void print(int o[]) { 
98           for (int i = 0; i < o.length; i++) 
99               System.out.println(o[i]); 
100      } 
101   
102      public static int sum(int o[]) { 
103          int s = 0; 
104          for (int i = 0; i < o.length; i++) 
105              s = s + o[i]; 
106          return s; 
107      } 
108   
109      public static int[] string2Int(String s[]) { 
110          int ia[] = new int[s.length]; 
111          for (int i = 0; i < s.length; i++) 
112              ia[i] = Integer.parseInt(s[i]); 
113          return ia; 
114      } 
115   
116      public static String[] 
117              getTokens(String s) { 
118          StringTokenizer 
119                  st = new StringTokenizer(s, 
120                          ", \t\n\r\f\\"); 
121          int n = st.countTokens(); 
122          System.out.println("n=" + n); 
123          String t[] = new String[n]; 
124          for (int i = 0; i < n; i++) 
125              t[i] = st.nextToken(); 
126          return t; 
127      } 
128   
129      public static void testGetFile() { 
130          try { 
131              String sa[] = getFile(Futil.getReadFile("select a text file")); 
132              print(sa); 
133          } catch (FileNotFoundException e) { 
134              e.printStackTrace(); 
135          } 
136      } 
137   
138      public static String[] getFile(File f) throws FileNotFoundException { 
139          Vector v = new Vector(); 
140          BufferedReader br = new BufferedReader(new FileReader(f)); 
141          String s = null; 
142          try { 
143              while ((s = br.readLine()) != null) 
144                  v.addElement(s); 
145          } catch (IOException e) { 
146              e.printStackTrace(); 
147          } 
148          close(br); 
149          String sa[] = new String[v.size()]; 
150          v.copyInto(sa); 
151          return sa; 
152      } 
153   
154      /** 
155       * Obtain a list of records using 
156       * a given record delimiter. For example, input 
157       * a file and a delimiter (^m) and return an array 
158       * of records. 
159       * <br> 
160       * String s[] = getRecords(f, '\r'); 
161       * <br> 
162       * In java, C and C++, '\r' = carrage return, 
163       * '\f' = form feed (or line feed) 
164       * ^m is a carrage... 
165       * 
166       * @return unparsed array of records as <code>String</code> 
167       */ 
168      public static SimpleAddressBook getAddressBook() { 
169          Futil.setSwing(false); 
170          File f = Futil.getReadFile("select a file to read"); 
171          try { 
172              return getAddressBook(f, ";"); 
173          } catch (FileNotFoundException e) { 
174              e.printStackTrace(); 
175          } catch (IOException e) { 
176              e.printStackTrace(); 
177          } 
178          return null; 
179      } 
180   
181      /** 
182       * Do a getRecords with 
183       * 
184       * @param f as the input file 
185       * @param c as the delimiting character 
186       * @return records in the order in which they appear in the file 
187       */ 
188      public static SimpleAddressBook getAddressBook(File f, String c) throws IOException { 
189          StringBuffer sb = new StringBuffer(); 
190          FileReader fr = new FileReader(f); 
191          BufferedReader br = new BufferedReader(fr); 
192          String s = br.readLine(); 
193          while (s != null) { 
194              sb.append(s); 
195              s = br.readLine(); 
196          } 
197          fr.close(); 
198          StringTokenizer st = new StringTokenizer(sb.toString(), c); 
199          SimpleAddress sa = getAddress(st); 
200          SimpleAddressBook sab = new SimpleAddressBook(); 
201          while (sa != null) { 
202              sab.add(sa); 
203              sa = getAddress(st); 
204          } 
205          return sab; 
206      } 
207   
208      private static SimpleAddress getAddress(StringTokenizer st) { 
209          int n = st.countTokens(); 
210          if (n < 6) return null; 
211          SimpleAddress sa = new SimpleAddress(); 
212          sa.setName(st.nextToken()); 
213          sa.setAddress(st.nextToken()); 
214          sa.setInfo(st.nextToken()); 
215          sa.setHomePhone(st.nextToken()); 
216          sa.setBusinessPhone(st.nextToken()); 
217          sa.setFaxPhone(st.nextToken()); 
218          return sa; 
219      } 
220   
221      public static void listFile() { 
222          Futil.setSwing(false); 
223          FileReader fr = 
224                  getFileReader("select a text file"); 
225          System.out.println("file is:" + fr.toString()); 
226          BufferedReader br = new BufferedReader(fr); 
227          String s = null; 
228          try { 
229              while ((s = br.readLine()) != null) { 
230                  System.out.println(s); 
231                  System.out.println("-----"); 
232              } 
233          } catch (IOException e) { 
234              e.printStackTrace(); 
235          } 
236          close(br); 
237      } 
238   
239      public static void readQuestion(String s) { 
240          System.out.println("question=" + s); 
241      } 
242   
243      public static void testGetTokenizer() { 
244          String s = getString("please enter a string of data:"); 
245          System.out.println("s=" + s); 
246          String a[] = getTokens(s); 
247          int ia[] = string2Int(a); 
248          print(ia); 
249          System.out.println("sum=" + sum(ia)); 
250      } 
251   
252      /** 
253       * isReadableFile() - validates the file is 
254       * exists and readable 
255       * 
256       * @param f The file to be examined. 
257       * @return boolean - file is readable 
258       *         true/false 
259       */ 
260      public static boolean isReadableFile(File f) { 
261          try { 
262              if (!f.exists()) 
263                  throw (new FileNotFoundException("No Such File :" + f)); 
264              if (!f.canRead()) 
265                  throw (new IOException("File Not Readable: " + 
266                          f)); 
267              return true; 
268          } catch (FileNotFoundException e) { 
269              System.out.println(e.getMessage() + "\n"); 
270              return false; 
271          } catch (IOException e) { 
272              System.out.println(e.getMessage() + "\n"); 
273              return false; 
274          } 
275      } 
276   
277      /** 
278       * "@param prompt 
279       */ 
280      public static BufferedReader getBufferedReader(String prompt) { 
281          return getBufferedReader(Futil.getReadFile(prompt)); 
282      } 
283   
284      /** 
285       * "@param f 
286       */ 
287      public static BufferedReader 
288              getBufferedReader(File f) { 
289          try { 
290              return new BufferedReader(new FileReader(f)); 
291          } catch (IOException e) { 
292              e.printStackTrace(); 
293          } 
294          return null; 
295      } 
296   
297      /** 
298       * @param br 
299       */ 
300      public static String readLine(BufferedReader br) { 
301          try { 
302              return br.readLine(); 
303          } catch (IOException e) { 
304              e.printStackTrace(); 
305              return null; 
306          } 
307      } 
308   
309      /** 
310       * "@param br 
311       */ 
312      public static void close(BufferedReader br) { 
313          try { 
314              br.close(); 
315          } catch (IOException e) { 
316              e.printStackTrace(); 
317          } 
318      } 
319   
320      /** 
321       * Prompt the user for a file select and 
322       * return the File instance. 
323       * 
324       * @param prompt String displayed in a dialog 
325       * @return File selected by a user 
326       */ 
327      private static File getReadFileAWT(String prompt) { 
328          FileDialog fd = new FileDialog(new Frame(), prompt); 
329          fd.setVisible(true); 
330          return new File(fd.getDirectory() + fd.getFile()); 
331      } 
332   
333      public static void testGetReadFile() { 
334          System.out.println("file=" + 
335                  Futil.getReadFile("select a file")); 
336      } 
337   
338      private static File getReadFileSwing(String prompt) { 
339          JFileChooser jfc = new JFileChooser(); 
340          jfc.showOpenDialog(new JFrame()); 
341          return jfc.getSelectedFile(); 
342      } 
343   
344      public static FileReader getFileReader(String prompt) { 
345          return getFileReader(Futil.getReadFile(prompt)); 
346      } 
347   
348      public static FileReader getFileReader(File file) { 
349          FileReader fr = null; 
350          try { 
351              fr = new FileReader(file); 
352          } catch (IOException e) { 
353              System.out.println("futil:Could not open file"); 
354          } 
355          return fr; 
356      } 
357   
358      public static void close(FileReader fr) { 
359          try { 
360              fr.close(); 
361          } // end try 
362          catch (IOException exe) { 
363              System.out.println("could not close FileReader"); 
364          } 
365      } 
366   
367      /** 
368       * @param file 
369       * @param data 
370       * @throws IOException 
371       */ 
372      public static void readDataFile(File file, 
373                                      double data[]) 
374              throws IOException { 
375          System.out.println("processing:\t" + file); 
376          FileReader fr = getFileReader(file); 
377          StreamTokenizer tokens = new StreamTokenizer(fr); 
378          int next = 0; 
379          int num = 0; 
380          while ((next = tokens.nextToken()) != 
381                  StreamTokenizer.TT_EOF) { 
382              switch (next) { 
383                  case StreamTokenizer.TT_WORD: 
384                      break; 
385                  case StreamTokenizer.TT_NUMBER: 
386                      data[num] = tokens.nval; 
387                      System.out.println(num + 
388                              ": " + 
389                              data[num]); 
390                      num = num + 1; 
391                      break; 
392                  case StreamTokenizer.TT_EOL: 
393                      break; 
394              } 
395          } 
396          close(fr); 
397      } 
398   
399      /** 
400       * Prompts the user for a file and counts the 
401       * tokens. 
402       * 
403       * @return returns the number of tokens in a 
404       *         file 
405       */ 
406      public static int countTokens(BufferedReader br) { 
407          String line = null; 
408          int k = 0; 
409          try { 
410              while ((line = 
411                      br.readLine()) != null) { 
412                  k = 
413                          k + 
414                          StringUtils.addTokens(line); 
415              } 
416          } catch (IOException e) { 
417          } 
418          return k; 
419      } 
420   
421      public static void listFilteredHrefFile(File file) { 
422          System.out.println("processing:\t" + file); 
423          try { 
424              FileReader fr = getFileReader(file); 
425              StreamTokenizer tokens = new 
426                      StreamTokenizer(fr); 
427              int next = 0; 
428              tokens.resetSyntax(); 
429              tokens.wordChars(0, 255); 
430              tokens.quoteChar('"'); 
431              while ((next = tokens.nextToken()) != 
432                      StreamTokenizer.TT_EOF) { 
433                  switch (next) { 
434                      case '"': 
435                          System.out.print('"'); 
436                          StringTokenizer st = 
437                                  new StringTokenizer(tokens.sval, 
438                                          " "); 
439                          while (st.hasMoreTokens()) { 
440                              System.out.print(st.nextToken()); 
441                              if (st.countTokens() > 
442                                      1) { 
443                                  System.out.print("%20"); 
444                              } 
445                          } 
446                          System.out.print('"'); 
447                          break; 
448                      case StreamTokenizer.TT_WORD: 
449                          System.out.print(tokens.sval + 
450                                  " "); 
451                          break; 
452                      case StreamTokenizer.TT_NUMBER: 
453                          System.out.print(tokens.nval + 
454                                  " "); 
455                          break; 
456                      case StreamTokenizer.TT_EOL: 
457                          System.out.println(); 
458                          break; 
459                  } 
460              } 
461              System.out.println(); 
462              fr.close(); 
463          } catch (Exception exe) { 
464              System.out.println("listFilteredHrefFile:er!"); 
465          } 
466      } 
467   
468      public static boolean parseBoolean(String s) 
469              throws BooleanException { 
470          if (s.equals("true")) return true; 
471          if (s.equals("false")) return false; 
472          throw new BooleanException(); 
473      } 
474   
475      public static boolean getBoolean(String prompt) { 
476          String s = getString(prompt); 
477          return parseBoolean(s); 
478      } 
479   
480      public static int getInt(String prompt) { 
481          String s = getString(prompt); 
482          int i = 0; 
483          try { 
484              i = Integer.parseInt(s); 
485          } catch (NumberFormatException e) { 
486              System.out.println(s + 
487                      " is not a valid int, try again"); 
488              return getInt(prompt); 
489          } 
490          return i; 
491      } 
492   
493      public static double getDouble(String prompt) { 
494          String s = getString(prompt); 
495          double d = 0; 
496          try { 
497              d = Double.parseDouble(s); 
498          } catch (NumberFormatException e) { 
499              System.out.println(s + 
500                      " is not a valid double, try again"); 
501              return getDouble(prompt); 
502          } 
503          return d; 
504      } 
505  } 
506