KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ecs > GenericElement


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Jakarta Element Construction Set",
29  * "Jakarta ECS" , and "Apache Software Foundation" must not be used
30  * to endorse or promote products derived
31  * from this software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * "Jakarta Element Construction Set" nor "Jakarta ECS" nor may "Apache"
36  * appear in their names without prior written permission of the Apache Group.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  *
57  */

58 package org.apache.ecs;
59
60 import java.io.Writer JavaDoc;
61 import java.io.StringWriter JavaDoc;
62 import java.io.PrintWriter JavaDoc;
63 import java.io.OutputStream JavaDoc;
64 import java.io.BufferedOutputStream JavaDoc;
65 import java.io.ByteArrayOutputStream JavaDoc;
66 import java.io.IOException JavaDoc;
67 import java.io.UnsupportedEncodingException JavaDoc;
68 import java.io.Serializable JavaDoc;
69 import org.apache.ecs.filter.CharacterFilter;
70 import java.util.Hashtable JavaDoc;
71
72 /**
73     This class implements the ElementFactory.
74
75     @version $Id: GenericElement.java,v 1.21 2003/04/27 09:45:34 rdonkin Exp $
76     @author <a HREF="mailto:snagy@servletapi.com">Stephan Nagy</a>
77     @author <a HREF="mailto:jon@clearink.com">Jon S. Stevens</a>
78 */

79 public abstract class GenericElement implements Element,Serializable JavaDoc
80 {
81     /**
82         Create default filter to use in all GenericElements
83         Can be overridden by setFilter method. Of course
84         using getFilter then modifying will have some interesting
85         side effects, like changing the global defaults.
86     */

87     private static Filter _defaultFilter = new CharacterFilter();
88
89     /**
90         Filter to use to escape input
91         @serial filter filter
92     */

93     private Filter filter = _defaultFilter;
94
95     /**
96         position of tag relative to start and end.
97         @serial position position
98     */

99     private int position = ECSDefaults.getDefaultPosition();
100
101     /**
102         Should we filter the value of &lt;&gt;VALUE&lt;/&gt;
103         @serial filter_state filter_state
104     */

105     private boolean filter_state = ECSDefaults.getDefaultFilterState();
106
107     /**
108         Should we print html in a more readable format?
109         @serial pretty_print pretty_print
110     */

111     private boolean pretty_print = ECSDefaults.getDefaultPrettyPrint();
112
113     /**
114         Version number for codebase
115         @serial VERSION VERSION
116     */

117     private static final String JavaDoc VERSION = "1.4.2";
118
119     /**
120         Default value to set case type
121         @serial case_type case_type
122     */

123     private int case_type = ECSDefaults.getDefaultCaseType();
124
125     /**
126         Where to get the element definition from
127         @serial element element
128     */

129     private Hashtable JavaDoc element = new Hashtable JavaDoc(4);
130
131     /**
132         Does this element need a closing tag?
133         @serial end_element end_element
134     */

135     private boolean end_element = ECSDefaults.getDefaultEndElement();
136
137     /**
138         What codeset are we going to use the default is 8859_1
139         @serial codeset codeset
140     */

141     private String JavaDoc codeset = ECSDefaults.getDefaultCodeset();
142
143     /**
144         What the start modifier should be
145         @serial begin_start_modifier begin_start_modifier
146     */

147     private char begin_start_modifier = ECSDefaults.getDefaultBeginStartModifier();
148
149     /**
150         What the start modifier should be
151         @serial end_start_modifier end_start_modifier
152     */

153     private char end_start_modifier = ECSDefaults.getDefaultEndStartModifier();
154
155     /**
156         What the end modifier should be
157         @serial begin_end_modifier begin_end_modifier
158     */

159     private char begin_end_modifier = ECSDefaults.getDefaultBeginEndModifier();
160
161     /**
162         What the end modifier should be
163         @serial end_end_modifier end_end_modifier
164     */

165     private char end_end_modifier = ECSDefaults.getDefaultEndEndModifier();
166
167     /**
168         What the modifier should be...optimization variable
169         @serial start_modifier_defined start_modifier_defined
170     */

171     private boolean start_modifier_defined = false;
172
173     /**
174         What the modifier should be...optimization variable
175         @serial begin_start_modifier_defined begin_start_modifier_defined
176     */

177     private boolean begin_start_modifier_defined = false;
178
179     /**
180         What the modifier should be...optimization variable
181         @serial end_start_modifier_defined end_start_modifier_defined
182     */

183     private boolean end_start_modifier_defined = false;
184
185     /**
186         What the modifier should be...optimization variable
187         @serial begin_end_modifier_defined begin_end_modifier_defined
188     */

189     private boolean begin_end_modifier_defined = false;
190
191     /**
192         What the modifier should be...optimization variable
193         @serial end_end_modifier_defined end_end_modifier_defined
194     */

195     private boolean end_end_modifier_defined = false;
196
197     /**
198         What the modifier should be...optimization variable
199         @serial end_modifier_defined end_modifier_defined
200     */

201     private boolean end_modifier_defined = false;
202
203     /**
204         Our current tab level.
205         @serial tabLevel tabLevel
206     */

207     protected int tabLevel = 0;
208
209     /** @serial start_tag start_tag */
210     private char start_tag = ECSDefaults.getDefaultStartTag();
211     /** @serial end_tag end_tag */
212     private char end_tag = ECSDefaults.getDefaultEndTag();
213     /** @serial __text __text */
214     private String JavaDoc __text = new String JavaDoc();
215
216     /**
217         Location to store element_type at.
218         @serial element_type element_type
219     */

220     private String JavaDoc element_type = new String JavaDoc();
221
222     /**
223         Base class for defining all elements.
224     */

225     public GenericElement()
226     {
227     }
228
229     /**
230         Do we want to do pretty printing?
231     */

232     public boolean getPrettyPrint()
233     {
234         return(pretty_print);
235     }
236
237     /**
238         Do we want to pretty print?
239         @param pretty_print turn pretty printing on or off.
240     */

241     public Element setPrettyPrint(boolean pretty_print)
242     {
243         this.pretty_print = pretty_print;
244         return(this);
245     }
246
247     /**
248         Get the Hashtable representation of an element.
249     */

250     protected Hashtable JavaDoc getElementHashEntry()
251     {
252         return(element);
253     }
254
255     /**
256         set the filter state of the element.
257         @param filter_state (true|false) do we want to use a filter? default true;
258     */

259     public Element setFilterState(boolean filter_state)
260     {
261         this.filter_state = filter_state;
262         return(this);
263     }
264
265     /**
266         get the filter state of the element.
267     */

268     public boolean getFilterState()
269     {
270         return(filter_state);
271     }
272
273     /**
274         Get the filter that is currently in use.
275     */

276     public Filter getFilter()
277     {
278         return(filter);
279     }
280
281     /**
282         Override the default filter with a user supplied filter.
283     */

284     public void setFilter(Filter filter)
285     {
286         this.filter = filter;
287     }
288
289     /**
290         What version of htmlGen is this?
291     */

292     public String JavaDoc getVersion()
293     {
294         return(VERSION);
295     }
296
297     /**
298         Determine what case the html element should be rendered. Default is MIXEDCASE
299     */

300     public void setCase(int case_type)
301     {
302         this.case_type = case_type;
303     }
304
305     /**
306         Find out what case the element is supposed to be rendered in.
307     */

308     public int getCase()
309     {
310         return(case_type);
311     }
312
313     /**
314         Determine what type of element this is A,BR,EM,ACRONYM
315     */

316     public void setElementType(String JavaDoc element_type)
317     {
318         this.element_type = element_type;
319     }
320
321     /**
322         Find out what type of element this is.
323     */

324     public String JavaDoc getElementType()
325     {
326         return(element_type);
327     }
328
329     /**
330         Determine what codeset should be used.
331     */

332     public void setCodeSet(String JavaDoc codeset)
333     {
334         this.codeset = codeset;
335     }
336
337     /**
338         Find out what the current codeset is.
339     */

340     public String JavaDoc getCodeSet()
341     {
342         return(codeset);
343     }
344
345     /**
346         Does this element need a closing tag. Default is true
347     */

348     public void setNeedClosingTag(boolean close)
349     {
350         end_element = close;
351     }
352
353     /**
354         Determine if this element needs to be closed.
355     */

356     public boolean getNeedClosingTag()
357     {
358         return(end_element);
359     }
360     
361     /**
362         Determine if this element needs a line break, if pretty printing.
363     */

364     public boolean getNeedLineBreak()
365     {
366         return true;
367     }
368
369     /**
370         Set the position of the tag to be rendered at in relation to start_element_char
371     */

372     public void setTagPosition(int position)
373     {
374         this.position = position;
375     }
376
377     /**
378         Get the position the tag is to be rendered at.
379     */

380     public int getTagPosition()
381     {
382         return(position);
383     }
384
385     /**
386         Alter the case of this element so that it is rendered according
387         to whatever the value of case_type is. Default is MIXEDCASE
388     */

389     protected String JavaDoc alterCase(String JavaDoc value)
390     {
391         switch (getCase())
392         {
393             case UPPERCASE:
394                 return value.toUpperCase();
395             case LOWERCASE:
396                 return value.toLowerCase();
397             default:
398                 return value;
399         }
400     }
401
402     /**
403         Set the element modifier the default is no modifier.
404         @param modifier the modifier for the element a ? would result <br>
405         in &lt;? ?&gt;
406     */

407     public Element setBeginModifier(char modifier)
408     {
409         setBeginStartModifier(modifier);
410         setBeginEndModifier(modifier);
411         setBeginStartModifierDefined(true);
412         setBeginEndModifierDefined(true);
413         return(this);
414     }
415
416     /**
417         Set the element modifier the default is no modifier.
418         @param modifier the modifier for the element a ? would result <br>
419         in &lt;? &gt;
420     */

421     public Element setBeginStartModifier(char modifier)
422     {
423         this.begin_start_modifier = modifier;
424         setBeginStartModifierDefined(true);
425         return(this);
426     }
427
428     /**
429         Set the element modifier the default is no modifier.
430         @param modifier the modifier for the element a ? would result <br>
431         in &lt; ?&gt;
432     */

433     public Element setBeginEndModifier(char modifier)
434     {
435         this.begin_end_modifier = modifier;
436         setBeginEndModifierDefined(true);
437         return(this);
438     }
439
440     /**
441         Set the element modifier the default is no modifier.
442         @param modifier the modifier for the element a ? would result <br>
443         in &lt;? ?&gt;
444     */

445     public Element setBeginModifier(char start_modifier,char end_modifier)
446     {
447         this.begin_start_modifier = start_modifier;
448         this.begin_end_modifier = end_modifier;
449         setBeginStartModifierDefined(true);
450         setBeginEndModifierDefined(true);
451         return(this);
452     }
453
454     /**
455         What is the element modifier for this tag.
456     */

457     public char getBeginStartModifier()
458     {
459         return(begin_start_modifier);
460     }
461
462     /**
463         What is the element modifier for this tag.
464     */

465     public char getBeginEndModifier()
466     {
467         return(begin_end_modifier);
468     }
469
470     /**
471         Remove the element modifier for this tag.
472     */

473     public Element removeBeginStartModifier()
474     {
475         setBeginStartModifierDefined(false);
476         return(this);
477     }
478
479     /**
480         Remove the element modifier for this tag.
481     */

482     public Element removeBeginEndModifier()
483     {
484         setBeginEndModifierDefined(false);
485         return(this);
486     }
487
488     /**
489         Modifier optimization
490     */

491     private void setBeginStartModifierDefined(boolean val)
492     {
493         this.begin_start_modifier_defined = val;
494     }
495
496     /**
497         Modifier optimization
498     */

499     private void setBeginEndModifierDefined(boolean val)
500     {
501         this.begin_end_modifier_defined = val;
502     }
503
504     /**
505         Modifier optimization
506     */

507     protected boolean getEndStartModifierDefined()
508     {
509         return this.end_start_modifier_defined;
510     }
511
512     /**
513         Modifier optimization
514     */

515     protected boolean getEndEndModifierDefined()
516     {
517         return this.end_end_modifier_defined;
518     }
519
520     /**
521         Set the element modifier the default is no modifier.
522         @param modifier the modifier for the element a ? would result <br>
523         in &lt;? ?&gt;
524     */

525     public Element setEndModifier(char modifier)
526     {
527         setEndStartModifier(modifier);
528         setEndEndModifier(modifier);
529         setEndStartModifierDefined(true);
530         setEndEndModifierDefined(true);
531         return(this);
532     }
533
534     /**
535         Set the element modifier the default is no modifier.
536         @param modifier the modifier for the element a ? would result <br>
537         in &lt;? &gt;
538     */

539     public Element setEndStartModifier(char modifier)
540     {
541         this.end_start_modifier = modifier;
542         setEndStartModifierDefined(true);
543         return(this);
544     }
545
546     /**
547         Set the element modifier the default is no modifier.
548         @param modifier the modifier for the element a ? would result <br>
549         in &lt; ?&gt;
550     */

551     public Element setEndEndModifier(char modifier)
552     {
553         this.end_end_modifier = modifier;
554         setEndEndModifierDefined(true);
555         return(this);
556     }
557
558     /**
559         Set the element modifier the default is no modifier.
560         @param modifier the modifier for the element a ? would result <br>
561         in &lt;? ?&gt;
562     */

563     public Element setEndModifier(char start_modifier,char end_modifier)
564     {
565         this.end_start_modifier = start_modifier;
566         this.end_end_modifier = end_modifier;
567         setEndStartModifierDefined(true);
568         setEndEndModifierDefined(true);
569         return(this);
570     }
571
572     /**
573         What is the element modifier for this tag.
574     */

575     public char getEndStartModifier()
576     {
577         return(end_start_modifier);
578     }
579
580     /**
581         What is the element modifier for this tag.
582     */

583     public char getEndEndModifier()
584     {
585         return(end_end_modifier);
586     }
587
588     /**
589         Remove the element modifier for this tag.
590     */

591     public Element removeEndStartModifier()
592     {
593         setEndStartModifierDefined(false);
594         return(this);
595     }
596
597     /**
598         Remove the element modifier for this tag.
599     */

600     public Element removeEndEndModifier()
601     {
602         setEndEndModifierDefined(false);
603         return(this);
604     }
605
606     /**
607         Modifier optimization
608     */

609     private void setEndStartModifierDefined(boolean val)
610     {
611         this.end_start_modifier_defined = val;
612     }
613
614     /**
615         Modifier optimization
616     */

617     private void setEndEndModifierDefined(boolean val)
618     {
619         this.end_end_modifier_defined = val;
620     }
621
622     /**
623         Modifier optimization
624     */

625     protected boolean getBeginStartModifierDefined()
626     {
627         return this.begin_start_modifier_defined;
628     }
629
630     /**
631         Modifier optimization
632     */

633     protected boolean getBeginEndModifierDefined()
634     {
635         return this.begin_end_modifier_defined;
636     }
637
638     /**
639         Set the start tag character.
640     */

641     public void setStartTagChar(char start_tag)
642     {
643         this.start_tag = start_tag;
644     }
645
646     /**
647         Get the start tag character.
648     */

649     public char getStartTagChar()
650     {
651         return(start_tag);
652     }
653
654     /**
655         Set the end tag character.
656     */

657     public void setEndTagChar(char end_tag)
658     {
659         this.end_tag = end_tag;
660     }
661
662     /**
663         Get the end tag character.
664     */

665     public char getEndTagChar()
666     {
667         return(end_tag);
668     }
669
670     public String JavaDoc getTagText()
671     {
672         return(__text);
673     }
674
675     public Element setTagText(String JavaDoc text)
676     {
677         this.__text = (text);
678         return(this);
679     }
680
681     public String JavaDoc createStartTag()
682     {
683         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
684         sb.append(getStartTagChar());
685
686         if(getBeginStartModifierDefined())
687         {
688             sb.append(getBeginStartModifier());
689         }
690         sb.append(getElementType());
691
692         if(getBeginEndModifierDefined())
693         {
694             sb.append(getBeginEndModifier());
695         }
696         sb.append(getEndTagChar());
697         return(sb.toString());
698     }
699
700     public String JavaDoc createEndTag()
701     {
702         setEndStartModifier('/');
703         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
704         sb.append(getStartTagChar());
705
706         if(getEndStartModifierDefined())
707         {
708             sb.append(getEndStartModifier());
709         }
710         sb.append(getElementType());
711
712         if(getEndEndModifierDefined())
713         {
714             sb.append(getEndEndModifier());
715         }
716         sb.append(getEndTagChar());
717         return(sb.toString());
718     }
719
720     /**
721         Override toString() method to print something meaningful
722     */

723     public final String JavaDoc toString()
724     {
725         StringWriter JavaDoc write = new StringWriter JavaDoc();
726         String JavaDoc out = null;
727         try
728         {
729             output(write);
730             write.flush();
731             out = write.toString();
732             write.close();
733         }
734         catch (UnsupportedEncodingException JavaDoc use)
735         {
736             use.printStackTrace();
737         }
738         catch (IOException JavaDoc ioe)
739         {
740             ioe.printStackTrace();
741         }
742         return(out);
743     }
744
745     /**
746         Override toString(encode) method to print something meaningful
747     */

748     public final String JavaDoc toString(String JavaDoc codeset)
749     {
750         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
751         BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(baos);
752         String JavaDoc out = null;
753         try
754         {
755             output(bos);
756             bos.flush();
757             out = baos.toString(codeset);
758             bos.close();
759             baos.close();
760         }
761         catch (UnsupportedEncodingException JavaDoc use)
762         {
763         }
764         catch (IOException JavaDoc ioe)
765         {
766         }
767         return(out);
768     }
769
770     /**
771         Add the element to the designated OutputStream.
772     */

773     public void output(OutputStream JavaDoc out)
774     {
775         String JavaDoc encoding = null;
776         if ( getCodeSet() != null )
777         {
778            encoding = getCodeSet();
779         }
780         else
781         {
782            //By default use Big Endian Unicode.
783
//In this way we will not loose any information.
784
encoding = "UTF-16BE";
785         }
786         
787         try
788         {
789             out.write(createStartTag().getBytes(encoding));
790
791             if(getFilterState())
792                 out.write(getFilter().process(getTagText()).getBytes(encoding));
793             else
794                 out.write(getTagText().getBytes(encoding));
795
796             if (getNeedClosingTag())
797                 out.write(createEndTag().getBytes(encoding));
798
799         }
800         catch (UnsupportedEncodingException JavaDoc uee)
801         {
802             uee.printStackTrace(new PrintWriter JavaDoc(out));
803         }
804         catch (IOException JavaDoc ioe)
805         {
806             ioe.printStackTrace(new PrintWriter JavaDoc(out));
807         }
808     }
809
810     /**
811         Add element to the designated PrintWriter.
812     */

813     public void output(PrintWriter JavaDoc out)
814     {
815
816         out.write(createStartTag());
817
818         if(getFilterState())
819             out.write(getFilter().process(getTagText()));
820         else
821             out.write(getTagText());
822
823         if (getNeedClosingTag())
824             out.write(createEndTag());
825
826     }
827
828     /**
829         Writer version of this method.
830     */

831     public void output(Writer JavaDoc out)
832     {
833         output ( new PrintWriter JavaDoc(out) );
834     }
835
836     /**
837         Set the tab level for pretty printing
838     */

839     public void setTabLevel(int tabLevel)
840     {
841         this.tabLevel = tabLevel;
842     }
843
844     public int getTabLevel()
845     {
846         return tabLevel;
847     }
848
849     protected void putTabs(int tabCount, OutputStream JavaDoc out)
850         throws IOException JavaDoc
851     {
852         for (int i = 0; i < tabCount; i++)
853             out.write('\t');
854     }
855
856     protected void putTabs(int tabCount, PrintWriter JavaDoc out)
857     {
858         for (int i = 0; i < tabCount; i++)
859             out.print('\t');
860     }
861
862 }
863
Popular Tags