Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
231 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 179 additions & 7 deletions
186
src/main/java/org/fcrepo/federation/bagit/BagItExtraPropertiesStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,207 @@ | ||
|
||
package org.fcrepo.federation.bagit; | ||
|
||
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; | ||
import org.modeshape.jcr.federation.spi.Connector; | ||
import org.modeshape.jcr.federation.spi.ExtraPropertiesStore; | ||
import org.modeshape.jcr.value.Name; | ||
import org.modeshape.jcr.value.Property; | ||
import org.modeshape.jcr.value.PropertyFactory; | ||
import org.modeshape.jcr.value.ValueFactories; | ||
import org.modeshape.jcr.value.ValueFactory; | ||
|
||
/** | ||
* 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 BagItConnector connector; | ||
private ValueFactories factories; | ||
private ValueFactory<String> stringFactory; | ||
private PropertyFactory propertyFactory; | ||
|
||
protected BagItExtraPropertiesStore( BagItConnector connector ) | ||
{ | ||
this.connector = connector; | ||
this.factories = this.connector.getContext().getValueFactories(); | ||
this.stringFactory = factories.getStringFactory(); | ||
this.propertyFactory = this.connector.getContext().getPropertyFactory(); | ||
} | ||
|
||
@Override | ||
public void storeProperties(String id, Map<Name, Property> properties) { | ||
// TODO Auto-generated method stub | ||
PrintWriter out = null; | ||
try | ||
{ | ||
try | ||
{ | ||
File bagInfo = bagInfoFile(id); | ||
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) ); | ||
} | ||
} | ||
finally | ||
{ | ||
out.close(); | ||
} | ||
} | ||
catch ( Exception ex ) | ||
{ | ||
throw new DocumentStoreException(id,ex); | ||
} | ||
} | ||
|
||
@Override | ||
public void updateProperties(String id, Map<Name, Property> properties) { | ||
// TODO Auto-generated method stub | ||
|
||
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 ) | ||
{ | ||
existing.remove(name); | ||
} | ||
else | ||
{ | ||
existing.put(name, prop); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Map<Name, Property> getProperties(String id) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
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) ); | ||
String key = null; | ||
String val = null; | ||
for ( String line = null; (line=buf.readLine()) != null; ) | ||
{ | ||
if ( key != null && | ||
(line.startsWith(" ") || line.startsWith("\t")) ) | ||
{ | ||
// continuation of previous line | ||
if ( val == null ) | ||
{ | ||
val = line.trim(); | ||
} | ||
else | ||
{ | ||
val += " " + line.trim(); | ||
} | ||
} | ||
else | ||
{ | ||
// process completed value | ||
if ( key != null && val != null ) | ||
{ | ||
addProperty( properties, key, 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 ( key != null && val != null ) | ||
{ | ||
addProperty( properties, key, val ); | ||
} | ||
} | ||
finally | ||
{ | ||
buf.close(); | ||
} | ||
} | ||
catch ( Exception ex ) | ||
{ | ||
throw new DocumentStoreException(id,ex); | ||
} | ||
return properties; | ||
} | ||
|
||
@Override | ||
public boolean removeProperties(String id) { | ||
// TODO Auto-generated method stub | ||
return false; | ||
File bagInfo = bagInfoFile(id); | ||
if ( !bagInfo.exists() ) | ||
{ | ||
return false; | ||
} | ||
else | ||
{ | ||
return bagInfo.delete(); | ||
} | ||
} | ||
|
||
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 ); | ||
} | ||
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 ) | ||
{ | ||
line.append(" " + words[i]); | ||
} | ||
else | ||
{ | ||
wrapped.append( line.toString() + "\n"); | ||
line.setLength(0); | ||
line.append(" "+words[i]); | ||
} | ||
} | ||
if ( line.length() > 0 ) { wrapped.append(line.toString()); } | ||
return wrapped.toString(); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/org/fcrepo/federation/bagit/BagItExtraPropertiesStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
package org.fcrepo.federation.bagit; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Map; | ||
|
||
import org.modeshape.jcr.value.Name; | ||
import org.modeshape.jcr.value.Property; | ||
|
||
|
||
public class BagItExtraPropertiesStoreTest { | ||
|
||
BagItExtraPropertiesStore store; | ||
|
||
@Before | ||
public void setUp() { | ||
BagItConnector connector = mock(BagItConnector.class); | ||
store = new BagItExtraPropertiesStore(connector); | ||
} | ||
|
||
/* | ||
@Test | ||
public void testRead() { | ||
// TODO read a properties file from disk | ||
} | ||
@Test | ||
public void testWrite() { | ||
// TODO write a properties file to disk then read it back | ||
} | ||
@Test | ||
public void testUpdate() { | ||
// TODO update a properties file and make sure new props saved, delete dprops removed | ||
} | ||
@Test | ||
public void testRemove() { | ||
// TODO remove a properties file | ||
} | ||
*/ | ||
} |