Example usage for com.liferay.portal.kernel.xml SAXReaderUtil createXPath

List of usage examples for com.liferay.portal.kernel.xml SAXReaderUtil createXPath

Introduction

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

Prototype

public static XPath createXPath(String xPathExpression) 

Source Link

Usage

From source file:com.liferay.portlet.journal.action.RSSAction.java

License:Open Source License

protected String processContent(JournalFeed feed, JournalArticle article, String languageId,
        ThemeDisplay themeDisplay, SyndEntry syndEntry, SyndContent syndContent) throws Exception {

    String content = article.getDescription(languageId);

    String contentField = feed.getContentField();

    if (contentField.equals(JournalFeedConstants.RENDERED_WEB_CONTENT)) {
        String rendererTemplateId = article.getTemplateId();

        if (Validator.isNotNull(feed.getRendererTemplateId())) {
            rendererTemplateId = feed.getRendererTemplateId();
        }//from  w w w  .  j  av  a2s. c  o  m

        JournalArticleDisplay articleDisplay = JournalContentUtil.getDisplay(feed.getGroupId(),
                article.getArticleId(), rendererTemplateId, null, languageId, themeDisplay, 1, _XML_REQUUEST);

        if (articleDisplay != null) {
            content = articleDisplay.getContent();
        }
    } else if (!contentField.equals(JournalFeedConstants.WEB_CONTENT_DESCRIPTION)) {

        Document document = SAXReaderUtil.read(article.getContentByLocale(languageId));

        XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element[@name='" + contentField + "']");

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

        if (results.size() == 0) {
            return content;
        }

        Element element = (Element) results.get(0);

        String elType = element.attributeValue("type");

        if (elType.equals("document_library")) {
            String url = element.elementText("dynamic-content");

            url = processURL(feed, url, themeDisplay, syndEntry);
        } else if (elType.equals("image") || elType.equals("image_gallery")) {
            String url = element.elementText("dynamic-content");

            url = processURL(feed, url, themeDisplay, syndEntry);

            content = content + "<br /><br /><img alt='' src='" + themeDisplay.getURLPortal() + url + "' />";
        } else if (elType.equals("text_box")) {
            syndContent.setType("text");

            content = element.elementText("dynamic-content");
        } else {
            content = element.elementText("dynamic-content");
        }
    }

    return content;
}

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  w w w.  j a va 2s . co  m*/

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

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

License:Open Source License

protected void copyArticleImages(JournalArticle oldArticle, JournalArticle newArticle) throws Exception {

    Document contentDoc = SAXReaderUtil.read(oldArticle.getContent());

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

    List<Node> imageNodes = xPathSelector.selectNodes(contentDoc);

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

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

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

        for (Element dynamicContentEl : dynamicContentEls) {
            long imageId = GetterUtil.getLong(dynamicContentEl.attributeValue("id"));
            String languageId = dynamicContentEl.attributeValue("language-id");

            Image oldImage = null;

            try {
                oldImage = imageLocalService.getImage(imageId);
            } catch (NoSuchImageException nsie) {
                continue;
            }//www .ja v a2s . com

            imageId = journalArticleImageLocalService.getArticleImageId(newArticle.getGroupId(),
                    newArticle.getArticleId(), newArticle.getVersion(), instanceId, name, languageId);

            imageLocalService.updateImage(imageId, oldImage.getTextObj());

            String elContent = "/image/journal/article?img_id=" + imageId + "&t="
                    + WebServerServletTokenUtil.getToken(imageId);

            dynamicContentEl.setText(elContent);
            dynamicContentEl.addAttribute("id", String.valueOf(imageId));
        }
    }

    newArticle.setContent(contentDoc.formattedString());
}

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

License:Open Source License

protected boolean isValidStructureField(long groupId, String structureId, String contentField) {

    if (contentField.equals(JournalFeedConstants.WEB_CONTENT_DESCRIPTION)
            || contentField.equals(JournalFeedConstants.RENDERED_WEB_CONTENT)) {

        return true;
    } else {//from   w  ww  .  j  a v  a2s  .  c  o m
        try {
            JournalStructure structure = journalStructurePersistence.findByG_S(groupId, structureId);

            Document document = SAXReaderUtil.read(structure.getXsd());

            XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element[@name='" + contentField + "']");

            Node node = xPathSelector.selectSingleNode(document);

            if (node != null) {
                return true;
            }
        } catch (Exception e) {
            _log.error(e, e);
        }
    }

    return false;
}

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

License:Open Source License

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

    StringBundler sb = new StringBundler();

    sb.append("entry[@");
    sb.append(attributeName);//from  ww w  .  ja v  a 2 s  .  c o m
    sb.append("='");
    sb.append(attributeValue);
    sb.append("']");

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

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

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

License:Open Source License

private static Element _getElementByInstanceId(Document document, String instanceId) {

    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element[@instance-id='" + instanceId + "']");

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

    if (nodes.size() == 1) {
        return (Element) nodes.get(0);
    } else {/*from   www .  ja va 2s.  c  om*/
        return null;
    }
}

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

License:Open Source License

private static void _removeOldContent(Stack<String> path, Element contentElement, Document xsdDocument,
        String elementPath) throws SystemException {

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

    if (Validator.isNull(name)) {
        return;/*ww  w  . j  a  v  a  2 s. com*/
    }

    String localPath = "dynamic-element[@name='" + name + "']";

    String fullPath = elementPath + "/" + localPath;

    XPath xPathSelector = SAXReaderUtil.createXPath(fullPath);

    List<Node> curNodes = xPathSelector.selectNodes(xsdDocument);

    if (curNodes.size() == 0) {
        contentElement.detach();
    }

    path.push(localPath);

    _removeOldContent(path, contentElement, xsdDocument);

    path.pop();
}

From source file:com.liferay.web.extender.internal.webbundle.WebBundleProcessor.java

License:Open Source License

protected void processPortletXML(String webContextpath) throws IOException {
    File portletXMLFile = new File(_deployedAppFolder, "WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_STANDARD);

    if (!portletXMLFile.exists()) {
        return;/*  ww  w.  ja va  2  s . c  o m*/
    }

    String content = FileUtil.read(portletXMLFile);

    Document document = null;

    try {
        document = SAXReaderUtil.read(content);
    } catch (DocumentException de) {
        throw new IOException(de);
    }

    Element rootElement = document.getRootElement();

    List<Element> portletElements = rootElement.elements("portlet");

    for (Element portletElement : portletElements) {
        String portletName = portletElement.elementText("portlet-name");

        String invokerPortletName = "osgi".concat(webContextpath).concat(StringPool.SLASH).concat(portletName);

        XPath xPath = SAXReaderUtil.createXPath(_INVOKER_PORTLET_NAME_XPATH);

        Element invokerPortletNameEl = (Element) xPath.selectSingleNode(portletElement);

        if (invokerPortletNameEl == null) {
            Element portletClassElement = portletElement.element("portlet-class");

            List<Node> children = portletElement.content();

            int pos = children.indexOf(portletClassElement);

            QName qName = rootElement.getQName();

            Element initParamElement = SAXReaderUtil
                    .createElement(SAXReaderUtil.createQName("init-param", qName.getNamespace()));

            initParamElement.addElement("name").setText("com.liferay.portal.invokerPortletName");
            initParamElement.addElement("value").setText(invokerPortletName);

            children.add(pos + 1, initParamElement);
        } else {
            Element valueElement = invokerPortletNameEl.element("value");

            invokerPortletName = valueElement.getTextTrim();

            if (!invokerPortletName.startsWith(StringPool.SLASH)) {
                invokerPortletName = StringPool.SLASH.concat(invokerPortletName);
            }

            invokerPortletName = "osgi".concat(webContextpath).concat(invokerPortletName);

            valueElement.setText(invokerPortletName);
        }
    }

    content = DDMXMLUtil.formatXML(document);

    FileUtil.write(portletXMLFile, content);
}

From source file:it.smc.calendar.sync.caldav.util.CalDAVUtil.java

License:Open Source License

public static Element getReportDateFilter() throws InvalidRequestException {
    try {/* ww w  . j a  v a2s.co  m*/
        Document document = CalDAVRequestThreadLocal.getRequestDocument();

        String xPathExpression = "//*[local-name()='time-range']";

        XPath xPathSelector = SAXReaderUtil.createXPath(xPathExpression);

        Node node = xPathSelector.selectSingleNode(document);

        if (node == null) {
            return null;
        }

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

        return timeRangeElement;
    } catch (Exception e) {
        throw new InvalidRequestException(e);
    }
}