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