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