001 package de.java2html.commandline;
002 
003 import java.io.File;
004 import java.io.FileInputStream;
005 import java.io.FileOutputStream;
006 import java.io.IOException;
007 import java.io.InputStream;
008 import java.io.StringWriter;
009 
010 import de.java2html.converter.IJavaSourceConverter;
011 import de.java2html.javasource.TypedSource;
012 import de.java2html.javasource.JavaSourceParser;
013 import de.java2html.options.JavaSourceConversionOptions;
014 import de.java2html.util.Ensure;
015 import de.java2html.util.IoUtilities;
016 
017 /**
018  @author Markus Gebhard
019  */
020 public abstract class AbstractJava2HtmlConversion implements IJava2HtmlConversion {
021   private IJavaSourceConverter converter;
022   private final JavaSourceConversionOptions options;
023 
024   public AbstractJava2HtmlConversion(IJavaSourceConverter converter, JavaSourceConversionOptions options) {
025     Ensure.ensureArgumentNotNull(converter);
026     Ensure.ensureArgumentNotNull(options);
027     this.options = options;
028     this.converter = converter;
029   }
030   
031   public final JavaSourceConversionOptions getConversionOptions() {
032     return options;
033   }
034 
035   public final IJavaSourceConverter getConverter() {
036     return converter;
037   }
038 
039   /**
040    * Read the contents from the specified file name.
041    
042    @param file
043    @return byte[]
044    @throws IOException
045    */
046   protected byte[] readFile(File filethrows IOException {
047     InputStream inputStream = null;
048     try {
049       inputStream = new FileInputStream(file);
050       return IoUtilities.readBytes(inputStream);
051     }
052     finally {
053       IoUtilities.close(inputStream);
054     }
055   }
056 
057   /**
058    * Invoke the converter on the specified file and return the converted contents.
059    
060    @param sourceFile The file to be converted
061    @return a String containing the converted result
062    @throws IOException when there is an io error reading the file. */
063   protected String readAndConvert(File sourceFilethrows IOException {
064     final StringWriter stringWriter = new StringWriter();
065 
066     converter.writeDocumentHeader(stringWriter, getConversionOptions()"")//$NON-NLS-1$
067     final JavaSourceParser parser = new JavaSourceParser(getConversionOptions());
068     final TypedSource source = parser.parse(sourceFile);
069     try {
070       converter.convert(source, getConversionOptions(), stringWriter);
071     }
072     catch (final IOException e) {
073       //Should never happen since we are a StringWriter
074       throw new RuntimeException(e);
075     }
076     converter.writeDocumentFooter(stringWriter, getConversionOptions());
077     return stringWriter.toString();
078   }
079 
080   /**
081    * Converts the source file to the targetfile using the specified converter
082    
083    @param sourceFile
084    @param targetFile the target file to write the output to or <code>null</code> if the converter
085    * shall determine an appropriate name for the output file itself.
086    */
087   protected void convertFile(File sourceFile, File targetFile) {
088     String text;
089     try {
090       text = readAndConvert(sourceFile);
091     }
092     catch (final IOException exception) {
093       //TODO Mar 11, 2004 (Markus Gebhard):
094       exception.printStackTrace();
095       return;
096     }
097     if (targetFile == null) {
098       targetFile = IoUtilities.exchangeFileExtension(sourceFile, getConverter().getMetaData().getDefaultFileExtension());
099     }
100 
101     IoUtilities.ensureFoldersExist(targetFile.getParentFile());
102     try {
103       writeFile(targetFile, text.getBytes());
104     }
105     catch (final IOException 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 targetFile, byte[] contentsthrows IOException {
119     FileOutputStream outputStream = null;
120     try {
121       outputStream = new FileOutputStream(targetFile);
122       outputStream.write(contents);
123     }
124     finally {
125       IoUtilities.close(outputStream);
126     }
127   }
128 }