001 package de.java2html.commandline;
002
003 import java.io.File;
004 import java.io.IOException;
005
006 import de.java2html.converter.IJavaSourceConverter;
007 import de.java2html.options.JavaSourceConversionOptions;
008 import de.java2html.util.Ensure;
009 import de.java2html.util.IoUtilities;
010
011 /**
012 * @author Markus Gebhard
013 */
014 public class Java2HtmlDirectoryConversion extends AbstractJava2HtmlConversion {
015
016 private final File sourceDirectory;
017 private final File targetDirectory;
018 private final String fileMask;
019 private final boolean copyUnprocessedFiles;
020
021 public Java2HtmlDirectoryConversion(
022 File sourceDirectory,
023 IJavaSourceConverter converter,
024 File targetDirectory,
025 String fileMask,
026 boolean copyUnprocessedFiles, JavaSourceConversionOptions options) {
027 super(converter, options);
028 Ensure.ensureArgumentNotNull(fileMask);
029 Ensure.ensureArgumentNotNull(sourceDirectory);
030
031 this.sourceDirectory = sourceDirectory;
032 if (targetDirectory == null) {
033 targetDirectory = sourceDirectory;
034 }
035 this.targetDirectory = targetDirectory;
036 this.fileMask = fileMask;
037 this.copyUnprocessedFiles = copyUnprocessedFiles;
038 }
039
040 public void execute() {
041 convertDirectory(sourceDirectory, targetDirectory);
042 }
043
044 /**
045 * Converts the specified source directory into the target directory
046 * including subdirectories using the specified converter.
047 *
048 * @param sourceDirectory may not be <code>null</code>
049 * @param targetDirectory may not be <code>null</code>
050 */
051 private void convertDirectory(File sourceDirectory, File targetDirectory) {
052 final File[] files = sourceDirectory.listFiles();
053 for (int i = 0; i < files.length; i++) {
054 final File file = files[i];
055 if (file.isDirectory()) {
056 File newTargetDirectory = null;
057 if (sourceDirectory.equals(targetDirectory)) {
058 newTargetDirectory = file;
059 }
060 else {
061 newTargetDirectory = new File(targetDirectory, file.getAbsolutePath().substring(
062 sourceDirectory.getAbsolutePath().length()));
063 }
064 convertDirectory(file, newTargetDirectory);
065 }
066 else {
067 if (matches(file)) {
068 File targetFile = null;
069 if (sourceDirectory.equals(targetDirectory)) {
070 targetFile = file;
071 }
072 else {
073 targetFile = new File(targetDirectory, file.getAbsolutePath().substring(
074 sourceDirectory.getAbsolutePath().length()));
075 }
076 targetFile = IoUtilities.exchangeFileExtension(targetFile, getConverter()
077 .getMetaData().getDefaultFileExtension());
078 convertFile(file, targetFile);
079 }
080 else {
081 if (copyUnprocessedFiles) {
082 if (!sourceDirectory.equals(targetDirectory)) {
083 final File targetFile = new File(targetDirectory, file.getAbsolutePath().substring(
084 sourceDirectory.getAbsolutePath().length()));
085 try {
086 IoUtilities.copy(file, targetFile);
087 }
088 catch (final IOException e) {
089 System.err.println("ERROR: Could cot copy file to target: "
090 + file.getAbsolutePath());
091 }
092 }
093 }
094 }
095 }
096 }
097 }
098
099 /**
100 * Return true if the mask matches the target
101 *
102 * e.g. valid mask is *.java, *.ext , etc...
103 */
104 private boolean matches(File file) {
105 return file.getAbsolutePath().toLowerCase().endsWith(fileMask.substring(1));
106 }
107 }
|