Skip to content

Commit 2dc2531

Browse files
committed
ADD contains and not-contains to metamorph; CHG extent filter to work with parameters like morph
1 parent 804e76a commit 2dc2531

File tree

8 files changed

+210
-26
lines changed

8 files changed

+210
-26
lines changed

metamorph/src/main/java/org/metafacture/metamorph/Filter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import org.metafacture.framework.annotations.Out;
2525
import org.metafacture.framework.helpers.DefaultStreamPipe;
2626
import org.metafacture.javaintegration.SingleValue;
27+
import org.metafacture.metamorph.api.InterceptorFactory;
28+
29+
import java.util.Map;
2730

2831
/**
2932
* Filters a stream based on a morph definition. A record is accepted if the
@@ -42,6 +45,7 @@ public final class Filter extends DefaultStreamPipe<StreamReceiver> {
4245
private final SingleValue singleValue = new SingleValue();
4346
private final Metamorph metamorph;
4447

48+
4549
public Filter(final String morphDef) {
4650
super();
4751
metamorph = new Metamorph(morphDef);
@@ -54,6 +58,20 @@ public Filter(final Metamorph metamorph) {
5458
metamorph.setReceiver(singleValue);
5559
}
5660

61+
public Filter(final String morphDef, final Map<String, String> vars) {
62+
63+
super();
64+
metamorph = new Metamorph(morphDef, vars);
65+
metamorph.setReceiver(singleValue);
66+
}
67+
68+
public Filter(final String morphDef, final InterceptorFactory interceptorFactory) {
69+
70+
super();
71+
metamorph = new Metamorph(morphDef, interceptorFactory);
72+
metamorph.setReceiver(singleValue);
73+
}
74+
5775
@Override
5876
protected void onSetReceiver() {
5977
buffer.setReceiver(getReceiver());
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2013, 2014 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.metafacture.metamorph.functions;
17+
18+
import org.metafacture.metamorph.api.helpers.AbstractFilter;
19+
20+
/**
21+
* Checks if the received value contains a given value.
22+
*
23+
* @author Hans-Georg Becker
24+
*/
25+
public final class Contains extends AbstractFilter {
26+
27+
@Override
28+
protected boolean accept(final String value) {
29+
30+
return value.contains(getString());
31+
}
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2013, 2014 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.metafacture.metamorph.functions;
17+
18+
import org.metafacture.metamorph.api.helpers.AbstractFilter;
19+
20+
/**
21+
* Checks whether the received value does not contain a
22+
* given value.
23+
*
24+
* @author Hans-Georg Becker
25+
*/
26+
public final class NotContains extends AbstractFilter {
27+
28+
@Override
29+
protected boolean accept(final String value) {
30+
return !value.contains(getString());
31+
}
32+
33+
}

metamorph/src/main/resources/morph-functions.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ replace org.metafacture.metamorph.functions.Replace
88
isbn org.metafacture.metamorph.functions.ISBN
99
equals org.metafacture.metamorph.functions.Equals
1010
not-equals org.metafacture.metamorph.functions.NotEquals
11+
contains org.metafacture.metamorph.functions.Contains
12+
not-contains org.metafacture.metamorph.functions.NotContains
1113
case org.metafacture.metamorph.functions.Case
1214
htmlanchor org.metafacture.metamorph.functions.HtmlAnchor
1315
trim org.metafacture.metamorph.functions.Trim

metamorph/src/main/resources/schemata/metamorph.xsd

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,27 @@
302302
<attribute name="value" type="string" use="required" />
303303

304304
<attribute name="reset" type="boolean" use="optional"
305-
default="false" />
305+
default="false" />
306306
<attribute name="sameEntity" type="boolean" use="optional"
307-
default="false" />
307+
default="false" />
308+
<attribute name="flushWith" type="string" use="optional" />
309+
</complexType>
310+
</element>
311+
312+
<element name="containsFilter">
313+
<complexType>
314+
<sequence>
315+
<element ref="tns:if" minOccurs="0" maxOccurs="1" />
316+
<group ref="tns:literal-rule" minOccurs="1" maxOccurs="unbounded" />
317+
<element ref="tns:postprocess" minOccurs="0" maxOccurs="1" />
318+
</sequence>
319+
<attribute name="name" type="string" use="required" />
320+
<attribute name="value" type="string" use="required" />
321+
322+
<attribute name="reset" type="boolean" use="optional"
323+
default="false" />
324+
<attribute name="sameEntity" type="boolean" use="optional"
325+
default="false" />
308326
<attribute name="flushWith" type="string" use="optional" />
309327
</complexType>
310328
</element>
@@ -653,6 +671,7 @@
653671
<element ref="tns:tuples" />
654672
<element ref="tns:range" />
655673
<element ref="tns:equalsFilter" />
674+
<element ref="tns:containsFilter" />
656675
</choice>
657676
</group>
658677

@@ -668,6 +687,8 @@
668687
<element ref="tns:isbn" />
669688
<element ref="tns:equals" />
670689
<element ref="tns:not-equals" />
690+
<element ref="tns:contains" />
691+
<element ref="tns:not-contains" />
671692
<element ref="tns:case" />
672693
<element ref="tns:timestamp" />
673694
<element ref="tns:dateformat" />
@@ -907,6 +928,16 @@
907928
</complexType>
908929
</element>
909930

931+
<element name="contains">
932+
<annotation>
933+
<documentation>Returns the value only if given string is contained.
934+
</documentation>
935+
</annotation>
936+
<complexType>
937+
<attribute name="string" type="string" use="required" />
938+
</complexType>
939+
</element>
940+
910941
<element name="unique">
911942
<annotation>
912943
<documentation>Filters out dublicate literals</documentation>
@@ -949,6 +980,16 @@
949980
</complexType>
950981
</element>
951982

983+
<element name="not-contains">
984+
<annotation>
985+
<documentation>Returns value only if given string is not contained.
986+
</documentation>
987+
</annotation>
988+
<complexType>
989+
<attribute name="string" type="string" use="required" />
990+
</complexType>
991+
</element>
992+
952993
<element name="buffer">
953994
<annotation>
954995
<documentation>Buffers literals and resleases them on a flush signal.

metamorph/src/test/java/org/metafacture/metamorph/functions/RegexpTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,4 @@ public void shouldIgnoreEmptyMatchGroups() {
9595
ordered.verify(receiver).endRecord();
9696
ordered.verifyNoMoreInteractions();
9797
}
98-
9998
}

metamorph/src/test/java/org/metafacture/metamorph/functions/TestVariousFunctions.java

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
import org.mockito.junit.MockitoJUnit;
2828
import org.mockito.junit.MockitoRule;
2929

30+
import java.io.BufferedOutputStream;
31+
import java.io.IOException;
32+
import java.io.ObjectOutputStream;
33+
import java.io.OutputStream;
34+
3035
/**
3136
* Tests for various Metamorph functions.
3237
*
@@ -213,31 +218,57 @@ public void testCaseFunction() {
213218
ordered.verifyNoMoreInteractions();
214219
}
215220

216-
@Test
217-
public void testEqualsFunction() {
218-
metamorph = InlineMorph.in(this)
219-
.with("<rules>")
220-
.with(" <data source='data' name='data1'>")
221-
.with(" <equals string='Aloha' />")
222-
.with(" </data>")
223-
.with(" <data source='data' name='data2'>")
224-
.with(" <not-equals string='Aloha' />")
225-
.with(" </data>")
226-
.with("</rules>")
227-
.createConnectedTo(receiver);
221+
@Test
222+
public void testEqualsFunction() {
223+
metamorph = InlineMorph.in(this)
224+
.with("<rules>")
225+
.with(" <data source='data' name='data1'>")
226+
.with(" <equals string='Aloha' />")
227+
.with(" </data>")
228+
.with(" <data source='data' name='data2'>")
229+
.with(" <not-equals string='Aloha' />")
230+
.with(" </data>")
231+
.with("</rules>")
232+
.createConnectedTo(receiver);
228233

229-
metamorph.startRecord("1");
230-
metamorph.literal("data", "Aloha");
231-
metamorph.literal("data", "Hawaii");
232-
metamorph.endRecord();
234+
metamorph.startRecord("1");
235+
metamorph.literal("data", "Aloha");
236+
metamorph.literal("data", "Hawaii");
237+
metamorph.endRecord();
233238

234-
final InOrder ordered = inOrder(receiver);
235-
ordered.verify(receiver).startRecord("1");
236-
ordered.verify(receiver).literal("data1", "Aloha");
237-
ordered.verify(receiver).literal("data2", "Hawaii");
238-
ordered.verify(receiver).endRecord();
239-
ordered.verifyNoMoreInteractions();
240-
}
239+
final InOrder ordered = inOrder(receiver);
240+
ordered.verify(receiver).startRecord("1");
241+
ordered.verify(receiver).literal("data1", "Aloha");
242+
ordered.verify(receiver).literal("data2", "Hawaii");
243+
ordered.verify(receiver).endRecord();
244+
ordered.verifyNoMoreInteractions();
245+
}
246+
247+
@Test
248+
public void testContainsFunction() {
249+
metamorph = InlineMorph.in(this)
250+
.with("<rules>")
251+
.with(" <data source='data' name='data1'>")
252+
.with(" <contains string='Periodical' />")
253+
.with(" </data>")
254+
.with(" <data source='data' name='data2'>")
255+
.with(" <not-contains string='Periodical' />")
256+
.with(" </data>")
257+
.with("</rules>")
258+
.createConnectedTo(receiver);
259+
260+
metamorph.startRecord("1");
261+
metamorph.literal("data", "1990 Periodical MultiVolumeBook");
262+
metamorph.literal("data", "2013 BibliographicResource Book Series");
263+
metamorph.endRecord();
264+
265+
final InOrder ordered = inOrder(receiver);
266+
ordered.verify(receiver).startRecord("1");
267+
ordered.verify(receiver).literal("data1", "1990 Periodical MultiVolumeBook");
268+
ordered.verify(receiver).literal("data2", "2013 BibliographicResource Book Series");
269+
ordered.verify(receiver).endRecord();
270+
ordered.verifyNoMoreInteractions();
271+
}
241272

242273
@Test
243274
public void testBufferFunction() {

metamorph/src/test/java/org/metafacture/metamorph/maps/FileMapTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,31 @@ public void shouldLookupValuesInFileBasedMap() {
6969
ordered.verifyNoMoreInteractions();
7070
}
7171

72+
@Test
73+
public void shouldWhitelistValuesInFileBasedMap() {
74+
metamorph = InlineMorph.in(this)
75+
.with("<rules>")
76+
.with(" <data source='1'>")
77+
.with(" <whitelist map='map1' />")
78+
.with(" </data>")
79+
.with("</rules>")
80+
.with("<maps>")
81+
.with(" <filemap name='map1' files='org/metafacture/metamorph/maps/file-map-test.txt' />")
82+
.with("</maps>")
83+
.createConnectedTo(receiver);
84+
85+
metamorph.startRecord("1");
86+
metamorph.literal("1", "gw");
87+
metamorph.literal("1", "fj");
88+
metamorph.literal("1", "bla");
89+
metamorph.endRecord();
90+
91+
final InOrder ordered = inOrder(receiver);
92+
ordered.verify(receiver).startRecord("1");
93+
ordered.verify(receiver).literal("1", "gw");
94+
ordered.verify(receiver).literal("1", "fj");
95+
ordered.verify(receiver).endRecord();
96+
ordered.verifyNoMoreInteractions();
97+
}
98+
7299
}

0 commit comments

Comments
 (0)