01 
02 /*
03  *  JavaDoq 1.0 - DOCUment JAVA In Source
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.javadoq.com>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package com.javadoq.html;
21 
22 import java.io.FileReader;
23 import java.io.IOException;
24 
25 import com.javadoq.SourceFile;
26 import com.javadoq.jjtree.ASTNode;
27 import com.javadoq.jjtree.JJTreeParser;
28 import com.javadoq.jjtree.JJTreeParserTokenManager;
29 import com.javadoq.jjtree.ParseException;
30 import com.javadoq.jjtree.ast.HTMLConversionVisitor;
31 import com.javadoq.jjtree.ast.SuperTypeVisitor;
32 
33 /**
34  * <p>Transforms Java, JavaCC or JJTree source files to HTML files.</p>
35  * 
36  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.javadoq.com" target="_blank">http://www.javadoq.com</a>
37  */
38 public final class DoqHTMLJavaFile extends DoqHTMLSourceFile
39 {
40     /**
41      * <p>Constructs a {@link DoqHTMLJavaFile} transformer.</p>
42      * <p>This constructor invokes its super constructor to create or open the target file,
43      * open HTML and HTML body, and write the header.</p>
44      * @param file The source file to transform.
45      * @since 1.0
46      */
47     public DoqHTMLJavaFile(SourceFile file) {
48         super(file);
49     }
50 
51     /**
52      * <p>Parses the source file and converts it to an HTML file.</p>
53      * @throws IOException A java.io.IOException.
54      * @throws ParseException A {@link ParseException}.
55      * @since 1.0
56      */
57     public final void parse() throws IOException, ParseException {
58         FileReader reader = new FileReader(file.source);
59         JJTreeParser parser = new JJTreeParser(
60                 new JJTreeParserTokenManager(this.new CharStream(reader))
61         );
62         ASTNode node = null;
63         if (file.isJava) {
64             node = parser.CompilationUnit();
65             // Need scan this twice.
66             new SuperTypeVisitor(file).visit(node, null);
67         } else if (file.isJavaCC || file.isJJTree) {
68             node = parser.javacc_input();
69         }
70         format = "%1$0" + new Integer(
71                 node.lastToken.endLine).toString().length() + "d ";
72         write(getLineNumber());
73         new HTMLConversionVisitor(this).visit(node, null);
74         reader.close();
75     }
76 }