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.google.code.pentahoflashcharts.charts.pfcxml.LineChartBuilder.java

License:Open Source License

protected void setupElements(Chart c, Node root, IPentahoResultSet data) {
    Style lineStyle = setupLineStyle(root);
    int columnCount = data.getMetaData().getColumnCount();
    LineChart[] elements = null;/*ww  w. j  av  a  2 s.co  m*/
    if (columnCount > 1) {
        elements = new LineChart[columnCount - 1];
        int rowCount = data.getRowCount();
        List colors = root.selectNodes("/chart/color-palette/color");
        for (int i = 1; i <= columnCount - 1; i++) {
            LineChart e = new LineChart(lineStyle);
            Number[] datas = new Number[rowCount];

            for (int j = 0; j < rowCount; j++) {
                datas[j] = (Number) data.getValueAt(j, i);
                e.addValues(datas[j].doubleValue());
            }
            String colour;
            if (colors != null && colors.size() > 1) {
                colour = ((Node) colors.get(i - 1)).getText().trim();
                e.setColour(colour);
            }
            e.setText((String) data.getMetaData().getColumnHeaders()[0][i]);
            setLink(e, root, "/chart/link");
            setOnClick(e, root, "/chart/on-click");
            elements[i - 1] = e;
        }
        setupXAxisLabels(data, c, rowCount);

    } else {
        elements = new LineChart[columnCount];
        LineChart e = new LineChart(setupLineStyle(root));

        Node chartBackGround = root.selectSingleNode("/chart/chart-background");
        if (chartBackGround != null) {

            e.setColour(chartBackGround.getText());
        }
        int rowCount = data.getRowCount();
        Number[] datas = new Number[rowCount];
        for (int i = 0; i < rowCount; i++) {
            datas[i] = (Number) data.getValueAt(i, 0);
        }
        e.addValues(datas);
        elements[0] = e;
        setLink(e, root, "/chart/link");
        setOnClick(e, root, "/chart/on-click");
    }
    c.addElements(elements);
}

From source file:com.google.code.pentahoflashcharts.charts.pfcxml.ScatterChartBuilder.java

License:Open Source License

private void setupColors(Node root, ScatterChart se, int i) {
    if (getValue(root.selectSingleNode("/chart/color-palette")) != null) {
        List colors = root.selectNodes("/chart/color-palette/color");
        String colour = getNodeValue((Node) colors.get(i));
        se.setColour(colour);/*from   ww w  .j av  a  2  s .  co m*/
    } else
        se.setColour("#86BBEF");
}

From source file:com.google.code.pentahoflashcharts.charts.pfcxml.SketchBarChartBuilder.java

License:Open Source License

protected void setupElements(Chart c, Node root, IPentahoResultSet data) {
    BarChart[] values = null;//from  w  w  w . j  a v  a2 s .c  om
    int rowCount = data.getRowCount();
    List bars = root.selectNodes("/chart/bars/bar");
    int barNum = bars.size();
    values = new BarChart[barNum];
    int functor = 5;
    for (int i = 0; i < barNum; i++) {
        Node bar = (Node) bars.get(i);

        Node textNode = bar.selectSingleNode("text");
        Node colorNode = bar.selectSingleNode("color");
        Node outlineColorNode = bar.selectSingleNode("outlineColor");
        String color = "";
        String outlineColor = "";
        if (colorNode != null && colorNode.getText().length() > 0) {
            color = colorNode.getText().trim();
        }
        if (outlineColorNode != null && outlineColorNode.getText().length() > 0) {
            outlineColor = outlineColorNode.getText().trim();
        }
        BarChart e = new SketchBarChart(color, outlineColor, functor);
        setBarchartData(data, rowCount, bar, colorNode, textNode, e);
        setOnClick(e, root, "/chart/bars/bar/on-click");
        setLink(e, root, "/chart/bars/bar/link");
        values[i] = e;
    }
    c.addElements(values);
}

From source file:com.google.code.pentahoflashcharts.charts.pfcxml.StackedBarChartBuilder.java

License:Open Source License

private void setupStacks(Chart c, Node root, IPentahoResultSet data, StackedBarChart sbc) {

    int rowCount = data.getRowCount();
    int columnCount = data.getColumnCount();
    List colors = root.selectNodes("/chart/color-palette/color");
    for (int i = 1; i < columnCount; i++) {
        StackValue[] datas = new StackValue[rowCount];
        for (int k = 0; k < rowCount; k++) {
            Node colorNode = (Node) colors.get(k);
            datas[k] = new StackValue((Number) data.getValueAt(k, i), getNodeValue(colorNode));
        }//w  ww  . j  a  v a 2  s  .c o  m
        sbc.newStack().addStackValues(datas);
    }
}

From source file:com.google.code.pentahoflashcharts.charts.pfcxml.StackedBarChartBuilder.java

License:Open Source License

private void setupKeys(Chart c, Node root, IPentahoResultSet data, StackedBarChart sbc) {
    int rowCount = data.getRowCount();
    List colors = root.selectNodes("/chart/color-palette/color");
    for (int j = 0; j < rowCount; j++) {
        Object obj = data.getValueAt(j, 0);
        String text = "" + obj;
        Integer fontsize = 14;/*from   w  w w  .  ja va  2 s.  co  m*/
        Node colorNode = (Node) colors.get(j);
        String colour = getNodeValue(colorNode);
        StackedBarChart.Key key1 = new StackedBarChart.Key(colour, text, fontsize);

        sbc.addKeys(key1);
    }

}

From source file:com.googlecode.fascinator.redbox.sru.NLAIdentity.java

License:Open Source License

private List<Map<String, String>> getSourceIdentities() {
    List<Map<String, String>> returnList = new ArrayList<Map<String, String>>();

    // Top level institution
    Map<String, String> idMap = new HashMap<String, String>();
    Node institutionNode = eac.selectSingleNode("eac:eac-cpf/eac:control/eac:maintenanceAgency/eac:agencyName");
    String institutionString = institutionNode.getText();
    // Top level name
    Node nlaNamesNode = eac.selectSingleNode("eac:eac-cpf/eac:cpfDescription/eac:identity");
    // Get all the names this ID lists
    List<Map<String, String>> nameList = getNames(nlaNamesNode);
    for (Map<String, String> name : nameList) {
        // Only use the longest top-level name for display purposes
        String oldDisplayName = idMap.get("displayName");
        String thisDisplayName = name.get("displayName");
        if (oldDisplayName == null
                || (thisDisplayName != null && thisDisplayName.length() > oldDisplayName.length())) {
            // Clear any old data
            idMap.clear();/*from ww w  .  j  a  v  a 2s.c o  m*/
            // Store this ID
            idMap.putAll(name);
            idMap.put("institution", institutionString);
        }
    }
    // And add to the list
    returnList.add(idMap);

    // All name entities from contributing insitutions
    @SuppressWarnings("unchecked")
    List<Node> sourceIdentities = eac.selectNodes("eac:eac-cpf/eac:cpfDescription//eac:eac-cpf");
    for (Node identity : sourceIdentities) {
        // Insitution for this ID
        institutionNode = identity.selectSingleNode("*//eac:maintenanceAgency/eac:agencyName");
        institutionString = institutionNode.getText();

        // Any names for this ID
        @SuppressWarnings("unchecked")
        List<Node> idNodes = identity.selectNodes("*//eac:identity");
        for (Node idNode : idNodes) {
            // A Map for each name
            idMap = new HashMap<String, String>();
            // Get all the names this ID lists
            nameList = getNames(idNode);
            for (Map<String, String> name : nameList) {
                idMap.putAll(name);
            }
            // Indicate the insitution for each one
            idMap.put("institution", institutionString);
            // And add to the list
            returnList.add(idMap);
        }
    }

    // Debugging
    //for (Map<String, String> id : returnList) {
    //    String display = id.get("displayName") + " (" + id.get("institution") + ")";
    //    log.debug("Identity: {}", display);
    //}

    return returnList;
}

From source file:com.googlecode.fascinator.redbox.sru.NLAIdentity.java

License:Open Source License

private List<Map<String, String>> getNames(Node node) {
    List<Map<String, String>> nameList = new ArrayList<Map<String, String>>();

    // Any names for this ID
    @SuppressWarnings("unchecked")
    List<Node> names = node.selectNodes("eac:nameEntry");
    for (Node name : names) {
        Map<String, String> nameMap = new HashMap<String, String>();

        String thisDisplay = null;
        String thisFirstName = null;
        String thisSurname = null;
        String title = null;/*from  w  w w.j a v  a2  s  . c  o  m*/

        // First name
        Node firstNameNode = name
                .selectSingleNode("eac:part[(@localType=\"forename\") or (@localType=\"givenname\")]");
        if (firstNameNode != null) {
            thisFirstName = firstNameNode.getText();
        }

        // Surname
        Node surnameNode = name
                .selectSingleNode("eac:part[(@localType=\"surname\") or (@localType=\"familyname\")]");
        if (surnameNode != null) {
            thisSurname = surnameNode.getText();
        }

        // Title
        Node titleNode = name.selectSingleNode("eac:part[@localType=\"title\"]");
        if (titleNode != null) {
            title = titleNode.getText();
        }

        // Display Name
        if (thisSurname != null) {
            thisDisplay = thisSurname;
            nameMap.put("surname", thisSurname);
            if (thisFirstName != null) {
                thisDisplay += ", " + thisFirstName;
                nameMap.put("firstName", thisFirstName);
            }
            if (title != null) {
                thisDisplay += " (" + title + ")";
            }
            nameMap.put("displayName", thisDisplay);
        }

        // Last ditch effort... we couldn't find simple name information from
        //  recommended values. So just concatenate what we can see.
        if (thisDisplay == null) {
            // Find every part
            @SuppressWarnings("unchecked")
            List<Node> parts = name.selectNodes("eac:part");
            for (Node part : parts) {
                // Grab the value and type of this value
                Element element = (Element) part;
                String value = element.getText();
                String type = element.attributeValue("localType");
                // Build a display value for this part
                if (type != null) {
                    value += " (" + type + ")";
                }
                // And add to the display name
                if (thisDisplay == null) {
                    thisDisplay = value;
                } else {
                    thisDisplay += ", " + value;
                }
            }
            nameMap.put("displayName", thisDisplay);
        }

        nameList.add(nameMap);
    }

    return nameList;
}

From source file:com.googlecode.fascinator.redbox.sru.SRUClient.java

License:Open Source License

/**
 * <p>/*from ww  w . j av  a  2  s  .c o m*/
 * Search for a record from the National Library of Australia with the provided identifier. If multiple records match this identifier only the
 * first will be returned.
 * </p>
 * 
 * @param id The identifier to search for
 * @return String The record matching this identifier. Null if not found
 */
public String nlaGetNationalId(String id) {
    Node node = nlaGetRecordNodeById(id);

    if (node == null) {
        return null;
    }

    @SuppressWarnings("unchecked")
    List<Node> otherIds = node.selectNodes("eac:control/eac:otherRecordId");
    for (Node idNode : otherIds) {
        String otherId = idNode.getText();
        if (otherId.startsWith("http://nla.gov.au")) {
            return otherId;
        }
    }

    return null;
}

From source file:com.ming800.core.p.service.impl.JmenuManagerImpl.java

/**
 * ??xmlJmenu jmenuMap//from w  ww .ja  v  a  2  s  .c o m
 */
private static Menu initJmenuMap(Document infoDocument) {
    Menu menu = new Menu();
    //        Document infoDocument = ResourcesUtil.getDocument(xmlPath);
    if (infoDocument != null) {

        List<Node> menuNodeList = infoDocument.selectNodes("menu");
        String name = menuNodeList.get(0).selectSingleNode("@name").getText();

        HashMap<String, Jmenu> jmenuMap = new HashMap<>();
        List<Node> jmenuNodeList = infoDocument.selectNodes("menu/jmenu");
        for (Node jMenuXmlNode : jmenuNodeList) {
            Jmenu jmenu = new Jmenu();
            jmenu.setChildren(new ArrayList<Jnode>());
            String id = jMenuXmlNode.selectSingleNode("@id").getText();
            jmenu.setId(id);
            List<Node> firstLayerList = jMenuXmlNode.selectNodes("jnode");
            for (Node firstLayerXmlNode : firstLayerList) {
                Jnode firstLayerJnode = JmenuManagerImpl.parseXmlNodeToJavaBean(firstLayerXmlNode);
                firstLayerJnode.setChildren(new ArrayList<Jnode>());
                List<Node> secondLayerList = firstLayerXmlNode.selectNodes("jnode");
                for (Node secondLayerXmlNode : secondLayerList) {
                    Jnode secondLayerJnode = JmenuManagerImpl.parseXmlNodeToJavaBean(secondLayerXmlNode);
                    firstLayerJnode.getChildren().add(secondLayerJnode);
                }
                jmenu.getChildren().add(firstLayerJnode);
            }
            jmenuMap.put(jmenu.getId(), jmenu);
        }

        menu.setName(name);
        menu.setJmenuHashMap(jmenuMap);
    }

    return menu;
}

From source file:com.mothsoft.alexis.engine.numeric.President2012DataSetImporter.java

License:Apache License

@SuppressWarnings("unchecked")
private Map<Date, PollResults> getPage(final String after, final int pageNumber) {
    final Map<Date, PollResults> pageMap = new LinkedHashMap<Date, PollResults>();

    HttpClientResponse httpResponse = null;

    try {//w  w  w.ja  v a 2 s .c  o m
        final URL url = new URL(String.format(BASE_URL, after, pageNumber));
        httpResponse = NetworkingUtil.get(url, null, null);

        final SAXReader saxReader = new SAXReader();

        org.dom4j.Document document;
        try {
            document = saxReader.read(httpResponse.getInputStream());
        } catch (DocumentException e) {
            throw new IOException(e);
        } finally {
            httpResponse.close();
        }

        final SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);

        final List<Node> questions = document.selectNodes(XPATH_QUESTIONS);
        for (final Node question : questions) {
            final Date endDate;
            final Node poll = question.selectSingleNode(ANCESTOR_POLL);
            final Node endDateNode = poll.selectSingleNode(END_DATE);
            try {
                endDate = format.parse(endDateNode.getText());
                logger.debug(String.format("%s: %s", END_DATE, format.format(endDate)));
            } catch (final ParseException e) {
                throw new RuntimeException(e);
            }

            final List<Node> responses = question.selectNodes(XPATH_RESPONSE_FROM_QUESTION);
            for (final Node response : responses) {
                final Node choiceNode = response.selectSingleNode(CHOICE);
                final String choice = choiceNode.getText();

                if (President2012DataSetImporter.CANDIDATE_OPTIONS.contains(choice)) {
                    final Node valueNode = response.selectSingleNode(VALUE);
                    final Double value = Double.valueOf(valueNode.getText());
                    append(pageMap, endDate, choice, value);
                }
            }
        }

        httpResponse.close();
        httpResponse = null;

    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (httpResponse != null) {
            httpResponse.close();
        }
    }

    return pageMap;
}