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

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

Introduction

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

Prototype

public void clearContent();

Source Link

Usage

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();// ww w  . j  a v a 2s.co 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.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 {// w  w w . j a v  a 2  s  .  co  m
            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();//from ww  w.  j av a 2 s. co  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, 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.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.j a  v  a  2s  .c  o m*/
            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());
            }
        }
    }
}