KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > converter > JavaSource2HTMLConverter


1 package de.java2html.converter;
2
3 import java.io.BufferedWriter JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.text.MessageFormat JavaDoc;
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 /**
21  * Algorithm and stuff for converting a
22  * {@link de.java2html.javasource.JavaSource} object to to a HTML string
23  * representation.
24  *
25  * The result is XHTML1.0 Transitional compliant.
26  *
27  * For questions, suggestions, bug-reports, enhancement-requests etc. I may be
28  * contacted at: <a HREF="mailto:markus@jave.de">markus@jave.de</a>
29  *
30  * The Java2html home page is located at: <a HREF="http://www.java2html.de">
31  * http://www.java2html.de</a>
32  *
33  * @author <a HREF="mailto:markus@jave.de">Markus Gebhard</a>
34  *
35  * Copyright (C) Markus Gebhard 2000-2002
36  *
37  * This program is free software; you can redistribute it and/or modify it
38  * under the terms of the GNU General Public License as published by the Free
39  * Software Foundation; either version 2 of the License, or (at your option)
40  * any later version.
41  *
42  * This program is distributed in the hope that it will be useful, but WITHOUT
43  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
44  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
45  * more details.
46  *
47  * You should have received a copy of the GNU General Public License along with
48  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
49  * Place - Suite 330, Boston, MA 02111-1307, USA.
50  */

51 public class JavaSource2HTMLConverter extends AbstractJavaSourceConverter {
52   /**
53    * Flag indication whether html output contains a link to the
54    * Java2Html-Homepage or not.
55    *
56    * @deprecated As of Jan 2, 2004 (Markus Gebhard) replaced by
57    * {@link de.java2html.options.JavaSourceConversionOptions#setShowJava2HtmlLink(boolean)}
58    */

59   public static boolean java2HtmlHomepageLinkEnabled = false;
60
61   /**
62    * Site header for a html page. Is not used by this class, but can be used
63    * from outside to add it to one or more converted
64    */

65   private final static String JavaDoc 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       // "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
67
// \"http://www.w3.org/TR/html4/loose.dtd\">\n"
68
+ "<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   /**
77    * Site footer for a html page. Is not used by this class, but can be used
78    * from outside to add it to one or more converted
79    */

80   private final static String JavaDoc HTML_SITE_FOOTER = "\n</body></html>";
81
82   /** Block seperator for between two blocks of converted source code */
83   private final static String JavaDoc HTML_BLOCK_SEPARATOR = "<p />\n";
84
85   /** HTML-Header for a block (!) of converted code */
86   private final static String JavaDoc 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   /** HTML-code for before the headline */
100   private final static String JavaDoc 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   /** HTML-code for after the headline */
107   private final static String JavaDoc HTML_HEAD_END = " </b></code>\n"
108       + " </font></center>\n"
109       + " </td>\n"
110       + " </tr>\n"
111       + " <!-- end headline -->\n";
112
113   /**
114    * HTML-code for before the second column (contaning the converted source
115    * code)
116    */

117   private final static String JavaDoc HTML_COL2_START = " <!-- start source code -->\n"
118       + " <td nowrap=\"nowrap\" valign=\"top\" align=\"left\">\n"
119       + " <code>\n";
120
121   /**
122    * HTML-code for after the second column (contaning the converted source
123    * code)
124    */

125   private final static String JavaDoc HTML_COL2_END = "</code>\n"
126       + " \n"
127       + " </td>\n"
128       + " <!-- end source code -->\n";
129
130   /**
131    * HTML-code for the bottom line (containing a little link to the
132    * java2html-homepage). Can be disabled by setting
133    * {@link #java2HtmlHomepageLinkEnabled}
134    */

135   private final static String JavaDoc 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   /** HTML-code for after the end of the block */
146   private final static String JavaDoc HTML_BLOCK_FOOTER =
147   //"\n"+ sieht schoener aus, wenn kein Zeilenumbruch mehr!
148
"</table>\n"
149       + "</div>\n"
150       + "<!-- = END of automatically generated HTML code = -->\n"
151       + "<!-- ======================================================== -->\n\n";
152
153   /**
154    * The html representation of the colors used for different source
155    */

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 JavaDoc getDocumentHeader(JavaSourceConversionOptions options, String JavaDoc title) {
164     if (title == null) {
165       title = ""; //$NON-NLS-1$
166
}
167     return MessageFormat.format(HTML_SITE_HEADER, new Object JavaDoc[]{ title });
168   }
169
170   public String JavaDoc getDocumentFooter(JavaSourceConversionOptions options) {
171     return HTML_SITE_FOOTER;
172   }
173
174   public String JavaDoc getBlockSeparator(JavaSourceConversionOptions options) {
175     return HTML_BLOCK_SEPARATOR;
176   }
177
178   public void convert(JavaSource source, JavaSourceConversionOptions options, BufferedWriter JavaDoc writer)
179       throws IOException JavaDoc {
180     if (source == null) {
181       throw new IllegalStateException JavaDoc("Trying to write out converted code without having source set.");
182     }
183
184     //Header
185
String JavaDoc alignValue = getHtmlAlignValue(options.getHorizontalAlignment());
186     String JavaDoc bgcolorValue = options.getStyleTable().get(JavaSourceType.BACKGROUND).getHtmlColor();
187     String JavaDoc borderValue = options.isShowTableBorder() ? "2" : "0";
188
189     writer.write(MessageFormat.format(HTML_BLOCK_HEADER, new Object JavaDoc[]{ 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     //5) Footer with link to web site
204
if (options.isShowJava2HtmlLink() || java2HtmlHomepageLinkEnabled) {
205       writer.write(HTML_LINK);
206     }
207     writer.write(HTML_BLOCK_FOOTER);
208   }
209
210   private String JavaDoc 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 JavaDoc writer) throws IOException JavaDoc {
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 JavaDoc writer)
236       throws IOException JavaDoc {
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 JavaDoc writer) throws IOException JavaDoc {
269     writer.write("</a>");
270   }
271
272   private void writeLineAnchorStart(JavaSourceConversionOptions options, BufferedWriter JavaDoc writer, int lineNumber)
273       throws IOException JavaDoc {
274     writer.write("<a name=\"");
275     writer.write(options.getLineAnchorPrefix() + lineNumber);
276     writer.write("\">");
277   }
278
279   private void writeLineNumber(JavaSourceConversionOptions options, BufferedWriter JavaDoc writer, int lineNo)
280       throws IOException JavaDoc {
281     JavaSourceStyleEntry styleEntry = options.getStyleTable().get(JavaSourceType.LINE_NUMBERS);
282     writeStyleStart(writer, styleEntry);
283
284     String JavaDoc 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("&nbsp;");
294   }
295
296   private void toHTML(JavaSourceStyleTable styleTable, JavaSourceRun run, BufferedWriter JavaDoc writer)
297       throws IOException JavaDoc {
298     // result.append(htmlColors[sourceTypes[start]]);
299
JavaSourceStyleEntry style = styleTable.get(run.getType());
300
301     writeStyleStart(writer, style);
302
303     String JavaDoc 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("&nbsp;");
309       }
310       else {
311         writer.write(ch);
312       }
313     }
314
315     writeStyleEnd(writer, style);
316   }
317
318   private void writeStyleStart(BufferedWriter JavaDoc writer, JavaSourceStyleEntry style) throws IOException JavaDoc {
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 JavaDoc writer, JavaSourceStyleEntry style) throws IOException JavaDoc {
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