Skip to content

Commit

Permalink
"Quick fix" for empty folders and folders with only ignored filed
Browse files Browse the repository at this point in the history
  • Loading branch information
sleroux-keep committed Dec 2, 2016
1 parent fa20318 commit 2d4e12b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/roda/rodain/rules/filters/IgnoredFilter.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package org.roda.rodain.rules.filters;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.roda.rodain.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Andre Pereira [email protected]
* @since 04-01-2016.
*/
public class IgnoredFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(IgnoredFilter.class.getName());


private static Set<String> rules = new HashSet<>();

private IgnoredFilter() {
Expand Down Expand Up @@ -63,6 +72,12 @@ public static boolean isIgnored(Path path) {
break;
}
}

if(!result){
if (Files.isDirectory(path)) {
result = !Utils.containsAtLeastOneNotIgnoredFile(path);
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public void updateAttributes() {

TreeItem<String> item = copy.get(i);
if (item == null || ((SourceTreeItem) item).getPath() == null) {
LOGGER.error("AAA");
//LOGGER.error("AAA");
} else {
paths.add(((SourceTreeItem) item).getPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,10 @@ protected Integer call() throws Exception {
private void addChild(List children, String sourceItem) {
SourceTreeItemState newState = PathCollection.getState(sourceItem);
Path sourceItemPath = Paths.get(sourceItem);
if (IgnoredFilter.isIgnored(sourceItemPath))
boolean ignored = IgnoredFilter.isIgnored(sourceItemPath);
if (ignored){
newState = SourceTreeItemState.IGNORED;
}

SourceTreeItem item;
Path sourcePath = Paths.get(sourceItem);
Expand Down
45 changes: 36 additions & 9 deletions src/main/java/org/roda/rodain/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -146,8 +147,11 @@ private static boolean validateSchemaWithoutCatch(String content, InputStream sc

/**
* Indents an XML document.
* @param input The input XML document Reader.
* @param output The Writer for the output of the indented XML document.
*
* @param input
* The input XML document Reader.
* @param output
* The Writer for the output of the indented XML document.
* @throws TransformerException
* @throws SAXParseException
*/
Expand All @@ -163,7 +167,9 @@ public static void indentXML(Reader input, Writer output) throws TransformerExce

/**
* Indents an XML document.
* @param xml The XML document string.
*
* @param xml
* The XML document string.
* @return A string with the XML document indented.
*/
public static String indentXML(String xml) {
Expand All @@ -188,7 +194,8 @@ public static String getCurrentVersion() throws ConfigurationException {
}

/**
* @return The latest application version available in the cloud. Will thrown an exception if no Internet connection is available.
* @return The latest application version available in the cloud. Will thrown
* an exception if no Internet connection is available.
* @throws URISyntaxException
* @throws IOException
*/
Expand Down Expand Up @@ -217,7 +224,8 @@ public static Date getCurrentVersionBuildDate() throws ConfigurationException {
}

/**
* @return The latest application version build time available in the cloud. Will thrown an exception if no Internet connection is available.
* @return The latest application version build time available in the cloud.
* Will thrown an exception if no Internet connection is available.
* @throws URISyntaxException
* @throws IOException
*/
Expand All @@ -241,12 +249,12 @@ public static DescObjMetadata updateTemplate(DescObjMetadata dm) {
if (metaTypesRaw != null) {
String[] metaTypes = metaTypesRaw.split(",");
for (String metaType : metaTypes) {
String typeKey = "metadata."+metaType+".type";
String versionKey = "metadata."+metaType+".version";
String typeKey = "metadata." + metaType + ".type";
String versionKey = "metadata." + metaType + ".version";
String type = AppProperties.getConfig(typeKey);
String version = AppProperties.getConfig(versionKey);
if(dm.getMetadataType()!=null && dm.getMetadataType().equalsIgnoreCase(type)){
if(dm.getMetadataVersion()!=null && dm.getMetadataVersion().equalsIgnoreCase(version)){
if (dm.getMetadataType() != null && dm.getMetadataType().equalsIgnoreCase(type)) {
if (dm.getMetadataVersion() != null && dm.getMetadataVersion().equalsIgnoreCase(version)) {
dm.setTemplateType(metaType);
dm.setCreatorOption(MetadataOptions.TEMPLATE);
}
Expand All @@ -256,4 +264,23 @@ public static DescObjMetadata updateTemplate(DescObjMetadata dm) {
return dm;
}

public static boolean containsAtLeastOneNotIgnoredFile(Path path) {
boolean res = true;
try {
if (path.toFile().listFiles().length == 0) {
res = false;
} else {
ValidFilesCounter visitor = new ValidFilesCounter();
Files.walkFileTree(path, visitor);
int validFiles = visitor.getValidFiles();
if (validFiles == 0) {
res = false;
}
}
} catch (Exception e) {
LOGGER.debug("Error while checking if directory contains at least on valid file: " + e.getMessage(), e);
}
return res;
}

}
51 changes: 51 additions & 0 deletions src/main/java/org/roda/rodain/utils/ValidFilesCounter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.roda.rodain.utils;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

import org.roda.rodain.rules.filters.IgnoredFilter;

class ValidFilesCounter implements FileVisitor<Path> {
int validFiles;

public ValidFilesCounter() {
validFiles = 0;
}

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
/*if (IgnoredFilter.isIgnored(dir)) {
return FileVisitResult.SKIP_SUBTREE;
}else{
return FileVisitResult.CONTINUE;
}*/
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!IgnoredFilter.isIgnored(file)) {
validFiles++;
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}

public int getValidFiles() {
return validFiles;
}

}

0 comments on commit 2d4e12b

Please sign in to comment.