Skip to content

Commit

Permalink
Minor refactoring to use try-with-resources and better map-building
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Apr 8, 2013
1 parent e5b2670 commit 8706ea4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 108 deletions.
185 changes: 80 additions & 105 deletions src/main/java/org/fcrepo/federation/bagit/BagItExtraPropertiesStore.java
@@ -1,12 +1,13 @@

package org.fcrepo.federation.bagit;

import static org.slf4j.LoggerFactory.getLogger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.modeshape.jcr.cache.DocumentStoreException;
Expand All @@ -16,19 +17,28 @@
import org.modeshape.jcr.value.PropertyFactory;
import org.modeshape.jcr.value.ValueFactories;
import org.modeshape.jcr.value.ValueFactory;
import org.slf4j.Logger;

import com.google.common.collect.ImmutableMap;

/**
* ExtraPropertiesStore implementation which stores properties in bag-info.txt.
* @see https://tools.ietf.org/html/draft-kunze-bagit-08#section-2.2.2
**/
public class BagItExtraPropertiesStore implements ExtraPropertiesStore {

private static final Logger logger =
getLogger(BagItExtraPropertiesStore.class);

private BagItConnector connector;

private ValueFactories factories;

private ValueFactory<String> stringFactory;

private PropertyFactory propertyFactory;

protected BagItExtraPropertiesStore( BagItConnector connector )
{
protected BagItExtraPropertiesStore(BagItConnector connector) {
this.connector = connector;
this.factories = this.connector.getContext().getValueFactories();
this.stringFactory = factories.getStringFactory();
Expand All @@ -38,168 +48,133 @@ protected BagItExtraPropertiesStore( BagItConnector connector )
@Override
public void storeProperties(String id, Map<Name, Property> properties) {
PrintWriter out = null;
try
{
try
{
try {
try {
File bagInfo = bagInfoFile(id);
out = new PrintWriter( new FileWriter(bagInfo) );
for ( Map.Entry<Name,Property> entry : properties.entrySet() )
{
out = new PrintWriter(new FileWriter(bagInfo));
for (Map.Entry<Name, Property> entry : properties.entrySet()) {
Name name = entry.getKey();
Property prop = entry.getValue();
String line = stringFactory.create( name ) + ": "
+ stringFactory.create( prop );
out.println( wrapLine(line) );
String line =
stringFactory.create(name) + ": " +
stringFactory.create(prop);
out.println(wrapLine(line));
}
}
finally
{
} finally {
out.close();
}
}
catch ( Exception ex )
{
throw new DocumentStoreException(id,ex);
} catch (Exception ex) {
throw new DocumentStoreException(id, ex);
}
}

@Override
public void updateProperties(String id, Map<Name, Property> properties) {
Map<Name,Property> existing = getProperties(id);
for ( Map.Entry<Name,Property> entry : properties.entrySet() )
{
Map<Name, Property> existing = getProperties(id);
for (Map.Entry<Name, Property> entry : properties.entrySet()) {
Name name = entry.getKey();
Property prop = entry.getValue();
if ( prop == null )
{
if (prop == null) {
existing.remove(name);
}
else
{
} else {
existing.put(name, prop);
}
}
}

@Override
public Map<Name, Property> getProperties(String id) {
Map<Name,Property> properties = new HashMap<Name,Property>();
BufferedReader buf = null;
try
{
try
{
File bagInfo = bagInfoFile(id);
System.err.println("XXX bagInfoFile(" + id + "):"+bagInfo.getAbsolutePath());
if (!bagInfo.exists() ) { return NO_PROPERTIES; }
else if ( !bagInfo.canRead() )
{
throw new DocumentStoreException(
"id", "Can't read " + bagInfo.getAbsolutePath()
);
}
buf = new BufferedReader( new FileReader(bagInfo) );
ImmutableMap.Builder<Name, Property> properties =
ImmutableMap.builder();
File bagInfo = bagInfoFile(id);
try {
try (BufferedReader buf =
new BufferedReader(new FileReader(bagInfo))) {

logger.debug("Operating on bagInfoFile(" + id + "):" +
bagInfo.getAbsolutePath());
if (!bagInfo.exists()) {
return NO_PROPERTIES;
} else if (!bagInfo.canRead()) {
throw new DocumentStoreException("id", "Can't read " +
bagInfo.getAbsolutePath());
};
String key = null;
String val = null;
for ( String line = null; (line=buf.readLine()) != null; )
{
if ( key != null &&
(line.startsWith(" ") || line.startsWith("\t")) )
{
for (String line = null; (line = buf.readLine()) != null;) {
if (key != null &&
(line.startsWith(" ") || line.startsWith("\t"))) {
// continuation of previous line
if ( val == null )
{
if (val == null) {
val = line.trim();
}
else
{
} else {
val += " " + line.trim();
}
}
else
{
} else {
// process completed value
if ( key != null && val != null )
{
addProperty( properties, key, val );
if (key != null && val != null) {
Name name = factories.getNameFactory().create(key);
properties.put(name, makeProperty(name, val));
}
key = null;
val = null;

// start new value
if ( line.indexOf(":") != -1 )
{
key = line.substring(0,line.indexOf(":") ).trim();
val = line.substring(line.indexOf(":") + 1 ).trim();
if (line.indexOf(":") != -1) {
key = line.substring(0, line.indexOf(":")).trim();
val = line.substring(line.indexOf(":") + 1).trim();
}
}
}
if ( key != null && val != null )
{
addProperty( properties, key, val );
if (key != null && val != null) {
Name name = factories.getNameFactory().create(key);
properties.put(name, makeProperty(name, val));
}
}
finally
{
buf.close();
}
}
catch ( Exception ex )
{
throw new DocumentStoreException(id,ex);
} catch (Exception ex) {
throw new DocumentStoreException(id, ex);
}
return properties;
return properties.build();
}

private Property makeProperty(Name name, String value) {
return propertyFactory.create(name, value);
}

@Override
public boolean removeProperties(String id) {
File bagInfo = bagInfoFile(id);
if ( !bagInfo.exists() )
{
if (!bagInfo.exists()) {
return false;
}
else
{
} else {
return bagInfo.delete();
}
}

private File bagInfoFile( String id )
{
private File bagInfoFile(String id) {
File dir = connector.fileFor(id);
return new File( dir, "bag-info.txt" );
}
private void addProperty( Map<Name,Property> properties, String key,
String val )
{
Name name = factories.getNameFactory().create(key);
Property prop = propertyFactory.create(name, val);
properties.put( name, prop );
return new File(dir, "bag-info.txt");
}
private static String wrapLine(String value)
{
if ( value == null || value.length() < 79 )
{

private static String wrapLine(String value) {
if (value == null || value.length() < 79) {
return value;
}
StringBuffer wrapped = new StringBuffer();
String[] words = value.split(" ");
StringBuffer line = new StringBuffer(words[0]);
for ( int i = 1; i < words.length; i++ )
{
if ( words[i].length() + line.length() < 79 )
{
for (int i = 1; i < words.length; i++) {
if (words[i].length() + line.length() < 79) {
line.append(" " + words[i]);
}
else
{
wrapped.append( line.toString() + "\n");
} else {
wrapped.append(line.toString() + "\n");
line.setLength(0);
line.append(" "+words[i]);
line.append(" " + words[i]);
}
}
if ( line.length() > 0 ) { wrapped.append(line.toString()); }
if (line.length() > 0) {
wrapped.append(line.toString());
}
return wrapped.toString();
}

Expand Down
Expand Up @@ -2,6 +2,7 @@
package org.fcrepo.federation.bagit;

import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
import java.io.FileReader;
Expand All @@ -21,14 +22,12 @@
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Function;

public class BagItWatchService implements WatchService {

private static final Logger logger = LoggerFactory
.getLogger(BagItWatchService.class);
private static final Logger logger = getLogger(BagItWatchService.class);

static final Pattern MANIFEST = Pattern.compile("^manifest-([^\\.]+).txt$");

Expand Down

0 comments on commit 8706ea4

Please sign in to comment.