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.noterik.bart.fs.fscommand.dynamic.presentation.playout.flash.java

License:Open Source License

private static List<Element> addVideos(Node presentationNode) {
    List<Node> videos = presentationNode.selectNodes("//videoplaylist/video");
    ArrayList<String> refers = new ArrayList<String>(videos.size());
    List<Element> vids = new ArrayList<Element>();

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

        String refer = video.selectSingleNode("@referid") == null ? ""
                : video.selectSingleNode("@referid").getText();
        logger.debug("getting video " + refer);
        if (!refer.equals("") && !refers.contains(refer)) {
            refers.add(refer);/*from  www  .ja  v a2s.  c om*/
            Document videoXml = cache.get(refer);
            if (videoXml == null) {
                videoXml = FSXMLRequestHandler.instance().getNodeProperties(refer, false);
                cache.put(refer, videoXml);
            }
            if (videoXml != null) {
                Element vid = (Element) videoXml.selectSingleNode("fsxml/video").detach();
                vid.addAttribute("fullid", refer);
                //System.out.println("U="+vid.asXML());
                vids.add(vid);
            }
        }
    }
    return vids;
}

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

License:Open Source License

private void addVideos(Node presentationNode) {
    List<Node> videos = presentationNode.selectNodes("//videoplaylist/video");

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

        String refer = video.selectSingleNode("@referid") == null ? ""
                : video.selectSingleNode("@referid").getText();
        if (refer != "") {
            Document videoXml = FSXMLRequestHandler.instance().getNodeProperties(refer, false);
            if (videoXml != null) {
                Node vid = videoXml.selectSingleNode("fsxml/video").detach();
                fsxml.add(vid);/*from w ww .ja  va  2 s  . c o  m*/
            }
        }
    }
}

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

License:Open Source License

/**
 * Move resource specified by the input parameters.
 * /*w  w w.  j  a v a 2  s.c o m*/
 * @param input      input parameters
 * @param uri      local directory
 * @return         status/error message
 */
private String move(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);

    // determine optional parameters
    boolean override = false;
    if (params != null) {
        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;

    // 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);

    // 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");
    }

    // check if copy was successful and remove original
    String vError = FSXMLParser.getErrorMessageFromXml(res);
    if (vError != null) {
        logger.error("copy before moving was unsuccessful.");
    } else {
        logger.debug("copy before moving was successful.");
        FSXMLRequestHandler.instance().deleteNodeProperties(sourceUri, true);
    }

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

From source file:com.weibo.datasys.crawler.base.factory.TaskFactory.java

License:Open Source License

/**
 * //  w  ww. j a  va2 s  . c om
 * ?
 * 
 * @param task
 * @param doc
 */
@SuppressWarnings("unchecked")
private static void buildParseStrategy(Task task, Document doc) throws Exception {
    ParseStrategy parseStrategy = new ParseStrategy(task);
    task.setParseStrategy(parseStrategy);
    // Field???
    List<Node> fieldRuleNodes = doc.selectNodes("//Config/ParseStrategy/FieldRule");
    for (Node fieldRuleNode : fieldRuleNodes) {
        FieldRule fieldRule = new FieldRule(task);
        fieldRule.configWithParameters(getParaMap(fieldRuleNode));
        parseStrategy.addFieldRule(fieldRule);
        // ???
        Node contentRuleNode = fieldRuleNode.selectSingleNode("contentExtractRule");
        String contentRuleClass = contentRuleNode.selectSingleNode("@class").getText().trim();
        AbstractContentExtractRule contentRule = (AbstractContentExtractRule) Class.forName(contentRuleClass)
                .getConstructor(Task.class).newInstance(task);
        contentRule.configWithParameters(getParaMap(contentRuleNode));
        fieldRule.setContentRule(contentRule);
        // ?s
        List<Node> processRuleNodes = fieldRuleNode.selectNodes("processRule");
        for (Node processRuleNode : processRuleNodes) {
            String processRuleClass = processRuleNode.selectSingleNode("@class").getText().trim();
            AbstractProcessRule processRule = (AbstractProcessRule) Class.forName(processRuleClass)
                    .getConstructor(Task.class).newInstance(task);
            processRule.configWithParameters(getParaMap(processRuleNode));
            fieldRule.addProcessRule(processRule);
        }
        // s
        List<Node> filterNodes = fieldRuleNode.selectNodes("filterRule");
        for (Node filterNode : filterNodes) {
            // ???
            String filterClass = filterNode.selectSingleNode("@class").getText().trim();
            AbstractFilterRule filterRule = (AbstractFilterRule) Class.forName(filterClass)
                    .getConstructor(Task.class).newInstance(task);
            // ???
            filterRule.configWithParameters(getParaMap(filterNode));
            fieldRule.addFilter(filterRule);
        }
    }
}

From source file:com.weibo.datasys.crawler.base.factory.TaskFactory.java

License:Open Source License

/**
 * //  ww  w .j a  v a 2s  .c  o  m
 * ?
 * 
 * @param task
 * @param doc
 * @throws Exception
 */
@SuppressWarnings("unchecked")
private static void buildCrawlStrategy(Task task, Document doc) throws Exception {
    CrawlStrategy crawlStrategy = new CrawlStrategy(task);
    task.setCrawlStrategy(crawlStrategy);
    // 
    String priorityString = doc.selectSingleNode("//Config/CrawlStrategy/priority").getText().trim();
    crawlStrategy.setPriority(StringUtils.parseInt(priorityString, 0));
    // ?
    String siteEncoding = doc.selectSingleNode("//Config/CrawlStrategy/siteEncoding").getText().trim();
    crawlStrategy.setSiteEncoding(siteEncoding);
    // ?
    String maxCrawlDepthString = doc.selectSingleNode("//Config/CrawlStrategy/maxCrawlDepth").getText().trim();
    int maxCrawlDepth = StringUtils.parseInt(maxCrawlDepthString, Integer.MAX_VALUE);
    if (maxCrawlDepth <= 0) {
        maxCrawlDepth = Integer.MAX_VALUE;
    }
    crawlStrategy.setMaxCrawlDepth(maxCrawlDepth);
    // ???
    Node seedRuleNode = doc.selectSingleNode("//Config/CrawlStrategy/seedGenerateRule");
    String seedRuleClass = seedRuleNode.selectSingleNode("@class").getText().trim();
    // ???
    AbstractSeedGenerateRule seedRule = (AbstractSeedGenerateRule) Class.forName(seedRuleClass)
            .getConstructor(Task.class).newInstance(task);
    seedRule.configWithParameters(getParaMap(seedRuleNode));
    crawlStrategy.setSeedRule(seedRule);
    // ?
    ConcurrentControlRule concurrentRule = new ConcurrentControlRule(task);
    crawlStrategy.setConcurrentRule(concurrentRule);
    Node controlRuleNode = doc.selectSingleNode("//Config/CrawlStrategy/concurrentControlRule");
    concurrentRule.configWithParameters(getParaMap(controlRuleNode));
    // ?
    List<Node> crawlRuleNodes = doc.selectNodes("//Config/CrawlStrategy/crawlRule");
    for (Node crawlRuleNode : crawlRuleNodes) {
        CrawlRule crawlRule = new CrawlRule(task);
        // ???
        crawlRule.configWithParameters(getParaMap(crawlRuleNode));
        // 
        crawlStrategy.addCrawlRule(crawlRule);
        // url???
        List<Node> urlExtractNodes = crawlRuleNode.selectNodes("urlExtractRule");
        for (Node urlExtractNode : urlExtractNodes) {
            UrlExtractRule urlExtractRule = new UrlExtractRule(task);
            // ???
            urlExtractRule.configWithParameters(getParaMap(urlExtractNode));
            crawlRule.addUrlExtractRule(urlExtractRule);
            // ?s
            List<Node> processNodes = urlExtractNode.selectNodes("processRule");
            for (Node processNode : processNodes) {
                // ???
                String processClass = processNode.selectSingleNode("@class").getText().trim();
                AbstractProcessRule processRule = (AbstractProcessRule) Class.forName(processClass)
                        .getConstructor(Task.class).newInstance(task);
                // ???
                processRule.configWithParameters(getParaMap(processNode));
                urlExtractRule.addProcessRule(processRule);
            }
            // s
            List<Node> filterNodes = urlExtractNode.selectNodes("filterRule");
            for (Node filterNode : filterNodes) {
                // ???
                String filterClass = filterNode.selectSingleNode("@class").getText().trim();
                AbstractFilterRule filterRule = (AbstractFilterRule) Class.forName(filterClass)
                        .getConstructor(Task.class).newInstance(task);
                // ???
                filterRule.configWithParameters(getParaMap(filterNode));
                urlExtractRule.addFilter(filterRule);
            }
        }
        // http?
        Node httpParaNode = crawlRuleNode.selectSingleNode("httpRequest");
        Map<String, String> paraMap = getParaMap(httpParaNode);
        crawlRule.setHttpReqParameters(paraMap);
    }
}

From source file:com.weibo.datasys.crawler.base.factory.TaskFactory.java

License:Open Source License

/**
 * /*from   ww  w  .  j  av  a 2  s. com*/
 * ???node??Map
 * 
 * @param node
 * @return
 */
@SuppressWarnings("unchecked")
private static Map<String, String> getParaMap(Node node) {
    Map<String, String> paraMap = new LinkedHashMap<String, String>();
    if (node != null) {
        List<Node> paraNodes = node.selectNodes("parameter");
        for (Node paraNode : paraNodes) {
            String key = paraNode.selectSingleNode("@key").getText().trim();
            String value = paraNode.getText().trim();
            Node valueNode = paraNode.selectSingleNode("@value");
            if (valueNode != null) {
                value = paraNode.selectSingleNode("@value").getText().trim();
            }
            paraMap.put(key, value);
        }
    }
    return paraMap;
}

From source file:com.weibo.datasys.crawler.impl.strategy.rule.parse.XpathMatchRule.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/* w ww . ja  v  a 2s  .  c  om*/
public List<CommonData> apply(CommonData in) {
    List<CommonData> fields = new ArrayList<CommonData>();
    String xml = in.getBaseField("src");
    try {
        Document doc = DocumentHelper.parseText(xml);
        List<Node> baseNodes = doc.selectNodes(baseNodeXpath);
        for (int i = 0; i < baseNodes.size(); i++) {
            Node baseNode = baseNodes.get(i);
            CommonData fieldData = new CommonData();
            // ??xpathField
            for (Entry<String, String> entry : attrXpathMap.entrySet()) {
                String attr = entry.getKey();
                String xpath = entry.getValue();
                // ?xml?
                List<Node> valueNodes = baseNode.selectNodes(xpath);
                StringBuilder builder = new StringBuilder();
                for (Node valueNode : valueNodes) {
                    String tmpValue = valueNode.getText();
                    if (!StringUtils.isEmptyString(tmpValue)) {
                        builder.append(tmpValue).append(MULTI_VALUE_SPLITER);
                    }
                }
                if (builder.length() > 0) {
                    builder.setLength(builder.length() - 1);
                }
                String value = builder.toString();
                if ("id".equalsIgnoreCase(attr)) {
                    fieldData.setId(value);
                } else {
                    fieldData.setBaseField(attr, value);
                }
            }
            // Field?
            fields.add(fieldData);
            if (!isMultiMatch) {
                // ??????
                break;
            }
        } // end of for each baseNode
    } catch (Exception e) {
        logger.error("[XpathMatchRuleError] - ", e);
    }
    return fields;
}

From source file:com.xebia.mojo.dashboard.reports.html.TaglistDashboardReport.java

License:Apache License

/** {@inheritDoc} */
protected Node postProcess(MavenProject subProject, Node contentNode, int column)
        throws MojoExecutionException {
    List rows = contentNode.selectNodes("tr");
    int count = 0;

    for (Iterator it = rows.iterator(); it.hasNext();) {
        Node row = (Element) it.next();
        List cells = row.selectNodes("td");

        if (!cells.isEmpty() && (cells.size() > 1)) {
            String text = ((Element) cells.get(1)).getText();
            count += Integer.parseInt(text);
        }/*  w w  w .ja  v  a  2s  . com*/
    }

    return DocumentHelper.createText(String.valueOf(count));
}

From source file:com.xebia.mojo.dashboard.util.TidyXmlUtil.java

License:Apache License

/** {@inheritDoc} */
public List findNodes(Node node, String xpath) throws MojoExecutionException {
    return node.selectNodes(xpath);
}

From source file:core.ComponentXMLData.java

License:Open Source License

/**
 * Constructor of this class. Basically, it loads up all the data from the
 * given XML file and puts them in the appropriate variables.
 * /*from  w  w w. j a  v a  2 s.  c  o  m*/
 * @param ctype
 *            The type of this component.
 * @param xmlfile
 *            The XML file used to load up data into this class.
 */
public ComponentXMLData(ComponentType ctype, File xmlfile) {
    // monitor if loading encounters errors or not
    loadedSuccessfully = true;

    // set the type of this component
    this.componentType = ctype;

    // sets the source of this class
    setSourceXMLFile(xmlfile);

    // load the XML file
    Document doc = SafeSAX.read(xmlfile, true);

    try {
        // Load configuration section
        Node nd = doc.selectSingleNode("/icomponent/config");
        if (nd == null) {
            System.out.println("No config tag found - Excluding component at ");
            // configuration tag is required
            loadedSuccessfully = false;

        } else {
            List<?> npops = nd.selectNodes(".//entry");
            Iterator<?> it = npops.iterator();

            while (it.hasNext()) {
                Element el = (Element) it.next();
                String key = el.valueOf("./@key");
                String value = el.valueOf("./@value");

                if (key.equals("classdir")) {
                    // get root class directory
                    setClassDir(value);
                } else if (key.equals("classname")) {
                    // get root class name
                    setClassName(value);
                } else if (key.equals("name")) {
                    // get component name
                    setName(value);
                } else if (key.equals("description")) {
                    // get description
                    setDescription(value);
                } else if (key.equals("author")) {
                    // get author
                    setAuthor(value);
                } else if (key.equals("version")) {
                    // get version
                    setVersion(value);
                } else if (key.equals("image")) {
                    // get reference image path
                    setImageLocation(value);
                } else if (key.equals("tags")) {
                    // read all tags
                    ArrayList<String> keywordlist = new ArrayList<String>();
                    Scanner sc = new Scanner(value);
                    sc.useDelimiter(",");
                    while (sc.hasNext())
                        keywordlist.add(sc.next());
                    setKeywords(keywordlist);
                    sc.close();
                } else if (key.equals("isBulkRepresentation")) {
                    if (Boolean.parseBoolean(value)) {
                        this.componentType = ComponentType.FREVO_BULKREPRESENTATION;
                    }
                }
            }

            // Load class file
            String classdir = getClassDir();

            String dir = FrevoMain.getInstallDirectory() + File.separator + "Components" + File.separator
                    + FrevoMain.getComponentTypeAsString(ctype) + "s" + File.separator + classdir
                    + File.separator;

            try {
                // Create a new class loader with the directory
                ComponentClassLoader cl = new ComponentClassLoader(dir);

                // Load in the class
                Class<?> loadedclass = cl.loadClass(getClassName(), true);

                setComponentClass(loadedclass.asSubclass(AbstractComponent.class));

            } catch (ClassNotFoundException e) {
                System.err.println("Class not found: " + getClassName());
                // root class is required
                loadedSuccessfully = false;
            } catch (Exception e) {
                // for anything else
                System.err.println("Class unavailable to load: " + getName());
                loadedSuccessfully = false;
            }
        }
        // Load properties section
        Node nd2 = doc.selectSingleNode("/icomponent/properties");
        if (nd2 == null) {
            System.out.println("No properties tag found for component " + getName());

        } else {
            List<?> npops = nd2.selectNodes(".//propentry");
            Iterator<?> it = npops.iterator();

            // create hashmap for the properties
            Hashtable<String, XMLFieldEntry> proptable = new Hashtable<String, XMLFieldEntry>();

            while (it.hasNext()) {
                Element el = (Element) it.next();
                String key = el.valueOf("./@key");
                XMLFieldType type = XMLFieldType.valueOf(el.valueOf("./@type"));
                String value = el.valueOf("./@value");

                String description = el.valueOf("./@description");

                if (!FrevoMain.checkType(type, value))
                    throw new IllegalArgumentException(
                            "Value \"" + value + "\" for Key \"" + key + "\" is not of type " + type + "!");

                if (type.equals(XMLFieldType.ENUM)) {
                    String enumName = el.valueOf("./@enumName");
                    proptable.put(key, new XMLFieldEntry(value, type, enumName, description));
                } else {
                    proptable.put(key, new XMLFieldEntry(value, type, description));
                }

            }
            // Add hashmap to the object
            setProperties(proptable);
        }

    } catch (OutOfMemoryError mem) {
        System.err.println("Could not import! (Out of memory)");
        loadedSuccessfully = false;
    } catch (IllegalArgumentException e) {
        System.out.println("IllegalArgumentException\n" + e.getMessage());
        loadedSuccessfully = false;
    }

}