diff --git a/src/main/java/org/roda/rodain/rules/filters/IgnoredFilter.java b/src/main/java/org/roda/rodain/rules/filters/IgnoredFilter.java index d9e8fd5b..7beccfd9 100644 --- a/src/main/java/org/roda/rodain/rules/filters/IgnoredFilter.java +++ b/src/main/java/org/roda/rodain/rules/filters/IgnoredFilter.java @@ -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 apereira@keep.pt * @since 04-01-2016. */ public class IgnoredFilter { + + private static final Logger LOGGER = LoggerFactory.getLogger(IgnoredFilter.class.getName()); + + private static Set rules = new HashSet<>(); private IgnoredFilter() { @@ -63,6 +72,12 @@ public static boolean isIgnored(Path path) { break; } } + + if(!result){ + if (Files.isDirectory(path)) { + result = !Utils.containsAtLeastOneNotIgnoredFile(path); + } + } return result; } } diff --git a/src/main/java/org/roda/rodain/source/ui/FileExplorerPane.java b/src/main/java/org/roda/rodain/source/ui/FileExplorerPane.java index 18f7eb2b..fc1f1fd1 100644 --- a/src/main/java/org/roda/rodain/source/ui/FileExplorerPane.java +++ b/src/main/java/org/roda/rodain/source/ui/FileExplorerPane.java @@ -354,7 +354,7 @@ public void updateAttributes() { TreeItem item = copy.get(i); if (item == null || ((SourceTreeItem) item).getPath() == null) { - LOGGER.error("AAA"); + //LOGGER.error("AAA"); } else { paths.add(((SourceTreeItem) item).getPath()); } diff --git a/src/main/java/org/roda/rodain/source/ui/items/SourceTreeDirectory.java b/src/main/java/org/roda/rodain/source/ui/items/SourceTreeDirectory.java index b1c3a4cd..b325289b 100644 --- a/src/main/java/org/roda/rodain/source/ui/items/SourceTreeDirectory.java +++ b/src/main/java/org/roda/rodain/source/ui/items/SourceTreeDirectory.java @@ -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); diff --git a/src/main/java/org/roda/rodain/utils/Utils.java b/src/main/java/org/roda/rodain/utils/Utils.java index deb795a6..a493c961 100644 --- a/src/main/java/org/roda/rodain/utils/Utils.java +++ b/src/main/java/org/roda/rodain/utils/Utils.java @@ -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; @@ -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 */ @@ -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) { @@ -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 */ @@ -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 */ @@ -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); } @@ -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; + } + } diff --git a/src/main/java/org/roda/rodain/utils/ValidFilesCounter.java b/src/main/java/org/roda/rodain/utils/ValidFilesCounter.java new file mode 100644 index 00000000..fe652a55 --- /dev/null +++ b/src/main/java/org/roda/rodain/utils/ValidFilesCounter.java @@ -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 { + 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; + } + +} \ No newline at end of file