KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > commandline > AbstractJava2HtmlConversion


1 package de.java2html.commandline;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.io.StringWriter JavaDoc;
9
10 import de.java2html.converter.IJavaSourceConverter;
11 import de.java2html.javasource.JavaSource;
12 import de.java2html.javasource.JavaSourceParser;
13 import de.java2html.options.JavaSourceConversionOptions;
14 import de.java2html.util.Ensure;
15 import de.java2html.util.IoUtilities;
16
17 /**
18  * @author Markus Gebhard
19  */

20 public abstract class AbstractJava2HtmlConversion implements IJava2HtmlConversion {
21   private IJavaSourceConverter converter;
22   private final JavaSourceConversionOptions options;
23
24   public AbstractJava2HtmlConversion(IJavaSourceConverter converter, JavaSourceConversionOptions options) {
25     Ensure.ensureArgumentNotNull(converter);
26     Ensure.ensureArgumentNotNull(options);
27     this.options = options;
28     this.converter = converter;
29   }
30   
31   public final JavaSourceConversionOptions getConversionOptions() {
32     return options;
33   }
34
35   public final IJavaSourceConverter getConverter() {
36     return converter;
37   }
38
39   /**
40    * Read the contents from the specified file name.
41    *
42    * @param file
43    * @return byte[]
44    * @throws IOException
45    */

46   protected byte[] readFile(File JavaDoc file) throws IOException JavaDoc {
47     InputStream JavaDoc inputStream = null;
48     try {
49       inputStream = new FileInputStream JavaDoc(file);
50       return IoUtilities.readBytes(inputStream);
51     }
52     finally {
53       IoUtilities.close(inputStream);
54     }
55   }
56
57   /**
58    * Invoke the converter on the specified file and return the converted contents.
59    *
60    * @param sourceFile The file to be converted
61    * @return a String containing the converted result
62    * @throws IOException when there is an io error reading the file. */

63   protected String JavaDoc readAndConvert(File JavaDoc sourceFile) throws IOException JavaDoc {
64     StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
65
66     converter.writeDocumentHeader(stringWriter, getConversionOptions(), ""); //$NON-NLS-1$
67
JavaSourceParser parser = new JavaSourceParser(getConversionOptions());
68     JavaSource source = parser.parse(sourceFile);
69     try {
70       converter.convert(source, getConversionOptions(), stringWriter);
71     }
72     catch (IOException JavaDoc e) {
73       //Should never happen since we are a StringWriter
74
throw new RuntimeException JavaDoc(e);
75     }
76     converter.writeDocumentFooter(stringWriter, getConversionOptions());
77     return stringWriter.toString();
78   }
79
80   /**
81    * Converts the source file to the targetfile using the specified converter
82    *
83    * @param sourceFile
84    * @param targetFile the target file to write the output to or <code>null</code> if the converter
85    * shall determine an appropriate name for the output file itself.
86    */

87   protected void convertFile(File JavaDoc sourceFile, File JavaDoc targetFile) {
88     String JavaDoc text;
89     try {
90       text = readAndConvert(sourceFile);
91     }
92     catch (IOException JavaDoc exception) {
93       //TODO Mar 11, 2004 (Markus Gebhard):
94
exception.printStackTrace();
95       return;
96     }
97     if (targetFile == null) {
98       targetFile = IoUtilities.exchangeFileExtension(sourceFile, getConverter().getMetaData().getDefaultFileExtension());
99     }
100
101     IoUtilities.ensureFoldersExist(targetFile.getParentFile());
102     try {
103       writeFile(targetFile, text.getBytes());
104     }
105     catch (IOException JavaDoc exception) {
106       //TODO Mar 11, 2004 (Markus Gebhard):
107
exception.printStackTrace();
108     }
109   }
110
111
112   /**
113    * Write the contents to the specified file. If the file already exists it will be overwritten.
114    *
115    * @param targetFile the file to write the contents to.
116    * @param contents the bytes to be written.
117    * @throws IOException if the is an error writing the file. */

118   private void writeFile(File JavaDoc targetFile, byte[] contents) throws IOException JavaDoc {
119     FileOutputStream JavaDoc outputStream = null;
120     try {
121       outputStream = new FileOutputStream JavaDoc(targetFile);
122       outputStream.write(contents);
123     }
124     finally {
125       IoUtilities.close(outputStream);
126     }
127   }
128 }
Popular Tags