Skip to content

Commit

Permalink
Add a glob-based filter API
Browse files Browse the repository at this point in the history
This adds an API for constructing rule-based glob filters for files.

Fix: #12
  • Loading branch information
io7m committed Apr 11, 2021
1 parent fff4de4 commit 4a08b85
Show file tree
Hide file tree
Showing 15 changed files with 772 additions and 0 deletions.
73 changes: 73 additions & 0 deletions com.io7m.jwheatsheaf.filter.glob/pom.xml
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>com.io7m.jwheatsheaf</artifactId>
<groupId>com.io7m.jwheatsheaf</groupId>
<version>3.0.0-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.jwheatsheaf.filter.glob</artifactId>

<name>com.io7m.jwheatsheaf.filter.glob</name>
<description>JavaFX File Chooser (Glob filter)</description>
<url>http://github.com/io7m/jwheatsheaf</url>

<properties>
<io7m.api.previousVersion>3.0.0-SNAPSHOT</io7m.api.previousVersion>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>com.io7m.jwheatsheaf.api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.bundle</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.versioning</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.io7m.immutables.style</groupId>
<artifactId>com.io7m.immutables.style</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Ignore dependencies that bytecode analysis gets wrong. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<failOnWarning>true</failOnWarning>
<ignoredUsedUndeclaredDependencies>
<ignoredUsedUndeclaredDependency>org.openjfx:*:*</ignoredUsedUndeclaredDependency>
</ignoredUsedUndeclaredDependencies>
<ignoredUnusedDeclaredDependencies>
<ignoredUnusedDeclaredDependency>org.openjfx:*:*</ignoredUnusedDeclaredDependency>
</ignoredUnusedDeclaredDependencies>
</configuration>
</plugin>
</plugins>
</build>

</project>
@@ -0,0 +1,46 @@
/*
* Copyright © 2021 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jwheatsheaf.filter.glob;

import com.io7m.jwheatsheaf.api.JWFileChooserFilterType;

/**
* The type of glob filter builders.
*/

public interface JWFilterGlobBuilderType
{
/**
* Add a rule to the filter.
*
* @param kind The kind of rule
* @param pattern The glob pattern
*
* @return this
*/

JWFilterGlobBuilderType addRule(
JWFilterGlobRuleKind kind,
String pattern
);

/**
* @return An immutable filter based on all of the rules given so far
*/

JWFileChooserFilterType build();
}
@@ -0,0 +1,42 @@
/*
* Copyright © 2021 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jwheatsheaf.filter.glob;

import com.io7m.jwheatsheaf.filter.glob.internal.JWFilterGlobBuilder;

/**
* A factory of glob filters.
*/

public final class JWFilterGlobFactory implements JWFilterGlobFactoryType
{
/**
* Create a glob filter factory.
*/

public JWFilterGlobFactory()
{

}

@Override
public JWFilterGlobBuilderType create(
final String description)
{
return new JWFilterGlobBuilder(description);
}
}
@@ -0,0 +1,35 @@
/*
* Copyright © 2021 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jwheatsheaf.filter.glob;

/**
* The type of filter glob factories.
*/

public interface JWFilterGlobFactoryType
{
/**
* Create a new filter builder.
*
* @param description The humanly-readable description
*
* @return A new filter builder
*/

JWFilterGlobBuilderType create(
String description);
}
@@ -0,0 +1,48 @@
/*
* Copyright © 2021 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jwheatsheaf.filter.glob;

/**
* The kind of filter rule.
*/

public enum JWFilterGlobRuleKind
{
/**
* The filter rule specifies an include.
*/

INCLUDE,

/**
* The filter rule specifies an exclude.
*/

EXCLUDE,

/**
* The filter rule specifies an include that immediately halts evaluation.
*/

INCLUDE_AND_HALT,

/**
* The filter rule specifies an exclude that immediately halts evaluation.
*/

EXCLUDE_AND_HALT
}
@@ -0,0 +1,78 @@
/*
* Copyright © 2021 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jwheatsheaf.filter.glob.internal;

import com.io7m.jwheatsheaf.api.JWFileChooserFilterType;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;

public final class JWFilterGlob implements JWFileChooserFilterType
{
private final String description;
private final List<JWFilterGlobRule> rules;

public JWFilterGlob(
final String inDescription,
final List<JWFilterGlobRule> inRules)
{
this.description =
Objects.requireNonNull(inDescription, "description");
this.rules =
Objects.requireNonNull(inRules, "rules");
}

@Override
public String description()
{
return this.description;
}

@Override
public boolean isAllowed(
final Path path)
{
final var filesystem = path.getFileSystem();

var included = false;
for (final var rule : this.rules) {
final var matcher =
filesystem.getPathMatcher(String.format("glob:%s", rule.pattern()));
final var matches =
matcher.matches(path);

if (matches) {
switch (rule.kind()) {
case INCLUDE:
included = true;
break;
case EXCLUDE:
included = false;
break;
case INCLUDE_AND_HALT:
return true;
case EXCLUDE_AND_HALT:
return Files.isDirectory(path);
}
}
}

return included || Files.isDirectory(path);
}
}
@@ -0,0 +1,58 @@
/*
* Copyright © 2021 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jwheatsheaf.filter.glob.internal;

import com.io7m.jwheatsheaf.api.JWFileChooserFilterType;
import com.io7m.jwheatsheaf.filter.glob.JWFilterGlobBuilderType;
import com.io7m.jwheatsheaf.filter.glob.JWFilterGlobRuleKind;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public final class JWFilterGlobBuilder implements JWFilterGlobBuilderType
{
private final String description;
private final ArrayList<JWFilterGlobRule> rules;

public JWFilterGlobBuilder(
final String inDescription)
{
this.description =
Objects.requireNonNull(inDescription, "description");
this.rules =
new ArrayList<>();
}

@Override
public JWFilterGlobBuilderType addRule(
final JWFilterGlobRuleKind kind,
final String pattern)
{
Objects.requireNonNull(kind, "kind");
Objects.requireNonNull(pattern, "pattern");

this.rules.add(JWFilterGlobRule.of(kind, pattern));
return this;
}

@Override
public JWFileChooserFilterType build()
{
return new JWFilterGlob(this.description, List.copyOf(this.rules));
}
}

0 comments on commit 4a08b85

Please sign in to comment.