Example usage for org.apache.commons.lang.text StrSubstitutor replace

List of usage examples for org.apache.commons.lang.text StrSubstitutor replace

Introduction

In this page you can find the example usage for org.apache.commons.lang.text StrSubstitutor replace.

Prototype

public static String replace(Object source, Map valueMap) 

Source Link

Document

Replaces all the occurrences of variables in the given source object with their matching values from the map.

Usage

From source file:org.eclipse.fx.ui.workbench.fx.internal.GraphicsLoaderImpl.java

@SuppressWarnings("null")
@NonNull/*from   ww  w.  j  a v  a  2s.  c o m*/
private static URI replaceDynamicValues(@NonNull URI uri,
        @NonNull Map<@NonNull String, @NonNull String> dynamicMap) {
    String s = uri.toString();
    s = StrSubstitutor.replace(s, dynamicMap);
    return URI.create(s);
}

From source file:org.eclipse.jubula.version.Vn.java

/** {@inheritDoc} */
public void start(BundleContext context) throws Exception {
    super.start(context);
    plugin = this;

    URL url = getBundle().getResource("version.properties"); //$NON-NLS-1$

    if (url != null) {
        Properties p = new Properties();
        InputStream versionInfo = null;
        try {//from   w  w w  .  jav a 2  s.  co m
            versionInfo = url.openStream();
            p.load(versionInfo);
            String versionString = StrSubstitutor.replace(p.getProperty(BUILD_VERSION_KEY), p);
            m_version = new Version(versionString);
        } catch (Throwable t) {
            log.warn(t.getLocalizedMessage(), t);
        } finally {
            if (versionInfo != null) {
                versionInfo.close();
            }
        }
    }
}

From source file:org.eclipse.wb.internal.core.model.property.event.ListenerMethodProperty.java

/**
 * @return the unique name of inner {@link TypeDeclaration} for event listener.
 *///from   w w  w . j a va2 s .  c o m
private String createInnerClassName() {
    Map<String, String> valueMap = Maps.newTreeMap();
    {
        String componentName = getComponentName(m_javaInfo);
        valueMap.put("component_name", componentName);
        valueMap.put("Component_name", StringUtils.capitalize(componentName));
        //
        String componentType = CodeUtils
                .getShortClass(m_javaInfo.getDescription().getComponentClass().getName());
        valueMap.put("component_className", componentType);
        valueMap.put("Component_className", StringUtils.capitalize(componentType));
        //
        String listenerMethodName = CodeUtils.getShortClass(m_listener.getInterface().getCanonicalName());
        valueMap.put("listener_className", listenerMethodName);
        valueMap.put("Listener_className", StringUtils.capitalize(listenerMethodName));
        //
        String listenerName = m_listener.getSimpleName();
        valueMap.put("listener_name", listenerName);
        valueMap.put("Listener_name", StringUtils.capitalize(listenerName));
    }
    // generate base name
    String template = m_preferences.getString(P_INNER_NAME_TEMPLATE);
    String baseName = StrSubstitutor.replace(template, valueMap);
    // generate unique name
    return m_javaInfo.getEditor().getUniqueTypeName(baseName);
}

From source file:org.eclipse.wb.internal.core.model.property.event.ListenerMethodProperty.java

/**
 * @return the name of the stub method.//from   w  w  w. ja v a 2 s .com
 */
private String getStubMethodName(ListenerMethodInfo methodInfo) {
    Map<String, String> valueMap = Maps.newTreeMap();
    {
        String componentName = getComponentName(m_javaInfo);
        valueMap.put("component_name", componentName);
        valueMap.put("Component_name", StringUtils.capitalize(componentName));
        //
        String componentType = CodeUtils
                .getShortClass(m_javaInfo.getDescription().getComponentClass().getName());
        valueMap.put("component_class_name", componentType);
        valueMap.put("Component_class_name", StringUtils.capitalize(componentType));
        //
        String methodName = methodInfo.getName();
        valueMap.put("event_name", methodName);
        valueMap.put("Event_name", StringUtils.capitalize(methodName));
    }
    //
    String template = m_preferences.getString(P_STUB_NAME_TEMPLATE);
    return StrSubstitutor.replace(template, valueMap);
}

From source file:org.eclipse.wb.internal.core.model.util.TemplateUtils.java

/**
 * Evaluates source {@link String}, with <code>"${variable}"</code> variables and
 * <code>"${expression}"</code> expressions.
 * <p>/*from   www  .  jav a2s.co m*/
 * Typical expression is <code>"${parent.firstChild[java.awt.LayoutManager].expression}"</code>.
 * 
 * @param source
 *          the source {@link String} to evaluate.
 * @param javaInfo
 *          the starting {@link JavaInfo}, relative to which expressions should be evaluated.
 * @param templateVariables
 *          the {@link Map} to variable names into values, may be <code>null</code>.
 * 
 * @return the source with replaced template variables/expressions.
 */
public static String evaluate(String source, JavaInfo javaInfo, Map<String, String> templateVariables)
        throws Exception {
    // replace template variables
    if (templateVariables != null) {
        source = StrSubstitutor.replace(source, templateVariables);
    }
    // replace template expressions
    while (true) {
        // extract single template expression
        String expression = StringUtils.substringBetween(source, "${", "}");
        if (expression == null) {
            break;
        }
        // replace template expression
        try {
            String result = evaluateExpression(expression, javaInfo);
            source = StringUtils.replace(source, "${" + expression + "}", result);
        } catch (Throwable e) {
            String message = String.format("Exception during evaluation |%s|.", source);
            throw new IllegalArgumentException(message, e);
        }
    }
    return source;
}

From source file:org.eclipse.wb.internal.core.model.variable.NamesManager.java

/**
 * @return the basic name of variable (without prefix/suffix or uniqueness test) for given
 *         component and text, or <code>null</code> if no valid name can be generated.
 *///  ww w  .ja  v a2s.  c  om
private static String getNameForText(JavaInfo javaInfo, String text) {
    ComponentDescription description = javaInfo.getDescription();
    IPreferenceStore preferences = description.getToolkit().getPreferences();
    // prepare component class name for variable
    String classNameForVariable;
    {
        String qualifiedClassName = ReflectionUtils.getCanonicalName(description.getComponentClass());
        String shortClassName = CodeUtils.getShortClass(qualifiedClassName);
        classNameForVariable = StringUtilities.stripLeadingUppercaseChars(shortClassName, 1);
    }
    // prepare "text" part of template
    String textPart;
    {
        textPart = text.toLowerCase();
        textPart = WordUtils.capitalize(textPart);
        // remove HTML tags
        textPart = StringUtilities.stripHtml(textPart);
        // get first "wordsLimit" words to prevent too long identifiers
        {
            int wordsLimit = preferences.getInt(IPreferenceConstants.P_VARIABLE_TEXT_WORDS_LIMIT);
            if (wordsLimit > 0) {
                String[] strings = StringUtils.split(textPart);
                if (strings.length > wordsLimit) {
                    textPart = "";
                    for (int i = 0; i < wordsLimit; i++) {
                        textPart += strings[i];
                    }
                }
            }
        }
        // remove invalid characters
        textPart = StringUtilities.removeNonLatinCharacters(textPart);
        // is there any remaining symbol? :)
        if (textPart.length() == 0) {
            return null;
        }
    }
    // use template
    String name;
    {
        Map<String, String> valueMap = Maps.newTreeMap();
        {
            valueMap.put("class_name", classNameForVariable);
            valueMap.put("text", textPart);
            valueMap.put("default_name", getName(javaInfo));
            valueMap.put("class_acronym", getAcronym(javaInfo));
        }
        // use template
        String template = preferences.getString(IPreferenceConstants.P_VARIABLE_TEXT_TEMPLATE);
        name = StrSubstitutor.replace(template, valueMap);
    }
    // final modifications
    name = StringUtils.uncapitalize(name);
    return name;
}

From source file:org.eclipse.wb.internal.core.model.variable.NamesManager.java

/**
 * @return <code>null</code> if given template is valid, or some error message if no. For example
 *         invalid template may attempt to use unsupported variables (or in wrong case).
 *///from  ww  w.j  a  v a 2s.  c  o m
public static String validate(String template) {
    // prepare empty variables
    Map<String, String> valueMap = Maps.newTreeMap();
    {
        valueMap.put("class_name", "");
        valueMap.put("text", "");
        valueMap.put("default_name", "");
        valueMap.put("class_acronym", "");
    }
    // use template
    String evaluated = StrSubstitutor.replace(template, valueMap);
    if (evaluated.indexOf("$") != -1) {
        return evaluated;
    }
    return null;
}

From source file:org.eclipse.wb.internal.core.model.variable.SyncParentChildVariableNameSupport.java

/**
 * @return the name of child {@link JavaInfo} that corresponds to the name of parent
 *         {@link JavaInfo}./*  w w w .jav  a 2  s . c om*/
 */
protected String generateName() {
    // prepare template
    String template = getTemplate();
    if (template.equals(getTemplateForDefault()) || StringUtils.isEmpty(template)) {
        return null;
    }
    // use template
    return StrSubstitutor.replace(template, getValueMap());
}

From source file:org.eclipse.wb.internal.ercp.wizards.project.AbstractProjectCreationOperation.java

/**
 * Creates new {@link IFile} using template.
 * /*from  www  .  jav a 2s . c o  m*/
 * @param templatePath
 *          the path to the template, relative to this {@link Class}.
 * @param folderPath
 *          the path of {@link IFolder}.
 * @param fileName
 *          the name of file in {@link IFolder}.
 * 
 * @return the create {@link IFile}.
 */
protected final IFile createTemplateFile(String templatePath, Map<String, String> valueMap, String folderPath,
        String fileName, IProgressMonitor monitor) throws Exception {
    IFile file;
    if (folderPath != null) {
        project.getFolder(new Path(folderPath)).create(true, true, monitor);
        file = project.getFile(new Path(folderPath + "/" + fileName));
    } else {
        file = project.getFile(new Path(fileName));
    }
    // create
    String template = IOUtils.toString(getClass().getResourceAsStream(templatePath));
    String contents = StrSubstitutor.replace(template, valueMap);
    IOUtils2.setFileContents(file, new ByteArrayInputStream(contents.getBytes()));
    //
    return file;
}

From source file:org.eclipse.wb.internal.ercp.wizards.project.AbstractProjectCreationOperation.java

/**
 * Creates new {@link ICompilationUnit} using template.
 * //from   w w w  .  j  av a  2s.c o  m
 * @param templatePath
 *          the path to the template, relative to this {@link Class}.
 * @param packageName
 *          the path of {@link IFolder}.
 * @param unitName
 *          the name of file in {@link IFolder}.
 * @return the created {@link ICompilationUnit}.
 */
protected final ICompilationUnit createTemplateUnit(String templatePath, Map<String, String> valueMap,
        String packageName, String unitName, IProgressMonitor monitor) throws Exception {
    String template = IOUtils.toString(getClass().getResourceAsStream(templatePath));
    String contents = StrSubstitutor.replace(template, valueMap);
    // create
    IPackageFragment packageFragment = m_packageFragmentRoot.createPackageFragment(packageName, true, monitor);
    return packageFragment.createCompilationUnit(unitName, contents, true, monitor);
}