/** * Searches the string for occurrences of the pattern $ENV{key} and * attempts to replace this pattern with a value from the System environment * obtained using the 'key'. For example, including "$ENV{USERNAME}" in * a string and calling this method would then attempt to replace the entire * pattern with the value of the environment variable "USERNAME". The System * environment is obtained in Java with a call to System.getenv(). An environment variable is typically * defined in the Linux shell or Windows property tabs. NOTE: A Java System * property is not the same as an environment variable. * @param string0 The string to perform substitution on such as "Hello $ENV{USERNAME}". * This string may be null, empty, or contain one or more substitutions. * @return A string with all occurrences of keys substituted with their * values obtained from the System environment. Can be null if the * original string was null. * @throws SubstitutionException Thrown if a starting string was found, but the * ending string was not. Also, thrown if a key value was empty such * as using "$ENV{}". Finally, thrown if the property key was not * found in the properties object (could not be replaced). * @see #substituteWithProperties(java.lang.String, java.lang.String, java.lang.String, java.util.Properties) */ public static String substituteWithEnvironment(String string0) throws SubstitutionException { // turn environment into properties Properties envProps = new Properties(); // add all system environment vars to the properties envProps.putAll(System.getenv()); // delegate to other method using the default syntax $ENV{<key>} return substituteWithProperties(string0, "$ENV{", "}", envProps); }
/** * Searches the string for occurrences of the pattern $ENV{key} and * attempts to replace this pattern with a value from the System environment * obtained using the 'key'. For example, including "$ENV{USERNAME}" in * a string and calling this method would then attempt to replace the entire * pattern with the value of the environment variable "USERNAME". The System * environment is obtained in Java with a call to System.getenv(). An environment variable is typically * defined in the Linux shell or Windows property tabs. NOTE: A Java System * property is not the same as an environment variable. * @param string0 The string to perform substitution on such as "Hello $ENV{USERNAME}". * This string may be null, empty, or contain one or more substitutions. * @return A string with all occurrences of keys substituted with their * values obtained from the System environment. Can be null if the * original string was null. * @throws SubstitutionException Thrown if a starting string was found, but the * ending string was not. Also, thrown if a key value was empty such * as using "$ENV{}". Finally, thrown if the property key was not * found in the properties object (could not be replaced). * @see #substituteWithProperties(java.lang.String, java.lang.String, java.lang.String, java.util.Properties) */ public static String substituteWithEnvironment(String string0) throws SubstitutionException { // turn environment into properties Properties envProps = new Properties(); // add all system environment vars to the properties envProps.putAll(System.getenv()); // delegate to other method using the default syntax $ENV{<key>} return substituteWithProperties(string0, "$ENV{", "}", envProps); }
/** * Creates an InputStream containing the document resulting from replacing template * parameters in the given InputStream source. * @param source The source stream * @param props The properties * @param startStr The String that marks the start of a replacement key such as "${" * @param endStr The String that marks the end of a replacement key such as "}" * @return An InputStream containing the resulting document. */ static public InputStream replaceProperties(InputStream source, Properties props, String startStr, String endStr) throws IOException, SubstitutionException { String template = streamToString(source); String replaced = StringUtil.substituteWithProperties(template, startStr, endStr, props); System.err.println(template); System.err.println(replaced); return new ByteArrayInputStream(replaced.getBytes()); }