KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > PlainTextRenderEngine


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.render;
27
28 import org.radeox.api.engine.RenderEngine;
29 import org.radeox.engine.context.BaseInitialRenderContext;
30 import org.radeox.api.engine.context.InitialRenderContext;
31 import org.radeox.api.engine.context.RenderContext;
32 import org.radeox.filter.EscapeFilter;
33 import org.radeox.filter.FilterPipe;
34 import org.radeox.filter.UrlFilter;
35 import org.radeox.filter.context.BaseFilterContext;
36 import org.radeox.filter.context.FilterContext;
37
38
39 import java.io.IOException JavaDoc;
40 import java.io.Writer JavaDoc;
41 import java.io.Reader JavaDoc;
42 import java.io.BufferedReader JavaDoc;
43
44 /**
45  * Renderengine for preformattet text.
46  *
47  * @author Matthias L. Jugel
48  * @version $Id: PlainTextRenderEngine.java 1606 2004-05-17 10:56:18Z leo $
49  */

50
51 public class PlainTextRenderEngine implements RenderEngine {
52   private InitialRenderContext initialContext;
53   protected FilterPipe fp;
54
55   public PlainTextRenderEngine() {
56     initialContext = new BaseInitialRenderContext();
57   }
58
59   protected void init() {
60     if (null == fp) {
61       fp = new FilterPipe(initialContext);
62       fp.addFilter(new EscapeFilter());
63       fp.addFilter(new UrlFilter());
64       fp.init();
65     }
66   }
67
68   public String JavaDoc getName() {
69     return "PlainText";
70   }
71
72   public String JavaDoc render(String JavaDoc content, RenderContext context) {
73     init();
74     FilterContext filterContext = new BaseFilterContext();
75     filterContext.setRenderContext(context);
76     StringBuffer JavaDoc plainText = new StringBuffer JavaDoc("<div id=\"code\"><pre>");
77     plainText.append(fp.filter(content, filterContext));
78     plainText.append("</pre></div>").toString();
79     return plainText.toString();
80   }
81
82   /**
83    * Render an input with text markup from a Reader and write the result to a writer
84    *
85    * @param in Reader to read the input from
86    * @param context Special context for the render engine, e.g. with
87    * configuration information
88    */

89   public String JavaDoc render(Reader JavaDoc in, RenderContext context) throws IOException JavaDoc {
90     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
91     BufferedReader JavaDoc inputReader = new BufferedReader JavaDoc(in);
92     String JavaDoc line;
93     while ((line = inputReader.readLine()) != null) {
94       buffer.append(line);
95     }
96     return render(buffer.toString(), context);
97   }
98
99   public void render(Writer JavaDoc out, String JavaDoc content, RenderContext context) throws IOException JavaDoc {
100     out.write(render(content, context));
101   }
102
103 }
104
Popular Tags