List of usage examples for org.apache.commons.lang.text StrSubstitutor replace
public String replace(Object source)
From source file:com.virtualparadigm.packman.processor.JPackageManager.java
public static void main(String[] args) { // ST stringTemplate = null; // String templateContent = null; // //from w w w . j ava 2 s .co m // // try // { // Configuration configuration = new PropertiesConfiguration(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/install.properties")); // // // templateContent = FileUtils.readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/server.xml")); // templateContent = templateContent.replace("${", "\\${"); // System.out.println(templateContent); // System.out.println(); // System.out.println(); // System.out.println(); // // stringTemplate = new ST(JPackageManager.ST_GROUP, templateContent); //// stringTemplate = new ST(FileUtils.readFileToString(pfile)); // JPackageManager.addTemplateAttributes(stringTemplate, configuration); // templateContent = stringTemplate.render(); // templateContent = templateContent.replace("\\${", "${"); // System.out.println(templateContent); // } // catch(Exception e) // { // e.printStackTrace(); // } // try // { // String str = "${foo} $bar$"; // str = str.replace("${", "\\${"); // System.out.println(str); // // // STGroup stGroup = new STGroup('$', '$'); // ST stringTemplate = new ST(stGroup, str); // stringTemplate.add("bar", "yoyoyo"); // str = stringTemplate.render(); // System.out.println(str); // // str = str.replace("\\${", "${"); // System.out.println(str); // //// STGroup stGroup = new STGroup('$', '$'); //// ST stringTemplate = new ST(stGroup, FileUtils.readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/logging.properties"))); //// ST stringTemplate = new ST(FileUtils.readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/logging.properties"))); //// //// stringTemplate.add("bar_yo", "yoyoyo"); //// //// //// System.out.println(stringTemplate.render()); // // } // catch(Exception e) // { // e.printStackTrace(); // } //// String foo = "handler.FILE.fileName=#{org.jboss.boot.log.file:boot.log}\nyoyoyoyo\nrem ### \nREM#"; // String foo = "rem ### -*- batch file -*- ######################################################\r\n" + //"rem # ##\r\n" + //"rem # JBoss Bootstrap Script Configuration ##\r\n" + //"rem # ##\r\n" + //"rem #############################################################################\r\n"; // String regex = "(#)(\\{)([^\\}]*)(\\:)([^\\}]*)(\\})"; //// String regex = "(#)(\\{)([^\\{]*)(\\:)([^\\}]*)(\\})"; //// String regex = "(#)(\\{*)(\\:)(^*\\})"; // System.out.println("regex=" + regex); // System.out.println("foo=" + foo); //// foo = foo.replaceAll(regex, "\\$$2$3$4$5$6"); // foo = foo.replaceAll(regex, "\\$$2$3$4$5$6"); // System.out.println("foo=" + foo); // VelocityEngine velocityEngine = new VelocityEngine(); // Properties vProps = new Properties(); // vProps.setProperty("resource.loader", "string"); // vProps.setProperty("string.resource.loader.class", "org.apache.velocity.runtime.resource.loader.StringResourceLoader"); // velocityEngine.init(vProps); // Template template = null; // // try // { // VelocityContext velocityContext = JPackageManager.createVelocityContext(new PropertiesConfiguration("C:/dev/workbench/paradigm-workspace/jpackage-manager/install.properties")); // StringResourceRepository stringResourceRepository = StringResourceLoader.getRepository(); // String templateContent = null; // StringWriter stringWriter = null; // //// templateContent = FileUtils.readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/appclient.conf.bat")); //// templateContent = "rem ### -*- batch file -*- ######################################################\r\n"; // templateContent = "rem %%% -*- batch file -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r\n"; // // // Pattern pattern = Pattern.compile("(\\$)(\\{)([^\\}]*)(\\:)([^\\{]*)(\\})"); // Matcher matcher = pattern.matcher(templateContent); // if(matcher.matches()) // { // System.out.println(" converting # to $"); // matcher.replaceAll("\\$$2$3$4$5$6"); // } // else // { // System.out.println(" NOPE"); // } // //// templateContent = templateContent.replaceAll("(\\$)(\\{)([^\\}]*)(\\:)([^\\}]*)(\\})", "#$2$3$4$5$6"); // System.out.println("content=" + templateContent); // // stringResourceRepository.putStringResource(JPackageManager.CURRENT_TEMPLATE_NAME, templateContent); // stringWriter = new StringWriter(); // template = velocityEngine.getTemplate(JPackageManager.CURRENT_TEMPLATE_NAME); // template.merge(velocityContext, stringWriter); // // templateContent = stringWriter.toString(); // // Pattern pattern2 = Pattern.compile("(\\$)(\\{)([^\\}]*)(\\:)([^\\{]*)(\\})"); // Matcher matcher2 = pattern2.matcher(templateContent); // if(matcher2.matches()) // { // System.out.println(" converting # to $"); // matcher2.replaceAll("\\$$2$3$4$5$6"); // } // else // { // System.out.println(" NOPE"); // } //// templateContent = templateContent.replaceAll("(#)(\\{)([^\\}]*)(\\:)([^\\{]*)(\\})", "\\$$2$3$4$5$6"); // // System.out.println(templateContent); // } // catch(Exception e) // { // e.printStackTrace(); // } // // //// StringTemplate templ = new StringTemplate("foo $fo$bo$r$ yo"); //// templ.setAttribute("success", "foobar"); //// templ.setAttribute("bo", "oba"); //// System.out.println(templ.toString()); // // // // try // { //// StringTemplateLoader stringTemplateLoader = new StringTemplateLoader(); // String firstTemplate = "firstTemplate"; // String content = "this should ${foobar} ${foo:bar.0.1}"; // String updatedContent = "this should ${foobar} #{foo:bar.0.1}"; //// String content = "this should ${foo:bar}"; // //// System.out.println(content.matches("\\$\\{.*\\:.*\\}")); // //// System.out.println(content.replaceAll("\\$\\{.*\\:.*\\}", "hahaha")); //// System.out.println(content.replaceAll("(\\$\\{.*)(\\:)(.*\\})", "$1-$3")); //// System.out.println(content.replaceAll("(\\$\\{.*)(\\:)(.*\\})", "$1-$3")); //// System.out.println(content.replaceAll("(\\$)(\\{.*)(\\:)(.*\\})", "#$2$3$4")); // System.out.println(updatedContent.replaceAll("(#)(\\{)([^\\}]*)(\\:)([^\\}]*)(\\})", "\\$$2$3$4$5$6")); // System.out.println(content.replaceAll("(\\$)(\\{)(.*)(\\:)(.*)(\\})", "--$2$3$4$5$6--")); // System.out.println(content.replaceAll("(\\$)(\\{\\w*\\:\\w*\\})", "#$2")); // //// stringTemplateLoader.putTemplate(firstTemplate, "this should ${foobar} ${foo:bar}"); //// //// freemarker.template.Configuration freeMarkerConfiguration = new freemarker.template.Configuration(); //// freeMarkerConfiguration.setTemplateLoader(stringTemplateLoader); //// Template template = freeMarkerConfiguration.getTemplate(firstTemplate); //// Map<String, Object> valueMap = new HashMap<String, Object>(); //// valueMap.put("foobar", "helloworld"); //// //// Writer out = new OutputStreamWriter(System.out); //// template.process(valueMap, out); //// out.flush(); //// //// freeMarkerConfiguration.clearTemplateCache(); // // } // catch(Exception e) // { // e.printStackTrace(); // } // // // // // System.out.println(""); // System.out.println(""); // // // VelocityEngine velocityEngine = new VelocityEngine(); // Properties vProps = new Properties(); //// vProps.put("file.resource.loader.path", ""); // vProps.setProperty("resource.loader", "string"); // vProps.setProperty("string.resource.loader.class", "org.apache.velocity.runtime.resource.loader.StringResourceLoader"); // velocityEngine.init(vProps); // Template template = null; // // // try // { // // // Configuration configuration = new PropertiesConfiguration(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/install.properties")); // VelocityContext velocityContext = JPackageManager.createVelocityContext(configuration); // // // StringResourceRepository repository = StringResourceLoader.getRepository(); // String strTemplate = FileUtils.readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/domain.sh")); // // //// strTemplate = strTemplate.replace(":", "\\:"); // strTemplate = strTemplate.replaceAll("(\\$)(\\{)([^\\}]*)(\\:)([^\\}]*)(\\})", "#$2$3$4$5$6"); // strTemplate = strTemplate.replaceAll("(\\$)(\\{)([^\\}]*)([^a-zA-Z\\d\\_])*([^\\}]*)(\\})", "#$2$3$4$5$6"); // //System.out.println(strTemplate); // // repository.putStringResource("template", strTemplate); // StringWriter writer = new StringWriter(); // template = velocityEngine.getTemplate("template"); // template.merge(velocityContext, writer); // // // String strResult = writer.toString().replaceAll("(\\#)(\\{)([^\\}]*)(\\:)([^\\}]*)(\\})", "$$2$3$4$5$6"); // // // System.out.println(strResult); // } // catch (Exception e) // { // e.printStackTrace(); // } try { Configuration configuration = new PropertiesConfiguration( new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/install.properties")); Map<String, String> substitutionContext = JPackageManager.createSubstitutionContext(configuration); StrSubstitutor strSubstitutor = new StrSubstitutor(substitutionContext); String strTemplate = FileUtils .readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/domain.sh")); System.out.println(strTemplate); System.out.println(strSubstitutor.replace(strTemplate)); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.virtualparadigm.packman.processor.JPackageManagerOld.java
public static void main(String[] args) { // ST stringTemplate = null; // String templateContent = null; // /* ww w .jav a 2 s .c o m*/ // // try // { // Configuration configuration = new PropertiesConfiguration(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/install.properties")); // // // templateContent = FileUtils.readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/server.xml")); // templateContent = templateContent.replace("${", "\\${"); // System.out.println(templateContent); // System.out.println(); // System.out.println(); // System.out.println(); // // stringTemplate = new ST(JPackageManager.ST_GROUP, templateContent); //// stringTemplate = new ST(FileUtils.readFileToString(pfile)); // JPackageManager.addTemplateAttributes(stringTemplate, configuration); // templateContent = stringTemplate.render(); // templateContent = templateContent.replace("\\${", "${"); // System.out.println(templateContent); // } // catch(Exception e) // { // e.printStackTrace(); // } // try // { // String str = "${foo} $bar$"; // str = str.replace("${", "\\${"); // System.out.println(str); // // // STGroup stGroup = new STGroup('$', '$'); // ST stringTemplate = new ST(stGroup, str); // stringTemplate.add("bar", "yoyoyo"); // str = stringTemplate.render(); // System.out.println(str); // // str = str.replace("\\${", "${"); // System.out.println(str); // //// STGroup stGroup = new STGroup('$', '$'); //// ST stringTemplate = new ST(stGroup, FileUtils.readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/logging.properties"))); //// ST stringTemplate = new ST(FileUtils.readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/logging.properties"))); //// //// stringTemplate.add("bar_yo", "yoyoyo"); //// //// //// System.out.println(stringTemplate.render()); // // } // catch(Exception e) // { // e.printStackTrace(); // } //// String foo = "handler.FILE.fileName=#{org.jboss.boot.log.file:boot.log}\nyoyoyoyo\nrem ### \nREM#"; // String foo = "rem ### -*- batch file -*- ######################################################\r\n" + //"rem # ##\r\n" + //"rem # JBoss Bootstrap Script Configuration ##\r\n" + //"rem # ##\r\n" + //"rem #############################################################################\r\n"; // String regex = "(#)(\\{)([^\\}]*)(\\:)([^\\}]*)(\\})"; //// String regex = "(#)(\\{)([^\\{]*)(\\:)([^\\}]*)(\\})"; //// String regex = "(#)(\\{*)(\\:)(^*\\})"; // System.out.println("regex=" + regex); // System.out.println("foo=" + foo); //// foo = foo.replaceAll(regex, "\\$$2$3$4$5$6"); // foo = foo.replaceAll(regex, "\\$$2$3$4$5$6"); // System.out.println("foo=" + foo); // VelocityEngine velocityEngine = new VelocityEngine(); // Properties vProps = new Properties(); // vProps.setProperty("resource.loader", "string"); // vProps.setProperty("string.resource.loader.class", "org.apache.velocity.runtime.resource.loader.StringResourceLoader"); // velocityEngine.init(vProps); // Template template = null; // // try // { // VelocityContext velocityContext = JPackageManager.createVelocityContext(new PropertiesConfiguration("C:/dev/workbench/paradigm-workspace/jpackage-manager/install.properties")); // StringResourceRepository stringResourceRepository = StringResourceLoader.getRepository(); // String templateContent = null; // StringWriter stringWriter = null; // //// templateContent = FileUtils.readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/appclient.conf.bat")); //// templateContent = "rem ### -*- batch file -*- ######################################################\r\n"; // templateContent = "rem %%% -*- batch file -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\r\n"; // // // Pattern pattern = Pattern.compile("(\\$)(\\{)([^\\}]*)(\\:)([^\\{]*)(\\})"); // Matcher matcher = pattern.matcher(templateContent); // if(matcher.matches()) // { // System.out.println(" converting # to $"); // matcher.replaceAll("\\$$2$3$4$5$6"); // } // else // { // System.out.println(" NOPE"); // } // //// templateContent = templateContent.replaceAll("(\\$)(\\{)([^\\}]*)(\\:)([^\\}]*)(\\})", "#$2$3$4$5$6"); // System.out.println("content=" + templateContent); // // stringResourceRepository.putStringResource(JPackageManager.CURRENT_TEMPLATE_NAME, templateContent); // stringWriter = new StringWriter(); // template = velocityEngine.getTemplate(JPackageManager.CURRENT_TEMPLATE_NAME); // template.merge(velocityContext, stringWriter); // // templateContent = stringWriter.toString(); // // Pattern pattern2 = Pattern.compile("(\\$)(\\{)([^\\}]*)(\\:)([^\\{]*)(\\})"); // Matcher matcher2 = pattern2.matcher(templateContent); // if(matcher2.matches()) // { // System.out.println(" converting # to $"); // matcher2.replaceAll("\\$$2$3$4$5$6"); // } // else // { // System.out.println(" NOPE"); // } //// templateContent = templateContent.replaceAll("(#)(\\{)([^\\}]*)(\\:)([^\\{]*)(\\})", "\\$$2$3$4$5$6"); // // System.out.println(templateContent); // } // catch(Exception e) // { // e.printStackTrace(); // } // // //// StringTemplate templ = new StringTemplate("foo $fo$bo$r$ yo"); //// templ.setAttribute("success", "foobar"); //// templ.setAttribute("bo", "oba"); //// System.out.println(templ.toString()); // // // // try // { //// StringTemplateLoader stringTemplateLoader = new StringTemplateLoader(); // String firstTemplate = "firstTemplate"; // String content = "this should ${foobar} ${foo:bar.0.1}"; // String updatedContent = "this should ${foobar} #{foo:bar.0.1}"; //// String content = "this should ${foo:bar}"; // //// System.out.println(content.matches("\\$\\{.*\\:.*\\}")); // //// System.out.println(content.replaceAll("\\$\\{.*\\:.*\\}", "hahaha")); //// System.out.println(content.replaceAll("(\\$\\{.*)(\\:)(.*\\})", "$1-$3")); //// System.out.println(content.replaceAll("(\\$\\{.*)(\\:)(.*\\})", "$1-$3")); //// System.out.println(content.replaceAll("(\\$)(\\{.*)(\\:)(.*\\})", "#$2$3$4")); // System.out.println(updatedContent.replaceAll("(#)(\\{)([^\\}]*)(\\:)([^\\}]*)(\\})", "\\$$2$3$4$5$6")); // System.out.println(content.replaceAll("(\\$)(\\{)(.*)(\\:)(.*)(\\})", "--$2$3$4$5$6--")); // System.out.println(content.replaceAll("(\\$)(\\{\\w*\\:\\w*\\})", "#$2")); // //// stringTemplateLoader.putTemplate(firstTemplate, "this should ${foobar} ${foo:bar}"); //// //// freemarker.template.Configuration freeMarkerConfiguration = new freemarker.template.Configuration(); //// freeMarkerConfiguration.setTemplateLoader(stringTemplateLoader); //// Template template = freeMarkerConfiguration.getTemplate(firstTemplate); //// Map<String, Object> valueMap = new HashMap<String, Object>(); //// valueMap.put("foobar", "helloworld"); //// //// Writer out = new OutputStreamWriter(System.out); //// template.process(valueMap, out); //// out.flush(); //// //// freeMarkerConfiguration.clearTemplateCache(); // // } // catch(Exception e) // { // e.printStackTrace(); // } // // // // // System.out.println(""); // System.out.println(""); // // // VelocityEngine velocityEngine = new VelocityEngine(); // Properties vProps = new Properties(); //// vProps.put("file.resource.loader.path", ""); // vProps.setProperty("resource.loader", "string"); // vProps.setProperty("string.resource.loader.class", "org.apache.velocity.runtime.resource.loader.StringResourceLoader"); // velocityEngine.init(vProps); // Template template = null; // // // try // { // // // Configuration configuration = new PropertiesConfiguration(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/install.properties")); // VelocityContext velocityContext = JPackageManager.createVelocityContext(configuration); // // // StringResourceRepository repository = StringResourceLoader.getRepository(); // String strTemplate = FileUtils.readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/domain.sh")); // // //// strTemplate = strTemplate.replace(":", "\\:"); // strTemplate = strTemplate.replaceAll("(\\$)(\\{)([^\\}]*)(\\:)([^\\}]*)(\\})", "#$2$3$4$5$6"); // strTemplate = strTemplate.replaceAll("(\\$)(\\{)([^\\}]*)([^a-zA-Z\\d\\_])*([^\\}]*)(\\})", "#$2$3$4$5$6"); // //System.out.println(strTemplate); // // repository.putStringResource("template", strTemplate); // StringWriter writer = new StringWriter(); // template = velocityEngine.getTemplate("template"); // template.merge(velocityContext, writer); // // // String strResult = writer.toString().replaceAll("(\\#)(\\{)([^\\}]*)(\\:)([^\\}]*)(\\})", "$$2$3$4$5$6"); // // // System.out.println(strResult); // } // catch (Exception e) // { // e.printStackTrace(); // } try { Configuration configuration = new PropertiesConfiguration( new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/install.properties")); Map<String, String> substitutionContext = JPackageManagerOld.createSubstitutionContext(configuration); StrSubstitutor strSubstitutor = new StrSubstitutor(substitutionContext); String strTemplate = FileUtils .readFileToString(new File("C:/dev/workbench/paradigm-workspace/jpackage-manager/domain.sh")); System.out.println(strTemplate); System.out.println(strSubstitutor.replace(strTemplate)); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.redhat.utils.PluginUtils.java
public static String getSubstitutedValue(String id, EnvVars env) { String text = id.replaceAll("\\$([a-zA-Z_]+[a-zA-Z0-9_]*)", "\\${$1}"); //replace $VAR instances with ${VAR}. StrSubstitutor sub1 = new StrSubstitutor(env); return sub1.replace(text).trim(); }
From source file:com.appdynamics.monitors.muleesb.JMXUtil.java
private static String buildUrl(String host, int port) { Map<String, String> valueMap = new HashMap<String, String>(); valueMap.put("HOST", host); valueMap.put("PORT", String.valueOf(port)); StrSubstitutor strSubstitutor = new StrSubstitutor(valueMap); return strSubstitutor.replace(JMX_URL); }
From source file:com.enonic.cms.framework.util.PropertiesUtil.java
/** * Interpolate properties names like ${..}. */// w ww. j a v a 2 s .c o m public static Properties interpolate(final Properties props) { Properties target = new Properties(); Properties source = new Properties(); source.putAll(System.getProperties()); source.putAll(props); StrLookup lookup = StrLookup.mapLookup(source); StrSubstitutor substitutor = new StrSubstitutor(lookup); for (Object key : props.keySet()) { String value = props.getProperty((String) key); try { value = substitutor.replace(value); } catch (IllegalStateException e) { // Do nothing } target.put(key, value); } return target; }
From source file:com.googlecode.xtecuannet.framework.catalina.manager.tomcat.constants.Constants.java
public static String getResolvedValue(String elKey, Class toResolve, PropertiesConfiguration cfg) { String out = null;//w w w .j av a 2 s.c o m String value = cfg.getString(elKey); if (value.contains("${")) { Map map = generateMapFromConfig(toResolve); StrSubstitutor subs = new StrSubstitutor(map); subs.setEnableSubstitutionInVariables(true); out = subs.replace(value); if (out != null && out.contains("${")) { out = evaluateTransformationFunction(out); } } else { out = value; } return out; }
From source file:com.googlecode.xtecuannet.framework.catalina.manager.tomcat.constants.Constants.java
public static String getResolvedValue(String elKey, Class toResolve) { String out = null;//from ww w .j a v a 2s . c om String value = getConfig().getString(elKey); if (value.contains("${")) { Map map = generateMapFromConfig(toResolve); StrSubstitutor subs = new StrSubstitutor(map); subs.setEnableSubstitutionInVariables(true); out = subs.replace(value); if (out != null && out.contains("${")) { out = evaluateTransformationFunction(out); } } else { out = value; } return out; }
From source file:ddf.test.itests.common.Library.java
/** * Variables to be replaced in a resource file should be in the format: $variableName$ * The variable to replace in the file should also also match the parameter names of the method calling getFileContent. * * @param filePath/*w w w . ja v a 2 s . c o m*/ * @param params * @return */ public static String getFileContent(String filePath, ImmutableMap<String, String> params) { StrSubstitutor strSubstitutor = new StrSubstitutor(params); strSubstitutor.setVariablePrefix(VARIABLE_DELIMETER); strSubstitutor.setVariableSuffix(VARIABLE_DELIMETER); String fileContent = null; try { fileContent = org.apache.commons.io.IOUtils.toString(Library.class.getResourceAsStream(filePath), "UTF-8"); } catch (IOException e) { throw new RuntimeException("Failed to read filepath: " + filePath); } return strSubstitutor.replace(fileContent); }
From source file:com.infullmobile.jenkins.plugin.restrictedregister.mail.impl.EnvVariablesDecorator.java
@Override public String getTransformedMessage(FormatterData formatterData, String input) { final IJenkinsDescriptor jenkinsDescriptor = formatterData.getDataForType(IJenkinsDescriptor.class); if (jenkinsDescriptor != null) { final Map<String, String> vars = jenkinsDescriptor.getMasterEnvironmentVariables(); final StrSubstitutor strSubstitutor = new StrSubstitutor(vars); return strSubstitutor.replace(input); } else {/* ww w .j a v a 2 s.co m*/ return input; } }
From source file:com.infullmobile.jenkins.plugin.restrictedregister.mail.impl.LocalVariablesDecorator.java
@Override public String getTransformedMessage(FormatterData formatterData, String input) { final LocalVariables localVariables = formatterData.getDataForType(LocalVariables.class); if (localVariables != null) { final Map<String, String> vars = localVariables.getVariables(); final StrSubstitutor strSubstitutor = new StrSubstitutor(vars); return strSubstitutor.replace(input); } else {/* w w w .j ava2s .c o m*/ return input; } }