Skip to content

Commit

Permalink
Merge branch 'UB-Dortmund-add_contains_functions_and_extents_filter_c…
Browse files Browse the repository at this point in the history
…ommand'
  • Loading branch information
dr0i committed Mar 12, 2020
2 parents 804e76a + 2dc2531 commit 1115f16
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 26 deletions.
18 changes: 18 additions & 0 deletions metamorph/src/main/java/org/metafacture/metamorph/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,6 +45,7 @@ public final class Filter extends DefaultStreamPipe<StreamReceiver> {
private final SingleValue singleValue = new SingleValue();
private final Metamorph metamorph;


public Filter(final String morphDef) {
super();
metamorph = new Metamorph(morphDef);
Expand All @@ -54,6 +58,20 @@ public Filter(final Metamorph metamorph) {
metamorph.setReceiver(singleValue);
}

public Filter(final String morphDef, final Map<String, String> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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());
}

}
2 changes: 2 additions & 0 deletions metamorph/src/main/resources/morph-functions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 43 additions & 2 deletions metamorph/src/main/resources/schemata/metamorph.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,27 @@
<attribute name="value" type="string" use="required" />

<attribute name="reset" type="boolean" use="optional"
default="false" />
default="false" />
<attribute name="sameEntity" type="boolean" use="optional"
default="false" />
default="false" />
<attribute name="flushWith" type="string" use="optional" />
</complexType>
</element>

<element name="containsFilter">
<complexType>
<sequence>
<element ref="tns:if" minOccurs="0" maxOccurs="1" />
<group ref="tns:literal-rule" minOccurs="1" maxOccurs="unbounded" />
<element ref="tns:postprocess" minOccurs="0" maxOccurs="1" />
</sequence>
<attribute name="name" type="string" use="required" />
<attribute name="value" type="string" use="required" />

<attribute name="reset" type="boolean" use="optional"
default="false" />
<attribute name="sameEntity" type="boolean" use="optional"
default="false" />
<attribute name="flushWith" type="string" use="optional" />
</complexType>
</element>
Expand Down Expand Up @@ -653,6 +671,7 @@
<element ref="tns:tuples" />
<element ref="tns:range" />
<element ref="tns:equalsFilter" />
<element ref="tns:containsFilter" />
</choice>
</group>

Expand All @@ -668,6 +687,8 @@
<element ref="tns:isbn" />
<element ref="tns:equals" />
<element ref="tns:not-equals" />
<element ref="tns:contains" />
<element ref="tns:not-contains" />
<element ref="tns:case" />
<element ref="tns:timestamp" />
<element ref="tns:dateformat" />
Expand Down Expand Up @@ -907,6 +928,16 @@
</complexType>
</element>

<element name="contains">
<annotation>
<documentation>Returns the value only if given string is contained.
</documentation>
</annotation>
<complexType>
<attribute name="string" type="string" use="required" />
</complexType>
</element>

<element name="unique">
<annotation>
<documentation>Filters out dublicate literals</documentation>
Expand Down Expand Up @@ -949,6 +980,16 @@
</complexType>
</element>

<element name="not-contains">
<annotation>
<documentation>Returns value only if given string is not contained.
</documentation>
</annotation>
<complexType>
<attribute name="string" type="string" use="required" />
</complexType>
</element>

<element name="buffer">
<annotation>
<documentation>Buffers literals and resleases them on a flush signal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,4 @@ public void shouldIgnoreEmptyMatchGroups() {
ordered.verify(receiver).endRecord();
ordered.verifyNoMoreInteractions();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -213,31 +218,57 @@ public void testCaseFunction() {
ordered.verifyNoMoreInteractions();
}

@Test
public void testEqualsFunction() {
metamorph = InlineMorph.in(this)
.with("<rules>")
.with(" <data source='data' name='data1'>")
.with(" <equals string='Aloha' />")
.with(" </data>")
.with(" <data source='data' name='data2'>")
.with(" <not-equals string='Aloha' />")
.with(" </data>")
.with("</rules>")
.createConnectedTo(receiver);
@Test
public void testEqualsFunction() {
metamorph = InlineMorph.in(this)
.with("<rules>")
.with(" <data source='data' name='data1'>")
.with(" <equals string='Aloha' />")
.with(" </data>")
.with(" <data source='data' name='data2'>")
.with(" <not-equals string='Aloha' />")
.with(" </data>")
.with("</rules>")
.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("<rules>")
.with(" <data source='data' name='data1'>")
.with(" <contains string='Periodical' />")
.with(" </data>")
.with(" <data source='data' name='data2'>")
.with(" <not-contains string='Periodical' />")
.with(" </data>")
.with("</rules>")
.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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,31 @@ public void shouldLookupValuesInFileBasedMap() {
ordered.verifyNoMoreInteractions();
}

@Test
public void shouldWhitelistValuesInFileBasedMap() {
metamorph = InlineMorph.in(this)
.with("<rules>")
.with(" <data source='1'>")
.with(" <whitelist map='map1' />")
.with(" </data>")
.with("</rules>")
.with("<maps>")
.with(" <filemap name='map1' files='org/metafacture/metamorph/maps/file-map-test.txt' />")
.with("</maps>")
.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();
}

}

0 comments on commit 1115f16

Please sign in to comment.