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

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

Introduction

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

Prototype

public StrSubstitutor(StrLookup variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher,
        char escape) 

Source Link

Document

Creates a new instance and initializes it.

Usage

From source file:at.bestsolution.persistence.java.Util.java

public static final ProcessedSQL processSQL(String sql, final Function<String, List<?>> listLookup) {
    final List<String> dynamicParameterNames = new ArrayList<String>();
    final Map<String, List<TypedValue>> typedValuesMap = new HashMap<String, List<TypedValue>>();
    String s = new StrSubstitutor(new StrLookup() {

        @Override//w ww .  jav a 2s . c o  m
        public String lookup(String key) {
            List<?> data = listLookup.execute(key);
            if (data == null) {
                dynamicParameterNames.add(key);
                return "?";
            } else {
                List<TypedValue> list = null;
                StringBuilder rv = new StringBuilder();
                for (Object o : data) {
                    if (rv.length() > 0) {
                        rv.append(", ");
                    }

                    if (o == null) {
                        rv.append("NULL");
                    } else if (o instanceof Long || o instanceof Integer) {
                        rv.append(o);
                    } else {
                        if (list == null) {
                            list = new ArrayList<TypedValue>();
                            typedValuesMap.put(key, list);
                        }
                        list.add(new TypedValue(o, JDBCType.fromJavaType(o.getClass())));
                        rv.append("?");
                    }
                }
                return rv.toString();
            }
        }
    }, "#{", "}", '#').replace(sql);
    return new ProcessedSQL(s, dynamicParameterNames, null);
}

From source file:at.bestsolution.persistence.java.Util.java

public static final ProcessedSQL processSQL(String sql) {
    final List<String> dynamicParameterNames = new ArrayList<String>();
    String s = new StrSubstitutor(new StrLookup() {

        @Override/*from   w  ww  . j a v  a2  s.  c om*/
        public String lookup(String key) {
            dynamicParameterNames.add(key);
            return "?";
        }
    }, "#{", "}", '#').replace(sql);
    return new ProcessedSQL(s, dynamicParameterNames, null);
}

From source file:ome.services.blitz.repo.ManagedRepositoryI.java

/**
 * Turn the current template into a relative path. Makes use of the data
 * returned by {@link #replacementMap(Ice.Current)}.
 *
 * @param curr/*from   w  ww . ja  v  a 2 s. c  o  m*/
 * @return
 */
protected String expandTemplate(final String template, EventContext ec) {

    if (template == null) {
        return ""; // EARLY EXIT.
    }

    final Map<String, String> map = replacementMap(ec);
    final StrSubstitutor strSubstitutor = new StrSubstitutor(new StrLookup() {
        @Override
        public String lookup(final String key) {
            return map.get(key);
        }
    }, "%", "%", '%');
    return strSubstitutor.replace(template);
}

From source file:org.eurekastreams.server.action.execution.notification.notifier.NotificationMessageBuilderHelper.java

/**
 * Returns a variable-substituted version of the activity's body.
 * //from  w ww.j  av  a  2 s.co  m
 * @param activity
 *            Activity.
 * @param context
 *            Velocity context.
 * @return Activity body text.
 */
public String resolveActivityBody(final ActivityDTO activity, final Context context) {
    StrSubstitutor transform = new StrSubstitutor(new StrLookup() {
        @Override
        public String lookup(final String variableName) {
            if ("ACTORNAME".equals(variableName)) {
                return activity.getActor().getDisplayName();
            } else {
                return null;
            }
        }
    }, VARIABLE_START_MARKER, VARIABLE_END_MARKER, StrSubstitutor.DEFAULT_ESCAPE);
    String result = transform.replace(activity.getBaseObjectProperties().get("content"));
    return result;
}