001 
002 /*
003  *  JavaDoq 1.0 - DOCUment JAVA In Source
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.javadoq.com>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package com.javadoq.html;
021 
022 import java.io.FileWriter;
023 import java.io.IOException;
024 
025 /**
026  * <p>An abstract base class for Java source code transformers.</p>
027  * 
028  * @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>
029  */
030 public abstract class Doq
031 {
032     /**
033      * <p>A line-separator.</p>
034      * @since 1.0
035      */
036     protected static final String LINE_SEPARATOR = System.getProperty("line.separator");
037     /**
038      * <p>Cascade style sheet.</p>
039      * @since 1.0
040      */
041     protected static final String STYLE_SHEET = "stylesheet.css";
042 
043     /**
044      * <p>Absolute path of the source file to transform.</p>
045      * @since 1.0
046      */
047     protected final String fileName;
048     /**
049      * <p>A FileWriter to the target file.</p>
050      * @since 1.0
051      */
052     protected final FileWriter fileWriter;
053 
054     /**
055      * <p>Constructs a {@link Doq} transformer, creates and opens the target file.</p>
056      * @param fileName The absolute path of the source file to transform.
057      * @since 1.0
058      */
059     protected Doq(String fileName) {
060         this.fileName = fileName;
061         try {
062             System.out.println("Creating: " + fileName);
063             this.fileWriter = new FileWriter(fileName);
064         } catch (IOException e) {
065             throw new RuntimeException(e);
066         }
067     }
068 
069     /**
070      * <p>Closes the target file.</p>
071      * @since 1.0
072      */
073     public void close() {
074         try {
075             fileWriter.close();
076         } catch (IOException e) {
077             throw new RuntimeException(e);
078         }
079     }
080 
081     /**
082      * <p>Writes a char to the target file.</p>
083      * @param c A char
084      * @since 1.0
085      */
086     public final void write(char c) {
087         try {
088             fileWriter.write(c);
089         } catch (IOException e) {
090             throw new RuntimeException(e);
091         }
092     }
093 
094     /**
095      * <p>Writes a string to the target file.</p>
096      * @param str A string
097      * @since 1.0
098      */
099     public final void write(String str) {
100         try {
101             fileWriter.write(str);
102         } catch (IOException e) {
103             throw new RuntimeException(e);
104         }
105     }
106 
107     /**
108      * <p>Writes a line to the target file.</p>
109      * @param str A line of string
110      * @since 1.0
111      */
112     public final void writeLine(String str) {
113         write(str + LINE_SEPARATOR);
114     }
115 }