1
|
package eu.dnetlib.dhp.common.java.jsonworkflownodes;
|
2
|
|
3
|
import java.util.regex.Matcher;
|
4
|
import java.util.regex.Pattern;
|
5
|
|
6
|
/**
|
7
|
* Extracts information about port name and its properties from a string
|
8
|
* of a form "{port_name, property_1, property_2, ...}"
|
9
|
* @author Mateusz Kobos
|
10
|
*/
|
11
|
public class StringPortSpecificationExtractor {
|
12
|
private final String[] propertiesRegexp;
|
13
|
private final String portSpecificationRegexp;
|
14
|
private final Pattern pattern;
|
15
|
|
16
|
public static class PortSpecification {
|
17
|
|
18
|
private final String name;
|
19
|
|
20
|
private final String[] properties;
|
21
|
|
22
|
public PortSpecification(String name, String[] properties) {
|
23
|
this.name = name;
|
24
|
this.properties = properties;
|
25
|
}
|
26
|
|
27
|
public String getName() {
|
28
|
return name;
|
29
|
}
|
30
|
|
31
|
public String[] getProperties() {
|
32
|
return properties;
|
33
|
}
|
34
|
}
|
35
|
|
36
|
/**
|
37
|
* @param propertiesRegexp regular expressions specifying pattern for
|
38
|
* each of the properties associated with a port. An example of a single
|
39
|
* specification: {@code "[\\w\\.]+"}.
|
40
|
*/
|
41
|
public StringPortSpecificationExtractor(String[] propertiesRegexp){
|
42
|
this.propertiesRegexp = propertiesRegexp;
|
43
|
this.portSpecificationRegexp = createRegexpString("[\\w\\._]+", propertiesRegexp);
|
44
|
this.pattern = Pattern.compile(this.portSpecificationRegexp);
|
45
|
}
|
46
|
|
47
|
private static String createRegexpString(String portNameRegexp, String[] propertiesRegexp){
|
48
|
StringBuilder regexp = new StringBuilder();
|
49
|
regexp.append("s*\\{\\s*");
|
50
|
regexp.append("("+portNameRegexp+")");
|
51
|
for(String propertyRegexp: propertiesRegexp){
|
52
|
regexp.append(",\\s*("+propertyRegexp+")");
|
53
|
}
|
54
|
regexp.append("\\s*\\}\\s*");
|
55
|
return regexp.toString();
|
56
|
}
|
57
|
|
58
|
private int getPropertiesCount(){
|
59
|
return propertiesRegexp.length;
|
60
|
}
|
61
|
|
62
|
public PortSpecification getSpecification(String text){
|
63
|
Matcher m = pattern.matcher(text);
|
64
|
if(!m.matches()){
|
65
|
throw new RuntimeException(String.format("Specification of " +
|
66
|
"the port (\"%s\") does not match regexp \"%s\"",
|
67
|
text, portSpecificationRegexp));
|
68
|
}
|
69
|
final int expectedGroupsCount = getPropertiesCount()+1;
|
70
|
if(m.groupCount() != expectedGroupsCount){
|
71
|
StringBuilder groups = new StringBuilder();
|
72
|
for(int i = 0; i < m.groupCount(); i++){
|
73
|
groups.append("\""+m.group(i)+"\"");
|
74
|
if(i != m.groupCount()-1) {
|
75
|
groups.append(", ");
|
76
|
}
|
77
|
}
|
78
|
throw new RuntimeException(String.format(
|
79
|
"Invalid output port specification \"%s\": got %d groups "+
|
80
|
"instead of %d (namely: %s)", text, m.groupCount(),
|
81
|
expectedGroupsCount, groups.toString()));
|
82
|
}
|
83
|
String[] properties = new String[getPropertiesCount()];
|
84
|
for(int i = 0; i < getPropertiesCount(); i++){
|
85
|
properties[i] = m.group(i+2);
|
86
|
}
|
87
|
return new PortSpecification(m.group(1), properties);
|
88
|
}
|
89
|
}
|