ca.carleton.gcrc.couch.command.impl.FileSetManifest Maven / Gradle / Ivy
package ca.carleton.gcrc.couch.command.impl;
import java.io.File;
import java.io.FileFilter;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.Vector;
import org.json.JSONArray;
import org.json.JSONObject;
public class FileSetManifest {
static private Comparator fileManifestComparator = new Comparator(){
@Override
public int compare(FileManifest m1, FileManifest m2) {
String relative1 = m1.getRelativePath();
String relative2 = m2.getRelativePath();
if( relative1 == relative2 ) {
return 0;
} else if( null == relative1 ) {
return -1;
} else if( null == relative2 ) {
return 1;
}
return relative1.compareTo(relative2);
}
};
static private DigestComputerSha1 digestComputer = new DigestComputerSha1();
static private FileFilter anyFilter = new FileFilter(){
@Override
public boolean accept(File arg0) {
return true;
}
};
static public FileSetManifest fromDirectory(File directory) throws Exception {
return fromDirectory(directory,anyFilter);
}
static public FileSetManifest fromDirectory(File directory, FileFilter filter) throws Exception {
if( null == directory ){
throw new Exception("A directory must be provided: null");
}
if( false == directory.exists() ){
throw new Exception("A directory must be provided: do not exist");
}
if( false == directory.isDirectory() ){
throw new Exception("A directory must be provided: not a directory");
}
FileSetManifest fileSetManifest = new FileSetManifest();
fromDirectory(fileSetManifest, directory,filter,new Stack());
return fileSetManifest;
}
static private void fromDirectory(
FileSetManifest fileSetManifest
,File directory
,FileFilter filter
,Stack names
) throws Exception {
String[] childrenNames = directory.list();
if( null != childrenNames ){
for(String childName : childrenNames){
names.push(childName);
// Check if we should include this file
File childFile = new File(directory,childName);
if( filter.accept(childFile) ) {
String name = null;
for(String n : names){
if( null == name ){
name = n;
} else {
name = name + "/" + n;
}
}
FileManifest fileManifest = new FileManifest();
fileManifest.setRelativePath(name);
if( childFile.isDirectory() ){
fileManifest.setDirectory(true);
fromDirectory(
fileSetManifest
,childFile
,filter
,names
);
} else {
try {
String digest = digestComputer.computeDocumentDigest(childFile);
fileManifest.setDigest(digest);
} catch(Exception e) {
throw new Exception("Error while computing digest on file: "+childFile.getAbsolutePath(), e);
}
}
fileSetManifest.addFileManifest(fileManifest);
}
names.pop();
}
}
}
static public FileSetManifest fromJSON(JSONObject jsonObj) throws Exception {
if( null == jsonObj ){
throw new Exception("JSON object must be provided when decoding a FileSetManifest");
}
FileSetManifest fileSetManifest = new FileSetManifest();
try {
JSONArray filesArray = jsonObj.optJSONArray("files");
if( null != filesArray ) {
for(int i=0,e=filesArray.length(); i manifests = new HashMap();
public FileManifest getFileManifest(String relativePath){
return manifests.get(relativePath);
}
public Collection getAllFileManifest(){
return manifests.values();
}
public void addFileManifest(FileManifest manifest){
if( null != manifest ) {
manifests.put(manifest.getRelativePath(), manifest);
}
}
public void removeFileManifest(String path){
manifests.remove(path);
}
public FileSetManifest clone(){
FileSetManifest c = new FileSetManifest();
for(FileManifest f : manifests.values()){
c.addFileManifest(f.clone());
}
return c;
}
public JSONObject toJson() throws Exception {
JSONObject jsonObject = new JSONObject();
List orderedFileManifests = new Vector();
orderedFileManifests.addAll( manifests.values() );
Collections.sort(orderedFileManifests, fileManifestComparator);
JSONArray filesArray = new JSONArray();
for(FileManifest fileManifest : orderedFileManifests){
JSONObject fileObj = new JSONObject();
fileObj.put("path", fileManifest.getRelativePath());
fileObj.put("dir", fileManifest.isDirectory());
String digest = fileManifest.getDigest();
if( null != digest ){
fileObj.put("digest", digest);
}
filesArray.put(fileObj);
}
jsonObject.put("files", filesArray);
return jsonObject;
}
public boolean equals(Object o){
if ( this == o ) return true;
if ( !(o instanceof FileSetManifest) ) return false;
FileSetManifest other = (FileSetManifest)o;
if( manifests.size() != other.manifests.size() ){
return false;
}
for(FileManifest f1 : manifests.values()){
FileManifest f2 = other.manifests.get(f1.getRelativePath());
if( null == f2 ) {
return false;
}
if( false == f1.equals(f2) ){
return false;
}
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy