1 package de.java2html.converter; 2 3 import java.io.BufferedWriter ; 4 import java.io.IOException ; 5 import java.text.MessageFormat ; 6 7 import de.java2html.Version; 8 import de.java2html.javasource.JavaSource; 9 import de.java2html.javasource.JavaSourceIterator; 10 import de.java2html.javasource.JavaSourceRun; 11 import de.java2html.javasource.JavaSourceType; 12 import de.java2html.options.HorizontalAlignment; 13 import de.java2html.options.IHorizontalAlignmentVisitor; 14 import de.java2html.options.JavaSourceConversionOptions; 15 import de.java2html.options.JavaSourceStyleEntry; 16 import de.java2html.options.JavaSourceStyleTable; 17 import de.java2html.util.HtmlUtilities; 18 import de.java2html.util.StringHolder; 19 20 51 public class JavaSource2HTMLConverter extends AbstractJavaSourceConverter { 52 59 public static boolean java2HtmlHomepageLinkEnabled = false; 60 61 65 private final static String HTML_SITE_HEADER = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" 66 + "<html><head>\n" 69 + "<title>{0}</title>\n" 70 + " <style type=\"text/css\">\n" 71 + " <!--code '{' font-family: Courier New, Courier; font-size: 10pt; margin: 0px; '}'-->\n" 72 + " </style>\n" 73 + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n" 74 + "</head><body>\n"; 75 76 80 private final static String HTML_SITE_FOOTER = "\n</body></html>"; 81 82 83 private final static String HTML_BLOCK_SEPARATOR = "<p />\n"; 84 85 86 private final static String HTML_BLOCK_HEADER = "\n\n" 87 + "<!-- ======================================================== -->\n" 88 + "<!-- = Java Sourcecode to HTML automatically converted code = -->\n" 89 + "<!-- = " 90 + Version.getJava2HtmlConverterTitle() 91 + " " 92 + Version.getBuildDate() 93 + " by Markus Gebhard markus@jave.de = -->\n" 94 + "<!-- = Further information: http://www.java2html.de = -->\n" 95 + "<div align=\"{0}\" class=\"java\">\n" 96 + "<table border=\"{1}\" cellpadding=\"3\" " 97 + "cellspacing=\"0\" bgcolor=\"{2}\">\n"; 98 99 100 private final static String HTML_HEAD_START = " <!-- start headline -->\n" 101 + " <tr>\n" 102 + " <td colspan=\"2\">\n" 103 + " <center><font size=\"+2\">\n" 104 + " <code><b>\n"; 105 106 107 private final static String HTML_HEAD_END = " </b></code>\n" 108 + " </font></center>\n" 109 + " </td>\n" 110 + " </tr>\n" 111 + " <!-- end headline -->\n"; 112 113 117 private final static String HTML_COL2_START = " <!-- start source code -->\n" 118 + " <td nowrap=\"nowrap\" valign=\"top\" align=\"left\">\n" 119 + " <code>\n"; 120 121 125 private final static String HTML_COL2_END = "</code>\n" 126 + " \n" 127 + " </td>\n" 128 + " <!-- end source code -->\n"; 129 130 135 private final static String HTML_LINK = " <!-- start Java2Html link -->\n" 136 + " <tr>\n" 137 + " <td align=\"right\">\n" 138 + "<small>\n" 139 + "<a HREF=\"http://www.java2html.de\" target=\"_blank\">Java2html</a>\n" 140 + "</small>\n" 141 + " </td>\n" 142 + " </tr>\n" 143 + " <!-- end Java2Html link -->\n"; 144 145 146 private final static String HTML_BLOCK_FOOTER = 147 "</table>\n" 149 + "</div>\n" 150 + "<!-- = END of automatically generated HTML code = -->\n" 151 + "<!-- ======================================================== -->\n\n"; 152 153 156 157 private int lineCifferCount; 158 159 public JavaSource2HTMLConverter() { 160 super(new ConverterMetaData("html", "XHTML 1.0 Transitional (inlined fonts)", "html")); 161 } 162 163 public String getDocumentHeader(JavaSourceConversionOptions options, String title) { 164 if (title == null) { 165 title = ""; } 167 return MessageFormat.format(HTML_SITE_HEADER, new Object []{ title }); 168 } 169 170 public String getDocumentFooter(JavaSourceConversionOptions options) { 171 return HTML_SITE_FOOTER; 172 } 173 174 public String getBlockSeparator(JavaSourceConversionOptions options) { 175 return HTML_BLOCK_SEPARATOR; 176 } 177 178 public void convert(JavaSource source, JavaSourceConversionOptions options, BufferedWriter writer) 179 throws IOException { 180 if (source == null) { 181 throw new IllegalStateException ("Trying to write out converted code without having source set."); 182 } 183 184 String alignValue = getHtmlAlignValue(options.getHorizontalAlignment()); 186 String bgcolorValue = options.getStyleTable().get(JavaSourceType.BACKGROUND).getHtmlColor(); 187 String borderValue = options.isShowTableBorder() ? "2" : "0"; 188 189 writer.write(MessageFormat.format(HTML_BLOCK_HEADER, new Object []{ alignValue, borderValue, bgcolorValue })); 190 191 if (options.isShowFileName() && source.getFileName() != null) { 192 writeFileName(source, writer); 193 } 194 195 writer.write(" <tr>"); 196 writer.newLine(); 197 198 writeSourceCode(source, options, writer); 199 200 writer.write(" </tr>"); 201 writer.newLine(); 202 203 if (options.isShowJava2HtmlLink() || java2HtmlHomepageLinkEnabled) { 205 writer.write(HTML_LINK); 206 } 207 writer.write(HTML_BLOCK_FOOTER); 208 } 209 210 private String getHtmlAlignValue(HorizontalAlignment alignment) { 211 final StringHolder stringHolder = new StringHolder(); 212 alignment.accept(new IHorizontalAlignmentVisitor() { 213 public void visitLeftAlignment(HorizontalAlignment horizontalAlignment) { 214 stringHolder.setValue("left"); 215 } 216 217 public void visitRightAlignment(HorizontalAlignment horizontalAlignment) { 218 stringHolder.setValue("right"); 219 } 220 221 public void visitCenterAlignment(HorizontalAlignment horizontalAlignment) { 222 stringHolder.setValue("center"); 223 } 224 }); 225 return stringHolder.getValue(); 226 } 227 228 private void writeFileName(JavaSource source, BufferedWriter writer) throws IOException { 229 writer.write(HTML_HEAD_START); 230 writer.write(source.getFileName()); 231 writer.newLine(); 232 writer.write(HTML_HEAD_END); 233 } 234 235 private void writeSourceCode(JavaSource source, JavaSourceConversionOptions options, BufferedWriter writer) 236 throws IOException { 237 writer.write(HTML_COL2_START); 238 239 lineCifferCount = String.valueOf(source.getLineCount()).length(); 240 241 JavaSourceIterator iterator = source.getIterator(); 242 int lineNumber = 1; 243 while (iterator.hasNext()) { 244 JavaSourceRun run = iterator.getNext(); 245 246 if (run.isAtStartOfLine()) { 247 if (options.isAddLineAnchors()) { 248 writeLineAnchorStart(options, writer, lineNumber); 249 } 250 if (options.isShowLineNumbers()) { 251 writeLineNumber(options, writer, lineNumber); 252 } 253 if (options.isAddLineAnchors()) { 254 writeLineAnchorEnd(writer); 255 } 256 lineNumber++; 257 } 258 259 toHTML(options.getStyleTable(), run, writer); 260 if (run.isAtEndOfLine() && iterator.hasNext()) { 261 writer.write("<br />"); 262 writer.newLine(); 263 } 264 } 265 writer.write(HTML_COL2_END); 266 } 267 268 private void writeLineAnchorEnd(BufferedWriter writer) throws IOException { 269 writer.write("</a>"); 270 } 271 272 private void writeLineAnchorStart(JavaSourceConversionOptions options, BufferedWriter writer, int lineNumber) 273 throws IOException { 274 writer.write("<a name=\""); 275 writer.write(options.getLineAnchorPrefix() + lineNumber); 276 writer.write("\">"); 277 } 278 279 private void writeLineNumber(JavaSourceConversionOptions options, BufferedWriter writer, int lineNo) 280 throws IOException { 281 JavaSourceStyleEntry styleEntry = options.getStyleTable().get(JavaSourceType.LINE_NUMBERS); 282 writeStyleStart(writer, styleEntry); 283 284 String lineNumber = String.valueOf(lineNo); 285 int cifferCount = lineCifferCount - lineNumber.length(); 286 while (cifferCount > 0) { 287 writer.write('0'); 288 --cifferCount; 289 } 290 291 writer.write(lineNumber); 292 writeStyleEnd(writer, styleEntry); 293 writer.write(" "); 294 } 295 296 private void toHTML(JavaSourceStyleTable styleTable, JavaSourceRun run, BufferedWriter writer) 297 throws IOException { 298 JavaSourceStyleEntry style = styleTable.get(run.getType()); 300 301 writeStyleStart(writer, style); 302 303 String t = HtmlUtilities.encode(run.getCode(), "\n "); 304 305 for (int i = 0; i < t.length(); ++i) { 306 char ch = t.charAt(i); 307 if (ch == ' ') { 308 writer.write(" "); 309 } 310 else { 311 writer.write(ch); 312 } 313 } 314 315 writeStyleEnd(writer, style); 316 } 317 318 private void writeStyleStart(BufferedWriter writer, JavaSourceStyleEntry style) throws IOException { 319 writer.write("<font color=\"" + style.getHtmlColor() + "\">"); 320 if (style.isBold()) { 321 writer.write("<b>"); 322 } 323 if (style.isItalic()) { 324 writer.write("<i>"); 325 } 326 } 327 328 private void writeStyleEnd(BufferedWriter writer, JavaSourceStyleEntry style) throws IOException { 329 if (style.isItalic()) { 330 writer.write("</i>"); 331 } 332 if (style.isBold()) { 333 writer.write("</b>"); 334 } 335 writer.write("</font>"); 336 } 337 } | Popular Tags |