1 package de.java2html.converter; 2 3 import java.io.BufferedWriter ; 4 import java.io.IOException ; 5 import java.io.StringWriter ; 6 7 import de.java2html.Version; 8 import de.java2html.javasource.JavaSource; 9 import de.java2html.javasource.JavaSourceIterator; 10 import de.java2html.javasource.JavaSourceParser; 11 import de.java2html.javasource.JavaSourceRun; 12 import de.java2html.javasource.JavaSourceType; 13 import de.java2html.options.JavaSourceConversionOptions; 14 import de.java2html.options.JavaSourceStyleTable; 15 import de.java2html.util.RGB; 16 17 47 public class JavaSource2TeXConverter extends AbstractJavaSourceConverter { 48 private static String [] texFormats; 49 static { 50 JavaSourceType[] allTypes = JavaSourceType.getAll(); 51 texFormats = new String [allTypes.length]; 52 for (int i = 0; i < allTypes.length; ++i) { 53 texFormats[i] = "\\jttstyle" + (char) ('a' + i) + " "; 54 } 55 } 56 57 private String createFormatDefinition(JavaSourceStyleTable styleTable) { 58 StringBuffer sb = new StringBuffer (); 59 sb.append("%Java2TeX style definitions\n"); 60 sb.append("%You can modify them to fit your needs\n"); 61 62 JavaSourceType[] allTypes = JavaSourceType.getAll(); 63 for (int i = 0; i < allTypes.length; ++i) { 64 sb.append("\\newcommand{\\jttstyle"); 65 sb.append((char) ('a' + i)); 66 sb.append("}{\\color[rgb]{"); 67 68 RGB color = styleTable.get(allTypes[i]).getColor(); 69 float[] cs = new float[]{ color.getRed() / 255.0f, color.getGreen() / 255.0f, color.getBlue() / 255.0f, }; 70 sb.append(floatToCharArray(cs[0])); 71 sb.append(','); 72 sb.append(floatToCharArray(cs[1])); 73 sb.append(','); 74 sb.append(floatToCharArray(cs[2])); 75 76 sb.append("}} %"); 77 sb.append(allTypes[i].getName()); 78 sb.append('\n'); 79 80 } 81 sb.append('\n'); 82 return sb.toString(); 83 } 84 85 86 protected final static String DOCUMENT_HEADER = "\\documentclass[11pt,a4paper]{article}\n" 87 + "\n" 88 + "\\usepackage{color}\n" 89 + "\n" 90 + "\\begin{document}\n" 91 + "\n"; 92 93 94 protected final static String DOCUMENT_FOOTER = "\\end{document}"; 95 96 97 protected final static String DOCUMENT_BLOCK_SEPARATOR = "\n\n"; 98 99 protected final static String BLOCK_HEADER = "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" 100 + "% Java Sourcecode to TeX automatically converted code\n" 101 + "% " 102 + Version.getJava2HtmlConverterTitle() 103 + " " 104 + Version.getBuildDate() 105 + "by Markus Gebhard markus@jave.de\n" 106 + "% Further information: http://www.java2html.de\n"; 107 108 public JavaSource2TeXConverter() { 109 super(new ConverterMetaData("tex", "TeX", "tex")); 110 } 111 112 protected final static char[] floatToCharArray(float f) { 113 if (f >= 1.0) { 114 return new char[]{ '1', '.', '0', '0' }; 115 } 116 117 return new char[]{ '.', (char) ('0' + f * 10), (char) ('0' + f * 100 % 10), (char) ('0' + f * 1000 % 10) }; 118 } 119 120 public String getDocumentHeader(JavaSourceConversionOptions options, String title) { 121 return DOCUMENT_HEADER + createFormatDefinition(options.getStyleTable()); 122 } 123 124 public String getDocumentFooter(JavaSourceConversionOptions options) { 125 return DOCUMENT_FOOTER; 126 } 127 128 public String getBlockSeparator(JavaSourceConversionOptions options) { 129 return DOCUMENT_BLOCK_SEPARATOR; 130 } 131 132 137 public void convert(JavaSource source, JavaSourceConversionOptions options, BufferedWriter writer) 138 throws IOException { 139 if (source == null) { 140 throw new IllegalStateException ("Trying to write out converted code without having source set."); 141 } 142 143 writer.write(BLOCK_HEADER); 144 145 if (options.isShowFileName() && source.getFileName() != null) { 147 } 149 150 writer.write("{"); 151 writer.newLine(); 152 writer.write("\\noindent \\ttfamily"); 153 writer.newLine(); 154 155 int lineCount = source.getLineCount(); 156 int lineNumber = 1; 157 JavaSourceIterator iterator = source.getIterator(); 158 while (iterator.hasNext()) { 159 JavaSourceRun run = iterator.getNext(); 160 if (run.isAtStartOfLine() && options.isShowLineNumbers()) { 161 writeLineNumber(writer, lineNumber++, lineCount); 162 } 163 toTeX(run, writer); 164 if (run.isAtEndOfLine()) { 165 writer.write("\\\\"); 166 writer.newLine(); 167 } 168 } 169 170 writer.newLine(); 171 writer.write("}"); 172 writer.newLine(); 173 } 174 175 public void writeLineNumber(BufferedWriter writer, int lineNumber, int lineCount) throws IOException { 176 writer.write(texFormats[JavaSourceType.LINE_NUMBERS.getID()]); 177 writer.write(leftSpace(lineNumber, lineCount)); 178 writer.write(String.valueOf(lineNumber)); 179 writer.write('~'); 180 } 181 182 protected void toTeX(JavaSourceRun run, BufferedWriter writer) throws IOException { 183 writer.write(texFormats[run.getType().getID()]); 184 185 String text = run.getCode(); 188 for (int i = 0; i < text.length(); ++i) { 189 char ch = text.charAt(i); 190 if (ch == ' ') 191 writer.write('~'); 192 else if (ch == '_' 193 || ch == '\\' 194 || ch == '^' 195 || ch == '~' 196 || ch == '\"' 197 || ch == '|' 198 || ch == '<' 199 || ch == '>' 200 || ch == '*') 201 writer.write("\\verb#" + ch + "#"); 202 else if (ch == '{' || ch == '}' || ch == '_' || ch == '&' || ch == '%' || ch == '$' || ch == '#') { 203 writer.write("\\" + ch); 204 } 205 else { 206 writer.write(ch); 207 } 208 } 209 } 210 211 protected final static String [] WHITESPACES = { "", "~", "~~", "~~~", "~~~~", }; 212 213 protected final static String whiteSpace(int size) { 214 if (size < WHITESPACES.length) 215 return WHITESPACES[size]; 216 217 char[] result = new char[size]; 218 while (size > 0) 219 result[--size] = '~'; 220 221 return new String (result); 222 } 223 224 protected final static String leftSpace(int num, int max) { 225 int count = (int) (Math.log(max) / Math.log(10)) - (int) (Math.log(num) / Math.log(10)); 226 227 return whiteSpace(count); 228 } 229 230 public static void main(String args[]) throws IOException { 232 long time0 = System.currentTimeMillis(); 233 JavaSource source = (new JavaSourceParser()).parse(new java.io.File ("JavaSourceParser.java")); 234 235 long time1 = System.currentTimeMillis(); 236 JavaSource2TeXConverter conn1 = new JavaSource2TeXConverter(); 237 conn1.convert(source, JavaSourceConversionOptions.getDefault(), new StringWriter ()); 238 long time2 = System.currentTimeMillis(); 239 JavaSource2HTMLConverter conn2 = new JavaSource2HTMLConverter(); 240 conn2.convert(source, JavaSourceConversionOptions.getDefault(), new StringWriter ()); 241 long time3 = System.currentTimeMillis(); 242 243 System.out.println("Parse: " + (time1 - time0) + "ms"); 244 System.out.println("toTeX: " + (time2 - time1) + "ms"); 245 System.out.println("toHTML: " + (time3 - time2) + "ms"); 246 } 247 } | Popular Tags |