Example usage for com.liferay.portal.kernel.util HtmlUtil escapeJS

List of usage examples for com.liferay.portal.kernel.util HtmlUtil escapeJS

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util HtmlUtil escapeJS.

Prototype

public static String escapeJS(String js) 

Source Link

Document

Escapes the JavaScript value so that it is safe to use in a JavaScript context.

Usage

From source file:com.fmdp.webform.util.WebFormUtil.java

License:Open Source License

public static boolean validate(String currentFieldValue, Map<String, String> fieldsMap, String validationScript)
        throws Exception {

    boolean validationResult = false;

    Context context = Context.enter();

    StringBundler sb = new StringBundler();

    sb.append("currentFieldValue = String('");
    sb.append(HtmlUtil.escapeJS(currentFieldValue));
    sb.append("');\n");

    sb.append("var fieldsMap = {};\n");

    for (String key : fieldsMap.keySet()) {
        sb.append("fieldsMap['");
        sb.append(key);/*w  w w .  j  a  v  a 2 s  .  co m*/
        sb.append("'] = '");

        String value = StringUtil.replace(fieldsMap.get(key), new String[] { "\r\n", "\r", "\n" },
                new String[] { "\\n", "\\n", "\\n" });

        sb.append(HtmlUtil.escapeJS(value));
        sb.append("';\n");
    }

    sb.append("function validation(currentFieldValue, fieldsMap) {\n");
    sb.append(validationScript);
    sb.append("}\n");
    sb.append("internalValidationResult = ");
    sb.append("validation(currentFieldValue, fieldsMap);");

    String script = sb.toString();

    try {
        Scriptable scope = context.initStandardObjects();

        Object jsFieldsMap = Context.javaToJS(fieldsMap, scope);

        ScriptableObject.putProperty(scope, "jsFieldsMap", jsFieldsMap);

        context.evaluateString(scope, script, "Validation Script", 1, null);

        Object obj = ScriptableObject.getProperty(scope, "internalValidationResult");

        if (obj instanceof Boolean) {
            validationResult = (Boolean) obj;
        } else {
            throw new Exception("The script must return a boolean value");
        }
    } catch (Exception e) {
        String msg = "The following script has execution errors:\n" + validationScript + "\n" + e.getMessage();

        _log.error(msg);

        throw new Exception(msg, e);
    } finally {
        Context.exit();
    }

    return validationResult;
}

From source file:com.liferay.content.targeting.portlet.SimulatorPortlet.java

License:Open Source License

protected void populateViewContext(String path, PortletRequest portletRequest, PortletResponse portletResponse,
        Template template, TemplateHashModel staticModels) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long[] originalUserSegmentIds = (long[]) portletRequest.getAttribute(WebKeys.ORIGINAL_USER_SEGMENT_IDS);

    long[] groupIds = ContentTargetingUtil.getAncestorsAndCurrentGroupIds(themeDisplay.getSiteGroupId());

    HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(portletRequest);

    HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(portletResponse);

    long[] simulatedUserSegmentIds = _userSegmentSimulator.getUserSegmentIds(httpServletRequest,
            httpServletResponse);/*w  ww  . j a v  a  2 s  .  co  m*/

    if (simulatedUserSegmentIds == null) {
        simulatedUserSegmentIds = originalUserSegmentIds;
    }

    template.put("simulatedUserSegmentIds", simulatedUserSegmentIds);

    List<Campaign> campaigns = _campaignLocalService.getCampaigns(groupIds, simulatedUserSegmentIds);

    template.put("campaigns", campaigns);

    List<Campaign> availableCampaigns = _campaignLocalService.getCampaigns(groupIds);

    List<Campaign> notMatchedCampaigns = new ArrayList<Campaign>();

    for (Campaign campaign : availableCampaigns) {
        if (!campaigns.contains(campaign)) {
            notMatchedCampaigns.add(campaign);
        }
    }

    template.put("notMatchedCampaigns", notMatchedCampaigns);

    boolean showCampaignsSearch = false;

    if ((notMatchedCampaigns.size() + campaigns.size()) > _SHOW_SEARCH_LIMIT) {

        showCampaignsSearch = true;
    }

    template.put("showCampaignsSearch", showCampaignsSearch);

    template.put("originalUserSegmentIds", originalUserSegmentIds);

    boolean isSimulatedUserSegments = GetterUtil
            .getBoolean(portletRequest.getAttribute(WebKeys.IS_SIMULATED_USER_SEGMENTS));

    template.put("isSimulatedUserSegments", isSimulatedUserSegments);

    List<UserSegment> userSegments = new ArrayList<UserSegment>();

    if (originalUserSegmentIds != null) {
        for (long userSegmentId : originalUserSegmentIds) {
            userSegments.add(_userSegmentLocalService.getUserSegment(userSegmentId));
        }
    }

    template.put("userSegments", userSegments);

    List<UserSegment> availableUserSegments = _userSegmentLocalService.getUserSegments(groupIds);

    List<UserSegment> notMatchedUserSegments = new ArrayList<UserSegment>();

    for (UserSegment userSegment : availableUserSegments) {
        if (!userSegments.contains(userSegment)) {
            notMatchedUserSegments.add(userSegment);
        }
    }

    template.put("notMatchedUserSegments", notMatchedUserSegments);

    boolean showUserSegmentSearch = false;

    if ((notMatchedUserSegments.size() + userSegments.size()) > _SHOW_SEARCH_LIMIT) {

        showUserSegmentSearch = true;
    }

    template.put("showUserSegmentSearch", showUserSegmentSearch);

    String refreshURL = PortalUtil.getLayoutURL(themeDisplay.getLayout(), themeDisplay);

    template.put("refreshURL", HtmlUtil.escapeJS(refreshURL));
}

From source file:com.liferay.dynamic.data.lists.web.internal.template.DDLDisplayTemplateHelper.java

License:Open Source License

public static String getHTMLContent(DDMFormFieldValue recordFieldValue, Locale locale) {

    Value value = recordFieldValue.getValue();

    String valueString = value.getString(locale);

    if (Validator.isNull(valueString)) {
        return StringPool.BLANK;
    }/*ww w.  ja  v  a 2s. c  o  m*/

    return HtmlUtil.escapeJS(valueString);
}

From source file:com.liferay.dynamic.data.mapping.internal.render.TextHTMLDDMFormFieldValueRenderer.java

License:Open Source License

@Override
protected ValueAccessor getValueAcessor(Locale locale) {
    return new ValueAccessor(locale) {

        @Override/*from w w w  .  j ava  2  s.c om*/
        public String get(DDMFormFieldValue ddmFormFieldValue) {
            Value value = ddmFormFieldValue.getValue();

            return StringUtil.replace(_HTML, new String[] { "[$DDM_FORM_FIELD_VALUE$]", "[$PREVIEW$]" },
                    new String[] { HtmlUtil.escapeJS(value.getString(locale)),
                            LanguageUtil.get(locale, "preview") });
        }

    };
}

From source file:com.liferay.exportimport.resources.importer.internal.portlet.configuration.icon.ViewJournalSourcePortletConfigurationIcon.java

License:Open Source License

protected String getDialogJS(JournalArticle article) {
    StringBundler sb = new StringBundler(6);

    sb.append("{bodyContent: '<pre>");
    sb.append(HtmlUtil.escapeJS(HtmlUtil.escape(article.getContent())));
    sb.append("</pre>', modal: true, toolbars: {footer: ");
    sb.append("[{label:Liferay.Language.get('close'), on: {click: ");
    sb.append("function(event) {event.domEvent.preventDefault(); ");
    sb.append("sourceModal.hide();}}}]}}");

    return sb.toString();
}

From source file:com.liferay.exportimport.resources.importer.internal.portlet.configuration.icon.ViewJournalSourcePortletConfigurationIcon.java

License:Open Source License

protected String getWindowJS(PortletRequest portletRequest, JournalArticle article) {

    StringBundler sb = new StringBundler(14);

    sb.append("Liferay.Util.Window.getWindow({bodyCssClass: ");
    sb.append("'dialog-with-footer', destroyOnHide: true, dialog: ");
    sb.append(getDialogJS(article));/*from   w  ww .  j  a  v a2 s. c  o  m*/
    sb.append(", id: '");

    ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);

    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

    sb.append(HtmlUtil.escape(portletDisplay.getNamespace()));

    sb.append("viewSource', namespace: '");
    sb.append(portletDisplay.getNamespace());
    sb.append("', portlet: '#p_p_id_");
    sb.append(portletDisplay.getId());
    sb.append("_', portletId: '");
    sb.append(portletDisplay.getId());
    sb.append("', title: '");
    sb.append(HtmlUtil.escapeJS(HtmlUtil.escape(article.getTitle(themeDisplay.getLocale()))));
    sb.append("'});");

    return sb.toString();
}

From source file:com.liferay.exportimport.web.internal.portlet.configuration.icon.ExportImportPortletConfigurationIcon.java

License:Open Source License

@Override
public String getOnClick(PortletRequest portletRequest, PortletResponse portletResponse) {

    StringBundler sb = new StringBundler(12);

    sb.append("Liferay.Portlet.openWindow({bodyCssClass: ");
    sb.append("'dialog-with-footer', namespace: '");

    ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);

    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

    sb.append(portletDisplay.getNamespace());

    sb.append("', portlet: '#p_p_id_");
    sb.append(portletDisplay.getId());//from   w w w.  ja v a2  s.  co m
    sb.append("_', portletId: '");
    sb.append(portletDisplay.getId());
    sb.append("', title: '");
    sb.append(LanguageUtil.get(themeDisplay.getLocale(), "export-import"));
    sb.append("', uri: '");
    sb.append(HtmlUtil.escapeJS(portletDisplay.getURLExportImport()));
    sb.append("'}); return false;");

    return sb.toString();
}

From source file:com.liferay.frontend.editor.alloyeditor.web.editor.configuration.BaseAlloyEditorConfigContributor.java

License:Open Source License

@Override
public void populateConfigJSONObject(JSONObject jsonObject, Map<String, Object> inputEditorTaglibAttributes,
        ThemeDisplay themeDisplay, RequestBackedPortletURLFactory requestBackedPortletURLFactory) {

    jsonObject.put("allowedContent", Boolean.TRUE);

    String contentsLanguageDir = getContentsLanguageDir(inputEditorTaglibAttributes);

    jsonObject.put("contentsLangDirection", HtmlUtil.escapeJS(contentsLanguageDir));

    String contentsLanguageId = getContentsLanguageId(inputEditorTaglibAttributes);

    jsonObject.put("contentsLanguage", contentsLanguageId.replace("iw_", "he_"));

    jsonObject.put("extraPlugins", "ae_autolink,ae_dragresize,ae_addimages,ae_placeholder,"
            + "ae_selectionregion,ae_tableresize,ae_tabletools,ae_uicore");

    String languageId = getLanguageId(themeDisplay);

    jsonObject.put("language", languageId.replace("iw_", "he_"));

    jsonObject.put("removePlugins",
            "contextmenu,elementspath,image,link,liststyle,resize,tabletools," + "toolbar");

    String namespace = GetterUtil
            .getString(inputEditorTaglibAttributes.get("liferay-ui:input-editor:namespace"));

    String name = namespace//from w  w  w .  j  av  a  2  s .c  o  m
            + GetterUtil.getString(inputEditorTaglibAttributes.get("liferay-ui:input-editor:name"));

    jsonObject.put("srcNode", name);
}

From source file:com.liferay.frontend.editor.alloyeditor.web.internal.editor.configuration.BaseAlloyEditorConfigContributor.java

License:Open Source License

@Override
public void populateConfigJSONObject(JSONObject jsonObject, Map<String, Object> inputEditorTaglibAttributes,
        ThemeDisplay themeDisplay, RequestBackedPortletURLFactory requestBackedPortletURLFactory) {

    jsonObject.put("allowedContent", Boolean.TRUE);

    String contentsLanguageDir = getContentsLanguageDir(inputEditorTaglibAttributes);

    jsonObject.put("contentsLangDirection", HtmlUtil.escapeJS(contentsLanguageDir));

    String contentsLanguageId = getContentsLanguageId(inputEditorTaglibAttributes);

    jsonObject.put("contentsLanguage", contentsLanguageId.replace("iw_", "he_"));

    jsonObject.put("disableNativeSpellChecker", Boolean.FALSE);

    jsonObject.put("extraPlugins", "ae_autolink,ae_dragresize,ae_addimages,ae_imagealignment,"
            + "ae_placeholder,ae_selectionregion,ae_tableresize," + "ae_tabletools,ae_uicore");

    jsonObject.put("imageScaleResize", "scale");

    String languageId = getLanguageId(themeDisplay);

    jsonObject.put("language", languageId.replace("iw_", "he_"));

    jsonObject.put("removePlugins",
            "contextmenu,elementspath,image,link,liststyle,magicline,resize," + "tabletools,toolbar");

    String namespace = GetterUtil
            .getString(inputEditorTaglibAttributes.get("liferay-ui:input-editor:namespace"));

    String name = namespace//from   ww  w. j  av a  2 s  . c  o m
            + GetterUtil.getString(inputEditorTaglibAttributes.get("liferay-ui:input-editor:name"));

    jsonObject.put("srcNode", name);
}

From source file:com.liferay.frontend.editor.ckeditor.web.editor.configuration.BaseCKEditorConfigContributor.java

License:Open Source License

@Override
public void populateConfigJSONObject(JSONObject jsonObject, Map<String, Object> inputEditorTaglibAttributes,
        ThemeDisplay themeDisplay, RequestBackedPortletURLFactory requestBackedPortletURLFactory) {

    jsonObject.put("allowedContent", Boolean.TRUE);

    String cssClasses = GetterUtil
            .getString(inputEditorTaglibAttributes.get("liferay-ui:input-editor:cssClasses"));

    jsonObject.put("bodyClass", "html-editor " + HtmlUtil.escape(cssClasses));

    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    String cssPath = HtmlUtil.escape(themeDisplay.getPathThemeCss());

    jsonArray.put(cssPath + "/aui.css");
    jsonArray.put(cssPath + "/main.css");

    jsonObject.put("contentsCss", jsonArray);

    String contentsLanguageDir = getContentsLanguageDir(inputEditorTaglibAttributes);

    jsonObject.put("contentsLangDirection", HtmlUtil.escapeJS(contentsLanguageDir));

    String contentsLanguageId = getContentsLanguageId(inputEditorTaglibAttributes);

    contentsLanguageId = contentsLanguageId.replace("iw", "he");
    contentsLanguageId = contentsLanguageId.replace("_", "-");

    jsonObject.put("contentsLanguage", contentsLanguageId);

    jsonObject.put("height", 265);

    String languageId = getLanguageId(themeDisplay);

    languageId = languageId.replace("iw", "he");
    languageId = languageId.replace("_", "-");

    jsonObject.put("language", languageId);

    boolean resizable = GetterUtil
            .getBoolean((String) inputEditorTaglibAttributes.get("liferay-ui:input-editor:resizable"));

    if (resizable) {
        jsonObject.put("resize_dir", "vertical");
    }/*from   w ww  .ja v a 2s .c  o  m*/

    jsonObject.put("resize_enabled", resizable);
}