/** * Creates a new instance and initializes it. Uses a default escaping character. * * @param <V> the type of the values in the map * @param valueMap the map with the variables' values, may be null * @param prefix the prefix for variables, not null * @param suffix the suffix for variables, not null * @throws IllegalArgumentException if the prefix or suffix is null */ public <V> StrSubstitutor(final Map<String, V> valueMap, final String prefix, final String suffix) { this(StrLookup.mapLookup(valueMap), prefix, suffix, DEFAULT_ESCAPE); }
@Test public void testSystemPropertiesLookup() { assertEquals(System.getProperty("os.name"), StrLookup.systemPropertiesLookup().lookup("os.name")); assertNull(StrLookup.systemPropertiesLookup().lookup("")); assertNull(StrLookup.systemPropertiesLookup().lookup("other")); try { StrLookup.systemPropertiesLookup().lookup(null); fail(); } catch (final NullPointerException ex) { // expected } }
@Test public void testNoneLookup() { assertNull(StrLookup.noneLookup().lookup(null)); assertNull(StrLookup.noneLookup().lookup("")); assertNull(StrLookup.noneLookup().lookup("any")); }
@Test public void testMapLookup() { final Map<String, Object> map = new HashMap<>(); map.put("key", "value"); map.put("number", Integer.valueOf(2)); assertEquals("value", StrLookup.mapLookup(map).lookup("key")); assertEquals("2", StrLookup.mapLookup(map).lookup("number")); assertNull(StrLookup.mapLookup(map).lookup(null)); assertNull(StrLookup.mapLookup(map).lookup("")); assertNull(StrLookup.mapLookup(map).lookup("other")); }
@Override public String lookup(String key) { String staticResult = staticLookup.lookup(key); if(staticResult != null) { return staticResult; } if(key.equals("date")) { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(clock.currentTime()); } return null; }
/** * Replaces all the occurrences of variables in the given source object with * their matching values from the system properties. * * @param source the source text containing the variables to substitute, null returns null * @return the result of the replace operation */ public static String replaceSystemProperties(final Object source) { return new StrSubstitutor(StrLookup.systemPropertiesLookup()).replace(source); }
/** * Internal method that resolves the value of a variable. * <p> * Most users of this class do not need to call this method. This method is * called automatically by the substitution process. * <p> * Writers of subclasses can override this method if they need to alter * how each substitution occurs. The method is passed the variable's name * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. * * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or <b>null</b> if the variable is unknown */ protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, final int endPos) { final StrLookup<?> resolver = getVariableResolver(); if (resolver == null) { return null; } return resolver.lookup(variableName); }
/** * Replaces all the occurrences of variables in the given source object with * their matching values from the system properties. * * @param source the source text containing the variables to substitute, null returns null * @return the result of the replace operation */ public static String replaceSystemProperties(final Object source) { return new StrSubstitutor(StrLookup.systemPropertiesLookup()).replace(source); }
/** * Creates a new instance and initializes it. Uses defaults for variable * prefix and suffix and the escaping character. * * @param <V> the type of the values in the map * @param valueMap the map with the variables' values, may be null */ public <V> StrSubstitutor(final Map<String, V> valueMap) { this(StrLookup.mapLookup(valueMap), DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE); }
/** * Tests that a lookup object for system properties sees changes on system * properties. This test is related to LANG-1141. */ @Test public void testSystemPropertiesLookupUpdatedProperty() { final String osName = "os.name"; final String oldOs = System.getProperty(osName); final String newOsName = oldOs + "_changed"; final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup(); System.setProperty(osName, newOsName); try { assertEquals("Changed properties not detected", newOsName, sysLookup.lookup(osName)); } finally { System.setProperty(osName, oldOs); } }
/** * Internal method that resolves the value of a variable. * <p> * Most users of this class do not need to call this method. This method is * called automatically by the substitution process. * <p> * Writers of subclasses can override this method if they need to alter * how each substitution occurs. The method is passed the variable's name * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. * * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or <b>null</b> if the variable is unknown */ protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, final int endPos) { final StrLookup<?> resolver = getVariableResolver(); if (resolver == null) { return null; } return resolver.lookup(variableName); }
/** * Replaces all the occurrences of variables in the given source object with * their matching values from the system properties. * * @param source the source text containing the variables to substitute, null returns null * @return the result of the replace operation */ public static String replaceSystemProperties(final Object source) { return new StrSubstitutor(StrLookup.systemPropertiesLookup()).replace(source); }
/** * Creates a new instance and initializes it. * * @param <V> the type of the values in the map * @param valueMap the map with the variables' values, may be null * @param prefix the prefix for variables, not null * @param suffix the suffix for variables, not null * @param escape the escape character * @throws IllegalArgumentException if the prefix or suffix is null */ public <V> StrSubstitutor(final Map<String, V> valueMap, final String prefix, final String suffix, final char escape) { this(StrLookup.mapLookup(valueMap), prefix, suffix, escape); }
/** * Tests that a lookup object for system properties can deal with a full * replacement of the system properties object. This test is related to * LANG-1055. */ @Test public void testSystemPropertiesLookupReplacedProperties() { final Properties oldProperties = System.getProperties(); final String osName = "os.name"; final String newOsName = oldProperties.getProperty(osName) + "_changed"; final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup(); final Properties newProps = new Properties(); newProps.setProperty(osName, newOsName); System.setProperties(newProps); try { assertEquals("Changed properties not detected", newOsName, sysLookup.lookup(osName)); } finally { System.setProperties(oldProperties); } }
/** * Internal method that resolves the value of a variable. * <p> * Most users of this class do not need to call this method. This method is * called automatically by the substitution process. * <p> * Writers of subclasses can override this method if they need to alter * how each substitution occurs. The method is passed the variable's name * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. * * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or <b>null</b> if the variable is unknown */ protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, final int endPos) { final StrLookup<?> resolver = getVariableResolver(); if (resolver == null) { return null; } return resolver.lookup(variableName); }
/** * Replaces all the occurrences of variables in the given source object with * their matching values from the system properties. * * @param source the source text containing the variables to substitute, null returns null * @return the result of the replace operation */ public static String replaceSystemProperties(final Object source) { return new StrSubstitutor(StrLookup.systemPropertiesLookup()).replace(source); }
/** * Creates a new instance and initializes it. * * @param <V> the type of the values in the map * @param valueMap the map with the variables' values, may be null * @param prefix the prefix for variables, not null * @param suffix the suffix for variables, not null * @param escape the escape character * @param valueDelimiter the variable default value delimiter, may be null * @throws IllegalArgumentException if the prefix or suffix is null * @since 3.2 */ public <V> StrSubstitutor(final Map<String, V> valueMap, final String prefix, final String suffix, final char escape, final String valueDelimiter) { this(StrLookup.mapLookup(valueMap), prefix, suffix, escape, valueDelimiter); }
/** * Internal method that resolves the value of a variable. * <p> * Most users of this class do not need to call this method. This method is * called automatically by the substitution process. * <p> * Writers of subclasses can override this method if they need to alter * how each substitution occurs. The method is passed the variable's name * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. * * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or <b>null</b> if the variable is unknown */ protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, final int endPos) { final StrLookup<?> resolver = getVariableResolver(); if (resolver == null) { return null; } return resolver.lookup(variableName); }
public BuildVariables(AgentRuntimeInfo agentRuntimeInfo, Clock clock) { this.clock = clock; this.staticLookup = StrLookup.mapLookup(map( "agent.location", agentRuntimeInfo.getLocation(), "agent.hostname", agentRuntimeInfo.getHostName() )); }