Example usage for com.liferay.portal.kernel.util StringUtil stripCDATA

List of usage examples for com.liferay.portal.kernel.util StringUtil stripCDATA

Introduction

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

Prototype

public static String stripCDATA(String s) 

Source Link

Document

Returns a string representing the string s with its <![CDATA[]]> wrapper removed.

Usage

From source file:com.liferay.journal.transformer.JournalTransformer.java

License:Open Source License

protected List<TemplateNode> getTemplateNodes(ThemeDisplay themeDisplay, Element element, long ddmStructureId)
        throws Exception {

    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(ddmStructureId);

    DDMForm ddmForm = ddmStructure.getDDMForm();

    Map<String, DDMFormField> ddmFormFieldsMap = ddmForm.getDDMFormFieldsMap(true);

    List<TemplateNode> templateNodes = new ArrayList<>();

    Map<String, TemplateNode> prototypeTemplateNodes = new HashMap<>();

    List<Element> dynamicElementElements = element.elements("dynamic-element");

    for (Element dynamicElementElement : dynamicElementElements) {
        Element dynamicContentElement = dynamicElementElement.element("dynamic-content");

        String data = StringPool.BLANK;

        if (dynamicContentElement != null) {
            data = dynamicContentElement.getText();
        }/*ww  w.j av  a 2 s  .c o m*/

        String name = dynamicElementElement.attributeValue("name", StringPool.BLANK);

        if (name.length() == 0) {
            throw new TransformException("Element missing \"name\" attribute");
        }

        String type = dynamicElementElement.attributeValue("type", StringPool.BLANK);

        Map<String, String> attributes = new HashMap<>();

        if (type.equals("image")) {
            JSONObject dataJSONObject = JSONFactoryUtil.createJSONObject(data);

            Iterator<String> itr = dataJSONObject.keys();

            while (itr.hasNext()) {
                String key = itr.next();

                String value = dataJSONObject.getString(key);

                attributes.put(key, value);
            }
        }

        if (dynamicContentElement != null) {
            for (Attribute attribute : dynamicContentElement.attributes()) {
                attributes.put(attribute.getName(), attribute.getValue());
            }
        }

        TemplateNode templateNode = new TemplateNode(themeDisplay, name, StringUtil.stripCDATA(data), type,
                attributes);

        if (dynamicElementElement.element("dynamic-element") != null) {
            templateNode.appendChildren(getTemplateNodes(themeDisplay, dynamicElementElement, ddmStructureId));
        } else if ((dynamicContentElement != null) && (dynamicContentElement.element("option") != null)) {

            List<Element> optionElements = dynamicContentElement.elements("option");

            for (Element optionElement : optionElements) {
                templateNode.appendOption(StringUtil.stripCDATA(optionElement.getText()));
            }
        }

        DDMFormField ddmFormField = ddmFormFieldsMap.get(name);

        if (ddmFormField != null) {
            DDMFormFieldOptions ddmFormFieldOptions = ddmFormField.getDDMFormFieldOptions();

            Map<String, LocalizedValue> options = ddmFormFieldOptions.getOptions();

            for (Entry<String, LocalizedValue> entry : options.entrySet()) {
                String optionValue = StringUtil.stripCDATA(entry.getKey());

                LocalizedValue localizedLabel = entry.getValue();

                String optionLabel = localizedLabel.getString(themeDisplay.getLocale());

                templateNode.appendOptionMap(optionValue, optionLabel);
            }
        }

        TemplateNode prototypeTemplateNode = prototypeTemplateNodes.get(name);

        if (prototypeTemplateNode == null) {
            prototypeTemplateNode = templateNode;

            prototypeTemplateNodes.put(name, prototypeTemplateNode);

            templateNodes.add(templateNode);
        }

        prototypeTemplateNode.appendSibling(templateNode);
    }

    return templateNodes;
}