KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > macro > code > Java2HtmlCodeFilter


1 /*
2  * This file is part of "SnipSnap Radeox Rendering Engine".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://radeox.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library 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 GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.render.macro.code;
27
28 import de.java2html.converter.JavaSource;
29 import de.java2html.converter.JavaSource2HTMLConverter;
30 import de.java2html.converter.JavaSourceType;
31 import de.java2html.util.HTMLTools;
32 import org.radeox.macro.code.SourceCodeFormatter;
33 import org.radeox.util.logging.Logger;
34 import org.radeox.filter.context.FilterContext;
35
36 import java.io.IOException JavaDoc;
37 import java.io.StringWriter JavaDoc;
38 import java.io.Writer JavaDoc;
39
40 /*
41  * Java2HtmlCodeFilter colourizes Java source code. Uses the Java2HTML library
42  * from http://www.java2html.de
43  *
44  * Original by MelamedZ
45  *
46  * @author stephan
47  * @team sonicteam
48  * @version $Id: Java2HtmlCodeFilter.java 1031 2003-10-14 08:41:58Z stephan $
49  */

50
51 public class Java2HtmlCodeFilter extends JavaSource2HTMLConverter implements SourceCodeFormatter {
52   public int getPriority() {
53     return 1;
54   }
55
56   public String JavaDoc getName() {
57     return "njava";
58   }
59
60   public String JavaDoc filter(String JavaDoc content, FilterContext context) {
61     StringWriter JavaDoc writer = new StringWriter JavaDoc();
62     try {
63       JavaSource source = new JavaSource(content);
64       source.doParse();
65       convert(writer, source);
66     } catch (IOException JavaDoc e) {
67       Logger.warn("JavaCodeFilter: unable to convert to html");
68     }
69     return writer.getBuffer().toString();
70   }
71
72   public void convert(Writer JavaDoc writer, JavaSource source) throws IOException JavaDoc {
73     setSource(source);
74     toHTML(writer);
75   }
76
77   public String JavaDoc getDocumentHeader() {
78     return "";
79   }
80
81   public String JavaDoc getDocumentFooter() {
82     return "";
83   }
84
85   protected void toHTML(Writer JavaDoc writer) throws IOException JavaDoc {
86     sourceCode = source.getCode();
87     sourceTypes = source.getClassification();
88
89     int start = 0;
90     int end = 0;
91
92     while (start < sourceTypes.length) {
93
94       while (end < sourceTypes.length - 1
95           && (sourceTypes[end + 1] == sourceTypes[start] || sourceTypes[end + 1] == JavaSourceType.EMPTY)) {
96         ++end;
97       }
98       toHTML(start, end, writer);
99       start = end + 1;
100       end = start;
101     }
102     //writer.write("{link:java2html|http://www.java2html.de/|img=\"none\"}");
103
}
104
105
106   protected void toHTML(int start, int end, Writer JavaDoc writer) throws IOException JavaDoc {
107     writer.write("<font color=\"" + sourceTypes[start].getHtmlColor() + "\">");
108
109     String JavaDoc t = HTMLTools.encode(sourceCode, start, end + 1);
110     writer.write(t);
111 // //Replace white space by non-breaking space and line breaks by <br>
112
// for (int i = 0; i < t.length(); ++i) {
113
// char ch = t.charAt(i);
114
// if (ch == ' ')
115
// writer.write("&nbsp;");
116
// else if (ch == '\n')
117
// writer.write("<br>\n");
118
// else
119
// writer.write(ch);
120
// }
121

122     writer.write("</font>");
123   }
124 }
Popular Tags