Skip to content

Commit 271fc14

Browse files
author
mgeipel
committed
fixed #19
1 parent cec781c commit 271fc14

File tree

3 files changed

+390
-361
lines changed

3 files changed

+390
-361
lines changed

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

Lines changed: 75 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,79 +13,78 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.culturegraph.mf.morph.functions;
17-
18-
import java.util.HashMap;
19-
import java.util.Map;
20-
import java.util.regex.Matcher;
21-
import java.util.regex.Pattern;
22-
23-
import org.culturegraph.mf.exceptions.MorphException;
24-
import org.culturegraph.mf.util.StringUtil;
25-
26-
27-
28-
29-
/**
30-
* Performs regexp matching
31-
* @author Markus Michael Geipel
32-
*/
33-
public final class Regexp extends AbstractSimpleStatelessFunction {
34-
35-
//private static final String TRUE = "true";
36-
private Matcher matcher;
37-
private String format;
38-
private final Map<String, String> tempVars = new HashMap<String, String>();
39-
40-
@Override
41-
public String process(final String value) {
42-
matcher.reset(value);
43-
final String result;
44-
45-
if(matcher.find()){
46-
if(null==format){
47-
result = matcher.group();
48-
}else{
49-
result = matchAndFormat();
50-
}
51-
}else{
52-
result = null;
53-
}
54-
return result;
55-
}
56-
57-
private String matchAndFormat(){
58-
tempVars.clear();
59-
for (int i = 0; i <= matcher.groupCount(); ++i) {
60-
tempVars.put(String.valueOf(i), matcher.group(i));
61-
}
62-
return StringUtil.format(format, tempVars);
63-
}
64-
65-
66-
/**
67-
* @param match the match to set
68-
*/
69-
public void setMatch(final String match) {
70-
this.matcher = Pattern.compile(match).matcher("");
71-
}
72-
73-
/**
74-
* @param format the output to set
75-
*/
76-
public void setFormat(final String format) {
77-
this.format = format;
78-
}
79-
80-
/**
81-
* Thrown if no match was found
82-
*
83-
*/
84-
public static final class PatternNotFoundException extends MorphException{
85-
private static final long serialVersionUID = 4113458605196557204L;
86-
public PatternNotFoundException(final Pattern pattern, final String input) {
87-
super("Pattern '" + pattern + "' not found in '" + input + "'");
88-
}
89-
}
90-
91-
}
16+
package org.culturegraph.mf.morph.functions;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import java.util.regex.Matcher;
21+
import java.util.regex.Pattern;
22+
23+
import org.culturegraph.mf.exceptions.MorphException;
24+
import org.culturegraph.mf.morph.NamedValueSource;
25+
import org.culturegraph.mf.util.StringUtil;
26+
27+
/**
28+
* Performs regexp matching
29+
*
30+
* @author Markus Michael Geipel
31+
*/
32+
public final class Regexp extends AbstractFunction {
33+
34+
// private static final String TRUE = "true";
35+
private Matcher matcher;
36+
private String format;
37+
private final Map<String, String> tempVars = new HashMap<String, String>();
38+
39+
@Override
40+
public void receive(final String name, final String value, final NamedValueSource source, final int recordCount,
41+
final int entityCount) {
42+
matcher.reset(value);
43+
if (null == format) {
44+
while (matcher.find()) {
45+
getNamedValueReceiver().receive(name, matcher.group(), source, recordCount, entityCount);
46+
}
47+
} else {
48+
while (matcher.find()) {
49+
getNamedValueReceiver().receive(name, matchAndFormat(), source, recordCount, entityCount);
50+
}
51+
}
52+
}
53+
54+
private String matchAndFormat() {
55+
tempVars.clear();
56+
for (int i = 0; i <= matcher.groupCount(); ++i) {
57+
tempVars.put(String.valueOf(i), matcher.group(i));
58+
}
59+
return StringUtil.format(format, tempVars);
60+
}
61+
62+
/**
63+
* @param match
64+
* the match to set
65+
*/
66+
public void setMatch(final String match) {
67+
this.matcher = Pattern.compile(match).matcher("");
68+
}
69+
70+
/**
71+
* @param format
72+
* the output to set
73+
*/
74+
public void setFormat(final String format) {
75+
this.format = format;
76+
}
77+
78+
/**
79+
* Thrown if no match was found
80+
*
81+
*/
82+
public static final class PatternNotFoundException extends MorphException {
83+
private static final long serialVersionUID = 4113458605196557204L;
84+
85+
public PatternNotFoundException(final Pattern pattern, final String input) {
86+
super("Pattern '" + pattern + "' not found in '" + input + "'");
87+
}
88+
}
89+
90+
}

0 commit comments

Comments
 (0)