/Users/lyon/j4p/src/gui/html/HtmlUtil.java

1    package gui.html; 
2     
3    import futils.Futil; 
4    import futils.ReaderUtil; 
5    import futils.WriterUtil; 
6    import futils.Ls; 
7     
8    import java.io.*; 
9    import java.util.StringTokenizer; 
10    
11   /** 
12    @author D. Lyon 
13    @version 1.1 
14    @see HtmlUtil 
15    */ 
16   public class HtmlUtil { 
17    
18    
19       private static void makeLinks() { 
20           File f = Futil.getReadFile("select a bookmarks file"); 
21           BufferedReader br = ReaderUtil.getBufferedReader(f); 
22           FileWriter fw = WriterUtil.getFileWriter("Enter file.gui.html"); 
23           PrintWriter pw = new PrintWriter(fw); 
24           String link; 
25           String line = null; 
26           // creating gui.html writeHeader for the output file 
27           String head = 
28                   "<HTML> \n <BODY>\n <B><I>" 
29                   + " Following HyperLinks are extracted from " 
30                   + f + " <B><I><P>"; 
31           // write the gui.html writeHeader 
32           pw.println(head); 
33    
34           while (((line = ReaderUtil.readLine(br)) != null)) { 
35               link = getHyperLink(line); 
36               // extract the hyperlink from the line read from the input file 
37               if (link != null) 
38                   pw.println("<LI><A HREF =\"" + link + "\">" + link + "</A><P>"); 
39    
40           } 
41           pw.println("</BODY> \n </HTML>"); 
42           WriterUtil.close(fw); 
43       } 
44    
45       /** 
46        * getHyperLink() - extract the hyperlinks from the line 
47        * 
48        *@param String - line from  the input file 
49        * 
50        *@return String - return the extracted hyperlink from the line 
51        */ 
52       public static String getHyperLink(String l) { 
53           String link; 
54           StringTokenizer st = new StringTokenizer(l, "\""); 
55           int tc = st.countTokens(); 
56           for (int i = 0; i < tc; i++) { 
57               String s = st.nextToken(); 
58               if (s.indexOf("http") == 0) { 
59                   return s; 
60               } 
61           } 
62           return null; 
63       } 
64    
65       public static void main(String args[]) { 
66           HtmlUtil.makeLinks(); 
67           System.out.println("done"); 
68       } 
69    
70       static public void filterHtmls() { 
71           File files[] = Ls.getWildFiles(".gui.html"); 
72           File input_dir = Futil.getReadFile("select an gui.html file"); 
73           System.out.println(files.length + 
74                   " file(s) to process"); 
75    
76           File output_dir = new File(input_dir.getParent(), "out"); 
77           if (output_dir.exists()) 
78               System.out.println("Output dir exists:" + 
79                       output_dir); 
80           else 
81               output_dir.mkdir(); 
82    
83           //for (int i=0; i < files.length; i++) 
84           //          writeFilteredHrefFile( 
85           //              input_dir+files[i], 
86           //              output_dir+ 
87           //              "/"+ 
88           //              files[i]); 
89    
90       } 
91    
92       public static void writeFilteredHrefFile() 
93               throws IOException { 
94           FileReader fr = ReaderUtil.getFileReader("select an gui.html file"); 
95           StreamTokenizer st = 
96                   new StreamTokenizer(fr); 
97    
98    
99           FileWriter fw = WriterUtil.getFileWriter("select an HTML file for output"); 
100          PrintWriter pw = new PrintWriter(fw); 
101          int i; 
102          int next = 0; 
103          st.resetSyntax(); 
104          st.wordChars(0, 255); 
105          st.quoteChar('"'); 
106          while ((next = st.nextToken()) != 
107                  StreamTokenizer.TT_EOF) { 
108              switch (next) { 
109                  case '"': 
110                      pw.print('"'); 
111                      for (i = 0; i < 
112                              st.sval.length(); i++) 
113                          if (st.sval.charAt(i) == 
114                                  ' ') 
115                              pw.print("%20"); 
116                          else 
117                              pw.print(st.sval.charAt(i)); 
118                      pw.print('"'); 
119                      break; 
120                  case StreamTokenizer.TT_WORD: 
121                      pw.print(st.sval + " "); 
122                      break; 
123                  case StreamTokenizer.TT_NUMBER: 
124                      pw.print(st.nval + " "); 
125                      break; 
126                  case StreamTokenizer.TT_EOL: 
127                      pw.println(); 
128                      break; 
129              } // end switch 
130          } // end while 
131          WriterUtil.close(fw); 
132          ReaderUtil.close(fr); 
133      } 
134   
135      public static void writeFilteredJavaFile() { 
136          try { 
137              FileReader is = ReaderUtil.getFileReader("select a java file"); 
138              StreamTokenizer tokens = new StreamTokenizer(is); 
139   
140              PrintStream output = System.out; 
141              int next = 0; 
142              tokens.resetSyntax(); 
143              tokens.wordChars(0, 255); 
144              tokens.quoteChar(';'); 
145              while ((next = tokens.nextToken()) != 
146                      StreamTokenizer.TT_EOF) { 
147                  switch (next) { 
148                      case ';': 
149   
150                          output.print("got a line:" + 
151                                  tokens.lineno()); 
152                          break; 
153                      case StreamTokenizer.TT_WORD: 
154                          output.print(tokens.sval + 
155                                  " "); 
156                          break; 
157                      case StreamTokenizer.TT_NUMBER: 
158                          output.print(tokens.nval + 
159                                  " "); 
160                          break; 
161                      case StreamTokenizer.TT_EOL: 
162                          output.println(); 
163                          break; 
164                  } // end switch 
165              } // end while 
166              is.close(); 
167          } // end try 
168          catch (Exception exe) { 
169              System.out.println("writeFilteredHrefFile:er!"); 
170          } 
171      } 
172   
173      /** 
174       * Inputs an gui.html file and uses %20 where spaces are seen in the 
175       * hrefs. 
176       */ 
177      public static void filterFileHrefs() 
178              throws IOException { 
179          FileReader fis = ReaderUtil.getFileReader("select an gui.html file"); 
180          StreamTokenizer tokens = new StreamTokenizer(fis); 
181          int next = 0; 
182          tokens.resetSyntax(); 
183          tokens.wordChars(0, 255); 
184          tokens.quoteChar('"'); 
185   
186          while ((next = tokens.nextToken()) 
187                  != StreamTokenizer.TT_EOF) { 
188              switch (next) { 
189                  case '"': 
190                      System.out.print('"'); 
191                      StringTokenizer st = 
192                              new StringTokenizer(tokens.sval, 
193                                      " "); 
194                      while (st.hasMoreTokens()) { 
195                          System.out.print(st.nextToken()); 
196                          if (st.countTokens() > 1) { 
197                              System.out.print("%20"); 
198                          } 
199                      } 
200                      System.out.print('"'); 
201                      break; 
202                  case StreamTokenizer.TT_WORD: 
203                      System.out.print(tokens.sval + " "); 
204                      break; 
205                  case StreamTokenizer.TT_NUMBER: 
206                      System.out.print(tokens.nval + " "); 
207                      break; 
208                  case StreamTokenizer.TT_EOL: 
209                      System.out.println(); 
210                      break; 
211              } // switch 
212          } 
213          System.out.println(); 
214          ReaderUtil.close(fis); 
215      } 
216   
217      public static void processJava() { 
218          FileReader fis = ReaderUtil.getFileReader("select a java file"); 
219          String line; 
220          BufferedReader dis = new BufferedReader(fis); 
221          System.out.println("<HTML><BODY><PRE>"); 
222          try { 
223              while ((line = dis.readLine()) != 
224                      null) 
225                  System.out.println(line); 
226          } catch (IOException e) { 
227              System.out.println("Futil: ER! in processJava"); 
228          } 
229          System.out.println("</PRE></BODY></HTML>"); 
230          ReaderUtil.close(fis); 
231      } 
232   
233      public static void listFilteredHrefFile(String file) { 
234          System.out.println("processing:\t" + file); 
235          try { 
236              FileInputStream is = new 
237                      FileInputStream(file); 
238              InputStreamReader isr = new InputStreamReader(is); 
239              BufferedReader br = new BufferedReader(isr); 
240              StreamTokenizer tokens = new StreamTokenizer(br); 
241              int next = 0; 
242              tokens.resetSyntax(); 
243              tokens.wordChars(0, 255); 
244              tokens.quoteChar('"'); 
245              while ((next = tokens.nextToken()) != 
246                      StreamTokenizer.TT_EOF) { 
247                  switch (next) { 
248                      case '"': 
249                          System.out.print('"'); 
250                          StringTokenizer st = 
251                                  new StringTokenizer(tokens.sval, 
252                                          " "); 
253                          while (st.hasMoreTokens()) { 
254                              System.out.print(st.nextToken()); 
255                              if (st.countTokens() > 
256                                      1) { 
257                                  System.out.print("%20"); 
258                              } 
259                          } 
260                          System.out.print('"'); 
261                          break; 
262                      case StreamTokenizer.TT_WORD: 
263                          System.out.print(tokens.sval + 
264                                  " "); 
265                          break; 
266                      case StreamTokenizer.TT_NUMBER: 
267                          System.out.print(tokens.nval + 
268                                  " "); 
269                          break; 
270                      case StreamTokenizer.TT_EOL: 
271                          System.out.println(); 
272                          break; 
273                  } 
274              } 
275              System.out.println(); 
276              Futil.close(is); 
277          } catch (Exception exe) { 
278              System.out.println("listFilteredHrefFile:er!"); 
279          } 
280      } 
281  } 
282