|
| 1 | +/* |
| 2 | + * Copyright 2013 Deutsche Nationalbibliothek |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.culturegraph.mf.stream.pipe; |
| 17 | + |
| 18 | +import java.util.regex.Matcher; |
| 19 | +import java.util.regex.Pattern; |
| 20 | + |
| 21 | +import org.culturegraph.mf.framework.DefaultObjectPipe; |
| 22 | +import org.culturegraph.mf.framework.ObjectReceiver; |
| 23 | +import org.culturegraph.mf.framework.annotations.Description; |
| 24 | +import org.culturegraph.mf.framework.annotations.In; |
| 25 | +import org.culturegraph.mf.framework.annotations.Out; |
| 26 | +import org.culturegraph.mf.types.Triple; |
| 27 | + |
| 28 | +/** |
| 29 | + * Filters triples. The patterns for subject, predicate and object are disjunctive. |
| 30 | + * |
| 31 | + * @author Christoph Böhme |
| 32 | + * |
| 33 | + */ |
| 34 | +@Description("Filters triple. The patterns for subject, predicate and object are disjunctive.") |
| 35 | +@In(Triple.class) |
| 36 | +@Out(Triple.class) |
| 37 | +public final class TripleFilter extends |
| 38 | + DefaultObjectPipe<Triple, ObjectReceiver<Triple>> { |
| 39 | + |
| 40 | + // A regexp that is guaranteed to never match ( an `a` after the end |
| 41 | + // of the string, see http://stackoverflow.com/a/1723225 for details): |
| 42 | + private static final Matcher MATCH_NOTHING = Pattern.compile("$a").matcher(""); |
| 43 | + |
| 44 | + private Matcher subjectMatcher = MATCH_NOTHING; |
| 45 | + private Matcher predicateMatcher = MATCH_NOTHING; |
| 46 | + private Matcher objectMatcher = MATCH_NOTHING; |
| 47 | + private boolean passMatches = true; |
| 48 | + |
| 49 | + public String getSubjectPattern() { |
| 50 | + return subjectMatcher.pattern().pattern(); |
| 51 | + } |
| 52 | + |
| 53 | + public void setSubjectPattern(final String pattern) { |
| 54 | + subjectMatcher = Pattern.compile(pattern).matcher(""); |
| 55 | + } |
| 56 | + |
| 57 | + public String getPredicatePattern() { |
| 58 | + return predicateMatcher.pattern().pattern(); |
| 59 | + } |
| 60 | + |
| 61 | + public void setPredicatePattern(final String pattern) { |
| 62 | + predicateMatcher = Pattern.compile(pattern).matcher(""); |
| 63 | + } |
| 64 | + |
| 65 | + public String getObjectPattern() { |
| 66 | + return objectMatcher.pattern().pattern(); |
| 67 | + } |
| 68 | + |
| 69 | + public void setObjectPattern(final String pattern) { |
| 70 | + objectMatcher = Pattern.compile(pattern).matcher(""); |
| 71 | + } |
| 72 | + |
| 73 | + public boolean isPassMatches() { |
| 74 | + return passMatches; |
| 75 | + } |
| 76 | + |
| 77 | + public void setPassMatches(final boolean passMatches) { |
| 78 | + this.passMatches = passMatches; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void process(final Triple obj) { |
| 83 | + subjectMatcher.reset(obj.getSubject()); |
| 84 | + predicateMatcher.reset(obj.getPredicate()); |
| 85 | + objectMatcher.reset(obj.getObject()); |
| 86 | + |
| 87 | + final boolean matches = subjectMatcher.matches() || predicateMatcher.matches() || objectMatcher.matches(); |
| 88 | + |
| 89 | + if ((matches && passMatches) || (!matches && !passMatches)) { |
| 90 | + getReceiver().process(obj); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments