[java-metadata-aggregator] branch master updated: MDA-184 - Add RegexFileFilter
Ian Young
ian at iay.org.uk
Mon Jul 31 09:30:19 EDT 2017
This is an automated email from the git hooks/post-receive script.
iay pushed a commit to branch master
in repository java-metadata-aggregator.
View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=d913f12f6e8078b407833352b4d4f6a6b551b6cb
The following commit(s) were added to refs/heads/master by this push:
new d913f12 MDA-184 - Add RegexFileFilter
d913f12 is described below
commit d913f12f6e8078b407833352b4d4f6a6b551b6cb
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Mon Jul 31 14:30:09 2017 +0100
MDA-184 - Add RegexFileFilter
---
.../shibboleth/metadata/util/RegexFileFilter.java | 68 ++++++++++++++++++++++
.../metadata/util/RegexFileFilterTest.java | 27 +++++++++
2 files changed, 95 insertions(+)
diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/util/RegexFileFilter.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/util/RegexFileFilter.java
new file mode 100644
index 0000000..5fd8950
--- /dev/null
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/util/RegexFileFilter.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.metadata.util;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.util.regex.Pattern;
+
+import javax.annotation.Nonnull;
+import javax.annotation.concurrent.ThreadSafe;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.logic.Constraint;
+import net.shibboleth.utilities.java.support.primitive.StringSupport;
+
+/**
+ * A {@link FileFilter} implementation that selects files on the basis of a regular expression match against the file
+ * name.
+ */
+ at ThreadSafe
+public class RegexFileFilter implements FileFilter {
+
+ /**
+ * Compiled regular expression.
+ */
+ private final Pattern pattern;
+
+ /**
+ * Constructor.
+ *
+ * @param regex Regular expression to match file names against.
+ */
+ public RegexFileFilter(@Nonnull @NotEmpty final String regex) {
+ pattern = Pattern.compile(Constraint.isNotNull(StringSupport.trimOrNull(regex),
+ "regex pattern argument may not be null or empty"));
+ }
+
+ /**
+ * Matches the regular expression against the last file name component of the supplied {@link File} object. As the
+ * whole of the name is being matched against, it is not necessary for the regular expression to contain anchor
+ * characters such as '^' and '$'.
+ *
+ * @param pathname {@link File} to match against the regular expression.
+ *
+ * @return <code>true</code> iff <code>pathname</code> matches the regular expression.
+ */
+ @Override
+ public boolean accept(@Nonnull final File pathname) {
+ assert pathname != null;
+ return pattern.matcher(pathname.getName()).matches();
+ }
+
+}
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/util/RegexFileFilterTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/util/RegexFileFilterTest.java
new file mode 100644
index 0000000..c6ea232
--- /dev/null
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/util/RegexFileFilterTest.java
@@ -0,0 +1,27 @@
+package net.shibboleth.metadata.util;
+
+import java.io.File;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/** {@link RegexFileFilter} unit tests. */
+public class RegexFileFilterTest {
+
+ /**
+ * Test acceptance of files corresponding to the UK federation use case.
+ */
+ @Test
+ public void testAccept() {
+ final RegexFileFilter a = new RegexFileFilter("uk\\d{6}\\.xml");
+ Assert.assertTrue(a.accept(new File("uk123456.xml")));
+ Assert.assertTrue(a.accept(new File("foo/uk123456.xml")));
+ Assert.assertFalse(a.accept(new File("imported.xml")));
+ Assert.assertFalse(a.accept(new File("uk123456.new")));
+ Assert.assertFalse(a.accept(new File("uk123456.xml~")));
+ Assert.assertFalse(a.accept(new File("x-123456.xml")));
+ Assert.assertFalse(a.accept(new File("foo/baruk123456.xml")));
+ Assert.assertFalse(a.accept(new File("uk123456.xml/bar")));
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list