Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds an API for constructing rule-based glob filters for files. Fix: #12
- Loading branch information
Showing
15 changed files
with
772 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
46 changes: 46 additions & 0 deletions
46
...f.filter.glob/src/main/java/com/io7m/jwheatsheaf/filter/glob/JWFilterGlobBuilderType.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,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(); | ||
} |
42 changes: 42 additions & 0 deletions
42
...sheaf.filter.glob/src/main/java/com/io7m/jwheatsheaf/filter/glob/JWFilterGlobFactory.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,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); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...f.filter.glob/src/main/java/com/io7m/jwheatsheaf/filter/glob/JWFilterGlobFactoryType.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,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); | ||
} |
48 changes: 48 additions & 0 deletions
48
...heaf.filter.glob/src/main/java/com/io7m/jwheatsheaf/filter/glob/JWFilterGlobRuleKind.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,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 | ||
} |
78 changes: 78 additions & 0 deletions
78
...eaf.filter.glob/src/main/java/com/io7m/jwheatsheaf/filter/glob/internal/JWFilterGlob.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,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); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...ter.glob/src/main/java/com/io7m/jwheatsheaf/filter/glob/internal/JWFilterGlobBuilder.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,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)); | ||
} | ||
} |
Oops, something went wrong.