Reputation: 1828
Have a java property like below
sample.properties
query={name} in {address:-}
Currently using StrSubstitutor to replace the values
valuesMap.put("name", "Har");
valuesMap.put("address", "Park Street");
String queryString= properties.get("query");
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(queryString);
resolvedString = Har in Park Street
What I need is that if the "address" isn't available, the resolved string should be as :
resolvedString = Har instead of resolvedString = Har in
Is it possible to achieve this using StrSubstitutor or by anyother means like using template engine?
Do not want any Java code dependency as the query pattern can change.
Upvotes: 0
Views: 1307
Reputation: 466
This could be a way not to loose generality:
query1={name} in {address}
query2={name}
...
String queryString;
if (valueMap.get("address") == "") {
queryString= properties.get("query2");
} else {
queryString= properties.get("query1");
}
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(queryString);
in this way even if in the future queries have to change, you'll be able to manage the difference between the two cases.
EDIT: if in the future you want to add new properties such in your comment: {name} as {alias} in {address}
I think a way to proceed could be to put even "as" and "in" into the query: query = {name} {as} {alias} {in} {address}
and the populate the database or the valueMap (don't know where the data come from in your application)) in the right way (so {as}="" when no "alias" and {in}="" when no address). Would that be feasible for you?
Upvotes: 1