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:com.chingo247.structureapi.plan.overview.StructureOverviewLoader.java

License:Open Source License

@Override
public List<StructureOverview> load(Element overviewsElement) throws StructureDataException {
    if (overviewsElement == null) {
        throw new AssertionError("Overviews element was null");
    }//  www .j a  va  2 s.co m

    if (!overviewsElement.getName().equals(Elements.STRUCTURE_OVERVIEWS)) {
        throw new AssertionError("Expected '" + Elements.STRUCTURE_OVERVIEWS + "' element, but got '"
                + overviewsElement.getName() + "'");
    }

    new StructureOverviewValidator().validate(overviewsElement);

    List<StructureOverview> overviews = new ArrayList<>();
    List<Node> overviewNodes = overviewsElement.selectNodes(Elements.STRUCTURE_OVERVIEW);

    for (Node overviewNode : overviewNodes) {
        Node xNode = overviewNode.selectSingleNode(Elements.X);
        Node yNode = overviewNode.selectSingleNode(Elements.Y);
        Node zNode = overviewNode.selectSingleNode(Elements.Z);

        int x = Integer.parseInt(xNode.getText());
        int y = Integer.parseInt(yNode.getText());
        int z = Integer.parseInt(zNode.getText());
        overviews.add(new StructureOverview(x, y, z));
    }

    return overviews;
}

From source file:com.chingo247.structureapi.plan.overview.StructureOverviewValidator.java

License:Open Source License

@Override
public void validate(Element e) throws StructureDataException {
    if (!e.getName().equals(Elements.STRUCTURE_OVERVIEWS)) {
        throw new AssertionError(
                "Expected '" + Elements.STRUCTURE_OVERVIEWS + "' but got '" + e.getName() + "'");
    }//from w  w w .j  a v  a 2s  .  c  o m

    List<Node> nodes = e.selectNodes(Elements.STRUCTURE_OVERVIEW);
    if (nodes != null && !nodes.isEmpty()) {
        int count = 0;
        for (Node n : nodes) {
            Node xNode = n.selectSingleNode(Elements.X);
            Node yNode = n.selectSingleNode(Elements.Y);
            Node zNode = n.selectSingleNode(Elements.Z);

            if (xNode == null) {
                throw new StructureDataException(
                        "Missing 'X' node for '" + Elements.STRUCTURE_OVERVIEW + "#" + count);
            }

            if (yNode == null) {
                throw new StructureDataException(
                        "Missing 'Y' node for '" + Elements.STRUCTURE_OVERVIEW + "#" + count);
            }

            if (zNode == null) {
                throw new StructureDataException(
                        "Missing 'Z' node for '" + Elements.STRUCTURE_OVERVIEW + "#" + count);
            }

            try {
                Integer.parseInt(xNode.getText());
            } catch (NumberFormatException nfe) {
                throw new StructureDataException(
                        "Invalid X value for '" + Elements.STRUCTURE_OVERVIEW + "#" + count);
            }

            try {
                Integer.parseInt(yNode.getText());
            } catch (NumberFormatException nfe) {
                throw new StructureDataException(
                        "Invalid Y value for '" + Elements.STRUCTURE_OVERVIEW + "#" + count);
            }

            try {
                Integer.parseInt(zNode.getText());
            } catch (NumberFormatException nfe) {
                throw new StructureDataException(
                        "Invalid Z value for '" + Elements.STRUCTURE_OVERVIEW + "#" + count);
            }
            count++;
        }
    }
}

From source file:com.chingo247.structureapi.plan.worldguard.StructureRegionFlagLoader.java

License:Open Source License

@Override
public List<StructureRegionFlag> load(Element regionFlagsElement) throws StructureDataException {
    if (!regionFlagsElement.getName().equals(Elements.REGIONFLAGS)) {
        throw new AssertionError("Expected element '" + Elements.REGIONFLAGS + "', but got '"
                + regionFlagsElement.getName() + "'");
    }/*ww w .  ja va  2 s.  c om*/

    new StructureRegionFlagValidator().validate(regionFlagsElement);
    List<StructureRegionFlag> flags = new LinkedList<>();

    Iterator<Node> it = regionFlagsElement.selectNodes(Elements.REGIONFLAG).iterator();
    while (it.hasNext()) {
        Node n = it.next();

        Flag f = DefaultFlag.fuzzyMatchFlag(n.selectSingleNode(Elements.NAME).getText());
        if (f == null) {
            throw new StructureDataException(
                    "Flag '" + n.selectSingleNode(Elements.NAME).getText() + "' not recognized");
        }

        try {
            Object v = f.parseInput(WorldGuardPlugin.inst(), Bukkit.getConsoleSender(),
                    n.selectSingleNode(Elements.VALUE).getText());
            StructureRegionFlag regionFlag = new StructureRegionFlag(f, v);
            flags.add(regionFlag);

        } catch (InvalidFlagFormat ex) {
            Logger.getLogger(StructurePlan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return flags;
}

From source file:com.chingo247.structureapi.plan.worldguard.StructureRegionFlagValidator.java

License:Open Source License

@Override
public void validate(Element en) throws StructureDataException {
    List<Node> nodes = en.selectNodes(Elements.REGIONFLAG);
    Iterator<Node> it = nodes.iterator();

    while (it.hasNext()) {
        Node e = it.next();

        if (e.selectSingleNode("Name") == null) {
            throw new StructureDataException("Missing 'name' node");
        }/*w  ww  .  j ava2s  .com*/
        if (e.selectSingleNode("Value") == null) {
            throw new StructureDataException("Missing 'value' node");
        }

        Flag f = DefaultFlag.fuzzyMatchFlag(e.selectSingleNode("Name").getText());
        if (f == null) {
            throw new StructureDataException(
                    "Flag '" + e.selectSingleNode("Name").getText() + "' not recognized");
        }

        try {
            Object v = f.parseInput(WorldGuardPlugin.inst(), Bukkit.getConsoleSender(),
                    e.selectSingleNode("Value").getText());

        } catch (InvalidFlagFormat ex) {
            Logger.getLogger(StructurePlan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.dtolabs.client.services.RundeckAPICentralDispatcher.java

License:Apache License

private List<ExecutionDetail> parseExecutionsResult(final WebserviceResponse response) {
    final Document resultDoc = response.getResultDoc();

    final Node node = resultDoc.selectSingleNode("/result/executions");
    final List items = node.selectNodes("execution");
    final ArrayList<ExecutionDetail> list = new ArrayList<ExecutionDetail>();
    if (null != items && items.size() > 0) {
        for (final Object o : items) {
            final Node node1 = (Node) o;
            ExecutionDetailImpl detail = new ExecutionDetailImpl();
            String url = node1.selectSingleNode("@href").getStringValue();
            url = makeAbsoluteURL(url);//  w ww . j ava  2s . c  o m
            detail.setId(stringNodeValue(node1, "@id", null));
            try {
                detail.setStatus(
                        ExecutionState.valueOf(stringNodeValue(node1, "@status", "").replaceAll("-", "_")));
            } catch (IllegalArgumentException e) {
            }
            detail.setUrl(url);
            detail.setUser(stringNodeValue(node1, "user", null));
            detail.setAbortedBy(stringNodeValue(node1, "abortedBy", null));
            detail.setDescription(stringNodeValue(node1, "description", null));
            detail.setArgString(stringNodeValue(node1, "argString", null));
            detail.setDateStarted(w3cDateNodeValue(node1, "date-started", null));
            detail.setDateCompleted(w3cDateNodeValue(node1, "date-started", null));
            final Node jobNode = node1.selectSingleNode("job");
            if (null != jobNode) {
                final String jobId = stringNodeValue(jobNode, "@id", null);
                StoredJobExecutionImpl job = new StoredJobExecutionImpl(jobId,
                        stringNodeValue(jobNode, "name", null), createJobURL(jobId),
                        stringNodeValue(jobNode, "group", null), stringNodeValue(jobNode, "description", null),
                        stringNodeValue(jobNode, "project", null),
                        longNodeValue(jobNode, "@averageDuration", -1));
                detail.setExecutionJob(job);
            }
            list.add(detail);
        }
    }
    return list;
}

From source file:com.dtolabs.client.services.RundeckAPICentralDispatcher.java

License:Apache License

private ArrayList<QueuedItem> parseExecutionListResult(final WebserviceResponse response) {
    final Document resultDoc = response.getResultDoc();

    final Node node = resultDoc.selectSingleNode("/result/executions");
    final List items = node.selectNodes("execution");
    final ArrayList<QueuedItem> list = new ArrayList<QueuedItem>();
    if (null != items && items.size() > 0) {
        for (final Object o : items) {
            final Node node1 = (Node) o;
            final String id = node1.selectSingleNode("@id").getStringValue();
            final Node jobname = node1.selectSingleNode("job/name");
            final Node desc = node1.selectSingleNode("description");
            final String name;
            if (null != jobname) {
                name = jobname.getStringValue();
            } else {
                name = desc.getStringValue();
            }//from w  ww.  j  a  v  a2 s .  co  m
            String url = node1.selectSingleNode("@href").getStringValue();
            url = makeAbsoluteURL(url);
            logger.info("\t" + ": " + name + " [" + id + "] <" + url + ">");
            list.add(QueuedItemResultImpl.createQueuedItem(id, url, name));
        }
    }
    return list;
}

From source file:com.dtolabs.client.services.RundeckAPICentralDispatcher.java

License:Apache License

private int intAttribute(final Node node, int defval, final String attribute) {
    if (node.selectSingleNode(attribute) != null) {
        try {/*ww w .ja  v  a 2  s. c om*/
            return Integer.parseInt(node.selectSingleNode(attribute).getStringValue());
        } catch (NumberFormatException e) {

        }
    }
    return defval;
}

From source file:com.dtolabs.client.services.RundeckAPICentralDispatcher.java

License:Apache License

public DispatcherResult killDispatcherExecution(final String execId) throws CentralDispatcherException {
    final HashMap<String, String> params = new HashMap<String, String>();
    final HashMap<String, String> data = new HashMap<String, String>();
    //:( trigger POST correctly
    data.put("a", "a");

    final String rundeckApiKillJobPath = substitutePathVariable(RUNDECK_API_KILL_JOB_PATH, "id", execId);
    //2. send request via ServerService
    final WebserviceResponse response;
    try {/* ww  w .  java  2 s .c o  m*/
        response = serverService.makeRundeckRequest(rundeckApiKillJobPath, params, null, "POST", null, data,
                null);
    } catch (MalformedURLException e) {
        throw new CentralDispatcherServerRequestException("Failed to make request", e);
    }

    final Envelope envelope = validateResponse(response);

    final Node result1 = envelope.doc.selectSingleNode("result");
    final String abortStatus = result1.selectSingleNode("abort/@status").getStringValue();
    final boolean result = !"failed".equals(abortStatus);
    final StringBuffer sb = envelope.successMessages();
    return new DispatcherResult() {
        public boolean isSuccessful() {
            return result;
        }

        public String getMessage() {
            return sb.toString();
        }
    };
}

From source file:com.dtolabs.client.services.RundeckAPICentralDispatcher.java

License:Apache License

private String stringNodeValue(Node result1, final String path, final String defValue) {
    return null != result1.selectSingleNode(path) ? result1.selectSingleNode(path).getStringValue() : defValue;
}

From source file:com.dtolabs.client.services.RundeckAPICentralDispatcher.java

License:Apache License

private Boolean boolNodeValue(final Node result1, final String path, final Boolean defValue) {
    if (null != result1.selectSingleNode(path)) {
        return Boolean.parseBoolean(result1.selectSingleNode(path).getStringValue());
    }// w  w w  .  j  a v  a2s .  co m
    return defValue;
}