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

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

Introduction

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

Prototype

public Node selectSingleNode(Object context);

Source Link

Usage

From source file:com.liferay.dynamic.data.mapping.io.internal.DDMFormXSDDeserializerImpl.java

License:Open Source License

protected Element fetchMetadataEntry(Element parentElement, String entryName) {

    XPath xPathSelector = _saxReader
            .createXPath("entry[@name=" + HtmlUtil.escapeXPathAttribute(entryName) + StringPool.CLOSE_BRACKET);

    return (Element) xPathSelector.selectSingleNode(parentElement);
}

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

License:Open Source License

@Override
public Element getMissingReferenceElement(ClassedModel classedModel) {
    StringBundler sb = new StringBundler(5);

    sb.append("missing-reference[@class-name='");
    sb.append(ExportImportClassedModelUtil.getClassName(classedModel));
    sb.append("' and @class-pk='");
    sb.append(String.valueOf(classedModel.getPrimaryKeyObj()));
    sb.append("']");

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

    Node node = xPath.selectSingleNode(_missingReferencesElement);

    return (Element) node;
}

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

License:Open Source License

protected Element getDataElement(Element parentElement, String attribute, String value) {

    if (parentElement == null) {
        return null;
    }//from   www.j  av a2  s  .  c  o m

    StringBundler sb = new StringBundler(5);

    sb.append("staged-model[@");
    sb.append(attribute);
    sb.append(StringPool.EQUAL);
    sb.append(HtmlUtil.escapeXPathAttribute(value));
    sb.append(StringPool.CLOSE_BRACKET);

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

    return (Element) xPath.selectSingleNode(parentElement);
}

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

License:Open Source License

protected List<Element> getReferenceDataElements(List<Element> referenceElements, Class<?> clazz) {

    List<Element> referenceDataElements = new ArrayList<>();

    for (Element referenceElement : referenceElements) {
        Element referenceDataElement = null;

        String path = referenceElement.attributeValue("path");

        if (Validator.isNotNull(path)) {
            referenceDataElement = getImportDataElement(clazz.getSimpleName(), "path", path);
        } else {//w  w  w.  j  a va  2 s.  c  o m
            String groupId = referenceElement.attributeValue("group-id");
            String uuid = referenceElement.attributeValue("uuid");

            StringBuilder sb = new StringBuilder(5);

            sb.append("staged-model[@uuid=");
            sb.append(HtmlUtil.escapeXPathAttribute(uuid));

            if (groupId != null) {
                sb.append(" and @group-id=");
                sb.append(HtmlUtil.escapeXPathAttribute(groupId));
            }

            sb.append(StringPool.CLOSE_BRACKET);

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

            Element groupElement = getImportDataGroupElement(clazz.getSimpleName());

            referenceDataElement = (Element) xPath.selectSingleNode(groupElement);
        }

        if (referenceDataElement == null) {
            continue;
        }

        referenceDataElements.add(referenceDataElement);
    }

    return referenceDataElements;
}

From source file:com.liferay.exportimport.test.util.lar.BaseWorkflowedStagedModelDataHandlerTestCase.java

License:Open Source License

protected Element getExportStagedModelElement(PortletDataContext portletDataContext, StagedModel stagedModel) {

    Element rootElement = portletDataContext.getExportDataRootElement();

    Class<?> modelClass = stagedModel.getModelClass();

    Element modelElement = rootElement.element(modelClass.getSimpleName());

    Assert.assertNotNull("Unable to find model element", modelElement);

    XPath xPath = SAXReaderUtil
            .createXPath("staged-model[@path ='" + ExportImportPathUtil.getModelPath(stagedModel) + "']");

    return (Element) xPath.selectSingleNode(modelElement);
}

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

License:Open Source License

protected Element fetchMetadataEntry(Element parentElement, String attributeName, String attributeValue) {

    StringBundler sb = new StringBundler(5);

    sb.append("entry[@");
    sb.append(attributeName);//from  ww w. j  a v  a  2 s .  c o m
    sb.append(StringPool.EQUAL);
    sb.append(HtmlUtil.escapeXPathAttribute(attributeValue));
    sb.append(StringPool.CLOSE_BRACKET);

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

    return (Element) xPathSelector.selectSingleNode(parentElement);
}

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

License:Open Source License

protected JournalArticleDisplay getArticleDisplay(JournalArticle article, String ddmTemplateKey,
        String viewMode, String languageId, int page, PortletRequestModel portletRequestModel,
        ThemeDisplay themeDisplay, boolean propagateException) throws PortalException {

    String content = null;/*from  w ww  .j  a  va 2 s. c  o  m*/

    if (page < 1) {
        page = 1;
    }

    int numberOfPages = 1;
    boolean paginate = false;
    boolean pageFlow = false;

    boolean cacheable = true;

    Map<String, String> tokens = JournalUtil.getTokens(article.getGroupId(), portletRequestModel, themeDisplay);

    if ((themeDisplay == null) && (portletRequestModel == null)) {
        tokens.put("company_id", String.valueOf(article.getCompanyId()));

        Group companyGroup = groupLocalService.getCompanyGroup(article.getCompanyId());

        tokens.put("article_group_id", String.valueOf(article.getGroupId()));
        tokens.put("company_group_id", String.valueOf(companyGroup.getGroupId()));

        // Deprecated tokens

        tokens.put("group_id", String.valueOf(article.getGroupId()));
    }

    tokens.put(TemplateConstants.CLASS_NAME_ID,
            String.valueOf(classNameLocalService.getClassNameId(DDMStructure.class)));
    tokens.put("article_resource_pk", String.valueOf(article.getResourcePrimKey()));

    DDMStructure ddmStructure = article.getDDMStructure();

    tokens.put("ddm_structure_key", String.valueOf(ddmStructure.getStructureKey()));
    tokens.put("ddm_structure_id", String.valueOf(ddmStructure.getStructureId()));

    // Deprecated token

    tokens.put("structure_id", article.getDDMStructureKey());

    String defaultDDMTemplateKey = article.getDDMTemplateKey();

    if (Validator.isNull(ddmTemplateKey)) {
        ddmTemplateKey = defaultDDMTemplateKey;
    }

    Document document = article.getDocument();

    document = document.clone();

    Element rootElement = document.getRootElement();

    List<Element> pages = rootElement.elements("page");

    if (!pages.isEmpty()) {
        pageFlow = true;

        String targetPage = null;

        Map<String, String[]> parameters = portletRequestModel.getParameters();

        if (parameters != null) {
            String[] values = parameters.get("targetPage");

            if ((values != null) && (values.length > 0)) {
                targetPage = values[0];
            }
        }

        Element pageElement = null;

        if (Validator.isNotNull(targetPage)) {
            targetPage = HtmlUtil.escapeXPathAttribute(targetPage);

            XPath xPathSelector = SAXReaderUtil.createXPath("/root/page[@id = " + targetPage + "]");

            pageElement = (Element) xPathSelector.selectSingleNode(document);
        }

        if (pageElement != null) {
            document = SAXReaderUtil.createDocument(pageElement);

            rootElement = document.getRootElement();

            numberOfPages = pages.size();
        } else {
            if (page > pages.size()) {
                page = 1;
            }

            pageElement = pages.get(page - 1);

            document = SAXReaderUtil.createDocument(pageElement);

            rootElement = document.getRootElement();

            numberOfPages = pages.size();
            paginate = true;
        }
    }

    JournalUtil.addAllReservedEls(rootElement, tokens, article, languageId, themeDisplay);

    try {
        if (_log.isDebugEnabled()) {
            _log.debug(
                    "Transforming " + article.getArticleId() + " " + article.getVersion() + " " + languageId);
        }

        // Try with specified template first (in the current group and the
        // global group). If a template is not specified, use the default
        // one. If the specified template does not exist, use the default
        // one. If the default one does not exist, throw an exception.

        DDMTemplate ddmTemplate = null;

        try {
            ddmTemplate = ddmTemplateLocalService.getTemplate(PortalUtil.getSiteGroupId(article.getGroupId()),
                    classNameLocalService.getClassNameId(DDMStructure.class), ddmTemplateKey, true);

            Group companyGroup = groupLocalService.getCompanyGroup(article.getCompanyId());

            if (companyGroup.getGroupId() == ddmTemplate.getGroupId()) {
                tokens.put("company_group_id", String.valueOf(companyGroup.getGroupId()));
            }
        } catch (NoSuchTemplateException nste) {
            if (!defaultDDMTemplateKey.equals(ddmTemplateKey)) {
                ddmTemplate = ddmTemplateLocalService.getTemplate(
                        PortalUtil.getSiteGroupId(article.getGroupId()),
                        classNameLocalService.getClassNameId(DDMStructure.class), defaultDDMTemplateKey);
            } else {
                throw nste;
            }
        }

        tokens.put("ddm_template_key", String.valueOf(ddmTemplate.getTemplateKey()));
        tokens.put("ddm_template_id", String.valueOf(ddmTemplate.getTemplateId()));

        // Deprecated token

        tokens.put("template_id", ddmTemplateKey);

        String script = ddmTemplate.getScript();
        String langType = ddmTemplate.getLanguage();
        cacheable = ddmTemplate.isCacheable();

        content = JournalUtil.transform(themeDisplay, tokens, viewMode, languageId, document,
                portletRequestModel, script, langType, propagateException);

        if (!pageFlow) {
            JournalServiceConfiguration journalServiceConfiguration = configurationProvider
                    .getCompanyConfiguration(JournalServiceConfiguration.class, article.getCompanyId());

            String[] pieces = StringUtil.split(content,
                    journalServiceConfiguration.journalArticlePageBreakToken());

            if (pieces.length > 1) {
                if (page > pieces.length) {
                    page = 1;
                }

                content = pieces[page - 1];
                numberOfPages = pieces.length;
                paginate = true;
            }
        }
    } catch (Exception e) {
        throw new SystemException(e);
    }

    return new JournalArticleDisplayImpl(article.getCompanyId(), article.getId(), article.getResourcePrimKey(),
            article.getGroupId(), article.getUserId(), article.getArticleId(), article.getVersion(),
            article.getTitle(languageId), article.getUrlTitle(), article.getDescription(languageId),
            article.getAvailableLanguageIds(), content, article.getDDMStructureKey(), ddmTemplateKey,
            article.isSmallImage(), article.getSmallImageId(), article.getSmallImageURL(), numberOfPages, page,
            paginate, cacheable);
}

From source file:com.liferay.portlet.dynamicdatamapping.model.impl.DDMStructureImpl.java

License:Open Source License

public Map<String, String> getFields(String fieldName, String attributeName, String attributeValue,
        String locale) {//from ww w.  j  av  a 2s  .c o  m

    try {
        StringBundler sb = new StringBundler(7);

        sb.append("//dynamic-element[@name=\"");
        sb.append(fieldName);
        sb.append("\"] //dynamic-element[@");
        sb.append(attributeName);
        sb.append("=\"");
        sb.append(attributeValue);
        sb.append("\"]");

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

        Node node = xPathSelector.selectSingleNode(getDocument());

        if (node != null) {
            return _getField((Element) node.asXPathResult(node.getParent()), locale);
        }
    } catch (Exception e) {
        _log.error(e, e);
    }

    return null;
}

From source file:com.liferay.portlet.dynamicdatamapping.model.impl.DDMStructureImpl.java

License:Open Source License

private Map<String, String> _getField(Element element, String locale) {
    Map<String, String> field = new HashMap<String, String>();

    List<String> availableLocales = getAvailableLocales();

    if ((locale != null) && !(availableLocales.contains(locale))) {
        locale = getDefaultLocale();//from www  . j  a v  a2  s . co m
    }

    String xPathExpression = "meta-data[@locale=\"".concat(locale).concat("\"]");

    XPath xPathSelector = SAXReaderUtil.createXPath(xPathExpression);

    Node node = xPathSelector.selectSingleNode(element);

    Element metaDataElement = (Element) node.asXPathResult(node.getParent());

    if (metaDataElement != null) {
        List<Element> childMetaDataElements = metaDataElement.elements();

        for (Element childMetaDataElement : childMetaDataElements) {
            String name = childMetaDataElement.attributeValue("name");
            String value = childMetaDataElement.getText();

            field.put(name, value);
        }
    }

    for (Attribute attribute : element.attributes()) {
        field.put(attribute.getName(), attribute.getValue());
    }

    return field;
}

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

License:Open Source License

public JournalArticleDisplay getArticleDisplay(JournalArticle article, String templateId, String viewMode,
        String languageId, int page, String xmlRequest, ThemeDisplay themeDisplay)
        throws PortalException, SystemException {

    String content = null;//from   ww w.  j av a 2s  .  c  om

    if (page < 1) {
        page = 1;
    }

    int numberOfPages = 1;
    boolean paginate = false;
    boolean pageFlow = false;

    boolean cacheable = true;

    if (Validator.isNull(xmlRequest)) {
        xmlRequest = "<request />";
    }

    Map<String, String> tokens = JournalUtil.getTokens(article.getGroupId(), themeDisplay, xmlRequest);

    tokens.put("article_resource_pk", String.valueOf(article.getResourcePrimKey()));

    String defaultTemplateId = article.getTemplateId();

    if (article.isTemplateDriven()) {
        if (Validator.isNull(templateId)) {
            templateId = defaultTemplateId;
        }

        tokens.put("structure_id", article.getStructureId());
        tokens.put("template_id", templateId);
    }

    String xml = article.getContent();

    try {
        Document document = null;

        Element rootElement = null;

        if (article.isTemplateDriven()) {
            document = SAXReaderUtil.read(xml);

            rootElement = document.getRootElement();

            Document requestDocument = SAXReaderUtil.read(xmlRequest);

            List<Element> pages = rootElement.elements("page");

            if (!pages.isEmpty()) {
                pageFlow = true;

                String targetPage = requestDocument
                        .valueOf("/request/parameters/parameter[name='targetPage']/" + "value");

                Element pageElement = null;

                if (Validator.isNotNull(targetPage)) {
                    XPath xPathSelector = SAXReaderUtil.createXPath("/root/page[@id = '" + targetPage + "']");

                    pageElement = (Element) xPathSelector.selectSingleNode(document);
                }

                if (pageElement != null) {
                    document = SAXReaderUtil.createDocument(pageElement);

                    rootElement = document.getRootElement();

                    numberOfPages = pages.size();
                } else {
                    if (page > pages.size()) {
                        page = 1;
                    }

                    pageElement = pages.get(page - 1);

                    document = SAXReaderUtil.createDocument(pageElement);

                    rootElement = document.getRootElement();

                    numberOfPages = pages.size();
                    paginate = true;
                }
            }

            rootElement.add(requestDocument.getRootElement().createCopy());

            JournalUtil.addAllReservedEls(rootElement, tokens, article, languageId);

            xml = DDMXMLUtil.formatXML(document);
        }
    } catch (DocumentException de) {
        throw new SystemException(de);
    } catch (IOException ioe) {
        throw new SystemException(ioe);
    }

    try {
        if (_log.isDebugEnabled()) {
            _log.debug(
                    "Transforming " + article.getArticleId() + " " + article.getVersion() + " " + languageId);
        }

        String script = null;
        String langType = null;

        if (article.isTemplateDriven()) {

            // Try with specified template first (in the current group and
            // the global group). If a template is not specified, use the
            // default one. If the specified template does not exit, use the
            // default one. If the default one does not exist, throw an
            // exception.

            JournalTemplate template = null;

            try {
                template = journalTemplatePersistence.findByG_T(article.getGroupId(), templateId);
            } catch (NoSuchTemplateException nste1) {
                try {
                    Group companyGroup = groupLocalService.getCompanyGroup(article.getCompanyId());

                    template = journalTemplatePersistence.findByG_T(companyGroup.getGroupId(), templateId);

                    tokens.put("company_group_id", String.valueOf(companyGroup.getGroupId()));
                } catch (NoSuchTemplateException nste2) {
                    if (!defaultTemplateId.equals(templateId)) {
                        template = journalTemplatePersistence.findByG_T(article.getGroupId(),
                                defaultTemplateId);
                    } else {
                        throw nste1;
                    }
                }
            }

            script = template.getXsl();
            langType = template.getLangType();
            cacheable = template.isCacheable();
        }

        content = JournalUtil.transform(themeDisplay, tokens, viewMode, languageId, xml, script, langType);

        if (!pageFlow) {
            String[] pieces = StringUtil.split(content, PropsValues.JOURNAL_ARTICLE_TOKEN_PAGE_BREAK);

            if (pieces.length > 1) {
                if (page > pieces.length) {
                    page = 1;
                }

                content = pieces[page - 1];
                numberOfPages = pieces.length;
                paginate = true;
            }
        }
    } catch (Exception e) {
        throw new SystemException(e);
    }

    return new JournalArticleDisplayImpl(article.getCompanyId(), article.getId(), article.getResourcePrimKey(),
            article.getGroupId(), article.getUserId(), article.getArticleId(), article.getVersion(),
            article.getTitle(languageId), article.getUrlTitle(), article.getDescription(languageId),
            article.getAvailableLocales(), content, article.getType(), article.getStructureId(), templateId,
            article.isSmallImage(), article.getSmallImageId(), article.getSmallImageURL(), numberOfPages, page,
            paginate, cacheable);
}