Example usage for com.liferay.portal.kernel.xml XPath selectNodes

List of usage examples for com.liferay.portal.kernel.xml XPath selectNodes

Introduction

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

Prototype

public List<Node> selectNodes(Object context);

Source Link

Usage

From source file:com.custom.portal.verify.CustomVerifyDynamicDataMapping.java

License:Open Source License

protected void verifyStructure(DDMStructure structure) throws Exception {
    boolean modified = false;

    String defaultLanguageId = structure.getDefaultLanguageId();

    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element");

    Document document = structure.getDocument();

    List<Node> nodes = xPathSelector.selectNodes(document);

    for (Node node : nodes) {
        Element dynamicElementElement = (Element) node;

        if (createDefaultMetadataElement(dynamicElementElement, defaultLanguageId)) {

            modified = true;/*from   ww  w  . j av  a2  s. c om*/
        }
    }

    if (modified) {
        updateStructure(structure, document.asXML());
    }
}

From source file:com.liferay.adaptive.media.journal.web.internal.exportimport.content.processor.AMJournalArticleContentHTMLReplacer.java

License:Open Source License

public String replace(String content, Replace replace) throws Exception {
    try {/*  w  w w  .java  2  s .com*/
        Document document = SAXReaderUtil.read(content);

        XPath xPath = SAXReaderUtil.createXPath("//dynamic-element[@type='text_area']");

        List<Node> ddmJournalArticleNodes = xPath.selectNodes(document);

        for (Node ddmJournalArticleNode : ddmJournalArticleNodes) {
            Element ddmJournalArticleElement = (Element) ddmJournalArticleNode;

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

            for (Element dynamicContentElement : dynamicContentElements) {
                String replacedHtml = replace.apply(dynamicContentElement.getStringValue());

                dynamicContentElement.clearContent();

                dynamicContentElement.addCDATA(replacedHtml);
            }
        }

        return document.asXML();
    } catch (DocumentException de) {
        if (_log.isDebugEnabled()) {
            _log.debug("Invalid content:\n" + content);
        }

        return content;
    }
}

From source file:com.liferay.dynamic.data.mapping.internal.util.DDMXMLImpl.java

License:Open Source License

protected List<Node> getElementsByName(Document document, String name) {
    name = HtmlUtil.escapeXPathAttribute(name);

    XPath xPathSelector = _saxReader.createXPath("//dynamic-element[@name=".concat(name).concat("]"));

    return xPathSelector.selectNodes(document);
}

From source file:com.liferay.dynamic.data.mapping.internal.util.DDMXMLImpl.java

License:Open Source License

protected void validate(Document document) throws Exception {
    XPath xPathSelector = _saxReader.createXPath("//dynamic-element");

    List<Node> nodes = xPathSelector.selectNodes(document);

    Set<String> elementNames = new HashSet<>();

    for (Node node : nodes) {
        Element element = (Element) node;

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

        if (Validator.isNull(name)) {
            throw new StructureDefinitionException(
                    "Element must have a name attribute " + element.formattedString());
        }/*from  ww w.  j  av a 2 s . com*/

        if (name.startsWith(DDMStructureConstants.XSD_NAME_RESERVED)) {
            throw new StructureDefinitionException("Element name " + name + " is reserved");
        }

        if (elementNames.contains(name)) {
            throw new StructureDuplicateElementException("Element with name " + name + " already exists");
        }

        elementNames.add(name);
    }
}

From source file:com.liferay.dynamic.data.mapping.test.util.DDMStructureTestUtil.java

License:Open Source License

public static Map<String, Map<String, String>> getXSDMap(String xsd) throws Exception {

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

    Document document = UnsecureSAXReaderUtil.read(xsd);

    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element");

    List<Node> nodes = xPathSelector.selectNodes(document);

    for (Node node : nodes) {
        Element dynamicElementElement = (Element) node;

        String elementName = getElementName(dynamicElementElement);

        map.put(elementName, getElementMap(dynamicElementElement));
    }/*from   w  w  w. j a  v  a 2 s.co  m*/

    return map;
}

From source file:com.liferay.exportimport.controller.LayoutImportController.java

License:Open Source License

protected List<Element> fetchPortletElements(Element rootElement) {
    List<Element> portletElements = new ArrayList<>();

    // Site portlets

    Element sitePortletsElement = rootElement.element("site-portlets");

    // LAR compatibility

    if (sitePortletsElement == null) {
        sitePortletsElement = rootElement.element("portlets");
    }//from ww w .  j a v  a2  s.  c om

    portletElements.addAll(sitePortletsElement.elements("portlet"));

    // Layout portlets

    XPath xPath = SAXReaderUtil.createXPath("staged-model/portlets/portlet");

    Element layoutsElement = rootElement.element(Layout.class.getSimpleName());

    List<Node> nodes = xPath.selectNodes(layoutsElement);

    Stream<Node> nodesStream = nodes.stream();

    nodesStream.map(node -> (Element) node).forEach(portletElements::add);

    return portletElements;
}

From source file:com.liferay.exportimport.lar.PortletDataContextImpl.java

License:Open Source License

protected List<Element> getReferenceElements(Element parentElement, String className, long groupId, String uuid,
        Serializable classPK, String referenceType) {

    if (parentElement == null) {
        return Collections.emptyList();
    }/*ww  w .  j a v a  2  s  .com*/

    Element referencesElement = parentElement.element("references");

    if (referencesElement == null) {
        return Collections.emptyList();
    }

    StringBundler sb = new StringBundler(13);

    sb.append("reference[@class-name=");
    sb.append(HtmlUtil.escapeXPathAttribute(className));

    if (groupId > 0) {
        sb.append(" and @group-id='");
        sb.append(groupId);
        sb.append(StringPool.APOSTROPHE);
    }

    if (Validator.isNotNull(uuid)) {
        sb.append(" and @uuid=");
        sb.append(HtmlUtil.escapeXPathAttribute(uuid));
    }

    if (Validator.isNotNull(classPK)) {
        sb.append(" and @class-pk='");
        sb.append(classPK);
        sb.append(StringPool.APOSTROPHE);
    }

    if (referenceType != null) {
        sb.append(" and @type=");
        sb.append(HtmlUtil.escapeXPathAttribute(referenceType));
    }

    sb.append(StringPool.CLOSE_BRACKET);

    XPath xPath = SAXReaderUtil.createXPath(sb.toString());

    List<Node> nodes = xPath.selectNodes(referencesElement);

    return ListUtil.fromArray(nodes.toArray(new Element[nodes.size()]));
}

From source file:com.liferay.journal.internal.exportimport.content.processor.ImageImportDDMFormFieldValueTransformer.java

License:Open Source License

@Override
public void transform(DDMFormFieldValue ddmFormFieldValue) throws PortalException {

    Value value = ddmFormFieldValue.getValue();

    for (Locale locale : value.getAvailableLocales()) {
        String valueString = value.getString(locale);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(valueString);

        String alt = jsonObject.getString("alt");
        long oldClassPK = jsonObject.getLong("fileEntryId");
        String type = jsonObject.getString("type");

        FileEntry importedFileEntry = fetchImportedFileEntry(_portletDataContext, oldClassPK);

        if (importedFileEntry == null) {
            continue;
        }/*from ww w . j  av  a2s. co  m*/

        String fileEntryJSON = toJSON(importedFileEntry, type, alt);

        value.addString(locale, fileEntryJSON);

        StringBundler sb = new StringBundler(4);

        sb.append("//dynamic-element[@type='image']");
        sb.append("/dynamic-content[contains(text(),'");
        sb.append(valueString);
        sb.append("')]");

        XPath xPath = SAXReaderUtil.createXPath(sb.toString());

        List<Node> imageNodes = xPath.selectNodes(_document);

        for (Node imageNode : imageNodes) {
            Element imageElement = (Element) imageNode;

            imageElement.clearContent();

            imageElement.addCDATA(fileEntryJSON);
        }
    }
}

From source file:com.liferay.journal.internal.exportimport.content.processor.JournalArticleExportImportContentProcessor.java

License:Open Source License

protected String replaceExportJournalArticleReferences(PortletDataContext portletDataContext,
        StagedModel stagedModel, String content, boolean exportReferencedContent) throws Exception {

    Group group = _groupLocalService.fetchGroup(portletDataContext.getGroupId());

    if (group.isStagingGroup()) {
        group = group.getLiveGroup();//from   w w w.  j  av a  2  s . c  o m
    }

    if (group.isStaged() && !group.isStagedRemotely() && !group.isStagedPortlet(JournalPortletKeys.JOURNAL)) {

        return content;
    }

    Document document = SAXReaderUtil.read(content);

    XPath xPath = SAXReaderUtil.createXPath("//dynamic-element[@type='ddm-journal-article']");

    List<Node> ddmJournalArticleNodes = xPath.selectNodes(document);

    for (Node ddmJournalArticleNode : ddmJournalArticleNodes) {
        Element ddmJournalArticleElement = (Element) ddmJournalArticleNode;

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

        for (Element dynamicContentElement : dynamicContentElements) {
            String jsonData = dynamicContentElement.getStringValue();

            JSONObject jsonObject = _jsonFactory.createJSONObject(jsonData);

            long classPK = GetterUtil.getLong(jsonObject.get("classPK"));

            JournalArticle journalArticle = _journalArticleLocalService.fetchLatestArticle(classPK);

            if (journalArticle == null) {
                if (_log.isInfoEnabled()) {
                    StringBundler messageSB = new StringBundler();

                    messageSB.append("Staged model with class name ");
                    messageSB.append(stagedModel.getModelClassName());
                    messageSB.append(" and primary key ");
                    messageSB.append(stagedModel.getPrimaryKeyObj());
                    messageSB.append(" references missing journal ");
                    messageSB.append("article with class primary key ");
                    messageSB.append(classPK);

                    _log.info(messageSB.toString());
                }

                continue;
            }

            String journalArticleReference = "[$journal-article-reference=" + journalArticle.getPrimaryKey()
                    + "$]";

            if (_log.isDebugEnabled()) {
                _log.debug("Replacing " + jsonData + " with " + journalArticleReference);
            }

            dynamicContentElement.clearContent();

            dynamicContentElement.addCDATA(journalArticleReference);

            if (exportReferencedContent) {
                StagedModelDataHandlerUtil.exportReferenceStagedModel(portletDataContext, stagedModel,
                        journalArticle, PortletDataContext.REFERENCE_TYPE_DEPENDENCY);
            } else {
                Element entityElement = portletDataContext.getExportDataElement(stagedModel);

                portletDataContext.addReferenceElement(stagedModel, entityElement, journalArticle,
                        PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);
            }
        }
    }

    return document.asXML();
}

From source file:com.liferay.journal.internal.exportimport.content.processor.JournalArticleExportImportContentProcessor.java

License:Open Source License

protected void validateJournalArticleReferences(String content) throws PortalException {

    List<Throwable> throwables = new ArrayList<>();

    try {/*  w  ww  .j a va  2 s. c  o m*/
        Document document = SAXReaderUtil.read(content);

        XPath xPath = SAXReaderUtil.createXPath("//dynamic-element[@type='ddm-journal-article']");

        List<Node> ddmJournalArticleNodes = xPath.selectNodes(document);

        for (Node ddmJournalArticleNode : ddmJournalArticleNodes) {
            Element ddmJournalArticleElement = (Element) ddmJournalArticleNode;

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

            for (Element dynamicContentElement : dynamicContentElements) {
                String json = dynamicContentElement.getStringValue();

                if (Validator.isNull(json)) {
                    if (_log.isDebugEnabled()) {
                        _log.debug("No journal article reference is specified");
                    }

                    continue;
                }

                JSONObject jsonObject = _jsonFactory.createJSONObject(json);

                long classPK = GetterUtil.getLong(jsonObject.get("classPK"));

                JournalArticle journalArticle = _journalArticleLocalService.fetchLatestArticle(classPK);

                if (journalArticle == null) {
                    Throwable throwable = new NoSuchArticleException(
                            "No JournalArticle exists with the key " + "{resourcePrimKey=" + classPK + "}");

                    throwables.add(throwable);
                }
            }
        }
    } catch (DocumentException de) {
        if (_log.isDebugEnabled()) {
            _log.debug("Invalid content:\n" + content);
        }
    }

    if (!throwables.isEmpty()) {
        throw new PortalException(
                new BulkException("Unable to validate journal article references", throwables));
    }
}