/Users/lyon/j4p/src/net/server/servlets/HtmlPage.java

1    package net.server.servlets; 
2     
3    /** 
4     * The HtmlPage class encapsulates the functionality 
5     * required to prepare an HTML script to display in 
6     * a browser. This class provides methods to construct 
7     * various HTML elements such as forms, list boxes, 
8     * anchor tags and headlines. 
9     * 
10    * @author Robert Lysik 
11    * @version 1.00 
12    */ 
13   public class HtmlPage { 
14       private String strTitle; 
15       private String strBody; 
16    
17       /** 
18        * This is the default constructor for the HtmlPage class. 
19        * An empty title tag is provided for the title string, as 
20        * well as the opening tag for the body section of the 
21        * HTML page. 
22        */ 
23       public HtmlPage() { 
24           strTitle = new String("<head><title></title></head>"); 
25           strBody = new String("<body>"); 
26       } 
27    
28       /** 
29        * This constructor takes a String as an argument which 
30        * is used to initialize the title of the page. 
31        * 
32        * @param String the title of the page 
33        */ 
34       public HtmlPage(String title) { 
35           strTitle = new String("<head><title>" 
36                   + title 
37                   + "</title></head>"); 
38    
39           strBody = new String("<body>"); 
40       } 
41    
42       /** 
43        * This function inserts a line break into the HTML 
44        * script. 
45        */ 
46       public void addBreak() { 
47           strBody += "<br>\r\n"; 
48       } 
49    
50       /** 
51        * This function inserts a headline tag with the 
52        * value provided as a parameter to the function, 
53        * as well as the text to be displayed. 
54        * 
55        * @param int the headline type 
56        * @param String the headline text 
57        */ 
58       public void addHeadline(int headlineType, 
59                               String headlineText) { 
60           strBody += "<h" + headlineType + ">"; 
61           strBody += headlineText; 
62           strBody += "</h" + headlineType + ">\r\n"; 
63       } 
64    
65       /** 
66        * This function inserts a hidden input control 
67        * tag into the HTML script. 
68        * 
69        * @param String name of hidden tag 
70        * @param String value of hidden tag 
71        */ 
72       public void addHidden(String name, 
73                             String value) { 
74           strBody += "<input type = " + 
75                   quote("hidden") + 
76                   " name = " + 
77                   quote(name) + 
78                   " value = " + 
79                   quote(value) + 
80                   ">\r\n"; 
81       } 
82    
83       /** 
84        * This function creates an input element for a form. Input 
85        * elements, such as text boxes, radio buttons, and checkboxes 
86        * allow the user of an HTML page to enter information which 
87        * may then be processed by another page, or by script within 
88        * the existing page. These elements must be enclosed within 
89        * form tags in order for correct processing. 
90        * 
91        * @param String the type of form input, i.e. text, radio, checkbox 
92        * @param String the name of the input element 
93        * @param String the value associated with this element 
94        * @param String the size of the element 
95        */ 
96       public void addInput(String type, 
97                            String name, 
98                            String value, 
99                            String size) { 
100          strBody += "<input type = " + 
101                  quote(type) + 
102                  " name = " + 
103                  quote(name) + 
104                  " value = " + 
105                  quote(value) + 
106                  " size = " + 
107                  quote(size) + 
108                  ">\r\n"; 
109      } 
110   
111      /** 
112       * This function adds submit button to a form in an HTML page. 
113       * 
114       * @param String the text to be displayed on the submit button 
115       */ 
116      public void addSubmit(String value) { 
117          strBody += "<input type = " + 
118                  quote("submit") + 
119                  " value = " + 
120                  quote(value) + 
121                  ">\r\n"; 
122      } 
123   
124      /** 
125       * This function adds a normal text string to the 
126       * HTML script. 
127       * 
128       * @param String the text to be displayed 
129       */ 
130      public void addText(String text) { 
131          strBody += text + "\r\n"; 
132      } 
133   
134      /** 
135       * This function adds a closing form HTML tag. 
136       */ 
137      public void endForm() { 
138          strBody += "</form>\r\n"; 
139      } 
140   
141      /** 
142       * This function returns the HTML script to generate the 
143       * page as it is currently defined. The title and body 
144       * elements of the HTML page are enclosed in opening and closing 
145       * HTML tags. 
146       */ 
147      public String getHtml() { 
148          String html = new String(); 
149   
150          html = "<html>" 
151                  + strTitle 
152                  + strBody 
153                  + "</body>" 
154                  + "</html>"; 
155   
156          return html; 
157      } 
158   
159      /** 
160       * This function creates an input element which is called a 
161       * select list. This is a drop down list box, if the size 
162       * of the list box is set to one, or a normal list box if 
163       * the size is greater than one. This element must be 
164       * encapsulated within the FORM section of an HTML page in 
165       * order for correct processing to occur. 
166       * 
167       * @param String the name of the select list 
168       * @param String the size of the select list. If set to 
169       *        one, this becomes a drop down list box. 
170       * @param String the values associated with the list box 
171       * @param String the text displayed in the list box. 
172       */ 
173      public String getSelect(String name, 
174                              String size, 
175                              String values[], 
176                              String text[]) { 
177          String select = new String(); 
178   
179          // The opening tag for the select list includes 
180          // the name of the list and its size. 
181          select += "<select name = " + 
182                  quote(name) + 
183                  " size = " + 
184                  quote(size) + 
185                  ">\r\n"; 
186   
187          // Each element of the list box is then added, prefixed 
188          // by the OPTION tag. 
189          for (int index = 0; index < values.length; index++) { 
190              select += "<option value = " + 
191                      quote(values[index]) + 
192                      ">" + 
193                      text[index] + 
194                      "\r\n"; 
195          } 
196   
197          // The closing tag for the select list is then added 
198          select += "</select>\r\n"; 
199   
200          return select; 
201      } 
202   
203      /** 
204       * This function returns a string which is surrounded 
205       * by quotes. 
206       * 
207       * @param String the string to be encapsulated in quotes 
208       */ 
209      public String quote(String s) { 
210          return '\"' + s + '\"'; 
211      } 
212   
213      /** 
214       * This function inserts the opening tag for a form into 
215       * the HTML script. 
216       * 
217       * @param String the method of form submission, either 
218       *        post or get. 
219       * @param String the action attribute of the form. 
220       */ 
221      public void startForm(String method, String action) { 
222          strBody += "<form method = " + 
223                  quote(method) + 
224                  " action = " + 
225                  quote(action) + 
226                  ">\r\n"; 
227      } 
228  } 
229