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

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

Introduction

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

Prototype

public static Document read(URL url) throws DocumentException 

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   www. j  ava  2  s . 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.lar.JournalContentPortletDataHandlerImpl.java

License:Open Source License

@Override
protected PortletPreferences doImportData(PortletDataContext portletDataContext, String portletId,
        PortletPreferences portletPreferences, String data) throws Exception {

    portletDataContext.importPermissions("com.liferay.portlet.journal", portletDataContext.getSourceGroupId(),
            portletDataContext.getScopeGroupId());

    if (Validator.isNull(data)) {
        return null;
    }/*  w  w  w.  j  av a 2s  . co  m*/

    long previousScopeGroupId = portletDataContext.getScopeGroupId();

    long importGroupId = GetterUtil.getLong(portletPreferences.getValue("groupId", null));

    if (importGroupId == portletDataContext.getSourceGroupId()) {
        portletDataContext.setScopeGroupId(portletDataContext.getGroupId());
    }

    Document document = SAXReaderUtil.read(data);

    Element rootElement = document.getRootElement();

    JournalPortletDataHandlerImpl.importReferencedData(portletDataContext, rootElement);

    Element structureElement = rootElement.element("structure");

    if (structureElement != null) {
        JournalPortletDataHandlerImpl.importStructure(portletDataContext, structureElement);
    }

    Element templateElement = rootElement.element("template");

    if (templateElement != null) {
        JournalPortletDataHandlerImpl.importTemplate(portletDataContext, templateElement);
    }

    Element articleElement = rootElement.element("article");

    if (articleElement != null) {
        JournalPortletDataHandlerImpl.importArticle(portletDataContext, articleElement);
    }

    String articleId = portletPreferences.getValue("articleId", null);

    if (Validator.isNotNull(articleId) && (articleElement != null)) {
        String importedArticleGroupId = articleElement.attributeValue("imported-article-group-id");

        if (Validator.isNull(importedArticleGroupId)) {
            importedArticleGroupId = String.valueOf(portletDataContext.getScopeGroupId());
        }

        portletPreferences.setValue("groupId", importedArticleGroupId);

        Map<String, String> articleIds = (Map<String, String>) portletDataContext
                .getNewPrimaryKeysMap(JournalArticle.class + ".articleId");

        articleId = MapUtil.getString(articleIds, articleId, articleId);

        portletPreferences.setValue("articleId", articleId);

        Layout layout = LayoutLocalServiceUtil.getLayout(portletDataContext.getPlid());

        JournalContentSearchLocalServiceUtil.updateContentSearch(portletDataContext.getScopeGroupId(),
                layout.isPrivateLayout(), layout.getLayoutId(), portletId, articleId, true);
    } else {
        portletPreferences.setValue("groupId", StringPool.BLANK);
        portletPreferences.setValue("articleId", StringPool.BLANK);
    }

    String templateId = portletPreferences.getValue("templateId", null);

    if (Validator.isNotNull(templateId)) {
        Map<String, String> templateIds = (Map<String, String>) portletDataContext
                .getNewPrimaryKeysMap(JournalTemplate.class + ".templateId");

        templateId = MapUtil.getString(templateIds, templateId, templateId);

        portletPreferences.setValue("templateId", templateId);
    } else {
        portletPreferences.setValue("templateId", StringPool.BLANK);
    }

    portletDataContext.setScopeGroupId(previousScopeGroupId);

    return portletPreferences;
}

From source file:com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl.java

License:Open Source License

@Override
protected PortletPreferences doImportData(PortletDataContext portletDataContext, String portletId,
        PortletPreferences portletPreferences, String data) throws Exception {

    portletDataContext.importPermissions("com.liferay.portlet.journal", portletDataContext.getSourceGroupId(),
            portletDataContext.getScopeGroupId());

    Document document = SAXReaderUtil.read(data);

    Element rootElement = document.getRootElement();

    importReferencedData(portletDataContext, rootElement);

    Element structuresElement = rootElement.element("structures");

    List<Element> structureElements = structuresElement.elements("structure");

    //Modification start
    long processedItems = 0, itemsToprocess = 0;

    itemsToprocess += structureElements.size();

    Element templatesElement = rootElement.element("templates");
    List<Element> templateElements = templatesElement.elements("template");
    itemsToprocess += templateElements.size();

    Element feedsElement = rootElement.element("feeds");
    List<Element> feedElements = feedsElement.elements("feed");
    itemsToprocess += feedElements.size();

    List<Element> articleElements = new ArrayList<Element>();
    if (portletDataContext.getBooleanParameter(_NAMESPACE, "web-content")) {
        Element articlesElement = rootElement.element("articles");

        articleElements = articlesElement.elements("article");
        itemsToprocess += articleElements.size();
    }//  w  w w.ja  v a 2s  . com
    StagingProgressUpdaterThreadLocal.getMonitor().getComponent(PublishProcessProgressMonitor.COMPONENT_JOURNAL)
            .setItemsCount(itemsToprocess);

    for (Element structureElement : structureElements) {
        StagingProgressUpdaterThreadLocal.getMonitor()
                .getComponent(PublishProcessProgressMonitor.COMPONENT_JOURNAL)
                .setItemsProcessed(processedItems++);

        importStructure(portletDataContext, structureElement);
    }

    for (Element templateElement : templateElements) {
        StagingProgressUpdaterThreadLocal.getMonitor()
                .getComponent(PublishProcessProgressMonitor.COMPONENT_JOURNAL)
                .setItemsProcessed(processedItems++);

        importTemplate(portletDataContext, templateElement);
    }

    for (Element feedElement : feedElements) {
        StagingProgressUpdaterThreadLocal.getMonitor()
                .getComponent(PublishProcessProgressMonitor.COMPONENT_JOURNAL)
                .setItemsProcessed(processedItems++);

        importFeed(portletDataContext, feedElement);
    }

    for (Element articleElement : articleElements) {
        StagingProgressUpdaterThreadLocal.getMonitor()
                .getComponent(PublishProcessProgressMonitor.COMPONENT_JOURNAL)
                .setItemsProcessed(processedItems++);

        try {
            importArticle(portletDataContext, articleElement);
        } catch (ArticleContentException ace) {
            if (_log.isWarnEnabled()) {
                String path = articleElement.attributeValue("path");

                _log.warn("Skipping article with path " + path + " because of invalid content");
            }
        }
    }
    //Modification end

    return portletPreferences;
}

From source file:com.liferay.portlet.journal.model.impl.JournalStructureImpl.java

License:Open Source License

public String getMergedXsd() {
    String parentStructureId = getParentStructureId();

    String xsd = getXsd();//from  w  w  w.  j  av  a  2  s  .c om

    if (Validator.isNull(parentStructureId)) {
        return xsd;
    }

    try {
        JournalStructure parentStructure = null;

        try {
            parentStructure = JournalStructureLocalServiceUtil.getStructure(getGroupId(), parentStructureId);
        } catch (NoSuchStructureException nsse) {
            Group group = GroupLocalServiceUtil.getGroup(getGroupId());

            Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(group.getCompanyId());

            if (getGroupId() == companyGroup.getGroupId()) {
                throw new NoSuchStructureException();
            }

            parentStructure = JournalStructureLocalServiceUtil.getStructure(companyGroup.getGroupId(),
                    parentStructureId);
        }

        Document doc = SAXReaderUtil.read(getXsd());

        Element root = doc.getRootElement();

        Document parentDoc = SAXReaderUtil.read(parentStructure.getMergedXsd());

        Element parentRoot = parentDoc.getRootElement();

        addParentStructureId(parentRoot, parentStructureId);

        root.content().addAll(0, parentRoot.content());

        xsd = root.asXML();
    } catch (Exception e) {
    }

    return xsd;
}

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. jav  a  2s .  c o 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 checkStructure(JournalArticle article)
        throws DocumentException, PortalException, SystemException {

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

    JournalStructure structure = null;/*from w ww .  ja v  a  2s .co  m*/

    try {
        structure = journalStructurePersistence.findByG_S(article.getGroupId(), article.getStructureId());
    } catch (NoSuchStructureException nsse) {
        structure = journalStructurePersistence.findByG_S(companyGroup.getGroupId(), article.getStructureId());
    }

    String content = GetterUtil.getString(article.getContent());

    Document contentDoc = SAXReaderUtil.read(content);
    Document xsdDoc = SAXReaderUtil.read(structure.getXsd());

    try {
        checkStructure(contentDoc, xsdDoc.getRootElement());
    } catch (StructureXsdException sxsde) {
        long groupId = article.getGroupId();
        String articleId = article.getArticleId();
        double version = article.getVersion();

        if (_log.isWarnEnabled()) {
            _log.warn("Article {groupId=" + groupId + ", articleId=" + articleId + ", version=" + version
                    + "} has content that does not match its " + "structure: " + sxsde.getMessage());
        }
    }
}

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;
            }//from  w  ww .  j  a v a2 s  .c o m

            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.JournalArticleLocalServiceImpl.java

License:Open Source License

protected String format(User user, long groupId, String articleId, double version, boolean incrementVersion,
        String content, String structureId, Map<String, byte[]> images)
        throws PortalException, SystemException {

    Document document = null;/*  ww w.jav a2 s  .com*/

    try {
        document = SAXReaderUtil.read(content);

        Element rootElement = document.getRootElement();

        if (Validator.isNotNull(structureId)) {
            format(user, groupId, articleId, version, incrementVersion, rootElement, images);
        } else {
            List<Element> staticContentElements = rootElement.elements("static-content");

            for (Element staticContentElement : staticContentElements) {
                String staticContent = staticContentElement.getText();

                staticContent = SanitizerUtil.sanitize(user.getCompanyId(), groupId, user.getUserId(),
                        JournalArticle.class.getName(), 0, ContentTypes.TEXT_HTML, staticContent);

                staticContentElement.setText(staticContent);
            }
        }

        content = DDMXMLUtil.formatXML(document);
    } catch (DocumentException de) {
        _log.error(de);
    } catch (IOException ioe) {
        _log.error(ioe);
    }

    content = HtmlUtil.replaceMsWordCharacters(content);

    return content;
}

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

License:Open Source License

protected void validateContent(String content) throws PortalException {
    if (Validator.isNull(content)) {
        throw new ArticleContentException();
    }/*w ww.  j  a v a2 s.c  o  m*/

    try {
        SAXReaderUtil.read(content);
    } catch (DocumentException de) {
        throw new ArticleContentException();
    }
}

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 w w  .  java2  s .  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;
}