Example usage for com.liferay.portal.kernel.xml Element addCDATA

List of usage examples for com.liferay.portal.kernel.xml Element addCDATA

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.xml Element addCDATA.

Prototype

public Element addCDATA(String cdata);

Source Link

Usage

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

protected void format(User user, long groupId, JournalArticle article, Element root) throws PortalException {

    for (Element element : root.elements()) {
        String elType = element.attributeValue("type", StringPool.BLANK);

        if (elType.equals("document_library")) {
            addDocumentLibraryFileEntries(element);
        } else if (elType.equals("image")) {
            addImageFileEntries(article, element);
        } else if (elType.equals("text_area") || elType.equals("text") || elType.equals("text_box")) {

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

            for (Element dynamicContentElement : dynamicContentElements) {
                String dynamicContent = dynamicContentElement.getText();

                if (Validator.isNotNull(dynamicContent)) {
                    String contentType = ContentTypes.TEXT_PLAIN;

                    if (elType.equals("text_area")) {
                        contentType = ContentTypes.TEXT_HTML;
                    }/*from  w  ww.  j a  va 2s  .  com*/

                    dynamicContent = SanitizerUtil.sanitize(user.getCompanyId(), groupId, user.getUserId(),
                            JournalArticle.class.getName(), 0, contentType, dynamicContent);

                    dynamicContentElement.clearContent();

                    dynamicContentElement.addCDATA(dynamicContent);
                }
            }
        }

        format(user, groupId, article, element);
    }
}

From source file:com.liferay.journal.test.util.JournalTestUtil.java

License:Open Source License

public static Element addMetadataElement(Element element, String locale, String label) {

    Element metadataElement = element.addElement("meta-data");

    metadataElement.addAttribute("locale", locale);

    Element entryElement = metadataElement.addElement("entry");

    entryElement.addAttribute("name", "label");

    entryElement.addCDATA(label);

    return entryElement;
}

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

License:Open Source License

protected void replace(Element root, long articleGroupId) throws Exception {
    for (Element element : root.elements()) {
        List<Element> dynamicContentElements = element.elements("dynamic-content");

        for (Element dynamicContentElement : dynamicContentElements) {
            String text = dynamicContentElement.getText();

            text = HtmlUtil.stripComments(text);
            text = HtmlUtil.stripHtml(text);
            text = text.trim();//from  www  . j ava  2s .  c  o m

            // [@articleId;elementName@]

            if (Validator.isNotNull(text) && (text.length() >= 7) && text.startsWith("[@")
                    && text.endsWith("@]")) {

                text = text.substring(2, text.length() - 2);

                int pos = text.indexOf(";");

                if (pos != -1) {
                    String articleId = text.substring(0, pos);
                    String elementName = text.substring(pos + 1);

                    JournalArticle article = JournalArticleLocalServiceUtil.getArticle(articleGroupId,
                            articleId);

                    dynamicContentElement.clearContent();
                    dynamicContentElement.addCDATA(getDynamicContent(article.getDocument(), elementName));
                }
            } else if ((text != null) && text.startsWith("/image/journal/article?img_id")) {

                // Make sure to point images to the full path

                dynamicContentElement.setText("@cdn_host@@root_path@" + text);
            }
        }

        replace(element, articleGroupId);
    }
}

From source file:com.liferay.journal.util.impl.JournalConverterImpl.java

License:Open Source License

protected void updateDynamicContentValue(Element dynamicContentElement, String fieldType, boolean multiple,
        String fieldValue) throws Exception {

    if (DDMImpl.TYPE_CHECKBOX.equals(fieldType)) {
        if (fieldValue.equals(Boolean.FALSE.toString())) {
            fieldValue = StringPool.BLANK;
        }//from  w  w w  .  j a  v  a2 s.c om

        dynamicContentElement.addCDATA(fieldValue);
    } else if (DDMImpl.TYPE_DDM_LINK_TO_PAGE.equals(fieldType) && Validator.isNotNull(fieldValue)) {

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(fieldValue);

        long groupId = jsonObject.getLong("groupId");

        long layoutId = jsonObject.getLong("layoutId");

        boolean privateLayout = jsonObject.getBoolean("privateLayout");

        StringBundler sb = new StringBundler((groupId > 0) ? 5 : 3);

        sb.append(layoutId);
        sb.append(StringPool.AT);

        if (privateLayout) {
            Group group = _groupLocalService.fetchGroup(groupId);

            if (group == null) {
                sb.append("private");
            } else if (group.isUser()) {
                sb.append("private-user");
            } else {
                sb.append("private-group");
            }
        } else {
            sb.append("public");
        }

        if (groupId > 0) {
            sb.append(StringPool.AT);
            sb.append(groupId);
        }

        dynamicContentElement.addCDATA(sb.toString());
    } else if (DDMImpl.TYPE_SELECT.equals(fieldType) && Validator.isNotNull(fieldValue)) {

        JSONArray jsonArray = null;

        try {
            jsonArray = JSONFactoryUtil.createJSONArray(fieldValue);
        } catch (JSONException jsone) {
            return;
        }

        if (multiple) {
            for (int i = 0; i < jsonArray.length(); i++) {
                Element optionElement = dynamicContentElement.addElement("option");

                optionElement.addCDATA(jsonArray.getString(i));
            }
        } else {
            dynamicContentElement.addCDATA(jsonArray.getString(0));
        }
    } else {
        dynamicContentElement.addCDATA(fieldValue);
    }
}

From source file:com.liferay.journal.util.impl.JournalUtil.java

License:Open Source License

private static void _addElementOptions(Element curContentElement, Element newContentElement) {

    List<Element> newElementOptions = newContentElement.elements("option");

    for (Element newElementOption : newElementOptions) {
        Element curElementOption = SAXReaderUtil.createElement("option");

        curElementOption.addCDATA(newElementOption.getText());

        curContentElement.add(curElementOption);
    }//from   w w w  .  ja va2  s .co m
}

From source file:com.liferay.journal.util.impl.JournalUtil.java

License:Open Source License

private static void _mergeArticleContentUpdate(Element curElement, Element newElement, String defaultLocale) {

    Attribute curTypeAttribute = curElement.attribute("type");
    Attribute newTypeAttribute = newElement.attribute("type");

    curTypeAttribute.setValue(newTypeAttribute.getValue());

    Attribute curIndexTypeAttribute = curElement.attribute("index-type");
    Attribute newIndexTypeAttribute = newElement.attribute("index-type");

    if (newIndexTypeAttribute != null) {
        if (curIndexTypeAttribute == null) {
            curElement.addAttribute("index-type", newIndexTypeAttribute.getValue());
        } else {/*from w  w w . jav a  2s .com*/
            curIndexTypeAttribute.setValue(newIndexTypeAttribute.getValue());
        }
    }

    List<Element> elements = newElement.elements("dynamic-content");

    if ((elements == null) || elements.isEmpty()) {
        return;
    }

    Element newContentElement = elements.get(0);

    String newLanguageId = newContentElement.attributeValue("language-id");
    String newValue = newContentElement.getText();

    String indexType = newElement.attributeValue("index-type");

    if (Validator.isNotNull(indexType)) {
        curElement.addAttribute("index-type", indexType);
    }

    List<Element> curContentElements = curElement.elements("dynamic-content");

    if (Validator.isNull(newLanguageId)) {
        for (Element curContentElement : curContentElements) {
            curContentElement.detach();
        }

        Element curContentElement = SAXReaderUtil.createElement("dynamic-content");

        if (newContentElement.element("option") != null) {
            _addElementOptions(curContentElement, newContentElement);
        } else {
            curContentElement.addCDATA(newValue);
        }

        curElement.add(curContentElement);
    } else {
        boolean alreadyExists = false;

        for (Element curContentElement : curContentElements) {
            String curLanguageId = curContentElement.attributeValue("language-id");

            if (newLanguageId.equals(curLanguageId)) {
                alreadyExists = true;

                curContentElement.clearContent();

                if (newContentElement.element("option") != null) {
                    _addElementOptions(curContentElement, newContentElement);
                } else {
                    curContentElement.addCDATA(newValue);
                }

                break;
            }
        }

        if (!alreadyExists) {
            Element curContentElement = curContentElements.get(0);

            String curLanguageId = curContentElement.attributeValue("language-id");

            if (Validator.isNull(curLanguageId)) {
                if (newLanguageId.equals(defaultLocale)) {
                    curContentElement.clearContent();

                    if (newContentElement.element("option") != null) {
                        _addElementOptions(curContentElement, newContentElement);
                    } else {
                        curContentElement.addCDATA(newValue);
                    }
                } else {
                    curElement.add(newContentElement.createCopy());
                }

                curContentElement.addAttribute("language-id", defaultLocale);
            } else {
                curElement.add(newContentElement.createCopy());
            }
        }
    }
}

From source file:com.liferay.portlet.dynamicdatamapping.storage.XMLStorageAdapter.java

License:Open Source License

private void _updateField(Element dynamicElementElement, String fieldName, String value) {

    Element dynamicContentElement = dynamicElementElement.element("dynamic-content");

    dynamicElementElement.addAttribute("name", fieldName);

    dynamicContentElement.clearContent();

    dynamicContentElement.addCDATA(value);
}

From source file:com.liferay.portlet.journal.util.ContentTransformerListener.java

License:Open Source License

protected void replace(Element root) throws Exception {
    Map<String, String> tokens = getTokens();

    long groupId = GetterUtil.getLong(tokens.get("group_id"));

    for (Element el : root.elements()) {
        Element dynamicContent = el.element("dynamic-content");

        if (dynamicContent != null) {
            String text = dynamicContent.getText();

            text = HtmlUtil.stripComments(text);
            text = HtmlUtil.stripHtml(text);
            text = text.trim();/*w w w  .j ava2s . c  om*/

            // [@articleId;elementName@]

            if (Validator.isNotNull(text) && text.length() >= 7 && text.startsWith("[@")
                    && text.endsWith("@]")) {

                text = text.substring(2, text.length() - 2);

                int pos = text.indexOf(";");

                if (pos != -1) {
                    String articleId = text.substring(0, pos);
                    String elementName = text.substring(pos + 1, text.length());

                    JournalArticle article = JournalArticleLocalServiceUtil.getArticle(groupId, articleId);

                    dynamicContent.clearContent();
                    dynamicContent.addCDATA(getDynamicContent(article.getContent(), elementName));
                }
            }

            // Make sure to point images to the full path

            else if ((text != null) && (text.startsWith("/image/journal/article?img_id"))) {

                dynamicContent.setText("@cdn_host@@root_path@" + text);
            }
        }

        replace(el);
    }
}

From source file:com.liferay.portlet.journal.util.JournalConverterImpl.java

License:Open Source License

protected void updateDynamicContentValue(Element dynamicContentElement, String fieldType, String fieldValue)
        throws Exception {

    if (DDMImpl.TYPE_CHECKBOX.equals(fieldType)) {
        if (fieldValue.equals(Boolean.FALSE.toString())) {
            fieldValue = StringPool.BLANK;
        }//from w  w w.  ja va  2s.  com

        dynamicContentElement.addCDATA(fieldValue);
    } else if (DDMImpl.TYPE_DDM_DOCUMENTLIBRARY.equals(fieldType) && Validator.isNotNull(fieldValue)) {
        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(fieldValue);
        FileEntry fileEntry = null;
        String uuid = jsonObject.getString("uuid");
        long groupId = jsonObject.getLong("groupId");
        _log.info("In updateDynamicContentValue uuid is " + uuid + " and the groupId is " + groupId);
        fileEntry = DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(uuid, groupId);
        fieldValue = DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK, false,
                true);
        _log.info(" fieldValue is " + fieldValue);
        dynamicContentElement.addCDATA(fieldValue);
    } else if (DDMImpl.TYPE_DDM_LINK_TO_PAGE.equals(fieldType) && Validator.isNotNull(fieldValue)) {

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(fieldValue);

        long groupId = jsonObject.getLong("groupId");

        String layoutId = jsonObject.getString("layoutId");

        boolean privateLayout = jsonObject.getBoolean("privateLayout");

        StringBundler sb = new StringBundler((groupId > 0) ? 5 : 3);

        sb.append(layoutId);
        sb.append(StringPool.AT);

        if (privateLayout) {
            sb.append("private");
        } else {
            sb.append("public");
        }

        if (groupId > 0) {
            sb.append(StringPool.AT);
            sb.append(groupId);
        }

        dynamicContentElement.addCDATA(sb.toString());
    } else if (DDMImpl.TYPE_SELECT.equals(fieldType) && Validator.isNotNull(fieldValue)) {

        JSONArray jsonArray = JSONFactoryUtil.createJSONArray(fieldValue);

        if (jsonArray.length() > 1) {
            for (int i = 0; i < jsonArray.length(); i++) {
                Element optionElement = dynamicContentElement.addElement("option");

                optionElement.addCDATA(jsonArray.getString(i));
            }
        } else {
            dynamicContentElement.addCDATA(jsonArray.getString(0));
        }
    } else {
        dynamicContentElement.addCDATA(fieldValue);
    }
}

From source file:com.liferay.portlet.journal.util.JournalUtil.java

License:Open Source License

private static void _mergeArticleContentUpdate(Element curElement, Element newElement, String defaultLocale) {

    Attribute curTypeAttribute = curElement.attribute("type");
    Attribute newTypeAttribute = newElement.attribute("type");

    curTypeAttribute.setValue(newTypeAttribute.getValue());

    Attribute curIndexTypeAttribute = curElement.attribute("index-type");
    Attribute newIndexTypeAttribute = newElement.attribute("index-type");

    if (newIndexTypeAttribute != null) {
        if (curIndexTypeAttribute == null) {
            curElement.addAttribute("index-type", newIndexTypeAttribute.getValue());
        } else {//ww  w  . j  a va2 s.c  om
            curIndexTypeAttribute.setValue(newIndexTypeAttribute.getValue());
        }
    }

    Element newContentElement = newElement.elements("dynamic-content").get(0);

    String newLanguageId = newContentElement.attributeValue("language-id");
    String newValue = newContentElement.getText();

    String indexType = newElement.attributeValue("index-type");

    if (Validator.isNotNull(indexType)) {
        curElement.addAttribute("index-type", indexType);
    }

    List<Element> curContentElements = curElement.elements("dynamic-content");

    if (Validator.isNull(newLanguageId)) {
        for (Element curContentElement : curContentElements) {
            curContentElement.detach();
        }

        Element curContentElement = SAXReaderUtil.createElement("dynamic-content");

        if (newContentElement.element("option") != null) {
            _addElementOptions(curContentElement, newContentElement);
        } else {
            curContentElement.addCDATA(newValue);
        }

        curElement.add(curContentElement);
    } else {
        boolean alreadyExists = false;

        for (Element curContentElement : curContentElements) {
            String curLanguageId = curContentElement.attributeValue("language-id");

            if (newLanguageId.equals(curLanguageId)) {
                alreadyExists = true;

                curContentElement.clearContent();

                if (newContentElement.element("option") != null) {
                    _addElementOptions(curContentElement, newContentElement);
                } else {
                    curContentElement.addCDATA(newValue);
                }

                break;
            }
        }

        if (!alreadyExists) {
            Element curContentElement = curContentElements.get(0);

            String curLanguageId = curContentElement.attributeValue("language-id");

            if (Validator.isNull(curLanguageId)) {
                if (newLanguageId.equals(defaultLocale)) {
                    curContentElement.clearContent();

                    if (newContentElement.element("option") != null) {
                        _addElementOptions(curContentElement, newContentElement);
                    } else {
                        curContentElement.addCDATA(newValue);
                    }
                } else {
                    curElement.add(newContentElement.createCopy());
                }

                curContentElement.addAttribute("language-id", defaultLocale);
            } else {
                curElement.add(newContentElement.createCopy());
            }
        }
    }
}