Package: MatchNode
MatchNode
| name | instruction | branch | complexity | line | method | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| MatchNode(AbstractNode, AbstractNode) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| equals(Object) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| hashCode() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| interpret(CommunicationManager) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| replaceTypes(CommunicationManager, String) | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
| toString() | 
 | 
 | 
 | 
 | 
 | ||||||||||||||||||||
Coverage
1: package pruefskript.parser.nodes;
2: 
3: import java.util.List;
4: import java.util.regex.Matcher;
5: import java.util.regex.Pattern;
6: 
7: import basic.PruefskriptConstants;
8: import capturinggroups.CapturingGroups;
9: import pruefskript.CommunicationManager;
10: import pruefskript.parser.exceptions.CheckScriptException;
11: import pruefskript.parser.exceptions.VariableTypeDoesNotExistException;
12: import pruefskript.parser.exceptions.WrongNodeException;
13: import pruefskript.parser.values.AbstractReturnValue;
14: import pruefskript.parser.values.BooleanValue;
15: import pruefskript.parser.values.StringValue;
16: 
17: /**
18:  * MatchNode.
19:  * 
20:  * @author Group B5
21:  * 
22:  */
23: public class MatchNode extends AbstractBinaryOperationNode {
24:         /**
25:          * MatchNode.
26:          * 
27:          * @param operand1
28:          *            AbstractNode
29:          * @param operand2
30:          *            AbstractNode
31:          */
32:         public MatchNode(final AbstractNode operand1, final AbstractNode operand2) {
33:                 super(operand1, operand2);
34:         }
35: 
36:         @Override
37:         public String toString() {
38:                 return PruefskriptConstants.MATCHNODETITLE + PruefskriptConstants.BRACKET_OPEN
39:                                 + super.toString() + PruefskriptConstants.BRACKET_CLOSE;
40:         }
41: 
42:         @Override
43:         public boolean equals(final Object obj) {
44:•                return super.equals(obj) && obj instanceof MatchNode;
45: 
46:         }
47: 
48:         @Override
49:         public int hashCode() {
50:                 return super.hashCode() + PruefskriptConstants.MATCHNODE_HASHCODE;
51:         }
52: 
53:         @Override
54:         public BooleanValue interpret(final CommunicationManager mgr) throws CheckScriptException {
55:                 final AbstractReturnValue op1 = this.getOperand1().interpret(mgr);
56:                 final AbstractReturnValue op2 = this.getOperand2().interpret(mgr);
57:•                if (op1.isStringValue() && op2.isStringValue()) {
58:                         final String string = op1.toStringValue().getValue();
59:                         String regex = op2.toStringValue().getValue();
60:                         regex = replaceTypes(mgr, regex);
61:                         final BooleanValue result = new BooleanValue(string.matches(regex));
62:•                        if (result.getValue()) {
63:                                 final CapturingGroups cap = new CapturingGroups();
64:                                 final List<String> list = cap.captureTheGroups(string, regex);
65:•                                if (list.get(0).contains(string)) {
66:                                         list.remove(0); // Die Entwickler der CapGroups geben das komplette Element in
67:                                                                         // die Liste,
68:                                                                         // falls es den Ausdruck komplett matched. Das sieht match hier
69:                                                                         // allerdings nicht vor, also wird dieser entfernt.
70:                                 }
71:                                 int i = 0;
72:•                                for (final String element : list) {
73:                                         mgr.addValue(PruefskriptConstants.MATCH_VARIABLE, i + 1,
74:                                                         new StringValue(element));
75:                                         i++;
76:                                 }
77:                         }
78:                         return result;
79:                 } else {
80:                         throw new WrongNodeException(PruefskriptConstants.UNEXPECTED_STRING_OPERATION);
81:                 }
82:         }
83: 
84:         /**
85:          * 
86:          * @param mgr
87:          *            The {@link CommunicationManager} to use.
88:          * @param nameregex
89:          *            .
90:          * @return den ausdruck mit den ersetzten typen.
91:          * @throws VariableTypeDoesNotExistException
92:          *             falls der typ nicht existiert.
93:          */
94:         private String replaceTypes(final CommunicationManager mgr, final String nameregex)
95:                         throws VariableTypeDoesNotExistException {
96: 
97:                 final String regex = "(re|RE):[a-zA-Z,_]*";
98:                 final Pattern pattern = Pattern.compile(regex);
99:                 final Matcher matcher = pattern.matcher(nameregex);
100:                 final StringBuffer sb = new StringBuffer();
101: 
102:•                while (matcher.find()) {
103:                         final String element = matcher.group();
104:                         final String cutElement = element.substring(3, element.length());
105:                         matcher.appendReplacement(sb, mgr.getVariableTypeRegEx(cutElement));
106:                 }
107:                 return matcher.appendTail(sb).toString();
108:         }
109: }