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:com.nokia.helium.antlint.checks.CheckScriptDef.java

License:Open Source License

/**
 * Check against the given node./* w  ww .  ja v a 2  s .c om*/
 * 
 * @param node
 *            is the node to check.
 */
@SuppressWarnings("unchecked")
public void checkScriptDef(String name, Node node) {
    List<Node> statements = node.selectNodes("//scriptdef[@name='" + name + "']/attribute");
    Pattern p1 = Pattern.compile("attributes.get\\([\"']([^\"']*)[\"']\\)");
    Matcher m1 = p1.matcher(node.getText());
    ArrayList<String> props = new ArrayList<String>();
    while (m1.find()) {
        props.add(m1.group(1));
    }

    if (!statements.isEmpty() && !props.isEmpty()) {
        for (Node statement : statements) {
            if (!props.contains(statement.valueOf("@name"))) {
                // for (String x : props)
                // log(x);
                log("Scriptdef " + name + " does not use " + statement.valueOf("@name"));
            }
        }
    }
}

From source file:com.nokia.helium.antlint.checks.CheckScriptDefNameAttributes.java

License:Open Source License

/**
 * Check against the given node.//ww w.  ja v  a 2 s  . c o  m
 * 
 * @param node
 *            is the node to check.
 */
@SuppressWarnings("unchecked")
public void checkScriptDefNameAttributes(String name, Node node) {
    List<Node> statements = node.selectNodes("//scriptdef[@name='" + name + "']/attribute");
    Pattern p1 = Pattern.compile("attributes.get\\([\"']([^\"']*)[\"']\\)");
    Matcher m1 = p1.matcher(node.getText());
    ArrayList<String> props = new ArrayList<String>();
    while (m1.find()) {
        props.add(m1.group(1));
    }

    ArrayList<String> attributes = new ArrayList<String>();
    for (Node statement : statements) {
        attributes.add(statement.valueOf("@name"));
    }
    for (String x : props) {
        if (!attributes.contains(x)) {
            log("Scriptdef " + name + " does not have attribute " + x);
        }
    }
}

From source file:com.nokia.helium.antlint.checks.CheckScriptDefStyle.java

License:Open Source License

/**
 * Check against the given node./*from www. j  a  v a2s . c o m*/
 * 
 * @param node
 *            is the node to check.
 */
@SuppressWarnings("unchecked")
public void checkScriptDefStyle(String name, Node node) {
    List<Node> statements = node.selectNodes("//scriptdef[@name='" + name + "']/attribute");
    Pattern p1 = Pattern.compile("attributes.get\\([\"']([^\"']*)[\"']\\)");
    Matcher m1 = p1.matcher(node.getText());
    ArrayList<String> props = new ArrayList<String>();
    while (m1.find()) {
        props.add(m1.group(1));
    }

    ArrayList<String> attributes = new ArrayList<String>();
    for (Node statement : statements) {
        attributes.add(statement.valueOf("@name"));
    }

    if (!statements.isEmpty() && props.isEmpty()) {
        log("Scriptdef " + name + " doesn't reference attributes directly, poor style");
    }
}

From source file:com.nokia.helium.antlint.checks.CheckUseOfIfInTargets.java

License:Open Source License

/**
 * Check against the given node./*from   w  ww .j a va2 s  .com*/
 * 
 * @param node
 *            is the node to check.
 */
@SuppressWarnings("unchecked")
private void checkUseOfIf(Element node) {
    String target = node.attributeValue("name");
    String targetxpath = "//target[@name='" + target + "']//if";

    List<Node> statements2 = node.selectNodes(targetxpath);
    for (Node statement : statements2) {
        List<Node> conditiontest = statement.selectNodes("./then/property");
        if (conditiontest != null && conditiontest.size() == 1) {
            List<Node> conditiontest2 = statement.selectNodes("./else/property");
            if (conditiontest2 != null && conditiontest2.size() == 1) {
                log("Target " + target + " poor use of if-else-property statement, use condition task");
            } else if (statement.selectNodes("./else").size() == 0) {
                log("Target " + target + " poor use of if-then-property statement, use condition task");
            }
        }
    }
    List<Node> statements = node.selectNodes("//target[@name='" + target + "']/*");
    if (!(statements.size() > 1)) {
        if (node.selectSingleNode(targetxpath + "/else") == null) {
            if (node.selectSingleNode(targetxpath + "/isset") != null
                    || node.selectSingleNode(targetxpath + "/not/isset") != null) {
                log("Target " + target + " poor use of if statement, use <target if|unless=\"prop\"");
            }
        }
    }
}

From source file:com.noterik.bart.fs.action.SearchAction.java

License:Open Source License

@Override
public String run() {
    // parse input
    Element element;// w w w.  j  a va2s.c  o m
    String eId;
    List<Element> iElements = script.getInput().getInputElements();
    for (Iterator<Element> iter = iElements.iterator(); iter.hasNext();) {
        element = iter.next();
        eId = element.attributeValue("id");
        if (eId != null && eId.equals("search")) {
            // parse properties
            Node pNode = element.selectSingleNode("properties");
            if (pNode != null) {
                List<Node> children = pNode.selectNodes("child::*");
                Node child;
                for (Iterator<Node> cIter = children.iterator(); cIter.hasNext();) {
                    child = cIter.next();
                    sProperties.put(child.getName(), child.getText());
                }
            }
        }
    }

    // check on uri
    String uri = sProperties.get("uri");
    if (uri == null) {
        logger.error("SearchAction: Required input parameters not specified");
        return null;
    }

    // build search uri
    String sUri = "", key, value;
    for (Iterator<String> iter = sProperties.keySet().iterator(); iter.hasNext();) {
        key = iter.next();
        if (!key.equals("uri")) {
            value = sProperties.get(key);
            sUri += "&" + key + "=" + value;
        }
    }
    sUri = sUri.replaceFirst("&", "?");

    // get service
    String domain = URIParser.getDomainFromUri(uri);
    Service service = ServiceHelper.getService(domain, "searchmanager");
    if (service == null) {
        logger.error("SearchAction: Service was null");
        return null;
    }

    // build final url
    String finalUrl = service.getUrl() + uri + sUri;

    logger.error("SearchAction: final url was " + finalUrl);

    // do request
    String response = HttpHelper.sendRequest("GET", finalUrl, null, null);

    // parse response
    String fsxml = parse2fsxml(response);

    logger.debug("result fsxml is " + fsxml);

    // delete old
    FSXMLRequestHandler.instance().handleDELETE(script.getID() + "/output/" + id + "/result", null);

    // save fsxml in output
    script.getOutput().setOutput(fsxml);

    return null;
}

From source file:com.noterik.bart.fs.fscommand.CopyCommand.java

License:Open Source License

/**
 * Copy resource specified by the input parameters.
 * //from  w w  w.  j  a va 2 s.c om
 * @param input      input parameters
 * @param uri      local directory
 * @return         status/error message
 */
private String copy(Properties input, String uri) {
    String res = null;

    // get input parameters
    String source = input.getProperty("source");
    String destination = input.getProperty("destination");
    String params = input.getProperty("params");
    logger.debug("source: " + source + ", destination: " + destination + ", params: " + params);
    //System.out.println("source: "+source+", destination: "+destination+", params: "+params);   

    // determine optional parameters
    boolean recursive = false, override = false;
    if (params != null) {
        recursive = params.contains("-r");
        override = params.contains("-o");
    }

    // check input parameters
    if (source == null || destination == null) {
        return FSXMLBuilder.getErrorMessage("500", "No source or destination",
                "Please provide the SOURCE and DESTINATION input parameters",
                "http://teamelements.noterik.com/team");
    }

    // resolve uris
    String sourceUri = URIParser.resolveLocalUri(source, uri);
    if (sourceUri.lastIndexOf("/") == sourceUri.length() - 1) {
        sourceUri = sourceUri.substring(0, sourceUri.lastIndexOf("/"));
    }
    String destinationUri = URIParser.resolveLocalUri(destination, uri);
    if (destinationUri.lastIndexOf("/") == destinationUri.length() - 1) {
        destinationUri = destinationUri.substring(0, destinationUri.lastIndexOf("/"));
    }
    logger.debug("ABSOLUTE URIS -- source: " + sourceUri + ", destination: " + destinationUri);

    // check if uri is an uri of type id
    String typeSource = URIParser.getResourceTypeFromUri(sourceUri);
    String cpDest = URIParser.getCurrentUriPart(destinationUri);
    if (!URIParser.isResourceId(sourceUri)) {
        return FSXMLBuilder.getErrorMessage("500", "Invalid source specified, only id nodes permitted.",
                "Invalid source specified, only id nodes permitted.", "http://teamelements.noterik.com/team");
    }

    String docStr = null;
    String refer = null;
    if (recursive) {
        // get properties of source   
        Document doc = getPropertiesOfUri(sourceUri, -1);
        logger.debug("document being created: " + doc.asXML());

        // exception for first node
        Element root = doc.getRootElement();
        Node first = root.selectSingleNode("//" + typeSource);
        refer = first.valueOf("@referid");
        List<Node> children = first.selectNodes("child::*");
        for (Iterator<Node> iter = children.iterator(); iter.hasNext();) {
            Node node = iter.next();
            docStr += node.asXML();
        }
        docStr = "<fsxml>" + docStr + "</fsxml>";
        logger.debug("document being created after first node exception: " + docStr);
    } else {
        // get properties of source   
        Document doc = getPropertiesOfUri(sourceUri, 0);
        logger.debug("document being created: " + doc.asXML());

        // exception for first node
        Element root = doc.getRootElement();
        Node first = root.selectSingleNode("//" + typeSource);
        Node properties = first.selectSingleNode("properties");
        refer = first.valueOf("@referid");
        docStr = "<fsxml>" + properties.asXML() + "</fsxml>";
        logger.debug("document being created after first node exception: " + docStr);
    }

    // check if dest ends with 'properties'
    String newResourceUri = null;
    if (cpDest.equals(FSXMLHelper.XML_PROPERTIES)) {
        logger.debug("putting to " + destinationUri);
        res = rHandler.handlePUT(destinationUri, docStr);
        newResourceUri = URIParser.getParentUri(destinationUri);
    } else if (URIParser.isResourceId(destinationUri)) {
        destinationUri += "/" + FSXMLHelper.XML_PROPERTIES;
        logger.debug("putting to " + destinationUri);
        res = rHandler.handlePUT(destinationUri, docStr);
        newResourceUri = URIParser.getParentUri(destinationUri);
    } else {
        logger.debug("posting to " + destinationUri);
        res = rHandler.handlePOST(destinationUri, docStr);
        try {
            if (FSXMLParser.getErrorMessageFromXml(res) == null) {
                Document respDoc = DocumentHelper.parseText(res);
                newResourceUri = respDoc.valueOf("//uri");
            }
        } catch (Exception e) {
            logger.error("", e);
        }
    }

    // add refer of first node
    if (refer != null && !refer.equals("")) {
        logger.debug("adding refer for first node (" + refer + ")");
        boolean ok = rHandler.saveAttributes(newResourceUri,
                "<fsxml><attributes><referid>" + refer + "</referid></attributes></fsxml>", "PUT");
        logger.debug("attributes added: " + ok);
    } else {
        logger.debug("not need for adding refer id");
    }

    logger.debug("response: " + res);
    return res;
}

From source file:com.noterik.bart.fs.fscommand.CopyCommand.java

License:Open Source License

/**
 * /*from   ww w  . j  av  a  2  s . c o  m*/
 * @param uri
 * @return
 */
private Document getPropertiesOfUri(String uri, int depth) {
    // refactor uri
    String cp = URIParser.getCurrentUriPart(uri);
    if (cp.equals(FSXMLHelper.XML_PROPERTIES)) {
        uri = URIParser.getParentUri(uri);
    }

    // get complete
    Document pDoc = null;
    if (depth == -1) {
        pDoc = rHandler.getNodeProperties(uri, true);
    } else {
        pDoc = rHandler.getNodeProperties(uri, depth, true);
    }

    // loop through xml and check referid's
    List<Node> rNodes = pDoc.selectNodes("//@referid");
    logger.debug("rNodes: " + rNodes);
    for (Iterator<Node> iter = rNodes.iterator(); iter.hasNext();) {
        // get referid attribute and parent node
        Node node = iter.next();
        String referid = node.getText();
        Element parent = node.getParent();
        logger.debug("parent: " + parent.asXML() + ", refer: " + referid);

        // get properties of referid
        Document rDoc = rHandler.getNodeProperties(referid, 0, false);
        logger.debug("rDoc: " + rDoc.asXML());
        Node properties = rDoc.selectSingleNode("//properties");
        List<Node> pNodes = properties.selectNodes("child::*");
        logger.debug("pNodes: " + pNodes);
        for (Iterator<Node> iter2 = pNodes.iterator(); iter2.hasNext();) {
            // select the same property elements that are in parent element and refer properties
            Node prop = iter2.next();
            List<Node> parentPNodes = parent.selectNodes("properties/child::*");
            for (Node parentPropNode : parentPNodes) {
                if (parentPropNode.getName().equals(prop.getName())
                        && parentPropNode.getText().equals(prop.getText())) {
                    logger.debug("removing: " + parentPropNode.asXML());
                    parentPropNode.detach();
                }
            }
        }
    }

    return pDoc;
}

From source file:com.noterik.bart.fs.fscommand.CreatePresentationFromBookmark.java

License:Open Source License

private String handlePresentation() {
    //get collection where to put presentation in
    Document config = XMLHelper.asDocument(xml);
    String collectionUri = config.selectSingleNode("//collection") == null ? ""
            : config.selectSingleNode("//collection").getText();

    if (collectionUri.equals("")) {
        logger.error("no collection supplied");
        return FSXMLBuilder.getErrorMessage("500", "No collection supplied", "Please provide a collection",
                "http://teamelements.noterik.com/team");
    }// ww  w.j  a  v a 2  s .c  o m

    domainid = URIParser.getDomainIdFromUri(collectionUri);
    user = URIParser.getUserIdFromUri(collectionUri);

    if (domainid.equals("")) {
        logger.error("got no domain from uri");
        return FSXMLBuilder.getErrorMessage("500", "No domain found", "Please provide a collection with domain",
                "http://teamelements.noterik.com/team");
    }
    if (user.equals("")) {
        logger.error("got no user from uri");
        return FSXMLBuilder.getErrorMessage("500", "No user found", "Please provide a collection with user",
                "http://teamelements.noterik.com/team");
    }

    //get collection presentation
    Document collectionPresentation = FSXMLRequestHandler.instance().getNodeProperties(uri, false);
    String presentationUri = collectionPresentation.selectSingleNode("//presentation/@referid") == null ? ""
            : collectionPresentation.selectSingleNode("//presentation/@referid").getText();

    if (presentationUri.equals("")) {
        logger.error("no presentation refered");
        return FSXMLBuilder.getErrorMessage("500", "No presentation refered", "",
                "http://teamelements.noterik.com/team");
    }

    Document presentation = FSXMLRequestHandler.instance().getNodeProperties(presentationUri, false);

    //remove statistics from original presentation
    Element statistics = (Element) presentation.selectSingleNode("//statistics");
    if (statistics != null) {
        statistics.detach();
    }

    StringBuffer presentationXml = new StringBuffer();

    // exception for first node
    Element root = presentation.getRootElement();
    Node first = root.selectSingleNode("//presentation");
    List<Node> children = first.selectNodes("child::*");
    for (Iterator<Node> iter = children.iterator(); iter.hasNext();) {
        Node node = iter.next();
        presentationXml.append(node.asXML());
    }
    String pXml = "<fsxml>" + presentationXml.toString() + "</fsxml>";
    String response = FSXMLRequestHandler.instance()
            .handlePOST("/domain/" + domainid + "/user/" + user + "/presentation", pXml);

    //response contains new presentation uri we have to refer to in collection
    Document resp = XMLHelper.asDocument(response);

    logger.debug("response = " + resp.asXML());
    String newPresentationUri = resp.selectSingleNode("//status[@id='400']/properties/uri") == null ? ""
            : resp.selectSingleNode("//status[@id='400']/properties/uri").getText();

    if (newPresentationUri.equals("")) {
        logger.error("could not create presentation");
        return FSXMLBuilder.getErrorMessage("500", "Could not create presentation", "Try again later",
                "http://teamelements.noterik.com/team");
    }

    //refer new presentation in the collection
    String response2 = FSXMLRequestHandler.instance().handlePOST(collectionUri + "/presentation",
            "<fsxml><attributes><referid>" + newPresentationUri + "</referid></attributes></fsxml>");

    Document resp2 = XMLHelper.asDocument(response2);
    logger.debug("response 2 = " + resp2.asXML());

    String newCollectionPresentationUri = resp2.selectSingleNode("//status[@id='400']/properties/uri") == null
            ? ""
            : resp2.selectSingleNode("//status[@id='400']/properties/uri").getText();

    if (newCollectionPresentationUri.equals("")) {
        logger.error("could not create refer presentation in collection");
        return FSXMLBuilder.getErrorMessage("500", "Could not create presentation in collection",
                "Try again later", "http://teamelements.noterik.com/team");
    }

    return resp2.asXML();
}

From source file:com.noterik.bart.fs.fscommand.dynamic.config.flash.java

License:Open Source License

private void addPlayer(Node configNode) {
    List<Node> players = configNode.selectNodes("//player");

    for (Iterator<Node> iter = players.iterator(); iter.hasNext();) {
        Element player = (Element) iter.next();

        String refer = player.selectSingleNode("@referid") == null ? ""
                : player.selectSingleNode("@referid").getText();
        if (!refer.equals("")) {
            Document playerXml = FSXMLRequestHandler.instance().getNodeProperties(refer, false);
            if (playerXml != null) {
                Element p = (Element) playerXml.selectSingleNode("fsxml/player").detach();
                p.addAttribute("fullid", refer);
                fsxml.add(p);//from ww w .ja v a  2  s.c om
            }
        }
    }
}

From source file:com.noterik.bart.fs.fscommand.dynamic.presentation.playout.flash.java

License:Open Source License

private static List<Element> addPlayer(Node configNode) {
    List<Node> players = configNode.selectNodes("//player");
    List<Element> result = new ArrayList<Element>();

    for (Iterator<Node> iter = players.iterator(); iter.hasNext();) {
        Element player = (Element) iter.next();

        String refer = player.selectSingleNode("@referid") == null ? ""
                : player.selectSingleNode("@referid").getText();
        if (!refer.equals("")) {
            Document playerXml = cache.get(refer);
            if (playerXml == null && !cache.isEmpty(refer)) {
                playerXml = FSXMLRequestHandler.instance().getNodeProperties(refer, false);
                // daniel
                cache.put(refer, playerXml);
            }// w w w .j a va 2s.com
            if (playerXml != null) {
                Element p = (Element) playerXml.selectSingleNode("fsxml/player").detach();
                p.addAttribute("fullid", refer);
                result.add(p);
            }
        }
    }
    return result;
}