
se.somath.publisher.Main Maven / Gradle / Ivy
package se.somath.publisher;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import se.somath.publisher.excpetion.PublishException;
import se.somath.publisher.formatter.HtmlFormatter;
import se.somath.publisher.includer.Includer;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
public class Main {
public void publish(String sourceDirectory, String targetDirectory) {
String defaultFileName = "index.html";
try {
List content = readSourceFile(sourceDirectory, defaultFileName);
content = formatHtml(content);
content = addIncludes(content);
writeTargetFile(targetDirectory, content, defaultFileName);
} catch (FileNotFoundException e) {
throw new PublishException(e);
} catch (IOException e) {
throw new PublishException(e);
}
}
private List readSourceFile(String sourceDirectory, String fileName) throws FileNotFoundException {
List content = new LinkedList();
String sourceFileName = sourceDirectory + File.separator + fileName;
Reader sourceFileReader = new FileReader(sourceFileName);
LineIterator sourceFileIterator = new LineIterator(sourceFileReader);
while (sourceFileIterator.hasNext()) {
String currentLine = sourceFileIterator.nextLine();
content.add(currentLine);
}
return content;
}
private List formatHtml(List unFormattedContent) {
HtmlFormatter formatter = new HtmlFormatter();
return formatter.format(unFormattedContent);
}
private List addIncludes(List unIncludedContent) {
Includer includer = new Includer();
return includer.addIncludes(unIncludedContent);
}
private void writeTargetFile(String targetDirectory, List content, String fileName) throws IOException {
String targetFileName = targetDirectory + File.separator + fileName;
File targetFile = new File(targetFileName);
createTargetDirectory(targetFile);
FileUtils.writeLines(targetFile, content);
}
private void createTargetDirectory(File targetFile) {
File targetDirectory = targetFile.getParentFile();
//noinspection ResultOfMethodCallIgnored
targetDirectory.mkdirs();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy