Example usage for org.dom4j Node selectSingleNode

List of usage examples for org.dom4j Node selectSingleNode

Introduction

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

Prototype

Node selectSingleNode(String xpathExpression);

Source Link

Document

selectSingleNode evaluates an XPath expression and returns the result as a single Node instance.

Usage

From source file:org.sonar.report.pdf.entity.Project.java

License:Open Source License

/**
 * Initialize: - Project basic data - Project measures - Project categories violations - Project most violated rules -
 * Project most violated files - Project most duplicated files
 * /*from   w w  w . j  a va 2  s . c o  m*/
 * @param sonarAccess
 * @throws HttpException
 * @throws IOException
 * @throws DocumentException
 * @throws ReportException
 */
public void initializeProject(SonarAccess sonarAccess)
        throws HttpException, IOException, DocumentException, ReportException {
    Logger.info("Retrieving project info for " + this.key);
    Document parent = sonarAccess
            .getUrlAsDocument(UrlPath.RESOURCES + this.key + UrlPath.PARENT_PROJECT + UrlPath.XML_SOURCE);
    if (parent.selectSingleNode(PROJECT) != null) {
        initFromNode(parent.selectSingleNode(PROJECT));
        initMeasures(sonarAccess);
        initMostViolatedRules(sonarAccess);
        initMostViolatedFiles(sonarAccess);
        initMostComplexElements(sonarAccess);
        initMostDuplicatedFiles(sonarAccess);
        Logger.debug("Accessing Sonar: getting child projects");
        Document childs = sonarAccess
                .getUrlAsDocument(UrlPath.RESOURCES + this.key + UrlPath.CHILD_PROJECTS + UrlPath.XML_SOURCE);
        List<Node> childNodes = childs.selectNodes(PROJECT);
        Iterator<Node> it = childNodes.iterator();
        setSubprojects(new ArrayList<Project>());
        if (!it.hasNext()) {
            Logger.debug(this.key + " project has no childs");
        }
        while (it.hasNext()) {
            Node childNode = it.next();
            if (childNode.selectSingleNode("scope").getText().equals("PRJ")) {
                Project childProject = new Project(childNode.selectSingleNode(KEY).getText());
                childProject.initializeProject(sonarAccess);
                getSubprojects().add(childProject);
            }
        }
    } else {
        Logger.info("Can't retrieve project info. Have you set username/password in Sonar settings?");
        throw new ReportException("Can't retrieve project info. Parent project node is empty. Authentication?");
    }
}

From source file:org.sonar.report.pdf.entity.Project.java

License:Open Source License

/**
 * Initialize project object and his childs (except categories violations).
 *///from ww  w  . j  a  va2s .c  o m
private void initFromNode(Node projectNode) {
    Node name = projectNode.selectSingleNode(NAME);
    if (name != null) {
        this.setName(name.getText());
    }
    Node description = projectNode.selectSingleNode(DESCRIPTION);
    if (description != null) {
        this.setDescription(description.getText());
    }
    this.setKey(projectNode.selectSingleNode(KEY).getText());
    this.setLinks(new LinkedList<String>());
    this.setSubprojects(new LinkedList<Project>());
    this.setMostViolatedRules(new LinkedList<Rule>());
    this.setMostComplexFiles(new LinkedList<FileInfo>());
    this.setMostDuplicatedFiles(new LinkedList<FileInfo>());
    this.setMostViolatedFiles(new LinkedList<FileInfo>());
}

From source file:org.sonar.report.pdf.entity.Project.java

License:Open Source License

private void initMostViolatedRulesFromNode(Node mostViolatedNode, SonarAccess sonarAccess)
        throws HttpException, ReportException, IOException, DocumentException {
    List<Node> measures = mostViolatedNode.selectNodes(ALL_MEASURES);
    Iterator<Node> it = measures.iterator();
    if (!it.hasNext()) {
        Logger.warn("There is not result on select //resources/resource/msr");
    }//from  ww w.j  av  a  2s .  c o m
    while (it.hasNext()) {
        Node measure = it.next();
        if (!measure.selectSingleNode(MEASURE_FRMT_VAL).getText().equals("0")) {
            Rule rule = Rule.initFromNode(measure);
            if (PDFReporter.reportType.equals("workbook")) {
                rule.loadViolatedResources(sonarAccess, this.key);
            }
            this.mostViolatedRules.add(rule);
        }
    }
}

From source file:org.sonar.report.pdf.entity.Rule.java

License:Open Source License

/**
 * Initialize a rule given an XML Node that contains one rule
 * //from   www .  j  ava  2s .  c  o  m
 * @return
 */
public static Rule initFromNode(Node ruleNode) {
    Rule rule = new Rule();
    rule.setKey(ruleNode.selectSingleNode(RULE_KEY).getText());
    rule.setName(ruleNode.selectSingleNode(RULE_NAME).getText());
    rule.setViolationsNumber(Float.valueOf(ruleNode.selectSingleNode(RULE_VIOLATIONS_NUMBER).getText()));
    rule.setViolationsNumberFormatted(ruleNode.selectSingleNode(RULE_VIOLATIONS_NUMBER_FORMATTED).getText());
    return rule;
}

From source file:org.sonar.report.pdf.entity.Rule.java

License:Open Source License

/**
 * This method provide the possibility of init a Rule without init all violated resources.
 * //ww w  .ja  va  2  s.  com
 * @return
 * @throws DocumentException
 * @throws IOException
 * @throws HttpException
 */
public void loadViolatedResources(SonarAccess sonarAccess, String projectKey)
        throws ReportException, HttpException, IOException, DocumentException {
    String ruleKey = getKey();

    if (ruleKey == null) {
        throw new ReportException("Rule not initialized. Forget call to initFromNode() previously?");
    } else {
        ruleKey = URLEncoder.encode(ruleKey, "UTF8");
        Logger.debug("Accessing Sonar: getting violated resurces by one given rule (" + getKey() + ")");
        Document violatedResourcesDocument = sonarAccess.getUrlAsDocument(UrlPath.VIOLATIONS + projectKey
                + UrlPath.VIOLATED_RESOURCES_BY_RULE + ruleKey + UrlPath.XML_SOURCE);
        List<Node> violatedResources = violatedResourcesDocument.selectNodes(RULE_VIOLATIONS);
        topViolatedResources = new LinkedList<Violation>();
        Iterator<Node> it = violatedResources.iterator();

        while (it.hasNext()) {
            Node resource = it.next();
            String resourceKey = resource.selectSingleNode(RESOURCE_KEY).getText();
            String line = "N/A";
            if (resource.selectSingleNode(RESOURCE_LINE) != null) {
                line = resource.selectSingleNode(RESOURCE_LINE).getText();
            }
            topViolatedResources.add(new Violation(line, resourceKey, ""));
        }
    }
}

From source file:org.springfield.mojo.linkedtv.Channel.java

License:Open Source License

public List<Episode> getEpisodes() {
    try {/*ww w  .ja  v  a2  s.co  m*/
        Document doc = DocumentHelper.parseText(channelXml);

        List<Node> nodes = doc.selectNodes("//mediaresources/mediaresource");
        ArrayList<Episode> episodes = new ArrayList<Episode>();

        for (Node node : nodes) {
            String id = node.selectSingleNode("id") == null ? null : node.selectSingleNode("id").getText();
            if (id != null) {
                episodes.add(new Episode(id));
            }
        }
        return episodes;
    } catch (DocumentException e) {
        return new ArrayList<Episode>();
    }
}

From source file:org.springfield.mojo.linkedtv.Channel.java

License:Open Source License

public Episode getLatestEpisode() {
    try {//from www . j av  a2 s .c om
        Document doc = DocumentHelper.parseText(channelXml);

        List<Node> nodes = doc.selectNodes("//mediaresources/mediaresource");

        if (nodes.size() > 0) {
            Node node = nodes.get(0);
            String id = node.selectSingleNode("id") == null ? null : node.selectSingleNode("id").getText();
            if (id != null) {
                return new Episode(id);
            }
        }

        return new Episode();
    } catch (DocumentException e) {
        return new Episode();
    }
}

From source file:org.talend.core.nexus.NexusServerManager.java

License:Open Source License

private static void search(String nexusUrl, String userName, String password, String repositoryId,
        String groupIdToSearch, String versionToSearch, int searchFrom, int searchCount,
        List<MavenArtifact> artifacts) throws BusinessException {
    HttpURLConnection urlConnection = null;
    int totalCount = 0;
    try {/* w w w  . ja  va  2 s.  c  o m*/
        String service = NexusConstants.SERVICES_SEARCH
                + getSearchQuery(versionToSearch, repositoryId, groupIdToSearch, searchFrom, searchCount);
        urlConnection = getHttpURLConnection(nexusUrl, service, userName, password);
        SAXReader saxReader = new SAXReader();

        InputStream inputStream = urlConnection.getInputStream();
        Document document = saxReader.read(inputStream);

        Node countNode = document.selectSingleNode("/searchNGResponse/totalCount");
        if (countNode != null) {
            try {
                totalCount = Integer.parseInt(countNode.getText());
            } catch (NumberFormatException e) {
                totalCount = 0;
            }
        }

        List<Node> list = document.selectNodes("/searchNGResponse/data/artifact");//$NON-NLS-1$ 
        for (Node arNode : list) {
            MavenArtifact artifact = new MavenArtifact();
            artifacts.add(artifact);
            artifact.setGroupId(arNode.selectSingleNode("groupId").getText());//$NON-NLS-1$ 
            artifact.setArtifactId(arNode.selectSingleNode("artifactId").getText());//$NON-NLS-1$ 
            artifact.setVersion(arNode.selectSingleNode("version").getText());//$NON-NLS-1$ 
            Node descNode = arNode.selectSingleNode("description");//$NON-NLS-1$ 
            if (descNode != null) {
                artifact.setDescription(descNode.getText());
            }
            Node urlNode = arNode.selectSingleNode("url");//$NON-NLS-1$ 
            if (urlNode != null) {
                artifact.setUrl(urlNode.getText());
            }
            Node licenseNode = arNode.selectSingleNode("license");//$NON-NLS-1$ 
            if (licenseNode != null) {
                artifact.setLicense(licenseNode.getText());
            }

            Node licenseUrlNode = arNode.selectSingleNode("licenseUrl");//$NON-NLS-1$ 
            if (licenseUrlNode != null) {
                artifact.setLicenseUrl(licenseUrlNode.getText());
            }

            Node distributionNode = arNode.selectSingleNode("distribution");//$NON-NLS-1$ 
            if (distributionNode != null) {
                artifact.setDistribution(distributionNode.getText());
            }

            List<Node> artLinks = arNode.selectNodes("artifactHits/artifactHit/artifactLinks/artifactLink");//$NON-NLS-1$ 
            for (Node link : artLinks) {
                Node extensionElement = link.selectSingleNode("extension");//$NON-NLS-1$ 
                String extension = null;
                String classifier = null;
                if (extensionElement != null) {
                    if ("pom".equals(extensionElement.getText())) {//$NON-NLS-1$ 
                        continue;
                    }
                    extension = extensionElement.getText();
                }
                Node classifierElement = link.selectSingleNode("classifier");//$NON-NLS-1$ 
                if (classifierElement != null) {
                    classifier = classifierElement.getText();
                }
                artifact.setType(extension);
                artifact.setClassifier(classifier);
            }
        }
        int searchDone = searchFrom + searchCount;
        int count = MAX_SEARCH_COUNT;
        if (searchDone < totalCount) {
            if (totalCount - searchDone < MAX_SEARCH_COUNT) {
                count = totalCount - searchDone;
            }
            search(nexusUrl, userName, password, repositoryId, groupIdToSearch, versionToSearch, searchDone + 1,
                    count, artifacts);
        }

    } catch (Exception e) {
        if (e instanceof ConnectException) {
            throw new BusinessException("Can not connect to nexus server ,please contact the administrator",
                    nexusUrl);
        } else {
            throw new BusinessException(e);
        }

    } finally {
        if (null != urlConnection) {
            urlConnection.disconnect();
        }
    }
}

From source file:org.talend.core.nexus.NexusServerUtils.java

License:Open Source License

private static void search(String nexusUrl, final String userName, final String password, String repositoryId,
        String groupIdToSearch, String artifactId, String versionToSearch, int searchFrom, int searchCount,
        List<MavenArtifact> artifacts) throws Exception {
    HttpURLConnection urlConnection = null;
    int totalCount = 0;
    final Authenticator defaultAuthenticator = NetworkUtil.getDefaultAuthenticator();
    if (userName != null && !"".equals(userName)) {
        Authenticator.setDefault(new Authenticator() {

            @Override//from  w  w w .  j av  a2  s .  c o m
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password.toCharArray());
            }

        });
    }
    try {
        String service = NexusConstants.SERVICES_SEARCH + getSearchQuery(repositoryId, groupIdToSearch,
                artifactId, versionToSearch, searchFrom, searchCount);
        urlConnection = getHttpURLConnection(nexusUrl, service, userName, password);
        SAXReader saxReader = new SAXReader();

        InputStream inputStream = urlConnection.getInputStream();
        Document document = saxReader.read(inputStream);

        Node countNode = document.selectSingleNode("/searchNGResponse/totalCount");
        if (countNode != null) {
            try {
                totalCount = Integer.parseInt(countNode.getText());
            } catch (NumberFormatException e) {
                totalCount = 0;
            }
        }

        List<Node> list = document.selectNodes("/searchNGResponse/data/artifact");//$NON-NLS-1$ 
        for (Node arNode : list) {
            MavenArtifact artifact = new MavenArtifact();
            artifacts.add(artifact);
            artifact.setGroupId(arNode.selectSingleNode("groupId").getText());//$NON-NLS-1$ 
            artifact.setArtifactId(arNode.selectSingleNode("artifactId").getText());//$NON-NLS-1$ 
            artifact.setVersion(arNode.selectSingleNode("version").getText());//$NON-NLS-1$ 
            Node descNode = arNode.selectSingleNode("description");//$NON-NLS-1$ 
            if (descNode != null) {
                artifact.setDescription(descNode.getText());
            }
            Node urlNode = arNode.selectSingleNode("url");//$NON-NLS-1$ 
            if (urlNode != null) {
                artifact.setUrl(urlNode.getText());
            }
            Node licenseNode = arNode.selectSingleNode("license");//$NON-NLS-1$ 
            if (licenseNode != null) {
                artifact.setLicense(licenseNode.getText());
            }

            Node licenseUrlNode = arNode.selectSingleNode("licenseUrl");//$NON-NLS-1$ 
            if (licenseUrlNode != null) {
                artifact.setLicenseUrl(licenseUrlNode.getText());
            }

            List<Node> artLinks = arNode.selectNodes("artifactHits/artifactHit/artifactLinks/artifactLink");//$NON-NLS-1$ 
            for (Node link : artLinks) {
                Node extensionElement = link.selectSingleNode("extension");//$NON-NLS-1$ 
                String extension = null;
                String classifier = null;
                if (extensionElement != null) {
                    if ("pom".equals(extensionElement.getText())) {//$NON-NLS-1$ 
                        continue;
                    }
                    extension = extensionElement.getText();
                }
                Node classifierElement = link.selectSingleNode("classifier");//$NON-NLS-1$ 
                if (classifierElement != null) {
                    classifier = classifierElement.getText();
                }
                artifact.setType(extension);
                artifact.setClassifier(classifier);
            }
        }
        int searchDone = searchFrom + searchCount;
        int count = MAX_SEARCH_COUNT;
        if (searchDone < totalCount) {
            if (totalCount - searchDone < MAX_SEARCH_COUNT) {
                count = totalCount - searchDone;
            }
            search(nexusUrl, userName, password, repositoryId, groupIdToSearch, artifactId, versionToSearch,
                    searchDone + 1, count, artifacts);
        }

    } finally {
        Authenticator.setDefault(defaultAuthenticator);
        if (null != urlConnection) {
            urlConnection.disconnect();
        }
    }
}

From source file:org.talend.mdm.webapp.base.server.ForeignKeyHelper.java

License:Open Source License

private static String parseRightValueOrPath(String xml, String dataObject, String rightValueOrPath,
        String currentXpath) throws Exception {
    if (rightValueOrPath == null || currentXpath == null) {
        throw new IllegalArgumentException();
    }/* w  w w. j av  a 2s .com*/
    // cases handle
    String result = rightValueOrPath;// by default result equals input value/path
    if (org.talend.mdm.webapp.base.shared.util.CommonUtil.isFilterValue(rightValueOrPath)) {
        result = rightValueOrPath.substring(1, rightValueOrPath.length() - 1);
    } else {
        if (xml != null) {
            // get context for expression
            org.dom4j.Document doc = XmlUtil.parseDocument(Util.parse(xml));
            org.dom4j.Node currentNode = doc.selectSingleNode(currentXpath);
            org.dom4j.Node targetNode = null;
            if (org.talend.mdm.webapp.base.shared.util.CommonUtil.isRelativePath(rightValueOrPath)) {
                targetNode = currentNode.selectSingleNode(rightValueOrPath);
            } else {
                String xpath = rightValueOrPath.startsWith("/") ? rightValueOrPath : "/" + rightValueOrPath; //$NON-NLS-1$//$NON-NLS-2$
                targetNode = currentNode.selectSingleNode(xpath);
            }
            if (targetNode != null && targetNode.getText() != null) {
                result = targetNode.getText();
            }
        }
    }

    return result;
}