1 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 ; 37 import java.io.StringWriter ; 38 import java.io.Writer ; 39 40 50 51 public class Java2HtmlCodeFilter extends JavaSource2HTMLConverter implements SourceCodeFormatter { 52 public int getPriority() { 53 return 1; 54 } 55 56 public String getName() { 57 return "njava"; 58 } 59 60 public String filter(String content, FilterContext context) { 61 StringWriter writer = new StringWriter (); 62 try { 63 JavaSource source = new JavaSource(content); 64 source.doParse(); 65 convert(writer, source); 66 } catch (IOException e) { 67 Logger.warn("JavaCodeFilter: unable to convert to html"); 68 } 69 return writer.getBuffer().toString(); 70 } 71 72 public void convert(Writer writer, JavaSource source) throws IOException { 73 setSource(source); 74 toHTML(writer); 75 } 76 77 public String getDocumentHeader() { 78 return ""; 79 } 80 81 public String getDocumentFooter() { 82 return ""; 83 } 84 85 protected void toHTML(Writer writer) throws IOException { 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 } 104 105 106 protected void toHTML(int start, int end, Writer writer) throws IOException { 107 writer.write("<font color=\"" + sourceTypes[start].getHtmlColor() + "\">"); 108 109 String t = HTMLTools.encode(sourceCode, start, end + 1); 110 writer.write(t); 111 122 writer.write("</font>"); 123 } 124 } | Popular Tags |