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.JavaSource;
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
021: IJava2HtmlConversion {
022: private IJavaSourceConverter converter;
023: private final JavaSourceConversionOptions options;
024:
025: public AbstractJava2HtmlConversion(IJavaSourceConverter converter,
026: JavaSourceConversionOptions options) {
027: Ensure.ensureArgumentNotNull(converter);
028: Ensure.ensureArgumentNotNull(options);
029: this .options = options;
030: this .converter = converter;
031: }
032:
033: public final JavaSourceConversionOptions getConversionOptions() {
034: return options;
035: }
036:
037: public final IJavaSourceConverter getConverter() {
038: return converter;
039: }
040:
041: /**
042: * Read the contents from the specified file name.
043: *
044: * @param file
045: * @return byte[]
046: * @throws IOException
047: */
048: protected byte[] readFile(File file) throws IOException {
049: InputStream inputStream = null;
050: try {
051: inputStream = new FileInputStream(file);
052: return IoUtilities.readBytes(inputStream);
053: } finally {
054: IoUtilities.close(inputStream);
055: }
056: }
057:
058: /**
059: * Invoke the converter on the specified file and return the converted contents.
060: *
061: * @param sourceFile The file to be converted
062: * @return a String containing the converted result
063: * @throws IOException when there is an io error reading the file. */
064: protected String readAndConvert(File sourceFile) throws IOException {
065: StringWriter stringWriter = new StringWriter();
066:
067: converter.writeDocumentHeader(stringWriter,
068: getConversionOptions(), ""); //$NON-NLS-1$
069: JavaSourceParser parser = new JavaSourceParser(
070: getConversionOptions());
071: JavaSource source = parser.parse(sourceFile);
072: try {
073: converter.convert(source, getConversionOptions(),
074: stringWriter);
075: } catch (IOException e) {
076: //Should never happen since we are a StringWriter
077: throw new RuntimeException(e);
078: }
079: converter.writeDocumentFooter(stringWriter,
080: getConversionOptions());
081: return stringWriter.toString();
082: }
083:
084: /**
085: * Converts the source file to the targetfile using the specified converter
086: *
087: * @param sourceFile
088: * @param targetFile the target file to write the output to or <code>null</code> if the converter
089: * shall determine an appropriate name for the output file itself.
090: */
091: protected void convertFile(File sourceFile, File targetFile) {
092: String text;
093: try {
094: text = readAndConvert(sourceFile);
095: } catch (IOException exception) {
096: //TODO Mar 11, 2004 (Markus Gebhard):
097: exception.printStackTrace();
098: return;
099: }
100: if (targetFile == null) {
101: targetFile = IoUtilities.exchangeFileExtension(sourceFile,
102: getConverter().getMetaData()
103: .getDefaultFileExtension());
104: }
105:
106: IoUtilities.ensureFoldersExist(targetFile.getParentFile());
107: try {
108: writeFile(targetFile, text.getBytes());
109: } catch (IOException exception) {
110: //TODO Mar 11, 2004 (Markus Gebhard):
111: exception.printStackTrace();
112: }
113: }
114:
115: /**
116: * Write the contents to the specified file. If the file already exists it will be overwritten.
117: *
118: * @param targetFile the file to write the contents to.
119: * @param contents the bytes to be written.
120: * @throws IOException if the is an error writing the file. */
121: private void writeFile(File targetFile, byte[] contents)
122: throws IOException {
123: FileOutputStream outputStream = null;
124: try {
125: outputStream = new FileOutputStream(targetFile);
126: outputStream.write(contents);
127: } finally {
128: IoUtilities.close(outputStream);
129: }
130: }
131: }
|