Example usage for org.dom4j Node selectNodes

List of usage examples for org.dom4j Node selectNodes

Introduction

In this page you can find the example usage for org.dom4j Node selectNodes.

Prototype

List<Node> selectNodes(String xpathExpression);

Source Link

Document

selectNodes evaluates an XPath expression and returns the result as a List of Node instances or String instances depending on the XPath expression.

Usage

From source file:org.craftercms.cstudio.alfresco.service.impl.ServicesConfigImpl.java

License:Open Source License

/**
 * load page/component/assets patterns configuration
 *
 * @param site/*from w  w w .  j  av a2  s. c o m*/
 * @param nodes
 */
@SuppressWarnings("unchecked")
protected void loadPatterns(SiteConfigTO site, RepositoryConfigTO repo, List<Node> nodes) {
    if (nodes != null) {
        for (Node node : nodes) {
            String patternKey = node.valueOf(ATTR_NAME);
            if (!StringUtils.isEmpty(patternKey)) {
                List<Node> patternNodes = node.selectNodes(ELM_PATTERN);
                if (patternNodes != null) {
                    List<String> patterns = new FastList<String>(patternNodes.size());
                    for (Node patternNode : patternNodes) {
                        String pattern = patternNode.getText();
                        if (!StringUtils.isEmpty(pattern)) {
                            patterns.add(pattern);
                        }
                    }
                    if (patternKey.equals(PATTERN_PAGE)) {
                        repo.setPagePatterns(patterns);
                    } else if (patternKey.equals(PATTERN_COMPONENT)) {
                        repo.setComponentPatterns(patterns);
                    } else if (patternKey.equals(PATTERN_ASSET)) {
                        repo.setAssetPatterns(patterns);
                    } else if (patternKey.equals(PATTERN_DOCUMENT)) {
                        repo.setDocumentPatterns(patterns);
                    } else if (patternKey.equals(PATTERN_RENDERING_TEMPLATE)) {
                        repo.setRenderingTemplatePatterns(patterns);
                    } else if (patternKey.equals(PATTERN_LEVEL_DESCRIPTOR)) {
                        repo.setLevelDescriptorPatterns(patterns);
                    } else if (patternKey.equals(PATTERN_PREVIEWABLE_MIMETYPES)) {
                        repo.setPreviewableMimetypesPaterns(patterns);
                    } else {
                        LOGGER.error(
                                "Unknown pattern key: " + patternKey + " is provided in " + site.getName());
                    }
                }
            } else {
                LOGGER.error("no pattern key provided in " + site.getName()
                        + " configuration. Skipping the pattern.");
            }
        }
    } else {
        LOGGER.warn(site.getName() + " does not have any pattern configuration.");
    }
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
protected void setupRepository(Document document, boolean importFromFilePath, String buildDataLocation) {
    if (LOGGER.isInfoEnabled())
        LOGGER.info("[IMPORTSERVICE] starting DM repository setup.");
    PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
    Element root = document.getRootElement();
    Node node = root.selectSingleNode("//repository/folders");
    String rootPath = node.valueOf("@root");
    String fileRootPath = node.valueOf("@file-root");
    NodeRef rootRef = persistenceManagerService.getNodeRef(rootPath);
    String basePath = buildDataLocation + "/" + fileRootPath;
    if (LOGGER.isInfoEnabled())
        LOGGER.info("[IMPORTSERVICE] importing from " + basePath + " to " + rootPath);

    if (rootRef != null) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(CStudioConstants.DATE_PATTERN_WORKFLOW);
        dateFormat.setTimeZone(TimeZone.getTimeZone(_timezone));
        createChildren(basePath, importFromFilePath, rootRef, node.selectNodes("folder"), true, dateFormat);
        createChildren(basePath, importFromFilePath, rootRef, node.selectNodes("file"), false, dateFormat);
    } else {//from w ww. j  a va  2s . co m
        LOGGER.error("[IMPORTSERVICE] Cannot locate the root folder: " + rootPath);
    }
    List<String> groups = createGroups(root.selectNodes("groups/group"), null);
    for (String group : groups) {
        String groupName = persistenceManagerService.getName(AuthorityType.GROUP, group);
        persistenceManagerService.setPermission(rootRef, groupName, PermissionService.COORDINATOR, true);
    }
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * create child folders and files /*from w  w  w  . j  a va  2 s.c  o m*/
 * 
 * @param parentClassFilePath
 * @param importFromFilePath
 * @param parentRef
 * @param childNodes
 * @param isFolder
 * @param dateFormat
 * @throws Exception
 */
@SuppressWarnings("unchecked")
protected void createChildren(String parentClassFilePath, boolean importFromFilePath, NodeRef parentRef,
        List<Node> childNodes, boolean isFolder, SimpleDateFormat dateFormat) {
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("[IMPORTSERVICE] createChildren parentClassFilePath[" + parentClassFilePath + "]");
    }
    PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
    if (parentRef != null && childNodes != null) {
        for (Node childNode : childNodes) {
            String childName = childNode.valueOf("@name");
            boolean importAll = (isFolder)
                    ? ContentFormatUtils.getBooleanValue(childNode.valueOf("@import-all"))
                    : false;
            if (importAll) {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("[IMPORTSERVICE] Importing all folders and files from " + childName);
                }
                NodeRef childRef = getFileFolderRef(parentRef, childName, ContentModel.TYPE_FOLDER);
                List<Node> aspectNodes = childNode.selectNodes("common-aspects/aspect");
                Map<QName, Map<QName, Serializable>> commonAspects = new FastMap<QName, Map<QName, Serializable>>();
                if (aspectNodes != null && aspectNodes.size() > 0) {
                    for (Node aspectNode : aspectNodes) {
                        QName aspectName = persistenceManagerService.createQName(aspectNode.valueOf("@name"));
                        Map<QName, Serializable> properties = getProperties(
                                aspectNode.selectNodes("properties/property"), dateFormat);
                        commonAspects.put(aspectName, properties);
                    }
                }
                importFileList(childRef, parentClassFilePath + "/" + childName, importFromFilePath,
                        commonAspects);
                createRules(childName, childRef, childNode.selectNodes("rule"));
            } else {
                String childType = childNode.valueOf("@type");
                QName type = persistenceManagerService.createQName(childType);
                if (childName != null && type != null) {
                    createContent(childNode, parentClassFilePath, importFromFilePath, parentRef, childName,
                            type, isFolder, dateFormat);
                }
            }
        }
    }
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * create folder or file/*w w  w .  java  2  s. co  m*/
 *
 * @param node
 * @param parentClassFilePath
 * @param parentRef
 * @param name
 * @param type
 * @param isFolder
 * @param dateFormat
 * @throws Exception
 */
@SuppressWarnings("unchecked")
protected void createContent(Node node, String parentClassFilePath, boolean importFromFilePath,
        NodeRef parentRef, String name, QName type, boolean isFolder, SimpleDateFormat dateFormat) {
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("[IMPORTSERVICE] createContent [" + parentClassFilePath + "]");
    }
    PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
    NodeRef nodeRef = getFileFolderRef(parentRef, name, type);
    addAspects(nodeRef, node, dateFormat);
    // set properties
    Map<QName, Serializable> properties = getProperties(node.selectNodes("properties/property"), dateFormat);
    if (properties != null) {
        persistenceManagerService.setProperties(nodeRef, properties);
    }
    String childClassFilePath = parentClassFilePath + "/" + name;
    if (isFolder) {
        // if the folder is identifiable, assign an ID unless the content already carries an id
        if (persistenceManagerService.hasAspect(nodeRef, CStudioContentModel.ASPECT_IDENTIFIABLE)) {
            String namespace = (String) persistenceManagerService.getProperty(nodeRef,
                    CStudioContentModel.PROP_IDENTIFIABLE_NAMESPACE);
            Long order = (Long) persistenceManagerService.getProperty(nodeRef,
                    CStudioContentModel.PROP_IDENTIFIABLE_ORDER);
            // if is-current flag is not found, it is current by default
            boolean current = (Boolean) persistenceManagerService.getProperty(nodeRef,
                    CStudioContentModel.PROP_IDENTIFIABLE_CURRENT);
            assignIdentifier(name, type, nodeRef, namespace, order, current);
        }
        createChildren(childClassFilePath, importFromFilePath, nodeRef, node.selectNodes("folder"), true,
                dateFormat);
        createChildren(childClassFilePath, importFromFilePath, nodeRef, node.selectNodes("file"), false,
                dateFormat);
        createRules(name, nodeRef, node.selectNodes("rule"));
    } else {
        boolean overwrite = ContentFormatUtils.getBooleanValue(node.valueOf("@overwrite"));
        ;
        if (overwrite) {
            try {
                copyFile(childClassFilePath, importFromFilePath, nodeRef, name);
            } catch (Exception e) {
                LOGGER.error("[IMPORTSERVICE] error copying file [" + childClassFilePath + "]", e);
            }
        }
    }
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * add aspects to content//from  www .j  a  va  2  s .c  o  m
 *
 * @param nodeRef
 * @param node
 * @param dateFormat
 */
@SuppressWarnings("unchecked")
protected void addAspects(NodeRef nodeRef, Node node, SimpleDateFormat dateFormat) {
    List<Node> aspectNodes = node.selectNodes("aspects/aspect");
    if (aspectNodes != null && aspectNodes.size() > 0) {
        PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
        for (Node aspectNode : aspectNodes) {
            String name = aspectNode.valueOf("@name");
            QName type = persistenceManagerService.createQName(name);
            if (type != null) {
                Map<QName, Serializable> properties = getProperties(
                        aspectNode.selectNodes("properties/property"), dateFormat);
                persistenceManagerService.addAspect(nodeRef, type, properties);
            }
        }
    }
}

From source file:org.craftercms.cstudio.alfresco.util.impl.ImportServiceImpl.java

License:Open Source License

/**
 * create groups specified//w  w w.j a va2s .co m
 *
 * @param nodes
 * @param parentName parent group name. if null, groups will be created at root
 */
@SuppressWarnings("unchecked")
protected List<String> createGroups(List<Node> nodes, String parentName) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("[IMPORTSERVICE] createGroups parentName[" + parentName + "]");
    if (nodes != null && nodes.size() > 0) {
        List<String> groups = new ArrayList<String>(nodes.size());
        PersistenceManagerService persistenceManagerService = getService(PersistenceManagerService.class);
        for (Node node : nodes) {
            String parentGroup = null;
            if (parentName != null) {
                parentGroup = "GROUP_" + parentName;
            }
            String name = node.valueOf("@name");
            if (!StringUtils.isEmpty(name)) {
                if (!persistenceManagerService.authorityExists(name)) {
                    // passing null for authority zone since it is not applicable
                    persistenceManagerService.createAuthority(AuthorityType.GROUP, parentGroup, name, null);
                    groups.add(name);
                }
                createGroups(node.selectNodes("group"), name);
            }
            // add users to the group if the list is provided
            String userList = node.valueOf("users");
            if (!StringUtils.isEmpty(userList)) {
                String[] users = userList.split(",");
                for (String user : users) {
                    if (persistenceManagerService.personExists(user)) {
                        persistenceManagerService.addAuthority("GROUP_" + name, user);
                    } else {
                        LOGGER.error(
                                "[IMPORTSERVICE] " + user + " does not exist and cannot be added to " + name);
                    }
                }
            }
            // only return the top level groups to add groups to the DM space
        }
        return groups;
    }
    return new ArrayList<String>(0);
}

From source file:org.craftercms.studio.impl.v1.service.configuration.ServicesConfigImpl.java

License:Open Source License

/**
 * load the web-project configuration//from  ww  w  .  j a  va  2 s.c  o m
 *
 * @param siteConfig
 * @param node
 */
@SuppressWarnings("unchecked")
protected void loadSiteRepositoryConfiguration(SiteConfigTO siteConfig, Node node) {
    RepositoryConfigTO repoConfigTO = new RepositoryConfigTO();
    repoConfigTO.setRootPrefix(node.valueOf("@rootPrefix"));
    repoConfigTO.setLevelDescriptorName(node.valueOf("level-descriptor"));
    loadFolderConfiguration(siteConfig, repoConfigTO, node.selectNodes("folders/folder"));
    loadPatterns(siteConfig, repoConfigTO, node.selectNodes("patterns/pattern-group"));
    List<String> displayPatterns = getStringList(
            node.selectNodes("display-in-widget-patterns/display-in-widget-pattern"));
    repoConfigTO.setDisplayPatterns(displayPatterns);
    siteConfig.setRepositoryConfig(repoConfigTO);
}

From source file:org.craftercms.studio.impl.v1.service.content.ImportServiceImpl.java

License:Open Source License

private void importFromConfigNode(final String site, String publishChannelGroup, final Node node,
        final String fileRoot, final String targetRoot, boolean publish, int chunkSize, int delayInterval,
        int delayLength) {
    if (!inProgress) {
        inProgress = true;/*from  ww  w.jav a  2  s.c  om*/
        if (delayInterval > 0)
            pauseEanbeld = true;
        this.currentDelayInterval = delayInterval * 1000;
        this.currentDelayLength = delayLength * 1000;
        final Set<String> importedPaths = new HashSet<String>();
        final List<String> importedFullPaths = new ArrayList<String>();
        logger.info("[IMPORT] started importing in " + site + ", pause enabled: " + pauseEanbeld
                + ", delay interval: " + this.currentDelayInterval + ", delay length: "
                + this.currentDelayLength);

        boolean overWrite = ContentFormatUtils.getBooleanValue(node.valueOf("@over-write"));
        final List<Node> folderNodes = node.selectNodes("folder");
        if (publish) {
            PublishingChannelGroupConfigTO configTO = siteService.getPublishingChannelGroupConfigs(site)
                    .get(publishChannelGroup);
            String user = securityService.getCurrentUser();
            List<PublishingChannel> channels = getChannels(site, configTO);
            logger.debug(
                    "[IMPORT] publishing user: " + user + ", publishing channel config: " + configTO.getName());

            this.nextStop = System.currentTimeMillis() + this.currentDelayInterval;
            createFolders(site, importedPaths, importedFullPaths, folderNodes, fileRoot, targetRoot, "",
                    overWrite, channels, user);
            logger.info("Starting Publish of Imported Files (Total " + importedFullPaths.size()
                    + " On chunkSize of " + chunkSize + " )");
            publish(site, publishChannelGroup, targetRoot, importedFullPaths, chunkSize);
        } else {
            this.nextStop = System.currentTimeMillis() + this.currentDelayInterval;
            createFolders(site, importedPaths, importedFullPaths, folderNodes, fileRoot, targetRoot, "",
                    overWrite, null, null);
        }
        inProgress = false;
    } else {
        logger.info("[IMPORT] an import process is currently running.");
    }
}

From source file:org.craftercms.studio.impl.v1.service.content.ImportServiceImpl.java

License:Open Source License

/**
 * create folders//from w w w. ja va2s  .co m
 *
 * @param site
 *            site name
 * @param importedPaths
 *            a list of imported files
 * @param importedFullPaths
 * @param nodes
 *            nodes representing folders
 * @param fileRoot
 *            the root location of files/folders being imported
 * @param targetRoot
 *            the target location root
 * @param parentPath
 *            the target location to import to
 * @param overWrite
 *            overwrite contents?
 * @param channels
 * @param user
 *
 */
@SuppressWarnings("unchecked")
private void createFolders(String site, Set<String> importedPaths, List<String> importedFullPaths,
        List<Node> nodes, String fileRoot, String targetRoot, String parentPath, boolean overWrite,
        List<PublishingChannel> channels, String user) {
    logger.info("[IMPORT] createFolders : site[" + site + "] " + "] fileRoot [" + fileRoot + "] targetRoot [ "
            + targetRoot + "] parentPath [" + parentPath + "] overwrite[" + overWrite + "]");

    if (nodes != null) {
        for (Node node : nodes) {
            String name = node.valueOf("@name");
            String value = node.valueOf("@over-write");
            boolean folderOverWrite = (StringUtils.isEmpty(value)) ? overWrite
                    : ContentFormatUtils.getBooleanValue(value);
            if (!StringUtils.isEmpty(name)) {
                String currentFilePath = fileRoot + "/" + name;
                String currentPath = parentPath + "/" + name;
                // check if the parent node exists and create the folder if
                // not
                boolean folderExists = contentService.contentExists(site, currentPath);
                if (!folderExists) {
                    contentService.createFolder(site, parentPath, name);
                }
                boolean importAll = ContentFormatUtils.getBooleanValue(node.valueOf("@import-all"));
                if (importAll) {
                    importRootFileList(site, importedPaths, importedFullPaths, fileRoot + "/" + name,
                            targetRoot, currentPath, folderOverWrite, channels, user);

                } else {
                    // create child folders
                    List<Node> childFolders = node.selectNodes("folder");
                    createFolders(site, importedPaths, importedFullPaths, childFolders, currentFilePath,
                            targetRoot, currentPath, folderOverWrite, channels, user);
                    // create child fiimportedPathsles
                    List<Node> childFiles = node.selectNodes("file");
                    createFiles(site, importedPaths, importedFullPaths, childFiles, currentFilePath, targetRoot,
                            currentPath, folderOverWrite, channels, user);
                }
            }
        }
    }
}

From source file:org.craftercms.studio.impl.v1.service.notification.NotificationServiceImpl.java

License:Open Source License

protected NotificationConfigTO loadConfiguration(final String site) {
    String configFullPath = getConfigPath().replaceFirst(StudioConstants.PATTERN_SITE, site);
    configFullPath = configFullPath + "/" + getConfigFileName();
    NotificationConfigTO config = null;/*from   w  w w . ja  v  a2s .  c  o m*/
    try {
        Document document = contentService.getContentAsDocument(site, configFullPath);
        if (document != null) {
            Element root = document.getRootElement();
            config = new NotificationConfigTO();
            Node configNode = root.selectSingleNode("/notification-config");
            if (configNode != null) {
                loadCannedMessages(config, configNode.selectNodes("canned-messages/messages"));
                loadEmailMessageTemplates(config,
                        configNode.selectNodes("email-message-templates/email-message-template"));
                Map<String, String> completeMessages = loadMessages(
                        configNode.selectNodes("complete-messages/message"));
                Map<String, String> errorMessages = loadMessages(
                        configNode.selectNodes("error-messages/message"));
                config.setCompleteMessages(completeMessages);
                config.setErrorMessages(errorMessages);
                Map<String, String> generalMessages = loadMessages(
                        configNode.selectNodes("general-messages/message"));
                config.setMessages(generalMessages);
                Map<String, Boolean> noticeMapping = loadSendNoticeMapping(
                        configNode.selectSingleNode("send-notifications"));
                config.setSendNoticeMapping(noticeMapping);
                Map<String, String> submitNotificationRules = loadSubmitNotificationRules(
                        configNode.selectSingleNode("submit-notifications"));
                config.setSubmitNotificationsMapping(submitNotificationRules);
                List<String> deploymentFailureNotifications = loadDeploymentFailureNotifications(
                        configNode.selectSingleNode("deployment-failure"));
                config.setDeploymentFailureNotifications(deploymentFailureNotifications);
                config.setSite(site);
                config.setLastUpdated(new Date());
            } else {
                logger.error("Notification config is not found for " + site);
            }
        }
    } catch (Exception ex) {
        logger.error("Notification config is not found for " + site, ex);
    }
    return config;
}