View Javadoc

1   /**
2    * 
3    */
4   package edu.odu.cs.codeAnnotation;
5   
6   import java.io.BufferedReader;
7   import java.io.BufferedWriter;
8   import java.io.FileReader;
9   import java.io.FileWriter;
10  import java.io.IOException;
11  import java.io.StringReader;
12  import java.io.StringWriter;
13  
14  /**
15   * main driver for code2html program.
16   * 
17   * @author zeil
18   *
19   */
20  public class Listing2HTML {
21  
22      private static BufferedReader input;
23      private static BufferedWriter output;
24      private static String graphicsPath;
25      private static String cssFile;
26      private static boolean embedcss;
27      private static String footer;
28      private static String title;
29      
30      private static String defaultCSS = 
31              ".cppcode      {font-size: 12pt; font-family: monospace; white-space: nowrap; background: #C0C0C0}\n\n" +
32              ".directive    {font-weight: bold; color: #7F0000; }\n\n" +
33              ".quote        {font-style: italic; color: #00007F;}\n\n" +
34              ".comment      {font-style: italic; color: #007F00;}\n\n" +
35              ".highlighted1  {background: #ffff99}\n\n" +
36              ".highlighted2  {background: #66ccff}\n\n" +
37              ".highlighted3  {background: #cc33ff}\n\n" +
38              ".keyword      {font-weight: bold;}";
39  
40      
41      
42      private static void processArgs(String[] args) throws IOException
43      {
44          int argsNum = 0;
45          String inputFileName = null;
46          String outputFileName = null;
47          title = null;
48          footer = "";
49          embedcss = false;
50          graphicsPath = "./";
51          cssFile = null;
52          while (argsNum < args.length) {
53             if (args[argsNum].equals("-css")) {
54                ++argsNum;
55                cssFile = args[argsNum];
56                ++argsNum;
57             }
58             else if (args[argsNum].equals("-embedcss")) {
59                ++argsNum;
60                embedcss = true;
61             }
62             else if (args[argsNum].equals("-graphics")) {
63                ++argsNum;
64                graphicsPath = args[argsNum];
65                ++argsNum;
66             }
67             else if (args[argsNum].equals("-footer")) {
68                ++argsNum;
69                footer = args[argsNum];
70                ++argsNum;
71             }
72             else if (args[argsNum].equals("-title")) {
73                ++argsNum;
74                title = args[argsNum];
75                ++argsNum;
76             }
77             else if (inputFileName == null) {
78                inputFileName = args[argsNum];
79                if (title == null) {
80                   title = inputFileName;
81                   if (title.contains("/")) {
82                       int k = title.lastIndexOf('/');
83                       title = title.substring(k+1);
84                   }
85                }
86                ++argsNum;
87             } 
88             else {
89                outputFileName = args[argsNum];
90                ++argsNum;
91             }
92          }
93          if (outputFileName == null) {
94             outputFileName = inputFileName + ".html";
95          }
96  
97          input = new BufferedReader(new FileReader(inputFileName));
98          output = new BufferedWriter(new FileWriter(outputFileName)); 
99  
100     }
101     
102     public static String test (String inputText) throws IOException
103     {
104         input = new BufferedReader(new StringReader(inputText));
105         StringWriter strOutput = new StringWriter();
106         output = new BufferedWriter(strOutput);
107         ListingScanner scanner = new ListingScanner(input);
108         scanner.output = output;
109         scanner.yylex();
110         output.close();
111         return strOutput.toString();
112     }
113  
114     public static void generateHTML() throws IOException
115     {
116         ListingScanner scanner = new ListingScanner(input);
117         scanner.output = output;
118         scanner.graphicsPath = graphicsPath;
119         output.write ("<html><head>\n");
120         output.write("<title>" + title + "</title>\n");
121         if (embedcss || cssFile == null) {
122             output.write ("<style type=\"text/css\">\n<!--");
123             if (cssFile == null) {
124                 output.write(defaultCSS);
125             } else {
126                 BufferedReader in = new BufferedReader(new FileReader(cssFile));
127                 String line = in.readLine();
128                 while (line != null) {
129                     output.write(line);
130                     output.write("\n");
131                     line = in.readLine();
132                 }
133                 in.close();
134             }
135             output.write ("\n-->\n</style>\n");
136         } else {
137             output.write ("<link REL='stylesheet' href='" + cssFile + "' type='text/css'>\n");
138         }
139         output.write ("</head><body>\n");
140         output.write("<h1>" + title + "</h1>\n");
141         output.write ("<div class='cppcode'>\n");
142         scanner.yylex();
143         output.write ("</div>\n");
144         output.write (footer);
145         output.write ("</body></html>\n");
146         output.close();
147     }
148     
149     /**
150      * @param args
151      * @throws IOException 
152      */
153     public static void main(String[] args) throws IOException {
154         processArgs(args);
155         generateHTML();
156     }
157 
158 }