Store changed name as Optional.
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 25 Nov 2013 20:55:37 +0000 (21:55 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 25 Nov 2013 20:55:37 +0000 (21:55 +0100)
pom.xml
src/main/java/de/todesbaum/jsite/application/FileOption.java
src/main/java/de/todesbaum/jsite/application/Project.java
src/main/java/de/todesbaum/jsite/application/ProjectInserter.java
src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java
src/main/java/de/todesbaum/jsite/main/Configuration.java

diff --git a/pom.xml b/pom.xml
index 7302bb8..1edf7b1 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -9,6 +9,11 @@
                        <artifactId>utils</artifactId>
                        <version>0.12.1</version>
                </dependency>
+               <dependency>
+                       <groupId>com.google.guava</groupId>
+                       <artifactId>guava</artifactId>
+                       <version>14.0.1</version>
+               </dependency>
        </dependencies>
        <repositories>
                <repository>
index c8824de..aa28b54 100644 (file)
 
 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<String> 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<String> 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.<String>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)) {
index 484de85..9982fac 100644 (file)
@@ -460,7 +460,7 @@ public class Project implements Comparable<Project> {
                        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);
                }
index 30d4d7a..f3cf98a 100644 (file)
@@ -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
-        *            <em>return</em> 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<String> 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);
index a9c4699..0969846 100644 (file)
@@ -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);
index 4b49577..1240f32 100644 (file)
@@ -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());
                                }
                        }