Skip to content

Package: GlobalFunctions

GlobalFunctions

nameinstructionbranchcomplexitylinemethod
convertToGeneralIdentifier(String)
M: 0 C: 54
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
createRERP(String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createRESP(String)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
generateRegEx(String)
M: 0 C: 73
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
getInstance()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getTheInstance()
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
posixCharacterReplacement(String)
M: 101 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%
searchVariableDefinition(RouterSoftware, String)
M: 18 C: 10
36%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 4
67%
M: 0 C: 1
100%
setInstance(GlobalFunctions)
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /**
2: *
3: */
4: package parser.states;
5:
6: import java.util.ArrayList;
7: import java.util.regex.Matcher;
8: import java.util.regex.Pattern;
9:
10: import model.RouterSoftware;
11: import model.definition.AbstractVariableDefinition;
12: import model.type.RefUnResState;
13: import model.type.RegEx;
14: import model.type.RegExPart;
15: import model.type.RegExRefPart;
16: import model.type.RegExStringPart;
17: import parser.ParserException;
18: import parser.states.typestates.PosixHashMap;
19:
20: /**
21: * @author Muri
22: *
23: */
24: public final class GlobalFunctions {
25:         /**
26:          *
27:          */
28:         private static GlobalFunctions instance;
29:
30:         /**
31:          *
32:          */
33:         private GlobalFunctions() {
34:                 super();
35:         }
36:
37:         /**
38:          *
39:          * @return theInstance
40:          */
41:         public static GlobalFunctions getTheInstance() {
42:•                if (GlobalFunctions.getInstance() == null) {
43:                         GlobalFunctions.setInstance(new GlobalFunctions());
44:                 }
45:                 return GlobalFunctions.getInstance();
46:         }
47:
48:         /**
49:          *
50:          * @param string
51:          * input String
52:          * @return ArrayList of RegExParts
53:          */
54:         public static RegEx generateRegEx(final String string) {
55:
56:                 final ArrayList<RegExPart> parts = new ArrayList<RegExPart>();
57:
58:                 String remainingString = string;
59:                 Boolean stringIsNotEmpty = true;
60:
61:•                while (stringIsNotEmpty) {
62:                         final String[] splitStringArray1 = remainingString.split("\\(RE:", 2);
63:•                        if (splitStringArray1.length == 2) {
64:                                 final String[] splitStringArray2 = splitStringArray1[1].split("\\)", 2);
65:•                                if (!splitStringArray1[0].isEmpty()) {
66:                                         parts.add(createRESP(splitStringArray1[0]));
67:                                 }
68:                                 parts.add(createRERP(splitStringArray2[0]));
69:                                 remainingString = splitStringArray2[1];
70:                         } else {
71:•                                if (!splitStringArray1[0].isEmpty()) {
72:                                         parts.add(createRESP(splitStringArray1[0]));
73:                                 }
74:                                 stringIsNotEmpty = false;
75:                         }
76:                 }
77:
78:                 return new RegEx(parts);
79:         }
80:
81:         /**
82:          * gets the VariableDefinition of the searched Identifier.
83:          *
84:          * @param rs
85:          * The {@link RouterSoftware} to use.
86:          * @param identifier
87:          * identifier
88:          * @return the VariableDefiniton for an Identifier
89:          * @throws ParserException
90:          * ParserException if VariableDefinition is not found
91:          */
92:         public AbstractVariableDefinition searchVariableDefinition(final RouterSoftware rs,
93:                         final String identifier) throws ParserException {
94:                 AbstractVariableDefinition aVD = rs.findDefinition(identifier);
95:•                if (aVD == null) {
96:                         aVD = rs.findDefinition(convertToGeneralIdentifier(identifier));
97:                 }
98:                 // if (aVD instanceof AbstractVariableDefinition) {
99:                 // return aVD;
100:                 // } else {
101:                 // throw new ParserException("VariableDefinition not found for " + genId);
102:                 // }
103:•                if (aVD == null) {
104:                         throw new ParserException("VariableDefinition not found for " + identifier);
105:                 } else {
106:                         return aVD;
107:                 }
108:
109:         }
110:
111:         /**
112:          * converts an array-elements identifier into its generalized name i.e.:
113:          * "PF_USR_CHAIN_2_RULE_4_COMMENT" -> "PF_USR_CHAIN_%_RULE_%_COMMENT".
114:          *
115:          * @param identifier
116:          * identifier for the element to be parsed
117:          * @return the generalized identifier
118:          */
119:         public String convertToGeneralIdentifier(final String identifier) {
120:                 String string = identifier;
121:                 // first, replace the _% pattern if existent at the end of the identifier.
122:                 final Pattern pattern0 = Pattern.compile("_([0-9]+)$");
123:                 final Matcher matcher0 = pattern0.matcher(new StringBuilder(string));
124:•                if (matcher0.find()) {
125:                         final String tmpString = matcher0.replaceFirst("_%");
126:                         final StringBuilder stringBuilder = new StringBuilder(tmpString);
127:                         string = stringBuilder.toString();
128:                 }
129:                 // than, replace all _%_ pattern in the identifier.
130:                 final Pattern pattern1 = Pattern.compile("_([0-9]+)_");
131:                 final Matcher matcher1 = pattern1.matcher(new StringBuilder(string));
132:•                if (matcher1.find()) {
133:                         final String tmpString = matcher1.replaceAll("_%_");
134:                         final StringBuilder stringBuilder = new StringBuilder(tmpString);
135:                         string = stringBuilder.toString();
136:                 }
137:                 return string;
138:         }
139:
140:         /**
141:          * This method replaces the POSIX character classes with the appropriated ASCII code.
142:          *
143:          * @param expression
144:          * the expression
145:          * @return new expression
146:          */
147:         public static String posixCharacterReplacement(final String expression) {
148:
149:                 final StringBuffer newExpression = new StringBuffer();
150:                 final ArrayList<String> expStringParts = new ArrayList<String>();
151:                 String remainingString = expression;
152:                 Boolean stringIsNotEmpty = true;
153:                 final PosixHashMap hashMap = new PosixHashMap();
154:
155:•                while (stringIsNotEmpty) {
156:                         final String[] splitString1 = remainingString.split("\\[:", 2);
157:•                        if (splitString1.length == 2) {
158:                                 final String[] splitString2 = splitString1[1].split(":\\]", 2);
159:
160:                                 splitString2[0] = hashMap.getHashMap().get(splitString2[0]);
161:
162:•                                if (!splitString1[0].isEmpty()) {
163:                                         expStringParts.add(splitString1[0]);
164:                                 }
165:                                 expStringParts.add(splitString2[0]);
166:                                 remainingString = splitString2[1];
167:                         } else {
168:•                                if (!splitString1[0].isEmpty()) {
169:                                         expStringParts.add(splitString1[0]);
170:                                 }
171:                                 stringIsNotEmpty = false;
172:                         }
173:                 }
174:
175:•                for (int i = 0; i < expStringParts.size(); i++) {
176:                         newExpression.append(expStringParts.get(i));
177:                 }
178:
179:                 return newExpression.toString();
180:         }
181:
182:         /**
183:          * totally senseless function for PMD to be quiet. Suppresses "Avoid instantiating new objects
184:          * inside loops" warning.
185:          *
186:          * @param string
187:          * string
188:          * @return RegExStringPart
189:          */
190:         private static RegExRefPart createRERP(final String string) {
191:                 return new RegExRefPart(new RefUnResState(string));
192:         }
193:
194:         /**
195:          * yet another totally senseless function for PMD to be quiet. Suppresses "Avoid instantiating
196:          * new objects inside loops" warning.
197:          *
198:          * @param string
199:          * string
200:          * @return RegExStringPart
201:          */
202:         private static RegExStringPart createRESP(final String string) {
203:                 return new RegExStringPart(string);
204:         }
205:
206:         /**
207:          * @return the instance
208:          */
209:         private static GlobalFunctions getInstance() {
210:                 return instance;
211:         }
212:
213:         /**
214:          * @param instance
215:          * the instance to set
216:          */
217:         private static void setInstance(final GlobalFunctions instance) {
218:                 GlobalFunctions.instance = instance;
219:         }
220: }