MCPcopy Create free account
hub / github.com/GateNLP/gate-core / replaceVariablesInString

Method replaceVariablesInString

src/main/java/gate/Utils.java:1159–1244  ·  view source on GitHub ↗

This will replace all occurrences of variables of the form $env{name}, $prop{name}, $doc{featname}, $pr_parm{inputAS} or $$env{name} etc in a String. The source for replacing the variable can be environment variables, system properties, or arbitrary maps or resources specified when calling the met

(
          String string, Object... sources)

Source from the content-addressed store, hash-verified

1157 *
1158 */
1159 @SuppressWarnings("unchecked")
1160 public static String replaceVariablesInString(
1161 String string, Object... sources)
1162 {
1163 // shortcut for strings where no replacement is possible (minimum content
1164 // would have to be $pr{x}
1165 if(string == null || string.isEmpty() || string.length() < 6) { return string; }
1166 Matcher matcher = varnamePattern.matcher(string);
1167 int findFrom = 0;
1168 int lastEnd = 0;
1169 StringBuilder sb = new StringBuilder(string.length()*2);
1170 while(findFrom < string.length() && matcher.find(findFrom)) {
1171 String dollars = matcher.group(1);
1172 String type = matcher.group(2);
1173 String varname = matcher.group(3);
1174 int matchStart = matcher.start();
1175 // whenever we have found something, we can immediately move the part
1176 // from the last end of match to the new start of match to the
1177 // return string, that is just unmodified string ...
1178 // But only if the length is > 0
1179 if((matchStart - lastEnd) > 0) {
1180 sb.append(string.substring(lastEnd,matchStart));
1181 }
1182 lastEnd = matcher.end();
1183 Object value = null;
1184 // for each match we find, go through all the sources in the order
1185 // listed and if the type of the source matches the requested type
1186 // then try to look the variable up. If we find something use it and
1187 // finish looking, otherwise continue until all sources have been
1188 // exhausted.
1189 // A variable where no value has been found anywhere is not replaced.
1190 // If a variable got replaced and it was a variable that started with
1191 // two dollar signs, then the replacement value is first getting
1192 // recursively replaced too.
1193 if(type.equals("env")) {
1194 value = System.getenv().get(varname);
1195 } else if(type.equals("prop")) {
1196 value = System.getProperties().get(varname);
1197 } else {
1198 for(Object source : sources) {
1199 if(type.isEmpty()) { // an empty variable type matches only maps from the sources
1200 if(source instanceof Map) {
1201 value = ((Map<String,?>)source).get(varname);
1202 }
1203 } else if(type.equals("pr") && (source instanceof ProcessingResource)) {
1204 value = ((FeatureBearer)source).getFeatures().get(varname);
1205 } else if(type.equals("pr_parm") && (source instanceof ProcessingResource)) {
1206 try {
1207 value = ((ProcessingResource)source).getParameterValue(varname);
1208 } catch(Exception ex) {
1209 // do nothing, leave the value null
1210 }
1211 } else if(type.equals("doc") && (source instanceof Document)) {
1212 value = ((FeatureBearer)source).getFeatures().get(varname);
1213 } else if(type.equals("controller") && (source instanceof Controller)) {
1214 value = ((FeatureBearer)source).getFeatures().get(varname);
1215 } else if(type.equals("corpus") && (source instanceof Corpus)) {
1216 value = ((FeatureBearer)source).getFeatures().get(varname);

Callers

nothing calls this directly

Calls 9

startMethod · 0.80
endMethod · 0.80
equalsMethod · 0.65
getMethod · 0.65
getFeaturesMethod · 0.65
getParameterValueMethod · 0.65
toStringMethod · 0.65
isEmptyMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected