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.struts2.views.java.simple.CheckboxListHandler.java

public void generate() throws IOException {

    Map<String, Object> params = context.getParameters();

    //Get parameters
    Object listObj = params.get("list");
    String listKey = (String) params.get("listKey");
    String listValue = (String) params.get("listValue");
    String name = (String) params.get("name");
    Object disabled = params.get("disabled");
    String id = (String) params.get("id");

    int cnt = 1;/* w  w w . j  av  a  2s  .c o  m*/

    //This will interate through all lists
    ValueStack stack = this.context.getStack();
    if (listObj != null) {
        Iterator itt = MakeIterator.convert(listObj);
        while (itt.hasNext()) {
            Object item = itt.next();
            stack.push(item);

            //key
            Object itemKey = findValue(listKey != null ? listKey : "top");
            String itemKeyStr = StringUtils.defaultString(itemKey == null ? null : itemKey.toString());

            //value
            Object itemValue = findValue(listValue != null ? listValue : "top");
            String itemValueStr = StringUtils.defaultString(itemValue == null ? null : itemValue.toString());

            //Checkbox button section
            Attributes a = new Attributes();
            a.add("type", "checkbox").add("name", name).add("value", itemKeyStr)
                    .addIfTrue("checked", isChecked(params, itemKeyStr))
                    .addIfTrue("readonly", params.get("readonly")).addIfTrue("disabled", disabled)
                    .addIfExists("tabindex", params.get("tabindex"))
                    .addIfExists("id", id + "-" + Integer.toString(cnt));
            start("input", a);
            end("input");

            //Label section
            a = new Attributes();
            a.add("for", id + "-" + Integer.toString(cnt)).addIfExists("class", params.get("cssClass"))
                    .addIfExists("style", params.get("cssStyle"));
            super.start("label", a);
            if (StringUtils.isNotEmpty(itemValueStr))
                characters(itemValueStr);
            super.end("label");

            //Hidden input section
            a = new Attributes();
            a.add("type", "hidden")
                    .add("id", "__multiselect_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(id)))
                    .add("name",
                            "__multiselect_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(name)))
                    .add("value", "").addIfTrue("disabled", disabled);
            start("input", a);
            end("input");

            stack.pop();
            cnt++;
        }
    }
}

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

public void characters(String text, boolean encode) throws IOException {
    writer.write(encode ? StringUtils.defaultString(StringEscapeUtils.escapeHtml4(text)) : text);
}

From source file:org.apparatus_templi.web.handler.SettingsHandler.java

private byte[] getResponse() {
    byte[] returnBytes = null;
    byte[] templateBytes = HttpHelper.getFileBytes(webserver.getResourceFolder() + "inc/template.html");
    if (templateBytes != null) {
        String template = new String(templateBytes);

        StringBuilder html = new StringBuilder();
        HashMap<String, String> prefs = Coordinator.getPrefs().getPreferencesMap();
        // remove any preferences that should be hidden from the frontend
        prefs.remove(Prefs.Keys.userName);
        prefs.remove(Prefs.Keys.userPass);

        String configFile = prefs.get(Prefs.Keys.configFile);

        if (webserver instanceof EncryptedWebServer) {
            if (!Prefs.isCredentialsSet()) {
                html.append(//from  w w  w .  j  a v a  2 s .  c o m
                        "<div class='info-box' style='width:600px; display: block; margin-right: auto; margin-left: auto; "
                                + "cursor: pointer; font-size: smaller; text-align: center; padding: 10px'><p>No password has been set. "
                                + "Access will be unrestricted until you <a onclick='window.open(\"/set_password\", \"password_change\", "
                                + "\"toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=750,height=550\");'>"
                                + "set a password</a>.</p></div>");
            }
        }

        // TODO update to a form so that the settings can be sent back in a POST request
        html.append("<div id=\"prefs_form\">");

        // Buttons
        html.append("<div id='settings-buttons'>");
        // TODO what modules can be restarted?
        html.append("<span id=\"restart_all_button\" class=\"btn-group closed\" >"
                + "<a class=\"btn btn-danger\" href=\"/restart_module?module=all\" title='Restarting the service will re-read "
                + "preferences from config file, restart all driver, and re-initialize the web server and the serial connection.'>"
                + "<i class=\"fa fa-refresh fa-fw\"></i> &nbsp;&nbsp;Restart Service</a>"
                + "<a class=\"btn btn-danger dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">"
                + "<span class=\"fa fa-caret-down\"></span></a>" + "<ul class=\"dropdown-menu\">"
                + "<li><a href=\"/restart_module?module=drivers\" title='Ask all drivers to terminate, then re-initialize all "
                + "drivers.  Only drivers specified in the driver list will be started.'><i class=\"fa fa-refresh fa-fw\"></i> "
                + "Restart Drivers</a></li><li><a href=\"/restart_module?module=web\" title='Restart the web server. This will "
                + "bind the web server to a new address and port number if those settings have been changed.'><i class=\"fa "
                + "fa-refresh fa-fw\"></i> Restart Web Server</a></li><li><a href=\"/restart_module?module=serial\" "
                + "title='Re-initialize the serial connection, discarding all partial messages'><i class=\"fa fa-refresh "
                + "fa-fw\"></i> Serial Connection</a></li></ul> </span>");

        // Save preferences button
        // if the config file is the default then we want the save preferences button to be
        // disabled until updated via javascript
        html.append("<span id='form_submit' ");
        if (configFile.equals(Coordinator.getPrefs().getDefPreference(Prefs.Keys.configFile))) {
            html.append("class='btn btn-success disabled'>");
        } else {
            html.append("class ='btn btn-success' ");
            html.append("form=\"prefs\" ");
            html.append("onclick = \"document.getElementById('prefs').submit()\" >");
        }
        html.append("<i class=\"fa fa-save\"></i>&nbsp;&nbsp;" + "Save Preferences to <span id='btn_conf_file'>"
                + configFile + "</span></div>");

        // end submit span
        html.append("</span>");

        // end buttons div
        // html.append("</div>");

        // clear the elements
        // html.append("<div class=\"clear\"></div>");

        html.append("<form name='prefs' id='prefs' action=\"update_settings\" " + "method=\"POST\" >\n");

        // settings boxes div
        html.append("<div id='settings_boxes'>");

        // Preferences for the main section
        html.append("<div id='prefs_section_main' class='prefs_section info-box'><div class='title'>"
                + "<i  class=\"fa fa-code-fork\"></i>&nbsp;Main" + "</div>");
        html.append("<div class='content'>");
        html.append("<div class=\"pref_input\"><span class=\"pref_key\">"
                + "<i class=\"fa fa-question-circle\" " + "title=\""
                + StringEscapeUtils.escapeHtml4(Coordinator.getPrefs().getPreferenceDesc(Prefs.Keys.configFile))
                + "\"></i>&nbsp;" + Prefs.Keys.configFile + "</span><span "
                + "class=\"pref_value\"><input id='f_config_file' type=\"text\" name=\"" + Prefs.Keys.configFile
                + "\" value=\"" + prefs.get(Prefs.Keys.configFile)
                + "\" onChange='updateConfigFile()' onkeypress='updateConfigFile()' onkeyup='updateConfigFile()' "
                + "onBlur='updateConfigFile()' /></span></div><br />\n");
        prefs.remove(Prefs.Keys.configFile);
        for (String key : new String[] { Prefs.Keys.serialPort, Prefs.Keys.driverList, Prefs.Keys.logFile,
                Prefs.Keys.emailList, Prefs.Keys.debugLevel }) {
            String value = prefs.get(key);
            // the serial port name can be a null value, but writing a null string
            // + will print "null" (a non-existent serial port). Write "" instead.
            if (key.equals(Prefs.Keys.serialPort) && value == null) {
                value = "";
            }
            html.append("<div class=\"pref_input\"><span class=\"pref_key \">"
                    + "<i class=\"fa fa-question-circle\" " + "title=\""
                    + StringEscapeUtils.escapeHtml4(Coordinator.getPrefs().getPreferenceDesc(key))
                    + "\"></i>&nbsp;" + Coordinator.getPrefs().getPreferenceName(key) + "</span><span "
                    + "class=\"pref_value\"><input "
                    + ((key == Prefs.Keys.userPass) ? " type='password' " : " type='text'") + " name=\"" + key
                    + "\" value=\"" + value + "\" /></span></div><br />\n");
            prefs.remove(key);
        }
        if (Prefs.isCredentialsSet() && webserver instanceof EncryptedWebServer) {
            html.append(
                    "<div style='margin-right: auto; margin-left: auto; cursor: pointer; text-align: center'><p><a "
                            + "onclick='window.open(\"/set_password\", \"password_change\", \"toolbar=no,location=no,status=no,"
                            + "menubar=no,scrollbars=yes,resizable=yes,width=750,height=550\");'>Change password</a></p></div>");
        }
        html.append("</div></div>");

        // Preferences for web server
        html.append("<div id='prefs_section_webserver'  class='prefs_section info-box'><div class='title'>"
                + "<i class=\"fa fa-cloud\"></i>&nbsp;Web Server" + "</div>");
        html.append("<div class='content'>");
        for (String key : new String[] { Prefs.Keys.portNum, Prefs.Keys.serverBindLocalhost,
                Prefs.Keys.encryptServer, Prefs.Keys.webResourceFolder }) {
            String value = prefs.get(key);
            html.append("<div class=\"pref_input\"><span class=\"pref_key\">"
                    + "<i class=\"fa fa-question-circle \" " + "title=\""
                    + StringEscapeUtils.escapeHtml4(Coordinator.getPrefs().getPreferenceDesc(key))
                    + "\"></i>&nbsp;" + Coordinator.getPrefs().getPreferenceName(key) + "</span><span "
                    + "class=\"pref_value\"><input "
                    + (key.equals((Prefs.Keys.portNum)) ? " type='number' " : "") + " type=\"text\" name=\""
                    + key + "\" value=\"" + value + "\" /></span></div><br />\n");
            prefs.remove(key);
        }
        html.append("</div></div>");

        // Preferences for the Twitter service
        html.append("<div id='prefs_section_twitter' class='prefs_section info-box'><div class='title'>"
                + "<i  class=\"fa fa-twitter\"></i>&nbsp;Twitter Service" + "</div>");
        html.append("<div class='content'>");
        for (String key : new String[] { Prefs.Keys.twtrAccess, Prefs.Keys.twtrAccessKey }) {
            html.append("<div class=\"pref_input\"><span class=\"pref_key\">"
                    + "<i class=\"fa fa-question-circle \" " + "title=\""
                    + StringEscapeUtils.escapeHtml4(Coordinator.getPrefs().getPreferenceDesc(key))
                    + "\"></i>&nbsp;" + Coordinator.getPrefs().getPreferenceName(key) + "</span><span "
                    + "class=\"pref_value\"><input " + " type=\"password\" name=\"" + key + "\" value=\""
                    + prefs.get(key) + "\" /></span></div><br />\n");
            prefs.remove(key);
        }
        html.append("</div>");
        html.append("<div class='warning'><p class=''>" + ENC_WARNING
                + "All passwords are stored in plaintext.</div>");
        html.append("</div>");

        // Preferences for the email service
        html.append("<div id='prefs_section_email' class='info-box prefs_section'><div class='title'>"
                + "<i  class=\"fa fa-envelope\"></i>&nbsp;Email Service" + "</div>");
        html.append("<div class='content'>");
        for (String key : new String[] { Prefs.Keys.emailAddress, Prefs.Keys.emailUsername,
                Prefs.Keys.emailPassword, Prefs.Keys.emailServer, Prefs.Keys.emailPort }) {
            html.append("<div class=\"pref_input\"><span class=\"pref_key\">"
                    + "<i class=\"fa fa-question-circle \" " + "title=\""
                    + StringEscapeUtils.escapeHtml4(Coordinator.getPrefs().getPreferenceDesc(key))
                    + "\"></i>&nbsp;" + Coordinator.getPrefs().getPreferenceName(key) + "</span><span "
                    + "class=\"pref_value\"><input "
                    + ((key == Prefs.Keys.emailPassword) ? "type = 'password' " : " type=\"text\"") + " name=\""
                    + key + "\" value=\"" + prefs.get(key) + "\" /></span></div><br />\n");
            prefs.remove(key);
        }
        html.append("</div>");
        html.append("<div class='warning'><p class=''>" + ENC_WARNING
                + "All passwords are stored in plaintext.</div>");
        html.append("</div>");

        // Any remaining unclassified preferences
        if (!prefs.isEmpty()) {
            html.append("<div id='prefs_section_unknown' class='info-box prefs_section'><div class='title'>"
                    + "<i  class=\"fa fa-question\"></i>&nbsp;Uncategorized" + "</div>");
            html.append("<div class='content'>");
            for (String key : prefs.keySet()) {
                html.append("<div class=\"pref_input\"><span class=\"pref_key\">"
                        + "<i class=\"fa fa-question-circle \" " + "title=\""
                        + StringEscapeUtils.escapeHtml4(Coordinator.getPrefs().getPreferenceDesc(key))
                        + "\"></i>&nbsp;" + Coordinator.getPrefs().getPreferenceName(key) + "</span><span "
                        + "class=\"pref_value\"><input " + " type=\"text\" name=\"" + key + "\" value=\""
                        + prefs.get(key) + "\" /></span></div><br />\n");
            }
            html.append("</div></div>");
        }
        // end settings boxes div
        html.append("</div>");
        html.append("</form>");
        // clear the elements
        html.append("<div class=\"clear\"></div>");
        // html.append("<hr class=\"fancy-line\"></hr>");

        // buttons div
        html.append("<div id=\"settings_buttons_div\">");

        html.append("</div></div>");
        template = template.replace("!PROTOCOL!", webserver.getProtocol());
        template = template.replace("!MAIN_CONTENT!", html.toString());
        template = template.replace("!JAVASCRIPT!",
                "<script type='text/javascript' src='/resource?file=js/settings.js'></script>");
        returnBytes = template.getBytes();
    }
    return returnBytes;
}

From source file:org.asqatasun.processing.ProcessRemarkServiceImpl.java

/**
 * /*from ww w .  jav a2  s. c  o m*/
 * @param element
 * @return 
 */
public String getSnippetFromElement(Element element) {
    String elementHtml = StringEscapeUtils.escapeHtml4(StringUtil.normaliseWhitespace(element.outerHtml()))
            .trim();
    if (element.children().isEmpty() || elementHtml.length() <= SNIPPET_MAX_LENGTH) {
        return elementHtml;
    }
    return properlyCloseSnippet(element, elementHtml, elementHtml.substring(0, SNIPPET_MAX_LENGTH));
}

From source file:org.asqatasun.webapp.report.expression.I18nExpression.java

@Override
public Object evaluate(Map fields, Map variables, Map parameters) {
    String key = keyRetriever.retrieveKey(fields, variables, parameters);
    if (!resourceBundleList.isEmpty()) {
        String i18nValue = retrieveI18nValue(key);
        if (escapeHtml) {
            return StringEscapeUtils.escapeHtml4(i18nValue);
        }//from w w  w  .  ja  v a2  s. c  o m
        return StringEscapeUtils.unescapeHtml4(i18nValue);
    }
    return key;
}

From source file:org.auraframework.impl.factory.SVGParser.java

@Override
public SVGDef getDefinition(DefDescriptor<SVGDef> descriptor, TextSource<SVGDef> source)
        throws SVGParserException, QuickFixException {
    if (descriptor.getDefType() == DefType.SVG) {
        XMLStreamReader reader = null;
        String contents = source.getContents();
        //If the file is too big throw before we parse the whole thing.
        SVGDef ret = new SVGDefHandler<>(descriptor, source).createDefinition();
        try (StringReader stringReader = new StringReader(contents)) {
            reader = xmlInputFactory.createXMLStreamReader(stringReader);
            if (reader != null) {
                LOOP: while (reader.hasNext()) {
                    int type = reader.next();
                    switch (type) {
                    case XMLStreamConstants.END_DOCUMENT:
                        break LOOP;
                    //This is plain text inside the file
                    case XMLStreamConstants.CHARACTERS:
                        if (DISSALOWED_LIST.matcher(reader.getText()).matches()) {
                            throw new InvalidDefinitionException(
                                    String.format("Text contains disallowed symbols: %s", reader.getText()),
                                    XMLParser.getLocation(reader, source));
                        }/*from  www  .j ava2 s.  c o m*/
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        String name = reader.getName().toString().toLowerCase();
                        if (!SVG_TAG_WHITELIST.contains(name)) {
                            throw new InvalidDefinitionException(
                                    String.format("Invalid SVG tag specified: %s", name),
                                    XMLParser.getLocation(reader, source));
                        }
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            QName qAttr = reader.getAttributeName(i);
                            String attr = qAttr.getLocalPart();
                            if (SVG_ATTR_BLACKLIST.contains(attr)) {
                                throw new InvalidDefinitionException(
                                        String.format("Invalid SVG attribute specified: %s", attr),
                                        XMLParser.getLocation(reader, source));
                            }
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                    case XMLStreamConstants.COMMENT:
                    case XMLStreamConstants.DTD:
                    case XMLStreamConstants.SPACE:
                        continue;
                    default:
                        throw new InvalidDefinitionException(String.format("Found unexpected element in xml."),
                                XMLParser.getLocation(reader, source));
                    }
                }
            }
        } catch (XMLStreamException e) {
            throw new SVGParserException(StringEscapeUtils.escapeHtml4(e.getMessage()));
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (XMLStreamException e) {
                    //Well I tried to play nicely
                }
            }
        }
        return ret;
    }
    return null;
}

From source file:org.auraframework.impl.root.MetaDefImpl.java

@Override
public String getMetaValue() {
    return StringEscapeUtils.escapeHtml4(this.value);
}

From source file:org.auraframework.impl.svg.parser.SVGParser.java

@Override
public SVGDef parse(DefDescriptor<SVGDef> descriptor, Source<SVGDef> source)
        throws SVGParserException, QuickFixException {
    if (descriptor.getDefType() == DefType.SVG) {
        XMLStreamReader reader = null;
        String contents = source.getContents();
        //If the file is too big throw before we parse the whole thing.
        SVGDef ret = new SVGDefHandler<>(descriptor, source).createDefinition();
        try (StringReader stringReader = new StringReader(contents)) {
            reader = xmlInputFactory.createXMLStreamReader(stringReader);
            if (reader != null) {
                LOOP: while (reader.hasNext()) {
                    int type = reader.next();
                    switch (type) {
                    case XMLStreamConstants.END_DOCUMENT:
                        break LOOP;
                    //This is plain text inside the file
                    case XMLStreamConstants.CHARACTERS:
                        if (DISSALOWED_LIST.matcher(reader.getText()).matches()) {
                            throw new InvalidDefinitionException(
                                    String.format("Text contains disallowed symbols: %s", reader.getText()),
                                    XMLParser.getLocation(reader, source));
                        }/*w  w  w .j  a va  2s  . c o  m*/
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        String name = reader.getName().toString().toLowerCase();
                        if (!SVG_TAG_WHITELIST.contains(name)) {
                            throw new InvalidDefinitionException(
                                    String.format("Invalid SVG tag specified: %s", name),
                                    XMLParser.getLocation(reader, source));
                        }
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            QName qAttr = reader.getAttributeName(i);
                            String attr = qAttr.getLocalPart();
                            if (SVG_ATTR_BLACKLIST.contains(attr)) {
                                throw new InvalidDefinitionException(
                                        String.format("Invalid SVG attribute specified: %s", attr),
                                        XMLParser.getLocation(reader, source));
                            }
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                    case XMLStreamConstants.COMMENT:
                    case XMLStreamConstants.DTD:
                    case XMLStreamConstants.SPACE:
                        continue;
                    default:
                        throw new InvalidDefinitionException(String.format("Found unexpected element in xml."),
                                XMLParser.getLocation(reader, source));
                    }
                }
            }
        } catch (XMLStreamException e) {
            throw new SVGParserException(StringEscapeUtils.escapeHtml4(e.getMessage()));
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (XMLStreamException e) {
                    //Well I tried to play nicely
                }
            }
        }
        return ret;
    }
    return null;
}

From source file:org.benjp.model.RoomBean.java

public String getEscapedFullname() {
    return StringEscapeUtils.escapeHtml4(this.fullname);
}

From source file:org.bitbucket.mlopatkin.android.logviewer.widgets.UiHelper.java

public static String covertToHtml(String value) {
    String escaped = StringEscapeUtils.escapeHtml4(value);
    String result = escaped.replace("\n", "<br>");
    return result;
}