From 2dc25311b5697bfba958b5336279a66024b86906 Mon Sep 17 00:00:00 2001 From: hagbeck Date: Mon, 2 Mar 2020 10:45:20 +0100 Subject: [PATCH] ADD contains and not-contains to metamorph; CHG extent filter to work with parameters like morph --- .../org/metafacture/metamorph/Filter.java | 18 +++++ .../metamorph/functions/Contains.java | 33 ++++++++ .../metamorph/functions/NotContains.java | 33 ++++++++ .../main/resources/morph-functions.properties | 2 + .../src/main/resources/schemata/metamorph.xsd | 45 ++++++++++- .../metamorph/functions/RegexpTest.java | 1 - .../functions/TestVariousFunctions.java | 77 +++++++++++++------ .../metamorph/maps/FileMapTest.java | 27 +++++++ 8 files changed, 210 insertions(+), 26 deletions(-) create mode 100644 metamorph/src/main/java/org/metafacture/metamorph/functions/Contains.java create mode 100644 metamorph/src/main/java/org/metafacture/metamorph/functions/NotContains.java diff --git a/metamorph/src/main/java/org/metafacture/metamorph/Filter.java b/metamorph/src/main/java/org/metafacture/metamorph/Filter.java index 12cc67d07..9b990c58e 100644 --- a/metamorph/src/main/java/org/metafacture/metamorph/Filter.java +++ b/metamorph/src/main/java/org/metafacture/metamorph/Filter.java @@ -24,6 +24,9 @@ import org.metafacture.framework.annotations.Out; import org.metafacture.framework.helpers.DefaultStreamPipe; import org.metafacture.javaintegration.SingleValue; +import org.metafacture.metamorph.api.InterceptorFactory; + +import java.util.Map; /** * Filters a stream based on a morph definition. A record is accepted if the @@ -42,6 +45,7 @@ public final class Filter extends DefaultStreamPipe { private final SingleValue singleValue = new SingleValue(); private final Metamorph metamorph; + public Filter(final String morphDef) { super(); metamorph = new Metamorph(morphDef); @@ -54,6 +58,20 @@ public Filter(final Metamorph metamorph) { metamorph.setReceiver(singleValue); } + public Filter(final String morphDef, final Map vars) { + + super(); + metamorph = new Metamorph(morphDef, vars); + metamorph.setReceiver(singleValue); + } + + public Filter(final String morphDef, final InterceptorFactory interceptorFactory) { + + super(); + metamorph = new Metamorph(morphDef, interceptorFactory); + metamorph.setReceiver(singleValue); + } + @Override protected void onSetReceiver() { buffer.setReceiver(getReceiver()); diff --git a/metamorph/src/main/java/org/metafacture/metamorph/functions/Contains.java b/metamorph/src/main/java/org/metafacture/metamorph/functions/Contains.java new file mode 100644 index 000000000..9c51f011b --- /dev/null +++ b/metamorph/src/main/java/org/metafacture/metamorph/functions/Contains.java @@ -0,0 +1,33 @@ +/* + * Copyright 2013, 2014 Deutsche Nationalbibliothek + * + * Licensed 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 org.metafacture.metamorph.functions; + +import org.metafacture.metamorph.api.helpers.AbstractFilter; + +/** + * Checks if the received value contains a given value. + * + * @author Hans-Georg Becker + */ +public final class Contains extends AbstractFilter { + + @Override + protected boolean accept(final String value) { + + return value.contains(getString()); + } + +} diff --git a/metamorph/src/main/java/org/metafacture/metamorph/functions/NotContains.java b/metamorph/src/main/java/org/metafacture/metamorph/functions/NotContains.java new file mode 100644 index 000000000..a3d84db79 --- /dev/null +++ b/metamorph/src/main/java/org/metafacture/metamorph/functions/NotContains.java @@ -0,0 +1,33 @@ +/* + * Copyright 2013, 2014 Deutsche Nationalbibliothek + * + * Licensed 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 org.metafacture.metamorph.functions; + +import org.metafacture.metamorph.api.helpers.AbstractFilter; + +/** + * Checks whether the received value does not contain a + * given value. + * + * @author Hans-Georg Becker + */ +public final class NotContains extends AbstractFilter { + + @Override + protected boolean accept(final String value) { + return !value.contains(getString()); + } + +} diff --git a/metamorph/src/main/resources/morph-functions.properties b/metamorph/src/main/resources/morph-functions.properties index 8387e7da1..346df769c 100644 --- a/metamorph/src/main/resources/morph-functions.properties +++ b/metamorph/src/main/resources/morph-functions.properties @@ -8,6 +8,8 @@ replace org.metafacture.metamorph.functions.Replace isbn org.metafacture.metamorph.functions.ISBN equals org.metafacture.metamorph.functions.Equals not-equals org.metafacture.metamorph.functions.NotEquals +contains org.metafacture.metamorph.functions.Contains +not-contains org.metafacture.metamorph.functions.NotContains case org.metafacture.metamorph.functions.Case htmlanchor org.metafacture.metamorph.functions.HtmlAnchor trim org.metafacture.metamorph.functions.Trim diff --git a/metamorph/src/main/resources/schemata/metamorph.xsd b/metamorph/src/main/resources/schemata/metamorph.xsd index 52c04d2c4..9bbea22db 100644 --- a/metamorph/src/main/resources/schemata/metamorph.xsd +++ b/metamorph/src/main/resources/schemata/metamorph.xsd @@ -302,9 +302,27 @@ + default="false" /> + default="false" /> + + + + + + + + + + + + + + + + @@ -653,6 +671,7 @@ + @@ -668,6 +687,8 @@ + + @@ -907,6 +928,16 @@ + + + Returns the value only if given string is contained. + + + + + + + Filters out dublicate literals @@ -949,6 +980,16 @@ + + + Returns value only if given string is not contained. + + + + + + + Buffers literals and resleases them on a flush signal. diff --git a/metamorph/src/test/java/org/metafacture/metamorph/functions/RegexpTest.java b/metamorph/src/test/java/org/metafacture/metamorph/functions/RegexpTest.java index 828cf4a27..136b002c6 100644 --- a/metamorph/src/test/java/org/metafacture/metamorph/functions/RegexpTest.java +++ b/metamorph/src/test/java/org/metafacture/metamorph/functions/RegexpTest.java @@ -95,5 +95,4 @@ public void shouldIgnoreEmptyMatchGroups() { ordered.verify(receiver).endRecord(); ordered.verifyNoMoreInteractions(); } - } diff --git a/metamorph/src/test/java/org/metafacture/metamorph/functions/TestVariousFunctions.java b/metamorph/src/test/java/org/metafacture/metamorph/functions/TestVariousFunctions.java index 87cfcc422..5dc154f9d 100644 --- a/metamorph/src/test/java/org/metafacture/metamorph/functions/TestVariousFunctions.java +++ b/metamorph/src/test/java/org/metafacture/metamorph/functions/TestVariousFunctions.java @@ -27,6 +27,11 @@ import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.OutputStream; + /** * Tests for various Metamorph functions. * @@ -213,31 +218,57 @@ public void testCaseFunction() { ordered.verifyNoMoreInteractions(); } - @Test - public void testEqualsFunction() { - metamorph = InlineMorph.in(this) - .with("") - .with(" ") - .with(" ") - .with(" ") - .with(" ") - .with(" ") - .with(" ") - .with("") - .createConnectedTo(receiver); + @Test + public void testEqualsFunction() { + metamorph = InlineMorph.in(this) + .with("") + .with(" ") + .with(" ") + .with(" ") + .with(" ") + .with(" ") + .with(" ") + .with("") + .createConnectedTo(receiver); - metamorph.startRecord("1"); - metamorph.literal("data", "Aloha"); - metamorph.literal("data", "Hawaii"); - metamorph.endRecord(); + metamorph.startRecord("1"); + metamorph.literal("data", "Aloha"); + metamorph.literal("data", "Hawaii"); + metamorph.endRecord(); - final InOrder ordered = inOrder(receiver); - ordered.verify(receiver).startRecord("1"); - ordered.verify(receiver).literal("data1", "Aloha"); - ordered.verify(receiver).literal("data2", "Hawaii"); - ordered.verify(receiver).endRecord(); - ordered.verifyNoMoreInteractions(); - } + final InOrder ordered = inOrder(receiver); + ordered.verify(receiver).startRecord("1"); + ordered.verify(receiver).literal("data1", "Aloha"); + ordered.verify(receiver).literal("data2", "Hawaii"); + ordered.verify(receiver).endRecord(); + ordered.verifyNoMoreInteractions(); + } + + @Test + public void testContainsFunction() { + metamorph = InlineMorph.in(this) + .with("") + .with(" ") + .with(" ") + .with(" ") + .with(" ") + .with(" ") + .with(" ") + .with("") + .createConnectedTo(receiver); + + metamorph.startRecord("1"); + metamorph.literal("data", "1990 Periodical MultiVolumeBook"); + metamorph.literal("data", "2013 BibliographicResource Book Series"); + metamorph.endRecord(); + + final InOrder ordered = inOrder(receiver); + ordered.verify(receiver).startRecord("1"); + ordered.verify(receiver).literal("data1", "1990 Periodical MultiVolumeBook"); + ordered.verify(receiver).literal("data2", "2013 BibliographicResource Book Series"); + ordered.verify(receiver).endRecord(); + ordered.verifyNoMoreInteractions(); + } @Test public void testBufferFunction() { diff --git a/metamorph/src/test/java/org/metafacture/metamorph/maps/FileMapTest.java b/metamorph/src/test/java/org/metafacture/metamorph/maps/FileMapTest.java index f7d0c574a..bde21a7f1 100644 --- a/metamorph/src/test/java/org/metafacture/metamorph/maps/FileMapTest.java +++ b/metamorph/src/test/java/org/metafacture/metamorph/maps/FileMapTest.java @@ -69,4 +69,31 @@ public void shouldLookupValuesInFileBasedMap() { ordered.verifyNoMoreInteractions(); } + @Test + public void shouldWhitelistValuesInFileBasedMap() { + metamorph = InlineMorph.in(this) + .with("") + .with(" ") + .with(" ") + .with(" ") + .with("") + .with("") + .with(" ") + .with("") + .createConnectedTo(receiver); + + metamorph.startRecord("1"); + metamorph.literal("1", "gw"); + metamorph.literal("1", "fj"); + metamorph.literal("1", "bla"); + metamorph.endRecord(); + + final InOrder ordered = inOrder(receiver); + ordered.verify(receiver).startRecord("1"); + ordered.verify(receiver).literal("1", "gw"); + ordered.verify(receiver).literal("1", "fj"); + ordered.verify(receiver).endRecord(); + ordered.verifyNoMoreInteractions(); + } + }