/Users/lyon/j4p/src/gui/browser/HtmlSynthesizer.java

1    package gui.browser; 
2     
3     
4    public class HtmlSynthesizer { 
5        private String imageHome = "d:\\images\\"; 
6     
7        public void print(double x) { 
8            System.out.println(x); 
9        } 
10    
11       public void print(String s) { 
12           System.out.println(s); 
13       } 
14    
15       public String getHtml(String s) { 
16           return "<gui.html>\n" + s + "\n</gui.html>"; 
17       } 
18    
19       public String getHomePage() { 
20           return "<a href=\"http://www.docjava.com\">home</a>\n"; 
21       } 
22    
23       public String getHomePage(int n) { 
24           String s = getHomePage(); 
25           for (int i = 0; i < n; i++) 
26               s = s + getHomePage(); 
27           return s; 
28       } 
29    
30       public String getListItem(String s) { 
31           return "<li>\n" + s + "\n</li>"; 
32       } 
33    
34       public String getBreak() { 
35           return "\n<br>\n "; 
36       } 
37    
38       public String getH1(String s) { 
39           return "<h1>\n" + s + "\n</h1>"; 
40       } 
41    
42       public String getSubmit() { 
43           return 
44                   "<input type=submit" 
45                   + " value=" + quote("submit") + ">\n"; 
46       } 
47    
48       public String getOption(String s) { 
49           return "\t<option value=" + s + " >" + s + '\n'; 
50       } 
51    
52       public String getSelect(String name, String options[]) { 
53           String s = ""; 
54           ; 
55           for (int i = 0; i < options.length; i++) 
56               s = s + getOption(options[i]); 
57           return "<select name=" + name + ">" + s + "</select>"; 
58       } 
59    
60       public String getSelect(String name) { 
61           String sn [] = {"1", "2", "3", "4", "5"}; 
62           return getSelect(name, sn); 
63       } 
64    
65       public String getH2(String s) { 
66           return "<h2>\n" + s + "\n</h2>"; 
67       } 
68    
69       public String getH3(String s) { 
70           return "<h3>\n" + s + "\n</h3>"; 
71       } 
72    
73       public String getH4(String s) { 
74           return "<h4>\n" + s + "\n</h4>"; 
75       } 
76    
77       public String getH5(String s) { 
78           return "<h5>\n" + s + "\n</h5>"; 
79       } 
80    
81       public String getH6(String s) { 
82           return "<h6>\n" + s + "\n</h6>"; 
83       } 
84    
85       public String getP(String s) { 
86           return "<p>\n" + s + "\n</p>"; 
87       } 
88    
89       /** 
90        * <code>getBody</code> sets the background color to white. 
91        * It would be really nice if the background color were decoded 
92        * from an instance of the <code>Color</code> class. 
93        */ 
94    
95       public String getBody(String s) { 
96           return "<body BGCOLOR=\"#FFFFFF\">\n" + s + "\n</body>"; 
97       } 
98    
99       public String getHead(String s) { 
100          return "<Head>\n" + s + "\n</Head>\n"; 
101      } 
102   
103      public String getTitle(String s) { 
104          return "<title>\n" + s + "\n</title>\n"; 
105      } 
106   
107      public String getCaption(String s) { 
108          return 
109                  "<caption>" 
110                  + s 
111                  + "</caption>"; 
112      } 
113   
114      public String getTr(String s) { 
115          return "<tr>" + s + "</tr>\n"; 
116      } 
117      //<td width="91" height="39">c11</td> 
118   
119      public String getTd(int w, int h, String s) { 
120          return "\n\t<td width=\"" 
121                  + w 
122                  + "\" height=\"" + h + "\">" 
123                  + s + 
124                  "</td>\n"; 
125      } 
126   
127      public String getTd(String s) { 
128          return "\n\t<td>" 
129                  + s + 
130                  "</td>\n"; 
131      } 
132   
133      public String getTd(String s1, String s2, String s3, String s4) { 
134          return "\n\t<td>" 
135                  + s1 + quote(s2) + s3 + s4 
136                  + "</td>\n"; 
137      } 
138   
139   
140      public String getRow(int r, int nc) { 
141          String s = ""; 
142          for (int c = 1; c <= nc; c++) 
143              s = s + getTd(r + "," + c + " "); 
144          return s; 
145   
146      } 
147   
148      public String getSheet(String a[][]) { 
149          String s = ""; 
150          for (int i = 0; i < a.length; i++) { 
151              s = "<tr>" + s; 
152              for (int j = 0; j < a[i].length; j++) { 
153                  s = s + getTd(a[i][j]); 
154              } 
155              s = s + "</tr><p>\n"; 
156          } 
157          return s; 
158      } 
159   
160      public String getTable(String s) { 
161          return "\n<table border=1>\n" + s + "\n</table>\n"; 
162      } 
163   
164      public String getInput( 
165              String type, 
166              String name, 
167              String value, 
168              int size) { 
169          return 
170                  "\n<input type=" + quote(type) 
171                  + "name=" + quote(name) 
172                  + " value=" + quote(value) 
173                  + "size=" + size 
174                  + ">\n"; 
175      } 
176   
177      public String getImage(String imageName, String altStr) { 
178          return "<img src=" 
179                  + quote(imageName) 
180                  + "alt=" + quote(altStr) 
181                  + ">"; 
182   
183      } 
184   
185      public String getImage(String imageName) { 
186          return "<img src=" 
187                  + quote(imageName) 
188                  + "alt=" + quote(imageName) 
189                  + ">"; 
190   
191      } 
192   
193      public String getInput( 
194              String type, 
195              String name, 
196              String value) { 
197          return 
198                  "\n<input type=" + type + ' ' 
199                  + "name=" + name 
200                  + " value=" + value 
201                  + " >"; 
202      } 
203      // <input type="radio" name="B1_Rating" value="Excellent"> 
204   
205      public String getRadio(String name, String value) { 
206          return 
207                  getInput("radio", name, value) + value; 
208      } 
209   
210      /** <code><input type="checkbox" name="B1_Rating" value="true" checked></code> 
211       *     returns the gui.html for a check box that is checked if b is true. 
212       */ 
213   
214      public String getCheckbox(String name, boolean b) { 
215          return 
216                  getInput("checkbox", name, b ? "\"true\" CHECKED" : "\"false\""); 
217      } 
218   
219      //<INPUT TYPE="text" NAME="UID" VALUE="" SIZE=30> 
220      public String getTextField(String name, String value, int size) { 
221          return 
222                  getInput("text", name, value, 30); 
223      } 
224   
225      public String getRadioButtons(String name, int b) { 
226          String s = ""; 
227          for (int i = 1; i <= b; i++) 
228              s = s + getRadio(name, i + ""); 
229          return "<p>" + s + ""; 
230      } 
231   
232      public String getPassField(String name, String value, int size) { 
233          return 
234                  getInput("password", name, value, 30); 
235      } 
236   
237      public String getTextField(String name, String value) { 
238          return 
239                  getTextField(name, value, 30); 
240      } 
241   
242      public String getPassField(String name, String value) { 
243          return 
244                  getPassField(name, value, 30); 
245      } 
246   
247      public String getPassField(String name) { 
248          return 
249                  getPassField(name, "", 30); 
250      } 
251   
252      public String getTextField(String name) { 
253          return 
254                  getTextField(name, "", 30); 
255      } 
256   
257      //<FORM action="" method="POST"> 
258      public String getForm(String action, String method, String s) { 
259          return "\n<form action=" + quote(action) 
260                  + "method=" + quote(method) + ">\n" 
261                  + s + ' ' 
262                  + "\n </form >\n"; 
263      } 
264   
265      public String quote(String s) { 
266          return '\"' + s + "\" "; 
267      } 
268   
269      public String getTable(int nr, int nc) { 
270          String s = "<TABLE BORDER=1>"; 
271          for (int r = 1; r <= nr; r++) { 
272              s = s + 
273                      getTr(getRow(r, nc)); 
274              // assume number of columns is the 
275              // same for each row 
276          } 
277          return s + "</table>"; 
278      } 
279   
280      public String testForm1() { 
281          return 
282                  getHtml( 
283                          getHead(getTitle("testForm")) + 
284                  getBody( 
285                          getForm("", "GET", 
286                                  getP(getTextField("name")) + 
287                  getPassField("password") + 
288                  getRadioButtons("q1", 5) 
289   
290                          ) 
291                  ) 
292                  ); 
293      } 
294   
295      public String[] getSelects(String name, int n) { 
296          String s[] = new String[n]; 
297          s[0] = name; 
298          for (int i = 1; i < n; i++) 
299              s[i] = getSelect(name); 
300          return s; 
301      } 
302   
303      public String testForm() { 
304   
305          String a [][] = { 
306              {"Student Name", "Analytic Skills", 
307               "Communication Skills", 
308               "creative problem solving"}, 
309              getSelects("doe", 4), 
310              getSelects("shmoe", 4), 
311              getSelects("wanker", 4), 
312              getSelects("spanker", 4), 
313              getSelects("peabody", 4), 
314          }; 
315          return 
316                  getHtml( 
317                          getHead( 
318                                  getTitle( 
319                                          "my title!")) + 
320                  getBody( 
321                          getHomePage(10) + 
322                  getForm( 
323                          "http://localhost/examples/servlet/HelloWorldExample", 
324                          "GET", 
325                          getTable( 
326                                  getCaption("My Caption") 
327                  + getSheet(a)) 
328                  + getSubmit() 
329                  + getImage( 
330                          "http://www.docjava.com/consulti/docjava.jpe") 
331   
332                  ) 
333                  ) 
334                  ); 
335      } 
336   
337      public String loadHomePage() { 
338   
339          return 
340                  getHtml( 
341                          getHead( 
342                                  getTitle( 
343                                          "DocJava Home Page")) + 
344                  getBody( 
345                          getHomePage(0) + 
346                  getForm("http://www.docjava.com", 
347                          "GET", 
348                          getTable( 
349                                  getTr( 
350                                          getTd("<a href=", "http://www.willcam.com/cmat/gui.html/crossname.gui.html", 
351                                                  "> ", "Comprehensive HTML Cross Reference</a>")) 
352                  + getTr( 
353                          getTd("<a href=", "http://www.lynda.com/hexh.gui.html", 
354                                  "> ", "Lynda's color chart</a>")) 
355                  + getTr( 
356                          getTd("<a href=", "http://www.zdnet.com/devhead/resources/href/ascii.gui.html", 
357                                  "> ", "ASCII Character Chart</a>")) 
358                  + getTr( 
359                          getTd("<a href=", "http://java.sun.com/docs/books/tutorial/index.gui.html", 
360                                  "> ", "Java Tutorial</a>")) 
361                  + getTr( 
362                          getTd("<a href=", "http://java.sun.com/docs/books/tutorial/uiswing/components/toolbar.gui.html", 
363                                  "> ", "How to use Tool Bars</a>")) 
364                          ) 
365                  ) 
366                  ) 
367                  ); 
368      } 
369   
370   
371      public static void main(String args[]) { 
372          HtmlSynthesizer hs = new HtmlSynthesizer(); 
373          System.out.println( 
374                  hs.getHtml( 
375                          hs.getForm("", "GET", 
376                                  hs.getTextField("name")) + 
377                  hs.getTable(9, 20))); 
378      } 
379  }