How can I use a different separator for interpolation lookups?

43 Views Asked by At

I use Apache Commons Configuration to load config files. In the past, the configs getProperty() method was overwritten so that a ${encoded 21$%Ds} within a property value was returned as a decoded value. This should now be improved.

I know that with a colon separated ${encoded:21$%Ds} I could easily retrieve and decode the value automatically by setting a lookup to the configs interpolator (1):

ConfigurationInterpolator interpolator = config.getInterpolator();
interpolator.registerLookup("encoded", new DecodeLookup());

with DecodeLookup looking like

public class BogisLookup implements Lookup {
  @Override
  public Object lookup(String varName) {
    String decodedVar = decode(varName);
    return decodedVar;
    }
}

Since the version with the space (${encoded 21$%Ds}) is already widely adopted I need to adjust the interpolation lookup separator from ":" to " ". How can I do this?

1

There are 1 best solutions below

2
On

Process data through regular expressions:

String[] parts = varName.split("\\s+");

In this way, an array can be obtained, where data with index 0 is Key and data with index 1 is Value. If the length exceeds 2, an error can be thrown. When the length verification is passed, the data with index 1 can be directly obtained for decoding