KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > HTMLElement


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.util.Enumeration JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import org.mmbase.module.ProcessorInterface;
17 import org.mmbase.util.logging.Logger;
18 import org.mmbase.util.logging.Logging;
19
20 /**
21 * Class which is the super-class for ALL the HTMLElements.
22 *
23 * Includes a parser wich fills all the variables
24 * and calls the abstract function generate which will be
25 * implemented in a HTMLElement
26 *
27 * @application SCAN
28 * @author Jan van Oosterom
29 * @version $Id: HTMLElement.java,v 1.9 2004/10/11 11:17:43 pierre Exp $
30 *
31 */

32 public abstract class HTMLElement {
33     // logger
34
private static Logger log = Logging.getLoggerInstance(HTMLElement.class.getName());
35
36     /**
37     * The processor which will be called by the parse routine.
38     */

39     protected ProcessorInterface processor = null;
40
41     /**
42     * The value what will be put after the NAME= tag
43     */

44     protected String JavaDoc name = null;
45
46     /**
47     * The value what will be passed to the Processor or will be the value
48     * of the item. (PROC ->Processor else the value)
49     */

50     protected String JavaDoc values = null;
51
52     /**
53     * contains the Vector with the items returned by the ProcessorInteface.getList(value) if the PROC tag is present
54     */

55     protected Vector JavaDoc valuesList = null;
56
57     /**
58     * added to the SIZE= tag
59     */

60     protected String JavaDoc size = null;
61
62     /**
63     * The element which will be selected (e.g.: <OPTION SELECTED>)
64     */

65     protected String JavaDoc selected = null;
66
67     /**
68     * added to ROWS=
69     */

70     protected String JavaDoc rows = null;
71
72     /**
73     * added to COLS=
74     */

75     protected String JavaDoc cols = null;
76
77     /**
78     * added to MAX=
79     */

80     protected int max = -1;
81
82     /**
83     * added to Sorted=
84     */

85     protected String JavaDoc sorted = null;
86
87     /**
88     * if this element is in the ValueList from the Processor it should be excluded
89     */

90     protected String JavaDoc exclude = null;
91
92     /**
93     * if true there is an excluded item
94     */

95     protected boolean ex = false;
96
97     /**
98     * if true it the Element should allow MULTIPLE selection
99     */

100
101     protected boolean multiple = false;
102     /**
103     * There is a (single) list of items (from the Processor)
104     */

105     protected boolean moreValues = false;
106
107     /**
108     * There is a double list of items (from the Processor)
109     */

110     protected boolean moredouble = false;
111
112     /**
113     * if true there is a Selected item
114     */

115     protected boolean sel = false;
116
117     /**
118     * The Item list should be VERTICAL (a <BR> tag should be added)
119     */

120     protected boolean vertical = false;
121
122     /**
123     * The Item list should be HORIZONTAL This is default
124     */

125     protected boolean horizontal = false;
126
127     /**
128     * The EMPTY tag is present in the macro.
129     * There should be a EMPTY SELECTION OPTION item added to the list
130     */

131     protected boolean empty = false;
132
133     /**
134     * The PROC tag is present in the macro.
135     * The processor will be called and should return a Vector with items
136     */

137     protected boolean proc = false;
138
139     /**
140     * The DOUBLE tag is present in the macro.
141     * The processor will be called and should return a Vector with items pairs
142     * 1st = item1a, 2nd= item1b, 3th=item2a, 4th=item 2b, etc
143     */

144     protected boolean procdouble = false;
145
146     /**
147     * The user name that is currently acessing this page
148     */

149     protected String JavaDoc user =null;
150
151     /**
152     * page from this request
153     */

154     //protected HttpServletRequest rq =null;
155
protected scanpage sp=null;
156
157     /**
158     * empty
159     */

160     public HTMLElement() {
161     }
162
163     /**
164     * Calls the parser and returns the String returned by generate.
165     */

166     protected String JavaDoc generateHTML(scanpage sp,ProcessorInterface proc, Vector JavaDoc macro) {
167         log.debug("generateHTML");
168         processor=proc;
169         //this.user = user;
170
this.sp = sp;
171         log.debug("marco: " + macro);
172         boolean ok = parse(macro);
173         //log.debug("after parse");
174
if (!ok) return "";
175         return generate();
176     }
177     /**
178     * Should be implemented by the subclass and and should return the generated HTML.
179     */

180     protected abstract String JavaDoc generate();
181
182     /**
183     * Reads the Vector and fills all the variables if the corespondending variables.
184     */

185     protected boolean parse (Vector JavaDoc macro) {
186         name = null;
187         values = null;
188         size = null;
189         selected = null;
190         rows = null;
191         cols = null;
192         exclude = null;
193         sorted = null;
194         max=-1;
195
196         ex = false;
197         multiple = false;
198         sel = false;
199         moreValues = false;
200         moredouble = false;
201         horizontal = false;
202         vertical = false;
203         empty = false;
204         proc = false;
205         procdouble = false;
206
207         Enumeration JavaDoc e = macro.elements();
208         if (e.hasMoreElements()) {
209             name = (String JavaDoc) e.nextElement();
210         } else {
211             log.error("HTMLElement: to few params, no name !!!");
212             return false;
213         }
214
215         if (e.hasMoreElements()) {
216             values = (String JavaDoc) e.nextElement();
217             if (this instanceof HTMLElementText) {
218                 //log.debug("HTMLElement instanceof Text, values: " + values);
219
if (values.indexOf("\"")==0) {
220                     values= values.substring(1,values.length()-1);
221                 }
222             }
223         } else if (!(this instanceof HTMLElementText ||
224                      this instanceof HTMLElementPassword)) {
225             log.error("HTMLElement: to few params, no value !!!");
226             return false;
227         }
228
229         while (e.hasMoreElements()) {
230             String JavaDoc str = (String JavaDoc) e.nextElement();
231
232             String JavaDoc type = getType(str);
233             if (type == null) {
234                 return false;
235             }
236             String JavaDoc value ;
237             value = getValue(str);
238             if ((type.equalsIgnoreCase("MULTIPLE") && value == null) ||
239                 (type.equalsIgnoreCase("CHECKED") && value == null) ||
240                 (type.equalsIgnoreCase("MAX") && value == null) ||
241                 (type.equalsIgnoreCase("SELECTED") && value == null) ||
242                 (type.equalsIgnoreCase("VERTICAL") && value == null) ||
243                 (type.equalsIgnoreCase("EMPTY") && value == null) ||
244                 (type.equalsIgnoreCase("PROC") && value == null) ||
245                 (type.equalsIgnoreCase("DOUBLE") && value == null) ||
246                 (type.equalsIgnoreCase("SORTED") && value == null) ||
247                 (type.equalsIgnoreCase("HORIZONTAL") && value == null)) {
248                 value = "whatever";
249             }
250
251             if (type != null && value != null ) {
252                 if (type.equalsIgnoreCase("SIZE")) {
253                     size = value;
254                 } else if ((type.equalsIgnoreCase("CHECKED")) || (type.equalsIgnoreCase("SELECTED"))) {
255                     selected = value;
256                     if (selected.charAt(0)== '\"') {
257                         selected = selected.substring(1,selected.length()-1);
258                     }
259                     sel = true;
260                 } else if (type.equalsIgnoreCase("ROWS")) {
261                     rows = value;
262                 } else if (type.equalsIgnoreCase("DOUBLE")) {
263                     procdouble = true;
264                 } else if (type.equalsIgnoreCase("PROC")) {
265                     proc = true;
266                 } else if (type.equalsIgnoreCase("SORTED")) {
267                     sorted = value;
268                 } else if (type.equalsIgnoreCase("EXCLUDE")) {
269                     ex = true;
270                     exclude = value;
271                     if (exclude.charAt(0)== '\"') {
272                         exclude = exclude.substring(1,exclude.length()-1);
273                     }
274                 } else if (type.equalsIgnoreCase("MULTIPLE")) {
275                     multiple = true;
276                 } else if (type.equalsIgnoreCase("VERTICAL")) {
277                     vertical = true;
278                 } else if (type.equalsIgnoreCase("HORIZONTAL")) {
279                     horizontal = true;
280                 } else if (type.equalsIgnoreCase("COLS")) {
281                     cols = value;
282                 } else if (type.equalsIgnoreCase("MAX")) {
283                     try {
284                         max=Integer.parseInt(value);
285                     } catch (Exception JavaDoc f) {};
286                 } else if (type.equalsIgnoreCase("PROCESSOR")) {
287                     // get processor not used at this time !!!
288
} else if (type.equalsIgnoreCase("EMPTY")) {
289                     empty = true;
290                 } else {
291                     log.error("HTMLElement.parse unknow MACRO HTML found: " + type + "=" + value);
292                 }
293             }
294         }
295
296         if (proc && !procdouble) {
297             //We found a PROC tag so put it in the Processor
298
valuesList = null;
299             // what is this, should tagger be empty ?
300
try {
301               valuesList = processor.getList(sp,new StringTagger(""),values);
302             } catch (RuntimeException JavaDoc pe) {}
303             if (valuesList == null) {
304                 //moreValues = false;
305
//values = "";
306
log.error("HTMLElement.parse: The processor return null !!");
307                 return false;
308             } else {
309                 moreValues = true;
310             }
311         }
312
313         if (procdouble) {
314             //We found a DOUBLE tag so put it in the Processor
315
valuesList = null;
316             try {
317                 // what is this, should tagger be empty ?
318
valuesList = processor.getList(sp,new StringTagger(""),values);
319             } catch (RuntimeException JavaDoc pe) {}
320             if (valuesList == null) {
321                 log.error("HTMLElement.parse: The processor returned null !!");
322                 return false;
323                 //moredouble = false;
324
//values = "";
325
} else {
326                 moredouble = true;
327             }
328         }
329         return true;
330     }
331
332     /**
333     * Returns the part in front of the '=' char of the String.
334     */

335     protected String JavaDoc getType(String JavaDoc str) {
336         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(str,"=");
337         if (tok.hasMoreTokens()) {
338             return (String JavaDoc)tok.nextElement();
339         }
340         log.error("getType: invalid format ...!!!"+str);
341         return null;
342     }
343
344     /**
345     * Returns the part after the '=' char of the String.
346     */

347     protected String JavaDoc getValue(String JavaDoc str) {
348         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(str,"=");
349         if (tok.hasMoreTokens()) {
350             // nextToken() would only return a Object dummy, becaue we are only
351
// interested in the value after the =
352
tok.nextElement();
353             if (tok.hasMoreTokens()) {
354                 return (String JavaDoc) tok.nextElement();
355             }
356         }
357         // no value found
358
return null;
359     }
360 }
361
Popular Tags