Skip to content

Commit

Permalink
Pure code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Apr 26, 2013
1 parent 2b98450 commit 306c8fb
Show file tree
Hide file tree
Showing 157 changed files with 10,651 additions and 9,023 deletions.
671 changes: 353 additions & 318 deletions src/main/java/gov/loc/repository/bagit/Bag.java

Large diffs are not rendered by default.

519 changes: 264 additions & 255 deletions src/main/java/gov/loc/repository/bagit/BagFactory.java

Large diffs are not rendered by default.

55 changes: 28 additions & 27 deletions src/main/java/gov/loc/repository/bagit/BagFile.java
@@ -1,3 +1,4 @@

package gov.loc.repository.bagit;

import java.io.InputStream;
Expand All @@ -8,31 +9,31 @@
* be an ordinary payload file.
*/
public interface BagFile {
/**
* Opens the file for reading.
* @return A new stream for reading the contents of the file.
* @throws RuntimeException Thrown if the file cannot be opened.
*/
InputStream newInputStream();
/**
* Gets the path of the file, relative to the bag directory.
* @return The path of the file. Will never be null.
*/
String getFilepath();
/**
* Determines whether the file exists on disk or not.
* @return Return <c>true</c> if the file file exists;
* <c>false</c> otherwise.
*/
boolean exists();
/**
* Gets the size of the file on disk.
* @return The size of the file, or 0 if the file does not exist.
*/
long getSize();

/**
* Opens the file for reading.
* @return A new stream for reading the contents of the file.
* @throws RuntimeException Thrown if the file cannot be opened.
*/
InputStream newInputStream();

/**
* Gets the path of the file, relative to the bag directory.
* @return The path of the file. Will never be null.
*/
String getFilepath();

/**
* Determines whether the file exists on disk or not.
* @return Return <c>true</c> if the file file exists;
* <c>false</c> otherwise.
*/
boolean exists();

/**
* Gets the size of the file on disk.
* @return The size of the file, or 0 if the file does not exist.
*/
long getSize();

}
157 changes: 88 additions & 69 deletions src/main/java/gov/loc/repository/bagit/BagHelper.java
@@ -1,12 +1,7 @@
package gov.loc.repository.bagit;

import java.io.File;
import java.text.MessageFormat;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package gov.loc.repository.bagit;

import static com.google.common.base.Throwables.propagate;
import gov.loc.repository.bagit.Bag.BagConstants;
import gov.loc.repository.bagit.filesystem.DirNode;
import gov.loc.repository.bagit.filesystem.FileNode;
Expand All @@ -18,67 +13,91 @@
import gov.loc.repository.bagit.utilities.SizeHelper;
import gov.loc.repository.bagit.v0_95.impl.BagConstantsImpl;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BagHelper {

private static final Logger log = LoggerFactory.getLogger(BagHelper.class);

private static final String BAGIT = "bagit.txt";

/*
* Returns format or null if unable to determine format.
*/
public static String getVersion(File bagFile) {
DirNode bagFileDirNode = null;
try {
bagFileDirNode = FileSystemFactory.getDirNodeForBag(bagFile);
} catch (UnknownFormatException e) {
log.debug(MessageFormat.format("Unable to determine version for {0} because unknown format.", bagFile.toString()));
return null;
} catch (UnsupportedFormatException e) {
log.debug(MessageFormat.format("Unable to determine version for {0} because unsupported format.", bagFile.toString()));
}
log.trace(MessageFormat.format("BagFileDirNode has filepath {0} and is a {1}", bagFileDirNode.getFilepath(), bagFileDirNode.getClass().getSimpleName()));

FileNode bagItNode = bagFileDirNode.childFile(BAGIT);
if (bagItNode == null || ! bagItNode.exists()) {
log.debug(MessageFormat.format("Unable to determine version for {0}.", bagFile.toString()));
return null;
}
BagItTxt bagItTxt = new BagItTxtImpl(new FileSystemBagFile(BAGIT, bagItNode), new BagConstantsImpl());
log.debug(MessageFormat.format("Determined that version for {0} is {1}.", bagFile.toString(), bagItTxt.getVersion()));
return bagItTxt.getVersion();

}

public static long generatePayloadOctetCount(Bag bag) {
long count = 0;
for(BagFile bagFile : bag.getPayload()) {
count = count + bagFile.getSize();
}
return count;
}

public static String generatePayloadOxum(Bag bag) {
return Long.toString(generatePayloadOctetCount(bag)) + "." + Long.toString(bag.getPayload().size());
}

public static long generateTagOctetCount(Bag bag) {
long count = 0;
for(BagFile bagFile : bag.getTags()) {
count = count + bagFile.getSize();
}
return count;
}

public static String generateBagSize(Bag bag) {
long count = generateTagOctetCount(bag) + generatePayloadOctetCount(bag);
return SizeHelper.getSize(count);
}

public static boolean isPayload(String filepath, BagConstants bagConstants) {
filepath = FilenameUtils.normalize(filepath);
return filepath.startsWith(bagConstants.getDataDirectory());
}



private static final Logger log = LoggerFactory.getLogger(BagHelper.class);

private static final String BAGIT = "bagit.txt";

/*
* Returns format or null if unable to determine format.
*/
public static String getVersion(final File bagFile) {
DirNode bagFileDirNode = null;
try {
bagFileDirNode = FileSystemFactory.getDirNodeForBag(bagFile);
} catch (final UnknownFormatException e) {
log.debug(MessageFormat
.format("Unable to determine version for {0} because unknown format.",
bagFile.toString()));
return null;
} catch (final UnsupportedFormatException e) {
log.debug(MessageFormat
.format("Unable to determine version for {0} because unsupported format.",
bagFile.toString()));
} catch (final IOException e) {
propagate(e);
}
log.trace(MessageFormat.format(
"BagFileDirNode has filepath {0} and is a {1}", bagFileDirNode
.getFilepath(), bagFileDirNode.getClass()
.getSimpleName()));

final FileNode bagItNode = bagFileDirNode.childFile(BAGIT);
if (bagItNode == null || !bagItNode.exists()) {
log.debug(MessageFormat.format(
"Unable to determine version for {0}.", bagFile.toString()));
return null;
}
final BagItTxt bagItTxt =
new BagItTxtImpl(new FileSystemBagFile(BAGIT, bagItNode),
new BagConstantsImpl());
log.debug(MessageFormat.format(
"Determined that version for {0} is {1}.", bagFile.toString(),
bagItTxt.getVersion()));
return bagItTxt.getVersion();

}

public static long generatePayloadOctetCount(final Bag bag) {
long count = 0;
for (final BagFile bagFile : bag.getPayload()) {
count = count + bagFile.getSize();
}
return count;
}

public static String generatePayloadOxum(final Bag bag) {
return Long.toString(generatePayloadOctetCount(bag)) + "." +
Long.toString(bag.getPayload().size());
}

public static long generateTagOctetCount(final Bag bag) {
long count = 0;
for (final BagFile bagFile : bag.getTags()) {
count = count + bagFile.getSize();
}
return count;
}

public static String generateBagSize(final Bag bag) {
final long count =
generateTagOctetCount(bag) + generatePayloadOctetCount(bag);
return SizeHelper.getSize(count);
}

public static boolean isPayload(String filepath,
final BagConstants bagConstants) {
filepath = FilenameUtils.normalize(filepath);
return filepath.startsWith(bagConstants.getDataDirectory());
}

}

0 comments on commit 306c8fb

Please sign in to comment.