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

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

Introduction

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

Prototype

public List<Element> elements(String name);

Source Link

Usage

From source file:com.liferay.portlet.polls.lar.PollsDisplayPortletDataHandlerImpl.java

License:Open Source License

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

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

    if (Validator.isNull(data)) {
        return null;
    }/*from   www .j  a  v a  2  s. c o m*/

    Document document = SAXReaderUtil.read(data);

    Element rootElement = document.getRootElement();

    Element questionsElement = rootElement.element("questions");

    for (Element questionElement : questionsElement.elements("question")) {
        String path = questionElement.attributeValue("path");

        if (!portletDataContext.isPathNotProcessed(path)) {
            continue;
        }

        PollsQuestion question = (PollsQuestion) portletDataContext.getZipEntryAsObject(path);

        PollsPortletDataHandlerImpl.importQuestion(portletDataContext, questionElement, question);
    }

    Element choicesElement = rootElement.element("choices");

    for (Element choiceElement : choicesElement.elements("choice")) {
        String path = choiceElement.attributeValue("path");

        if (!portletDataContext.isPathNotProcessed(path)) {
            continue;
        }

        PollsChoice choice = (PollsChoice) portletDataContext.getZipEntryAsObject(path);

        PollsPortletDataHandlerImpl.importChoice(portletDataContext, choice);
    }

    if (portletDataContext.getBooleanParameter(_NAMESPACE, "votes")) {
        Element votesElement = rootElement.element("votes");

        for (Element voteElement : votesElement.elements("vote")) {
            String path = voteElement.attributeValue("path");

            if (!portletDataContext.isPathNotProcessed(path)) {
                continue;
            }

            PollsVote vote = (PollsVote) portletDataContext.getZipEntryAsObject(path);

            PollsPortletDataHandlerImpl.importVote(portletDataContext, vote);
        }
    }

    long questionId = GetterUtil.getLong(portletPreferences.getValue("questionId", StringPool.BLANK));

    if (questionId > 0) {
        Map<Long, Long> questionPKs = (Map<Long, Long>) portletDataContext
                .getNewPrimaryKeysMap(PollsQuestion.class);

        questionId = MapUtil.getLong(questionPKs, questionId, questionId);

        portletPreferences.setValue("questionId", String.valueOf(questionId));
    }

    return portletPreferences;
}

From source file:com.liferay.portlet.polls.lar.PollsPortletDataHandlerImpl.java

License:Open Source License

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

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

    Document document = SAXReaderUtil.read(data);

    Element rootElement = document.getRootElement();

    Element questionsElement = rootElement.element("questions");

    for (Element questionElement : questionsElement.elements("question")) {
        String path = questionElement.attributeValue("path");

        if (!portletDataContext.isPathNotProcessed(path)) {
            continue;
        }//from  w ww.  j a v a  2s.  c om

        PollsQuestion question = (PollsQuestion) portletDataContext.getZipEntryAsObject(path);

        importQuestion(portletDataContext, questionElement, question);
    }

    Element choicesElement = rootElement.element("choices");

    for (Element choiceElement : choicesElement.elements("choice")) {
        String path = choiceElement.attributeValue("path");

        if (!portletDataContext.isPathNotProcessed(path)) {
            continue;
        }

        PollsChoice choice = (PollsChoice) portletDataContext.getZipEntryAsObject(path);

        importChoice(portletDataContext, choice);
    }

    if (portletDataContext.getBooleanParameter(_NAMESPACE, "votes")) {
        Element votesElement = rootElement.element("votes");

        for (Element voteElement : votesElement.elements("vote")) {
            String path = voteElement.attributeValue("path");

            if (!portletDataContext.isPathNotProcessed(path)) {
                continue;
            }

            PollsVote vote = (PollsVote) portletDataContext.getZipEntryAsObject(path);

            importVote(portletDataContext, vote);
        }
    }

    return null;
}

From source file:com.liferay.portlet.PortletBagFactory.java

License:Open Source License

protected Router newFriendlyURLRouter(Portlet portlet) throws Exception {
    if (Validator.isNull(portlet.getFriendlyURLRoutes())) {
        return null;
    }/*from  w  ww.j  a v  a  2s . co  m*/

    Router router = new RouterImpl();

    String xml = getContent(portlet.getFriendlyURLRoutes());

    Document document = SAXReaderUtil.read(xml, true);

    Element rootElement = document.getRootElement();

    for (Element routeElement : rootElement.elements("route")) {
        String pattern = routeElement.elementText("pattern");

        Route route = router.addRoute(pattern);

        for (Element generatedParameterElement : routeElement.elements("generated-parameter")) {

            String name = generatedParameterElement.attributeValue("name");
            String value = generatedParameterElement.getText();

            route.addGeneratedParameter(name, value);
        }

        for (Element ignoredParameterElement : routeElement.elements("ignored-parameter")) {

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

            route.addIgnoredParameter(name);
        }

        for (Element implicitParameterElement : routeElement.elements("implicit-parameter")) {

            String name = implicitParameterElement.attributeValue("name");
            String value = implicitParameterElement.getText();

            route.addImplicitParameter(name, value);
        }

        for (Element overriddenParameterElement : routeElement.elements("overridden-parameter")) {

            String name = overriddenParameterElement.attributeValue("name");
            String value = overriddenParameterElement.getText();

            route.addOverriddenParameter(name, value);
        }
    }

    return router;
}

From source file:com.liferay.portlet.rss.lar.RSSPortletDataHandlerImpl.java

License:Open Source License

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

    if (Validator.isNull(data)) {
        return null;
    }/*from  w  w w  . ja v  a  2 s. c o  m*/

    Document document = SAXReaderUtil.read(data);

    Element rootElement = document.getRootElement();

    JournalPortletDataHandlerImpl.importReferencedData(portletDataContext, rootElement);

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

    for (Element structureElement : structureElements) {
        JournalPortletDataHandlerImpl.importStructure(portletDataContext, structureElement);
    }

    List<Element> templateElements = rootElement.elements("template");

    for (Element templateElement : templateElements) {
        JournalPortletDataHandlerImpl.importTemplate(portletDataContext, templateElement);
    }

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

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

    Element footerArticleElement = rootElement.element("footer-article");

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

    String[] footerArticleValues = portletPreferences.getValues("footerArticleValues",
            new String[] { "0", "" });

    String footerArticleId = footerArticleValues[1];

    if (Validator.isNotNull(footerArticleId)) {
        footerArticleId = MapUtil.getString(articleIds, footerArticleId, footerArticleId);

        portletPreferences.setValues("footerArticleValues",
                new String[] { String.valueOf(portletDataContext.getScopeGroupId()), footerArticleId });

        JournalContentSearchLocalServiceUtil.updateContentSearch(portletDataContext.getScopeGroupId(),
                layout.isPrivateLayout(), layout.getLayoutId(), portletId, footerArticleId, true);
    }

    Element headerArticleElement = rootElement.element("header-article");

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

    String[] headerArticleValues = portletPreferences.getValues("headerArticleValues",
            new String[] { "0", "" });

    String headerArticleId = headerArticleValues[1];

    if (Validator.isNotNull(headerArticleId)) {
        headerArticleId = MapUtil.getString(articleIds, headerArticleId, headerArticleId);

        portletPreferences.setValues("headerArticleValues",
                new String[] { String.valueOf(portletDataContext.getScopeGroupId()), headerArticleId });

        JournalContentSearchLocalServiceUtil.updateContentSearch(portletDataContext.getScopeGroupId(),
                layout.isPrivateLayout(), layout.getLayoutId(), portletId, headerArticleId, true);
    }

    return portletPreferences;
}

From source file:com.liferay.portlet.social.util.SocialConfigurationUtil.java

License:Open Source License

private static void _read(ClassLoader classLoader, String xml, List<Object> objects) throws Exception {

    if (xml == null) {
        return;//from www  .  j a  va 2s  . c  o m
    }

    xml = JavaFieldsParser.parse(classLoader, xml);

    Document document = SAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    List<Element> activityElements = rootElement.elements("activity");

    for (Element activityElement : activityElements) {
        _readActivity(classLoader, objects, activityElement);
    }
}

From source file:com.liferay.portlet.social.util.SocialConfigurationUtil.java

License:Open Source License

private static void _readAchievement(ClassLoader classLoader, List<Object> objects,
        SocialActivityDefinition activityDefinition, Element achievementElement) throws Exception {

    String achievementClassName = GetterUtil.getString(achievementElement.elementText("achievement-class"));

    SocialAchievement achievement = (SocialAchievement) ProxyFactory.newInstance(classLoader,
            SocialAchievement.class, achievementClassName);

    String name = GetterUtil.getString(achievementElement.elementText("name"));

    achievement.setName(name);//from   www. java2  s. c  o  m

    String icon = GetterUtil.getString(achievementElement.elementText("icon"));

    achievement.setIcon(icon);

    List<Element> propertyElements = achievementElement.elements("property");

    for (Element propertyElement : propertyElements) {
        _readAchievementProperty(achievement, propertyElement);
    }

    achievement.initialize(activityDefinition);

    List<SocialAchievement> achievements = activityDefinition.getAchievements();

    achievements.add(achievement);

    Tuple tuple = new Tuple(activityDefinition, achievement);

    objects.add(tuple);
}

From source file:com.liferay.portlet.social.util.SocialConfigurationUtil.java

License:Open Source License

private static void _readActivity(ClassLoader classLoader, List<Object> objects, Element activityElement)
        throws Exception {

    String modelName = GetterUtil.getString(activityElement.elementText("model-name"));

    Map<Integer, SocialActivityDefinition> activityDefinitions = _activityDefinitions.get(modelName);

    if (activityDefinitions == null) {
        activityDefinitions = new HashMap<Integer, SocialActivityDefinition>();

        _activityDefinitions.put(modelName, activityDefinitions);
    }//from  ww  w. j  a va  2s.c  o m

    int activityType = GetterUtil.getInteger(activityElement.elementText("activity-type"));

    SocialActivityDefinition activityDefinition = activityDefinitions.get(activityType);

    if (activityDefinition == null) {
        activityDefinition = new SocialActivityDefinition();

        activityDefinition.setModelName(modelName);
        activityDefinition.setActivityType(activityType);

        _readActivity(classLoader, activityElement, activityDefinition);

        activityDefinitions.put(activityType, activityDefinition);

        objects.add(activityDefinition);
    }

    List<Element> counterElements = activityElement.elements("counter");

    for (Element counterElement : counterElements) {
        _readCounter(objects, activityDefinition, counterElement);
    }

    List<Element> achievementElements = activityElement.elements("achievement");

    for (Element achievementElement : achievementElements) {
        _readAchievement(classLoader, objects, activityDefinition, achievementElement);
    }
}

From source file:com.liferay.portlet.wiki.importers.mediawiki.MediaWikiImporter.java

License:Open Source License

protected void processRegularPages(long userId, WikiNode node, Element rootElement,
        List<String> specialNamespaces, Map<String, String> usersMap, InputStream imagesInputStream,
        Map<String, String[]> options) {

    boolean importLatestVersion = MapUtil.getBoolean(options, WikiImporterKeys.OPTIONS_IMPORT_LATEST_VERSION);
    boolean strictImportMode = MapUtil.getBoolean(options, WikiImporterKeys.OPTIONS_STRICT_IMPORT_MODE);

    ProgressTracker progressTracker = ProgressTrackerThreadLocal.getProgressTracker();

    int count = 0;

    int percentage = 10;

    int maxPercentage = 50;

    if (imagesInputStream == null) {
        maxPercentage = 99;// ww  w.j a v a 2s  .c  o  m
    }

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

    for (int i = 0; i < pageElements.size(); i++) {
        Element pageElement = pageElements.get(i);

        String title = pageElement.elementText("title");

        if (isSpecialMediaWikiPage(title, specialNamespaces)) {
            continue;
        }

        title = normalizeTitle(title);

        percentage = Math.min(10 + (i * (maxPercentage - percentage)) / pageElements.size(), maxPercentage);

        progressTracker.setPercent(percentage);

        List<Element> revisionElements = pageElement.elements("revision");

        if (importLatestVersion) {
            Element lastRevisionElement = revisionElements.get(revisionElements.size() - 1);

            revisionElements = new ArrayList<Element>();

            revisionElements.add(lastRevisionElement);
        }

        for (Element revisionElement : revisionElements) {
            Element contributorElement = revisionElement.element("contributor");

            String author = contributorElement.elementText("username");

            String content = revisionElement.elementText("text");
            String summary = revisionElement.elementText("comment");

            try {
                importPage(userId, author, node, title, content, summary, usersMap, strictImportMode);
            } catch (Exception e) {
                if (_log.isWarnEnabled()) {
                    _log.warn("Page with title " + title + " could not be imported", e);
                }
            }
        }

        count++;
    }

    if (_log.isInfoEnabled()) {
        _log.info("Imported " + count + " pages into " + node.getName());
    }
}

From source file:com.liferay.portlet.wiki.importers.mediawiki.MediaWikiImporter.java

License:Open Source License

protected void processSpecialPages(long userId, WikiNode node, Element rootElement,
        List<String> specialNamespaces) throws PortalException {

    ProgressTracker progressTracker = ProgressTrackerThreadLocal.getProgressTracker();

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

    for (int i = 0; i < pageElements.size(); i++) {
        Element pageElement = pageElements.get(i);

        String title = pageElement.elementText("title");

        if (!title.startsWith("Category:")) {
            if (isSpecialMediaWikiPage(title, specialNamespaces)) {
                rootElement.remove(pageElement);
            }/*from   www. j  a v  a2s. com*/

            continue;
        }

        String categoryName = title.substring("Category:".length());

        categoryName = normalize(categoryName, 75);

        Element revisionElement = pageElement.element("revision");

        String description = revisionElement.elementText("text");

        description = normalizeDescription(description);

        try {
            AssetTag assetTag = null;

            try {
                assetTag = AssetTagLocalServiceUtil.getTag(node.getGroupId(), categoryName);
            } catch (NoSuchTagException nste) {
                ServiceContext serviceContext = new ServiceContext();

                serviceContext.setAddGroupPermissions(true);
                serviceContext.setAddGuestPermissions(true);
                serviceContext.setScopeGroupId(node.getGroupId());

                assetTag = AssetTagLocalServiceUtil.addTag(userId, categoryName, null, serviceContext);

                if (PropsValues.ASSET_TAG_PROPERTIES_ENABLED && Validator.isNotNull(description)) {

                    AssetTagPropertyLocalServiceUtil.addTagProperty(userId, assetTag.getTagId(), "description",
                            description);
                }
            }
        } catch (SystemException se) {
            _log.error(se, se);
        }

        if ((i % 5) == 0) {
            progressTracker.setPercent((i * 10) / pageElements.size());
        }
    }
}

From source file:com.liferay.portlet.wiki.importers.mediawiki.MediaWikiImporter.java

License:Open Source License

protected List<String> readSpecialNamespaces(Element root) throws ImportFilesException {

    List<String> namespaces = new ArrayList<String>();

    Element siteinfoElement = root.element("siteinfo");

    if (siteinfoElement == null) {
        throw new ImportFilesException("Invalid pages XML file");
    }/*from  w ww.j  a  v a2 s . co m*/

    Element namespacesElement = siteinfoElement.element("namespaces");

    List<Element> namespaceElements = namespacesElement.elements("namespace");

    for (Element namespaceElement : namespaceElements) {
        Attribute attribute = namespaceElement.attribute("key");

        String value = attribute.getValue();

        if (!value.equals("0")) {
            namespaces.add(namespaceElement.getText());
        }
    }

    return namespaces;
}