Skip to content

Commit ada293f

Browse files
committed
Don't send empty matches to receiver of regexp morph function
1 parent a62d463 commit ada293f

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

src/main/java/org/culturegraph/mf/morph/functions/Regexp.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,29 @@ public void receive(final String name, final String value, final NamedValueSourc
4242
matcher.reset(value);
4343
if (null == format) {
4444
while (matcher.find()) {
45-
getNamedValueReceiver().receive(name, matcher.group(), source, recordCount, entityCount);
45+
final String group = matcher.group();
46+
if (!group.isEmpty()) {
47+
getNamedValueReceiver().receive(name, group, source, recordCount, entityCount);
48+
}
4649
}
4750
} else {
4851
while (matcher.find()) {
49-
getNamedValueReceiver().receive(name, matchAndFormat(), source, recordCount, entityCount);
52+
if (!matchAndFormatIsEmpty()) {
53+
getNamedValueReceiver().receive(name,
54+
StringUtil.format(format, tempVars), source, recordCount, entityCount);
55+
}
5056
}
5157
}
5258
}
5359

54-
private String matchAndFormat() {
60+
private boolean matchAndFormatIsEmpty() {
5561
tempVars.clear();
5662
for (int i = 0; i <= matcher.groupCount(); ++i) {
57-
tempVars.put(String.valueOf(i), matcher.group(i));
63+
if (!matcher.group(i).isEmpty()) {
64+
tempVars.put(String.valueOf(i), matcher.group(i));
65+
}
5866
}
59-
return StringUtil.format(format, tempVars);
67+
return tempVars.isEmpty();
6068
}
6169

6270
/**

src/test/java/org/culturegraph/mf/morph/functions/FunctionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
* @author Markus Michael Geipel
2525
*/
2626
@RunWith(TestSuite.class)
27-
@TestDefinitions({"Basic.xml","Lookup.xml","Stateful.xml", "Unique.xml", "Misc.xml", "Script.xml"})
27+
@TestDefinitions({"Basic.xml","Lookup.xml","Stateful.xml", "Unique.xml", "Misc.xml", "Script.xml", "Regexp.xml"})
2828
public final class FunctionTest {/*bind to xml test*/}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<metamorph-test version="1.0"
3+
xmlns="http://www.culturegraph.org/metamorph-test" xmlns:cgxml="http://www.culturegraph.org/cgxml"
4+
xmlns:mm="http://www.culturegraph.org/metamorph">
5+
<test-case name="Regexp function">
6+
<input type="text/x-cg+xml">
7+
<cgxml:cgxml version="1.0">
8+
<cgxml:records>
9+
<cgxml:record id="1">
10+
<cgxml:entity name="001">
11+
<cgxml:literal name="" value="184000" />
12+
</cgxml:entity>
13+
</cgxml:record>
14+
</cgxml:records>
15+
</cgxml:cgxml>
16+
</input>
17+
<transformation type="text/x-metamorph+xml">
18+
<mm:metamorph version="1">
19+
<mm:rules>
20+
<mm:data source="001." name="subject">
21+
<mm:regexp match=".*" format="resource:P${0}" />
22+
</mm:data>
23+
<mm:data source="001." name="subject">
24+
<mm:regexp match=".*" />
25+
</mm:data>
26+
</mm:rules>
27+
</mm:metamorph>
28+
</transformation>
29+
<result type="text/x-cg+xml">
30+
<cgxml:cgxml version="1.0">
31+
<cgxml:records>
32+
<cgxml:record id="1">
33+
<cgxml:literal name="subject" value="resource:P184000" />
34+
<cgxml:literal name="subject" value="184000" />
35+
</cgxml:record>
36+
</cgxml:records>
37+
</cgxml:cgxml>
38+
</result>
39+
</test-case>
40+
</metamorph-test>

0 commit comments

Comments
 (0)