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.
*
/** 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;
private String customKey;
/** The changed name. */
- private String changedName;
+ private Optional<String> changedName = absent();
/** The default MIME type. */
private final String defaultMimeType;
insert = DEFAULT_INSERT;
insertRedirect = DEFAULT_INSERT_REDIRECT;
customKey = DEFAULT_CUSTOM_KEY;
- changedName = DEFAULT_CHANGED_NAME;
this.defaultMimeType = defaultMimeType;
mimeType = defaultMimeType;
}
}
/**
- * 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;
}
* 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();
}
/**
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)) {
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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;
}
/**
- * 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)}.
*
* @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()) {
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());
}
/**
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);