/** * Replaces all the occurrences of variables in the given source object with * their matching values from the map. * * @param source the source text containing the variables to substitute, null returns null * @param valueMap the map with the values, may be null * @return the result of the replace operation */ public static String replace(Object source, Map valueMap) { return new StrSubstitutor(valueMap).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(String variableName, StrBuilder buf, int startPos, int endPos) { StrLookup resolver = getVariableResolver(); if (resolver == null) { return null; } return resolver.lookup(variableName); }
/** * Creates a new instance and initializes it. * * @param variableResolver the variable resolver, 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 StrSubstitutor(StrLookup variableResolver, String prefix, String suffix, char escape) { this.setVariableResolver(variableResolver); this.setVariablePrefix(prefix); this.setVariableSuffix(suffix); this.setEscapeChar(escape); }
/** * Creates a new instance and initializes it. * * @param variableResolver the variable resolver, may be null * @param prefixMatcher the prefix for variables, not null * @param suffixMatcher the suffix for variables, not null * @param escape the escape character * @throws IllegalArgumentException if the prefix or suffix is null */ public StrSubstitutor( StrLookup variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher, char escape) { this.setVariableResolver(variableResolver); this.setVariablePrefixMatcher(prefixMatcher); this.setVariableSuffixMatcher(suffixMatcher); this.setEscapeChar(escape); }
/** * Replaces the given text by looking up values from the given resource bundle. * @param bundle resource bundle * @param text text to replace * @param variablePrefix variable reference prefix. "${" by default. * @param variableSuffix variable reference suffix. "}" by default. * @param escapeChar escape character which can be put just before a variable reference to ignore the expression. * @param escapeMessageXml whether or not to escape a message value having &,>,<,", and '. * @return replaced string by the values found in the given resource bundle */ public static String replaceMessagesByBundle(ResourceBundle bundle, String text, String variablePrefix, String variableSuffix, Character escapeChar, boolean escapeMessageXml) { if (bundle == null) { throw new IllegalArgumentException("The bundle must not be null."); } StrSubstitutor subst = new StrSubstitutor(new ResourceBundleVariableResolver(bundle, escapeMessageXml), StrSubstitutor.DEFAULT_PREFIX, StrSubstitutor.DEFAULT_SUFFIX, DEFAULT_ESCAPE); if (StringUtils.isNotBlank(variablePrefix)) { subst.setVariablePrefix(variablePrefix); } if (StringUtils.isNotBlank(variableSuffix)) { subst.setVariableSuffix(variableSuffix); } if (escapeChar != null) { subst.setEscapeChar(escapeChar); } return subst.replace(text); }
/** * Returns the object that is responsible for variable interpolation. * * @return the object responsible for variable interpolation * @since 1.4 */ public synchronized StrSubstitutor getSubstitutor() { if (substitutor == null) { substitutor = new StrSubstitutor(createInterpolator()); } return substitutor; }
/** * Returns a string with variables replaced by system property values if they exist * * @return */ public static String resolveProperties(String str) { return StrSubstitutor.replaceSystemProperties(str); }
public static Properties interpolateVariables(Properties properties, Map<String, String> variables) { Properties result = new Properties(); Enumeration keys = properties.keys(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); String value = (String) properties.get(key); String interpolatedValue = StrSubstitutor.replace(value, variables, "${env:", "}"); result.setProperty(key, interpolatedValue); } return result; }
/** * Returns the object that is responsible for variable interpolation. * * @return the object responsible for variable interpolation * @since 1.4 */ public synchronized StrSubstitutor getSubstitutor() { if (substitutor == null) { substitutor = new StrSubstitutor(createInterpolator()); } return substitutor; }
/** * Creates a new instance and initializes it. * * @param variableResolver the variable resolver, may be null * @param prefixMatcher the prefix for variables, not null * @param suffixMatcher the suffix for variables, not null * @param escape the escape character * @throws IllegalArgumentException if the prefix or suffix is null */ public StrSubstitutor( StrLookup variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher, char escape) { this.setVariableResolver(variableResolver); this.setVariablePrefixMatcher(prefixMatcher); this.setVariableSuffixMatcher(suffixMatcher); this.setEscapeChar(escape); }
/** * Returns a List with variables replaced by system property values if they exist * * @return */ public static List<String> resolveProperties(List<String> list) { for (final ListIterator<String> i = list.listIterator(); i.hasNext(); ) { final String item = StrSubstitutor.replaceSystemProperties(i.next()); i.set(item); } return list; }
/** * Replaces all the occurrences of variables in the given source object with * their matching values from the map. This method allows to specifiy a * custom variable prefix and suffix * * @param source the source text containing the variables to substitute, null returns null * @param valueMap the map with the values, may be null * @param prefix the prefix of variables, not null * @param suffix the suffix of variables, not null * @return the result of the replace operation * @throws IllegalArgumentException if the prefix or suffix is null */ public static String replace(Object source, Map valueMap, String prefix, String suffix) { return new StrSubstitutor(valueMap, prefix, suffix).replace(source); }
/** * Replaces all the occurrences of variables in the given source object with their matching * values from the properties. * * @param source the source text containing the variables to substitute, null returns null * @param valueProperties the properties with values, may be null * @return the result of the replace operation * @since 2.6 */ public static String replace(Object source, Properties valueProperties) { if (valueProperties == null) { return source.toString(); } Map valueMap = new HashMap(); Enumeration propNames = valueProperties.propertyNames(); while (propNames.hasMoreElements()) { String propName = (String)propNames.nextElement(); String propValue = valueProperties.getProperty(propName); valueMap.put(propName, propValue); } return StrSubstitutor.replace(source, valueMap); }
public MavenVoiceCompiler(String mvn, File compileDir, String voiceName, String voiceVersion, Locale locale, String gender, String domain, int samplingRate, boolean isUnitSelectionVoice, File[] filesForResources, File[] filesForFilesystem, Map<String, String> extraVariablesToSubstitute) { this.mvn = mvn; this.compileDir = compileDir; this.voiceName = voiceName.replaceAll("[^\\w\\-]", ""); this.voiceVersion = voiceVersion; this.locale = locale; this.gender = gender; this.domain = domain; this.samplingRate = samplingRate; this.isUnitSelectionVoice = isUnitSelectionVoice; this.substitutor = new StrSubstitutor(getVariableSubstitutionMap(extraVariablesToSubstitute)); this.filesForResources = filesForResources; this.filesForFilesystem = filesForFilesystem; }
/** * Creates a new instance and initializes it. * * @param variableResolver the variable resolver, 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 StrSubstitutor(StrLookup variableResolver, String prefix, String suffix, char escape) { this.setVariableResolver(variableResolver); this.setVariablePrefix(prefix); this.setVariableSuffix(suffix); this.setEscapeChar(escape); }
/** * Creates a new instance and initializes it. * * @param variableResolver the variable resolver, may be null * @param prefixMatcher the prefix for variables, not null * @param suffixMatcher the suffix for variables, not null * @param escape the escape character * @throws IllegalArgumentException if the prefix or suffix is null */ public StrSubstitutor( StrLookup variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher, char escape) { this.setVariableResolver(variableResolver); this.setVariablePrefixMatcher(prefixMatcher); this.setVariableSuffixMatcher(suffixMatcher); this.setEscapeChar(escape); }
/** * Replace any ${prop} with system properties. * * @param props current state of properties that contain placeholders of the form ${property}. * @return the given property object with system property placeholders switched to their actual * values. */ @VisibleForTesting static Properties substituteSystemPropertyPlaceholders(Properties props) { Properties filtered = new Properties(); for (Map.Entry<?, ?> entry : props.entrySet()) { filtered.put( StrSubstitutor.replaceSystemProperties(entry.getKey()), StrSubstitutor.replaceSystemProperties(entry.getValue())); StrSubstitutor.replaceSystemProperties(new Object()); } return filtered; } }
/** * Returns the {@code ConfigurationInterpolator} object that manages * the lookup objects for resolving variables. <em>Note:</em> If this * object is manipulated (e.g. new lookup objects added), synchronization * has to be manually ensured. Because * {@code ConfigurationInterpolator} is not thread-safe concurrent * access to properties of this configuration instance (which causes the * interpolator to be invoked) may cause race conditions. * * @return the {@code ConfigurationInterpolator} associated with this * configuration * @since 1.4 */ public ConfigurationInterpolator getInterpolator() { return (ConfigurationInterpolator) getSubstitutor() .getVariableResolver(); }
/** * 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(Object source) { return new StrSubstitutor(StrLookup.systemPropertiesLookup()).replace(source); }