Example usage for org.apache.commons.lang3 StringEscapeUtils escapeHtml4

List of usage examples for org.apache.commons.lang3 StringEscapeUtils escapeHtml4

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringEscapeUtils escapeHtml4.

Prototype

public static final String escapeHtml4(final String input) 

Source Link

Document

Escapes the characters in a String using HTML entities.

For example:

"bread" & "butter"

becomes:

"bread" & "butter".

Usage

From source file:org.apache.sling.testing.clients.util.XSSUtils.java

/**
 * Use to encapsulate old-style escaping of HTML (using StringEscapeUtils).
 * NB: newer code uses XSSAPI (based on OWASP's ESAPI).
 *
 * @param htmlString the string to be escaped
 * @return the escaped string//from   w  w w. j av a  2  s  . c o  m
 */
public static String escapeHtml(String htmlString) {
    return StringEscapeUtils.escapeHtml4(htmlString);
}

From source file:org.apache.struts2.components.Property.java

private String prepare(String value) {
    String result = value;//w  w  w.  ja  v  a  2s.  c  o  m
    if (escapeHtml) {
        result = StringEscapeUtils.escapeHtml4(result);
    }
    if (escapeJavaScript) {
        result = StringEscapeUtils.escapeEcmaScript(result);
    }
    if (escapeXml) {
        result = StringEscapeUtils.escapeXml(result);
    }
    if (escapeCsv) {
        result = StringEscapeUtils.escapeCsv(result);
    }

    return result;
}

From source file:org.apache.struts2.config_browser.ActionNamesAction.java

public String getNamespace() {
    return StringEscapeUtils.escapeHtml4(namespace);
}

From source file:org.apache.struts2.showcase.model.Skill.java

public String getName() {
    return StringEscapeUtils.escapeEcmaScript(StringEscapeUtils.escapeHtml4(name));
}

From source file:org.apache.struts2.showcase.model.Skill.java

public String getDescription() {
    return StringEscapeUtils.escapeEcmaScript(StringEscapeUtils.escapeHtml4(description));
}

From source file:org.apache.struts2.util.TextProviderHelper.java

/**
 * <p>Get a message from the first TextProvider encountered in the stack.
 * If the first TextProvider doesn't provide the message the default message is returned.</p>
 * <p>The search for a TextProvider is iterative from the root of the stack.</p>
 * <p>This method was refactored from  {@link org.apache.struts2.components.Text} to use a
 * consistent implementation across UIBean components.</p>
 * @param key             the message key in the resource bundle
 * @param defaultMessage  the message to return if not found (evaluated for OGNL)
 * @param args            an array args to be used in a {@link java.text.MessageFormat} message
 * @param stack           the value stack to use for finding the text
 * @param searchStack     search stack for the key
 *
 * @return the message if found, otherwise the defaultMessage
 *//*ww  w  .  j  ava 2  s. com*/
public static String getText(String key, String defaultMessage, List<Object> args, ValueStack stack,
        boolean searchStack) {
    String msg = null;
    TextProvider tp = null;

    for (Object o : stack.getRoot()) {
        if (o instanceof TextProvider) {
            tp = (TextProvider) o;
            msg = tp.getText(key, null, args, stack);

            break;
        }
    }

    if (msg == null) {
        // evaluate the defaultMessage as an OGNL expression
        if (searchStack)
            msg = stack.findString(defaultMessage);

        if (msg == null) {
            // use the defaultMessage literal value
            msg = defaultMessage;
            msg = StringEscapeUtils.escapeEcmaScript(msg);
            msg = StringEscapeUtils.escapeHtml4(msg);
            LOG.debug("Message for key '{}' is null, returns escaped default message [{}]", key, msg);
        }

        if (LOG.isWarnEnabled()) {
            if (tp != null) {
                LOG.warn(
                        "The first TextProvider in the ValueStack ({}) could not locate the message resource with key '{}'",
                        tp.getClass().getName(), key);
            } else {
                LOG.warn(
                        "Could not locate the message resource '{}' as there is no TextProvider in the ValueStack.",
                        key);
            }
            if (defaultMessage.equals(msg)) {
                LOG.warn(
                        "The default value expression '{}' was evaluated and did not match a property. The literal value '{}' will be used.",
                        defaultMessage, defaultMessage);
            } else {
                LOG.warn("The default value expression '{}' evaluated to '{}'", defaultMessage, msg);
            }
        }
    }
    return msg;
}

From source file:org.apache.struts2.views.java.Attributes.java

public Attributes add(String key, String value, boolean encode) {
    put(key, (encode ? StringUtils.defaultString(StringEscapeUtils.escapeHtml4(value)) : value));
    return this;
}

From source file:org.apache.struts2.views.java.Attributes.java

/**
 * Add a key/value pair to the attributes only if the value is not null.
 * @param attrName attribute name// ww w. j  a  v  a  2 s  . c o  m
 * @param paramValue value of attribute
 * @param encode html encode the value
 * @return this
 */
public Attributes addIfExists(String attrName, Object paramValue, boolean encode) {
    if (paramValue != null) {
        String val = paramValue.toString();
        if (StringUtils.isNotBlank(val))
            put(attrName, (encode ? StringUtils.defaultString(StringEscapeUtils.escapeHtml4(val)) : val));
    }
    return this;
}

From source file:org.apache.struts2.views.java.Attributes.java

/**
 * Add a key/value pair to the attributes, if the value is null, it will be set as an empty string.
 * @param attrName attribute name/*from w  ww  .j a  va  2s. c  o  m*/
 * @param paramValue value of attribute
 * @param encode html encode the value
 * @return this
 */
public Attributes addDefaultToEmpty(String attrName, Object paramValue, boolean encode) {
    if (paramValue != null) {
        String val = paramValue.toString();
        put(attrName, (encode ? StringUtils.defaultString(StringEscapeUtils.escapeHtml4(val)) : val));
    } else {
        put(attrName, "");
    }
    return this;
}

From source file:org.apache.struts2.views.java.simple.CheckboxHandler.java

public void generate() throws IOException {
    Map<String, Object> params = context.getParameters();
    Attributes attrs = new Attributes();

    String fieldValue = (String) params.get("fieldValue");
    String id = (String) params.get("id");
    String name = (String) params.get("name");
    Object disabled = params.get("disabled");

    attrs.add("type", "checkbox").add("name", name).add("value", fieldValue)
            .addIfTrue("checked", params.get("nameValue")).addIfTrue("readonly", params.get("readonly"))
            .addIfTrue("disabled", disabled).addIfExists("tabindex", params.get("tabindex"))
            .addIfExists("id", id).addIfExists("class", params.get("cssClass"))
            .addIfExists("style", params.get("cssStyle")).addIfExists("title", params.get("title"));
    start("input", attrs);
    end("input");

    //hidden input
    attrs = new Attributes();
    attrs.add("type", "hidden")
            .add("id", "__checkbox_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(id)))
            .add("name", "__checkbox_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(name)))
            .add("value", "__checkbox_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(fieldValue)))
            .addIfTrue("disabled", disabled);
    start("input", attrs);
    end("input");
}