X-Git-Url: https://git.pterodactylus.net/?p=jSite.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Fjsite%2Fapplication%2Fvalidation%2FProjectValidator.java;fp=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Fjsite%2Fapplication%2Fvalidation%2FProjectValidator.java;h=2e03e6c34a57c2e9124cd5efea2f3af488084fde;hp=0000000000000000000000000000000000000000;hb=2d6256e4191697fdc4d033e93d53a59308a516fe;hpb=37618fed55c2d4afe2fdcb9b4b8de1539fdf163d
diff --git a/src/main/java/de/todesbaum/jsite/application/validation/ProjectValidator.java b/src/main/java/de/todesbaum/jsite/application/validation/ProjectValidator.java
new file mode 100644
index 0000000..2e03e6c
--- /dev/null
+++ b/src/main/java/de/todesbaum/jsite/application/validation/ProjectValidator.java
@@ -0,0 +1,107 @@
+package de.todesbaum.jsite.application.validation;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import de.todesbaum.jsite.application.FileOption;
+import de.todesbaum.jsite.application.Project;
+import de.todesbaum.jsite.gui.FileScanner;
+import de.todesbaum.jsite.gui.ScannedFile;
+
+/**
+ * Validates a project and returns a number of {@link Issue}s in a {@link CheckReport}.
+ *
+ * @author David âBombeâ Roden
+ */
+public class ProjectValidator {
+
+ private static final Logger logger = Logger.getLogger(ProjectValidator.class.getName());
+
+ public static CheckReport validateProject(Project project) {
+ CheckReport checkReport = new CheckReport();
+ if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
+ checkReport.addIssue("error.no-local-path", true);
+ return checkReport;
+ }
+ if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
+ checkReport.addIssue("error.no-path", true);
+ }
+ if ((project.getIndexFile() == null) || (project.getIndexFile().length() == 0)) {
+ checkReport.addIssue("warning.empty-index", false);
+ } else {
+ File indexFile = new File(project.getLocalPath(), project.getIndexFile());
+ if (!indexFile.exists()) {
+ checkReport.addIssue("error.index-missing", true);
+ }
+ }
+ String indexFile = project.getIndexFile();
+ boolean hasIndexFile = (indexFile != null) && (indexFile.length() > 0);
+ List allowedIndexContentTypes = Arrays.asList("text/html", "application/xhtml+xml");
+ if (hasIndexFile && !allowedIndexContentTypes.contains(project.getFileOption(indexFile).getMimeType())) {
+ checkReport.addIssue("warning.index-not-html", false);
+ }
+ Map fileOptions = project.getFileOptions();
+ Set> fileOptionEntries = fileOptions.entrySet();
+ boolean insert = fileOptionEntries.isEmpty();
+ for (Entry fileOptionEntry : fileOptionEntries) {
+ String fileName = fileOptionEntry.getKey();
+ FileOption fileOption = fileOptionEntry.getValue();
+ insert |= fileOption.isInsert() || fileOption.isInsertRedirect();
+ if (fileName.equals(project.getIndexFile()) && !fileOption.isInsert() && !fileOption.isInsertRedirect()) {
+ checkReport.addIssue("error.index-not-inserted", true);
+ }
+ if (!fileOption.isInsert() && fileOption.isInsertRedirect() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(
+ fileOption.getCustomKey()))) {
+ checkReport.addIssue("error.no-custom-key", true, fileName);
+ }
+ }
+ if (!insert) {
+ checkReport.addIssue("error.no-files-to-insert", true);
+ }
+ Set fileNames = new HashSet<>();
+ for (Entry fileOptionEntry : fileOptionEntries) {
+ FileOption fileOption = fileOptionEntry.getValue();
+ if (!fileOption.isInsert() && !fileOption.isInsertRedirect()) {
+ logger.log(Level.FINEST, "Ignoring {0}.", fileOptionEntry.getKey());
+ continue;
+ }
+ String fileName = fileOption.getChangedName().orElse(fileOptionEntry.getKey());
+ logger.log(Level.FINEST, "Adding â{0}â for {1}.", new Object[] { fileName, fileOptionEntry.getKey() });
+ if (!fileNames.add(fileName)) {
+ checkReport.addIssue("error.duplicate-file", true, fileName);
+ }
+ }
+ long totalSize = 0;
+ final CountDownLatch completionLatch = new CountDownLatch(1);
+ FileScanner fileScanner = new FileScanner(project, (error, files) -> completionLatch.countDown());
+ fileScanner.startInBackground();
+ while (completionLatch.getCount() > 0) {
+ try {
+ completionLatch.await();
+ } catch (InterruptedException ie1) {
+ /* TODO: logging */
+ }
+ }
+ for (ScannedFile scannedFile : fileScanner.getFiles()) {
+ String fileName = scannedFile.getFilename();
+ FileOption fileOption = project.getFileOption(fileName);
+ if ((fileOption != null) && !fileOption.isInsert()) {
+ continue;
+ }
+ totalSize += new File(project.getLocalPath(), fileName).length();
+ }
+ if (totalSize > 2 * 1024 * 1024) {
+ checkReport.addIssue("warning.site-larger-than-2-mib", false);
+ }
+ return checkReport;
+ }
+
+}