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

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

Introduction

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

Prototype

public Element createCopy();

Source Link

Usage

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

License:Open Source License

private static void _mergeArticleContentUpdate(Document curDocument, Element newParentElement,
        Element newElement, int pos, String defaultLocale) throws Exception {

    _mergeArticleContentUpdate(curDocument, newElement, defaultLocale);

    String instanceId = newElement.attributeValue("instance-id");

    Element curElement = _getElementByInstanceId(curDocument, instanceId);

    if (curElement != null) {
        _mergeArticleContentUpdate(curElement, newElement, defaultLocale);
    } else {//from  w ww .  j a  v  a  2  s. c  o  m
        String parentInstanceId = newParentElement.attributeValue("instance-id");

        if (Validator.isNull(parentInstanceId)) {
            Element curRoot = curDocument.getRootElement();

            List<Element> curRootElements = curRoot.elements();

            curRootElements.add(pos, newElement.createCopy());
        } else {
            Element curParentElement = _getElementByInstanceId(curDocument, parentInstanceId);

            if (curParentElement != null) {
                List<Element> curParentElements = curParentElement.elements();

                curParentElements.add(pos, newElement.createCopy());
            }
        }
    }
}

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  . j a va2s . 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.knowledgebase.admin.util.KBStructureContentUtil.java

License:Open Source License

protected static Element updateLocalizations(Element rootElement, Element defaultKBStructureContentElement,
        Map<String, Element> elements) {

    for (Map.Entry<String, Element> entry : elements.entrySet()) {
        Element oldKBStructureContentElement = entry.getValue();

        Element newKBStructureContentElement = oldKBStructureContentElement.createCopy();

        for (Element newKBStructureFieldElement : newKBStructureContentElement.elements()) {

            newKBStructureFieldElement.detach();
        }//from w w w  .j  a v  a  2  s  .c o  m

        for (Element defaultKBStructureFieldElement : defaultKBStructureContentElement.elements()) {

            String defaultKBStructureFieldId = defaultKBStructureFieldElement
                    .attributeValue("kb-structure-field-id");

            Element oldKBStructureFieldElement = (Element) oldKBStructureContentElement.selectSingleNode(
                    "kb-structure-field[@kb-structure-field-id='" + defaultKBStructureFieldId + "']");

            if (oldKBStructureFieldElement == null) {
                newKBStructureContentElement.add(defaultKBStructureFieldElement.createCopy());

                continue;
            }

            Element newKBStructureFieldElement = oldKBStructureFieldElement.createCopy();

            for (Element newKBStructureOptionElement : newKBStructureFieldElement.elements()) {

                newKBStructureOptionElement.detach();
            }

            for (Element defaultKBStructureOptionElement : defaultKBStructureFieldElement.elements()) {

                String defaultKBStructureOptionId = defaultKBStructureOptionElement
                        .attributeValue("kb-structure-option-id");

                Element oldKBStructureOptionElement = (Element) oldKBStructureFieldElement.selectSingleNode(
                        "kb-structure-option[@kb-structure-option-id='" + defaultKBStructureOptionId + "']");

                if (oldKBStructureOptionElement == null) {
                    newKBStructureFieldElement.add(defaultKBStructureOptionElement.createCopy());
                } else {
                    newKBStructureFieldElement.add(oldKBStructureOptionElement.createCopy());
                }
            }

            newKBStructureContentElement.add(newKBStructureFieldElement);
        }

        rootElement.add(newKBStructureContentElement);
    }

    return rootElement;
}

From source file:com.liferay.portlet.dynamicdatamapping.service.impl.DDMStructureLocalServiceImpl.java

License:Open Source License

protected void appendNewStructureRequiredFields(DDMStructure structure, Document templateDocument) {

    String xsd = structure.getXsd();

    Document structureDocument = null;

    try {//from   ww w  . j a  v  a  2  s  .  c om
        structureDocument = SAXReaderUtil.read(xsd);
    } catch (DocumentException de) {
        if (_log.isWarnEnabled()) {
            _log.warn(de, de);
        }

        return;
    }

    Element templateElement = templateDocument.getRootElement();

    XPath structureXPath = SAXReaderUtil
            .createXPath("//dynamic-element[.//meta-data/entry[@name=\"required\"]=" + "\"true\"]");

    List<Node> nodes = structureXPath.selectNodes(structureDocument);

    Iterator<Node> itr = nodes.iterator();

    while (itr.hasNext()) {
        Element element = (Element) itr.next();

        String name = element.attributeValue("name");

        XPath templateXPath = SAXReaderUtil.createXPath("//dynamic-element[@name=\"" + name + "\"]");

        if (!templateXPath.booleanValueOf(templateDocument)) {
            templateElement.add(element.createCopy());
        }
    }
}

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  ava2 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());
            }
        }
    }
}