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

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