KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > tags > PreTag


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.forum.tags;
27
28 import javax.servlet.jsp.JspException JavaDoc;
29 import javax.servlet.jsp.JspTagException JavaDoc;
30 import javax.servlet.jsp.JspWriter JavaDoc;
31 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
32 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
33
34 public class PreTag extends BodyTagSupport JavaDoc
35 {
36     private String JavaDoc nohtml = "false";
37
38   public int doStartTag() throws JspException JavaDoc
39   {
40     return EVAL_BODY_TAG;
41   }
42
43   public int doEndTag()
44   {
45     return EVAL_PAGE;
46   }
47
48     public int doAfterBody() throws JspTagException JavaDoc
49     {
50         BodyContent JavaDoc bc = getBodyContent();
51         // get the bc as string
52
String JavaDoc query = bc.getString();
53         // clean up
54
bc.clearBody();
55
56         try
57         {
58             printPre(bodyContent.getEnclosingWriter(), query, nohtml.equals("true"));
59         }
60         catch (Exception JavaDoc e)
61         {
62             throw new JspTagException JavaDoc("PreTag exception: " + e);
63         }
64
65         return SKIP_BODY;
66     }
67
68     /*public String getNohtml()
69     {
70         return nohtml;
71     }*/

72
73     public void setNohtml(String JavaDoc nohtml)
74     {
75         this.nohtml = nohtml;
76     }
77
78     public static void printPre(JspWriter JavaDoc out, String JavaDoc s, boolean noHtml) throws java.io.IOException JavaDoc
79     {
80         if (s == null)return;
81
82         char[] sa = s.toCharArray();
83
84         for (int i = 0; i < sa.length; i++)
85         {
86             if (sa[i] == '\n')
87             {
88                 out.println("<br />");
89                 for (; i < sa.length-1 && (sa[i+1] == ' ' || sa[i+1] == '\t'); i++)
90                     out.print(sa[i+1] == '\t'? "&nbsp;&nbsp;&nbsp;" : "&nbsp;");
91             }
92             else if (i < sa.length-5 && sa[i] == '&' && sa[i+1] == 'q' && sa[i+2] == 'u' && sa[i+3] == 'o' && sa[i+4] == 't' && sa[i+5] == ';')
93             {
94                 i+=5;
95                 out.print("\"");
96             }
97             else if (sa[i] == '<')
98             {
99                 if (noHtml)
100                     out.print("&lt;");
101                 else
102                     out.print('<');
103             }
104             else if (sa[i] == '>')
105             {
106                 if (noHtml)
107                     out.print("&gt;");
108                 else
109                     out.print('>');
110             }
111             else if (sa[i] == '&')
112             {
113                 if (noHtml)
114                     out.print("&amp;");
115                 else
116                     out.print('&');
117             }
118             else
119                 out.print(sa[i]);
120         }
121     }
122 }
123
Popular Tags