KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > format > render > bbcodehtml > ToHTMLRenderer


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9
10 package org.jboss.portal.format.render.bbcodehtml;
11
12 import org.jboss.portal.format.parser.ParseEvent;
13 import org.jboss.portal.format.parser.ParseEventHandler;
14 import org.jboss.portal.format.parser.TextEvent;
15 import org.jboss.portal.format.parser.bbcode.BBCodeParser;
16 import org.jboss.portal.format.parser.entity.CharacterEntityReferenceEvent;
17 import org.jboss.portal.format.parser.entity.HTML4_0CharacterEntityReferenceParser;
18 import org.jboss.portal.format.parser.entity.XML1_0CharacterEntityReferenceParser;
19 import org.jboss.portal.format.parser.html.TagParser;
20 import org.jboss.portal.format.parser.linebreak.LineBreakParser;
21 import org.jboss.portal.format.render.AbstractRenderer;
22 import org.jboss.portal.format.template.DelegateContext;
23
24
25 /**
26  *
27  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
28  * @version $Revision: 1.1.1.1 $
29  */

30 public class ToHTMLRenderer extends AbstractRenderer
31 {
32
33    /*
34     * Different parsers.
35     */

36
37    private final BBCodeParser codeParser = new BBCodeParser();
38    private final TagParser tagParser = new TagParser();
39    private final XML1_0CharacterEntityReferenceParser XMLCERP = new XML1_0CharacterEntityReferenceParser();
40    private final HTML4_0CharacterEntityReferenceParser HTMLCERP = new HTML4_0CharacterEntityReferenceParser();
41    private final LineBreakParser lineBreakParser = new LineBreakParser();
42
43    /** Context used by the code handler templates. */
44    private final DelegateContext ctx = new DelegateContext();
45
46    /** Code depth. */
47    private int isInPre = 0;
48
49    /** Config. */
50    private ToHTMLConfig config;
51
52    /*
53     * Templates.
54     */

55
56    /**
57     * Setup the renderer with the given template repository.
58     */

59    public ToHTMLRenderer(ToHTMLConfig config)
60    {
61       this.config = config;
62       
63       codeParser.setHandler(bbCodeHandler);
64       tagParser.setHandler(tagHandler);
65       XMLCERP.setHandler(XMLCRPHandler);
66       HTMLCERP.setHandler(HTMLCRPHandler);
67       lineBreakParser.setHandler(lineBreakHandler);
68       ctx.put("L_CODE", "${bb.Code}");
69       ctx.put("L_QUOTE", "${bb.Quote}");
70       ctx.put("L_WROTE", "");
71 // ctx.put("L_WROTE", "${bb.wrote}");
72
}
73    
74    public ToHTMLRenderer()
75    {
76       this(new ToHTMLConfig());
77    }
78
79    public ToHTMLConfig getConfig()
80    {
81       return config;
82    }
83
84    public void setConfig(ToHTMLConfig config)
85    {
86       this.config = config;
87    }
88
89    public void render(char[] chars, int offset, int length)
90    {
91       codeParser.parse(chars, offset, length);
92    }
93
94    public ParseEventHandler bbCodeHandler = new ParseEventHandler()
95    {
96       public void handle(ParseEvent event)
97       {
98          if (event instanceof BBCodeParser.OpenEvent)
99          {
100             BBCodeParser.OpenEvent openEvent = (BBCodeParser.OpenEvent)event;
101             String JavaDoc string = openEvent.getString();
102             switch (openEvent.getType())
103             {
104             case BBCodeParser.EVENT_BOLD:
105                config.r_b_open.render(ctx, writer);
106                break;
107             case BBCodeParser.EVENT_ITALIC:
108                config.r_i_open.render(ctx, writer);
109                break;
110             case BBCodeParser.EVENT_UNDERLINE:
111                config.r_u_open.render(ctx, writer);
112                break;
113             case BBCodeParser.EVENT_COLOR:
114                ctx.put("COLOR", string);
115                config.r_color_open.render(ctx, writer);
116                break;
117             case BBCodeParser.EVENT_SIZE:
118                ctx.put("SIZE", string);
119                config.r_size_open.render(ctx, writer);
120                break;
121             case BBCodeParser.EVENT_QUOTE:
122                if (string == null)
123                {
124                   config.r_quote_open.render(ctx, writer);
125                }
126                else
127                {
128                   ctx.put("USERNAME", string);
129                   config.r_quote_username_open.render(ctx, writer);
130                }
131                break;
132             case BBCodeParser.EVENT_CODE:
133                isInPre++;
134                config.r_code_open.render(ctx, writer);
135                break;
136             case BBCodeParser.EVENT_UNORDERED_LIST:
137                config.r_ulist_open.render(ctx, writer);
138                break;
139             case BBCodeParser.EVENT_NUMERICALLY_ORDERED_LIST:
140                ctx.put("LIST_TYPE", "1");
141                config.r_olist_open.render(ctx, writer);
142                break;
143             case BBCodeParser.EVENT_ALPHABETICALLY_ORDERED_LIST:
144                ctx.put("LIST_TYPE", "a");
145                config.r_olist_open.render(ctx, writer);
146                break;
147             case BBCodeParser.EVENT_ITEM:
148                config.r_listitem.render(ctx, writer);
149                break;
150             case BBCodeParser.EVENT_LINK:
151                ctx.put("URL", string);
152                ctx.put("DESCRIPTION", string);
153                config.r_url.render(ctx, writer);
154                break;
155             }
156          }
157          else if (event instanceof BBCodeParser.CloseEvent)
158          {
159             BBCodeParser.CloseEvent closeEvent = (BBCodeParser.CloseEvent)event;
160             switch (closeEvent.getType())
161             {
162             case BBCodeParser.EVENT_BOLD:
163                config.r_b_close.render(ctx, writer);
164                break;
165             case BBCodeParser.EVENT_ITALIC:
166                config.r_i_close.render(ctx, writer);
167                break;
168             case BBCodeParser.EVENT_UNDERLINE:
169                config.r_u_close.render(ctx, writer);
170                break;
171             case BBCodeParser.EVENT_COLOR:
172                config.r_color_close.render(ctx, writer);
173                break;
174             case BBCodeParser.EVENT_SIZE:
175                config.r_size_close.render(ctx, writer);
176                break;
177             case BBCodeParser.EVENT_QUOTE:
178                config.r_quote_close.render(ctx, writer);
179                break;
180             case BBCodeParser.EVENT_CODE:
181                isInPre--;
182                config.r_code_close.render(ctx, writer);
183                break;
184             case BBCodeParser.EVENT_UNORDERED_LIST:
185                config.r_ulist_close.render(ctx, writer);
186                break;
187             case BBCodeParser.EVENT_NUMERICALLY_ORDERED_LIST:
188                config.r_olist_close.render(ctx, writer);
189                break;
190             case BBCodeParser.EVENT_ALPHABETICALLY_ORDERED_LIST:
191                config.r_olist_close.render(ctx, writer);
192                break;
193             case BBCodeParser.EVENT_ITEM:
194                break;
195             case BBCodeParser.EVENT_LINK:
196                break;
197             }
198          }
199          else
200          {
201             TextEvent textEvent = (TextEvent)event;
202             if (isInPre > 0)
203             {
204                XMLCERP.parse(textEvent.chars(), textEvent.offset(), textEvent.length());
205             }
206             else
207             {
208                tagParser.parse(textEvent.chars(), textEvent.offset(), textEvent.length());
209             }
210          }
211       }
212    };
213
214    public ParseEventHandler tagHandler = new ParseEventHandler()
215    {
216       public void handle(ParseEvent event)
217       {
218          if (event instanceof TagParser.OpenEvent)
219          {
220             TagParser.OpenEvent openEvent = (TagParser.OpenEvent)event;
221             switch (openEvent.getType())
222             {
223             case TagParser.EVENT_TAG:
224                String JavaDoc tag = openEvent.getTag();
225                if (config.print(tag))
226                {
227                   if ("pre".equals(tag))
228                   {
229                      isInPre++;
230                   }
231                   write("<");
232                   write(tag);
233                   write(openEvent.getAttribute());
234                   write(">");
235                }
236                else if (config.getOuputMode() == ToHTMLConfig.OUTPUT_MODE_DISPLAY)
237                {
238                   write("&lt;");
239                   write(tag);
240                   write(openEvent.getAttribute());
241                   write("&gt;");
242                }
243                break;
244             }
245          }
246          else if (event instanceof TagParser.CloseEvent)
247          {
248             TagParser.CloseEvent closeEvent = (TagParser.CloseEvent)event;
249             switch (closeEvent.getType())
250             {
251             case TagParser.EVENT_TAG:
252                String JavaDoc tag = closeEvent.getTag();
253                if (config.print(tag))
254                {
255                   if ("pre".equals(tag))
256                   {
257                      isInPre--;
258                   }
259                   write("</");
260                   write(tag);
261                   write(">");
262                }
263                else if (config.getOuputMode() == ToHTMLConfig.OUTPUT_MODE_DISPLAY)
264                {
265                   write("&lt;/");
266                   write(tag);
267                   write("&gt;");
268                }
269                break;
270             }
271          }
272          else
273          {
274             TextEvent textEvent = (TextEvent)event;
275             XMLCERP.parse(textEvent.chars(), textEvent.offset(), textEvent.length());
276          }
277       }
278    };
279
280    public ParseEventHandler XMLCRPHandler = new ParseEventHandler()
281    {
282       public void handle(ParseEvent event)
283       {
284          if (event instanceof CharacterEntityReferenceEvent)
285          {
286             CharacterEntityReferenceEvent cere = (CharacterEntityReferenceEvent)event;
287             lineBreakParser.parse(cere.chars(), cere.offset(), cere.length());
288          }
289          else
290          {
291             TextEvent te = (TextEvent)event;
292             HTMLCERP.parse(te.chars(), te.offset(), te.length());
293          }
294       }
295    };
296
297    public ParseEventHandler HTMLCRPHandler = new ParseEventHandler()
298    {
299       public void handle(ParseEvent event)
300       {
301          TextEvent textEvent = (TextEvent)event;
302          lineBreakParser.parse(textEvent.chars(), textEvent.offset(), textEvent.length());
303       }
304    };
305
306    public ParseEventHandler lineBreakHandler = new ParseEventHandler()
307    {
308       public void handle(ParseEvent event)
309       {
310          if (event instanceof TextEvent)
311          {
312             TextEvent textEvent = (TextEvent)event;
313             if (isInPre == 0)
314             {
315                write(textEvent.chars(), textEvent.offset(), textEvent.length());
316             }
317             else
318             {
319                // Break the content in small chunks
320
char[] chars = textEvent.chars();
321                int offset = textEvent.offset();
322                int length = textEvent.length();
323                int maxSize = config.getMaxTextWidth();
324                while (length > maxSize)
325                {
326                   write(chars, offset, maxSize);
327                   length -= maxSize;
328                   offset += maxSize;
329                   write("\n");
330                }
331                // Ouput the last chunk
332
write(chars, offset, length);
333             }
334          }
335          else
336          {
337             if (isInPre == 0)
338             {
339                write("<br/>");
340             }
341             else
342             {
343                write("\n");
344             }
345          }
346       }
347    };
348 }
349
Popular Tags