From da487bd7b258bc47ad888c22f89343c6162569a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 25 Nov 2013 21:55:37 +0100 Subject: [PATCH] Store changed name as Optional. --- pom.xml | 5 +++ .../de/todesbaum/jsite/application/FileOption.java | 28 ++++--------- .../de/todesbaum/jsite/application/Project.java | 2 +- .../jsite/application/ProjectInserter.java | 49 +++++++--------------- .../de/todesbaum/jsite/gui/ProjectFilesPage.java | 6 +-- .../de/todesbaum/jsite/main/Configuration.java | 2 +- 6 files changed, 33 insertions(+), 59 deletions(-) diff --git a/pom.xml b/pom.xml index 7302bb8..1edf7b1 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,11 @@ utils 0.12.1 + + com.google.guava + guava + 14.0.1 + diff --git a/src/main/java/de/todesbaum/jsite/application/FileOption.java b/src/main/java/de/todesbaum/jsite/application/FileOption.java index c8824de..aa28b54 100644 --- a/src/main/java/de/todesbaum/jsite/application/FileOption.java +++ b/src/main/java/de/todesbaum/jsite/application/FileOption.java @@ -18,6 +18,11 @@ package de.todesbaum.jsite.application; +import static com.google.common.base.Optional.absent; +import static com.google.common.base.Optional.of; + +import com.google.common.base.Optional; + /** * Container for various file options. * @@ -34,9 +39,6 @@ public class FileOption { /** The default for the custom key. */ private static final String DEFAULT_CUSTOM_KEY = "CHK@"; - /** The default changed name. */ - private static final String DEFAULT_CHANGED_NAME = null; - /** The insert state. */ private boolean insert; @@ -62,7 +64,7 @@ public class FileOption { private String customKey; /** The changed name. */ - private String changedName; + private Optional changedName = absent(); /** The default MIME type. */ private final String defaultMimeType; @@ -80,7 +82,6 @@ public class FileOption { insert = DEFAULT_INSERT; insertRedirect = DEFAULT_INSERT_REDIRECT; customKey = DEFAULT_CUSTOM_KEY; - changedName = DEFAULT_CHANGED_NAME; this.defaultMimeType = defaultMimeType; mimeType = defaultMimeType; } @@ -278,24 +279,13 @@ public class FileOption { } /** - * Returns whether this file has a changed name. Use - * {@link #getChangedName()} is this method returns {@code true}. - * - * @return {@code true} if this file has a changed name, {@code false} - * otherwise - */ - public boolean hasChangedName() { - return (changedName != null) && (changedName.length() > 0); - } - - /** * Returns the changed name for this file. This method will return {@code * null} or an empty {@link String} if this file should not be renamed. * * @return The changed name, or {@code null} if this file should not be * renamed */ - public String getChangedName() { + public Optional getChangedName() { return changedName; } @@ -307,7 +297,7 @@ public class FileOption { * The new changed name for this file */ public void setChangedName(String changedName) { - this.changedName = changedName; + this.changedName = ((changedName != null) && (changedName.length() > 0)) ? of(changedName) : Optional.absent(); } /** @@ -349,7 +339,7 @@ public class FileOption { if (!customKey.equals(DEFAULT_CUSTOM_KEY)) { return true; } - if (((changedName != null) && !changedName.equals(DEFAULT_CHANGED_NAME)) || ((DEFAULT_CHANGED_NAME != null) && !DEFAULT_CHANGED_NAME.equals(changedName))) { + if (changedName.isPresent()) { return true; } if (!defaultMimeType.equals(mimeType)) { diff --git a/src/main/java/de/todesbaum/jsite/application/Project.java b/src/main/java/de/todesbaum/jsite/application/Project.java index 484de85..9982fac 100644 --- a/src/main/java/de/todesbaum/jsite/application/Project.java +++ b/src/main/java/de/todesbaum/jsite/application/Project.java @@ -460,7 +460,7 @@ public class Project implements Comparable { if ((fileOption.getCurrentHash() != null) && (fileOption.getCurrentHash().length() > 0) && (!fileOption.getCurrentHash().equals(fileOption.getLastInsertHash()) || fileOption.isForceInsert())) { fileOption.setLastInsertEdition(edition); fileOption.setLastInsertHash(fileOption.getCurrentHash()); - fileOption.setLastInsertFilename(fileOption.hasChangedName() ? fileOption.getChangedName() : fileOptionEntry.getKey()); + fileOption.setLastInsertFilename(fileOption.getChangedName().or(fileOptionEntry.getKey())); } fileOption.setForceInsert(false); } diff --git a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java index 30d4d7a..f3cf98a 100644 --- a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java @@ -20,6 +20,7 @@ package de.todesbaum.jsite.application; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -35,6 +36,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.util.io.StreamCopier.ProgressListener; + +import com.google.common.base.Optional; import de.todesbaum.jsite.gui.FileScanner; import de.todesbaum.jsite.gui.FileScanner.ScannedFile; import de.todesbaum.jsite.gui.FileScannerListener; @@ -284,30 +287,6 @@ public class ProjectInserter implements FileScannerListener, Runnable { } /** - * Creates an input stream that delivers the given file, replacing edition - * tokens in the file’s content, if necessary. - * - * @param filename - * The name of the file - * @param fileOption - * The file options - * @param edition - * The current edition - * @param length - * An array containing a single long which is used to - * return the final length of the file, after all - * replacements - * @return The input stream for the file - * @throws IOException - * if an I/O error occurs - */ - private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException { - File file = new File(project.getLocalPath(), filename); - length[0] = file.length(); - return new FileInputStream(file); - } - - /** * Creates a file entry suitable for handing in to * {@link ClientPutComplexDir#addFileEntry(FileEntry)}. * @@ -318,7 +297,6 @@ public class ProjectInserter implements FileScannerListener, Runnable { * @return A file entry for the given file */ private FileEntry createFileEntry(ScannedFile file, int edition) { - FileEntry fileEntry = null; String filename = file.getFilename(); FileOption fileOption = project.getFileOption(filename); if (fileOption.isInsert()) { @@ -327,21 +305,25 @@ public class ProjectInserter implements FileScannerListener, Runnable { if (!project.isAlwaysForceInsert() && !fileOption.isForceInsert() && file.getHash().equals(fileOption.getLastInsertHash())) { /* only insert a redirect. */ logger.log(Level.FINE, String.format("Inserting redirect to edition %d for %s.", fileOption.getLastInsertEdition(), filename)); - return new RedirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename()); + return new RedirectFileEntry(fileOption.getChangedName().or(filename), fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename()); } try { - long[] fileLength = new long[1]; - InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength); - fileEntry = new DirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), fileEntryInputStream, fileLength[0]); + return createFileEntry(filename, fileOption.getChangedName(), fileOption.getMimeType()); } catch (IOException ioe1) { /* ignore, null is returned. */ } } else { if (fileOption.isInsertRedirect()) { - fileEntry = new RedirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), fileOption.getCustomKey()); + return new RedirectFileEntry(fileOption.getChangedName().or(filename), fileOption.getMimeType(), fileOption.getCustomKey()); } } - return fileEntry; + return null; + } + + private FileEntry createFileEntry(String filename, Optional changedName, String mimeType) throws FileNotFoundException { + File physicalFile = new File(project.getLocalPath(), filename); + InputStream fileEntryInputStream = new FileInputStream(physicalFile); + return new DirectFileEntry(changedName.or(filename), mimeType, fileEntryInputStream, physicalFile.length()); } /** @@ -399,10 +381,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { logger.log(Level.FINEST, "Ignoring {0}.", fileOptionEntry.getKey()); continue; } - String fileName = fileOptionEntry.getKey(); - if (fileOption.hasChangedName()) { - fileName = fileOption.getChangedName(); - } + String fileName = fileOption.getChangedName().or(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); diff --git a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java index a9c4699..0969846 100644 --- a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java +++ b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java @@ -628,9 +628,9 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis fileOptionsInsertRedirectCheckBox.setSelected(fileOption.isInsertRedirect()); fileOptionsCustomKeyTextField.setEnabled(fileOption.isInsertRedirect()); fileOptionsCustomKeyTextField.setText(fileOption.getCustomKey()); - fileOptionsRenameCheckBox.setSelected(fileOption.hasChangedName()); - fileOptionsRenameTextField.setEnabled(fileOption.hasChangedName()); - fileOptionsRenameTextField.setText(fileOption.getChangedName()); + fileOptionsRenameCheckBox.setSelected(fileOption.getChangedName().isPresent()); + fileOptionsRenameTextField.setEnabled(fileOption.getChangedName().isPresent()); + fileOptionsRenameTextField.setText(fileOption.getChangedName().or("")); fileOptionsMIMETypeComboBox.getModel().setSelectedItem(fileOption.getMimeType()); } else { defaultFileCheckBox.setSelected(false); diff --git a/src/main/java/de/todesbaum/jsite/main/Configuration.java b/src/main/java/de/todesbaum/jsite/main/Configuration.java index 4b49577..1240f32 100644 --- a/src/main/java/de/todesbaum/jsite/main/Configuration.java +++ b/src/main/java/de/todesbaum/jsite/main/Configuration.java @@ -430,7 +430,7 @@ public class Configuration { fileOptionNode.append("insert", String.valueOf(fileOption.isInsert())); fileOptionNode.append("insert-redirect", String.valueOf(fileOption.isInsertRedirect())); fileOptionNode.append("custom-key", fileOption.getCustomKey()); - fileOptionNode.append("changed-name", fileOption.getChangedName()); + fileOptionNode.append("changed-name", fileOption.getChangedName().orNull()); fileOptionNode.append("mime-type", fileOption.getMimeType()); } } -- 2.7.4