Example usage for org.dom4j Node getText

List of usage examples for org.dom4j Node getText

Introduction

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

Prototype

String getText();

Source Link

Document

Returns the text of this node.

Usage

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

License:Open Source License

protected void setStartAngle(Node root, PieChart e) {
    Node startAngleNode = root.selectSingleNode("/chart/start-angle");
    if (startAngleNode != null && startAngleNode.getText().length() > 0) {
        e.setStartAngle(Integer.parseInt(startAngleNode.getText().trim()));
    } else/*from www  .j  a  v a2s. com*/
        e.setStartAngle(35);
}

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 ww  w.  j a v a  2s.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.gdt.handler.impl.ExcelxHandler.java

License:Open Source License

/**
 * /*from  ww w  .jav a  2s.c o m*/
 * @param inputFile
 * @throws IOException
 * @throws InvalidFormatException
 */
@Override
public void handle(String inputFile, ProgressLevel pLevel) throws IOException, InvalidFormatException {
    String outPutFile = getOuputFileName(inputFile);
    ZipInputStream zis = new ZipInputStream(new FileInputStream(inputFile));
    ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(outPutFile));
    ZipEntry zipEntry;
    int wordCount = 0;
    int pBarUpdate = 0;
    while ((zipEntry = zis.getNextEntry()) != null) {
        if (zipEntry.getName().equals("xl/sharedStrings.xml")) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[BUFFER];
            int len;
            while ((len = zis.read(buffer)) > 0) {
                baos.write(buffer, 0, len);
            }
            baos.flush();

            InputStream clone = new ByteArrayInputStream(baos.toByteArray());
            SAXReader saxReader = new SAXReader();
            Document doc = null;
            Map<String, String> nameSpaceMap = new HashMap<String, String>();
            nameSpaceMap.put("xmlns", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
            try {
                XPath xPath = new Dom4jXPath("//xmlns:t");
                xPath.setNamespaceContext(new SimpleNamespaceContext(nameSpaceMap));
                doc = saxReader.read(clone);
                List<Node> nodes = xPath.selectNodes(doc);
                wordCount = nodes.size();
                pLevel.setValue(0);
                pLevel.setMaxValue(wordCount);
                pLevel.setStringPainted(true);
                pLevel.setTrFileName(outPutFile);
                pBarUpdate = 0;
                for (Node node : nodes) {
                    pBarUpdate++;
                    pLevel.setValue(pBarUpdate);
                    if (isInterrupted) {
                        zis.close();
                        zout.close();
                        new File(outPutFile).delete();
                        pLevel.setString("cancelled");
                        return;
                    }
                    String inputText = "";
                    try {
                        inputText = node.getText();
                        String translatedTxt = inputText;
                        translatedTxt = translator.translate(inputText);
                        node.setText(translatedTxt);
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, "Translation fails for the inputText : " + inputText, e);
                    }
                }
            } catch (DocumentException e) {
                logger.log(Level.SEVERE, "cannot parse slide", e);
            } catch (JaxenException e) {
                e.printStackTrace();
            }

            zout.putNextEntry(new ZipEntry(zipEntry.getName()));
            byte data[] = doc.asXML().getBytes("UTF8");
            zout.write(data, 0, data.length);
        } else {
            zout.putNextEntry(new ZipEntry(zipEntry.getName()));
            int len;
            byte data[] = new byte[BUFFER];
            while ((len = zis.read(data, 0, BUFFER)) != -1) {
                zout.write(data, 0, len);
            }
        }
    }
    zis.close();
    zout.close();
    pLevel.setValue(wordCount);
    pLevel.setString("done");
}

From source file:com.google.gdt.handler.impl.PowerpointxHandler.java

License:Open Source License

/**
 * /*  w  w w . j  a v  a2s.  c o m*/
 * @param inputFile
 * @param pLevel
 * @throws IOException
 * @throws InvalidFormatException
 */
@Override
public void handle(String inputFile, ProgressLevel pLevel) throws IOException, InvalidFormatException {
    String outPutFile = getOuputFileName(inputFile);
    ZipInputStream zis = new ZipInputStream(new FileInputStream(inputFile));
    ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(outPutFile));

    int slideCount = getSlideCount(inputFile);
    pLevel.setValue(0);
    pLevel.setMaxValue(slideCount);
    pLevel.setStringPainted(true);
    pLevel.setTrFileName(outPutFile);
    ZipEntry zipEntry;
    int pBarUpdate = 0;
    while ((zipEntry = zis.getNextEntry()) != null) {
        if (zipEntry.getName().contains("ppt/slides/slide")) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[BUFFER];
            int len;
            while ((len = zis.read(buffer)) > 0) {
                baos.write(buffer, 0, len);
            }
            baos.flush();

            InputStream clone = new ByteArrayInputStream(baos.toByteArray());
            SAXReader saxReader = new SAXReader();
            Document doc = null;
            try {
                doc = saxReader.read(clone);
                List<Node> nodes = doc.selectNodes("//a:t");
                for (Node node : nodes) {
                    if (isInterrupted) {
                        zis.close();
                        zout.close();
                        new File(outPutFile).delete();
                        pLevel.setString("cancelled");
                        return;
                    }
                    String inputText = "";
                    try {
                        inputText = node.getText();
                        if ((null == inputText) || (inputText.trim().equals("")))
                            continue;
                        String translatedText = translator.translate(inputText);
                        node.setText(translatedText);
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, "Translation fails for the inputText : " + inputText, e);
                    }
                }
            } catch (DocumentException e) {
                logger.log(Level.SEVERE, "cannot parse slide", e);
            }

            zout.putNextEntry(new ZipEntry(zipEntry.getName()));
            byte data[] = doc.asXML().getBytes("UTF8");
            zout.write(data, 0, data.length);
            pBarUpdate++;
            pLevel.setValue(pBarUpdate);
        } else {
            zout.putNextEntry(new ZipEntry(zipEntry.getName()));
            int len;
            byte data[] = new byte[BUFFER];
            while ((len = zis.read(data, 0, BUFFER)) != -1) {
                zout.write(data, 0, len);
            }
        }
    }
    zis.close();
    zout.close();
    pLevel.setValue(slideCount);
    pLevel.setString("done");
}

From source file:com.google.gdt.handler.impl.WordxHandler.java

License:Open Source License

/**
 * //  w  w w  .  ja  v a 2  s . c  om
 * @param inputFile
 * @param pLevel
 * @throws IOException
 * @throws InvalidFormatException
 */
@Override
public void handle(String inputFile, ProgressLevel pLevel) throws IOException, InvalidFormatException {
    String outPutFile = getOuputFileName(inputFile);
    ZipInputStream zis = new ZipInputStream(new FileInputStream(inputFile));
    ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(outPutFile));
    ZipEntry zipEntry;
    int wordCount = 0;
    int pBarUpdate = 0;
    while ((zipEntry = zis.getNextEntry()) != null) {
        if (zipEntry.getName().equals("word/document.xml")) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[BUFFER];
            int len;
            while ((len = zis.read(buffer)) > 0) {
                baos.write(buffer, 0, len);
            }
            baos.flush();

            InputStream clone = new ByteArrayInputStream(baos.toByteArray());
            SAXReader saxReader = new SAXReader();
            Document doc = null;
            try {
                doc = saxReader.read(clone);
                List<Node> nodes = doc.selectNodes("//w:t");
                wordCount = nodes.size();
                pLevel.setValue(0);
                pLevel.setMaxValue(wordCount);
                pLevel.setStringPainted(true);
                pLevel.setTrFileName(outPutFile);
                pBarUpdate = 0;
                for (Node node : nodes) {
                    if (isInterrupted) {
                        zis.close();
                        zout.close();
                        new File(outPutFile).delete();
                        pLevel.setString("cancelled");
                        return;
                    }
                    String inputText = "";
                    try {
                        inputText = node.getText();
                        pBarUpdate++;
                        pLevel.setValue(pBarUpdate);
                        if ((null == inputText) || (inputText.trim().equals("")))
                            continue;
                        String translatedText = translator.translate(inputText);
                        node.setText(translatedText);
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, "Translation fails for the inputText : " + inputText, e);
                    }
                }
            } catch (DocumentException e) {
                logger.log(Level.SEVERE, "cannot parse slide", e);
            }

            zout.putNextEntry(new ZipEntry(zipEntry.getName()));
            byte data[] = doc.asXML().getBytes("UTF8");
            zout.write(data, 0, data.length);
        } else {
            zout.putNextEntry(new ZipEntry(zipEntry.getName()));
            int len;
            byte data[] = new byte[BUFFER];
            while ((len = zis.read(data, 0, BUFFER)) != -1) {
                zout.write(data, 0, len);
            }
        }
    }
    zis.close();
    zout.close();
    pLevel.setValue(wordCount);
    pLevel.setString("done");
}

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

License:Open Source License

/**
 * <p>Default Constructor. Extract some basic information.</p>
 * //from   www  . ja  v  a2 s. c o m
 * @param searchResponse A parsed DOM4J Document
 * @throws SRUException If any of the XML structure does not look like expected
 */
public NLAIdentity(Node node) throws SRUException {
    eac = node;

    // Identity
    @SuppressWarnings("unchecked")
    List<Node> otherIds = eac.selectNodes("eac:eac-cpf/eac:control/eac:otherRecordId");
    for (Node idNode : otherIds) {
        String otherId = idNode.getText();
        if (otherId.startsWith("http://nla.gov.au")) {
            nlaId = otherId;
        }
    }
    if (nlaId == null) {
        throw new SRUException("Error processing Identity; Cannot find ID");
    }

    knownIds = getSourceIdentities();

    // Cosmetically we want to use the first row (should be the longest top-level name we found)
    firstName = knownIds.get(0).get("firstName");
    surname = knownIds.get(0).get("surname");
    displayName = knownIds.get(0).get("displayName");
    // For institution we want the first one we find that isn't NLA or Libraries Australia
    for (Map<String, String> id : knownIds) {
        if (institution == null
                // But we'll settle for those in a pinch
                || "National Library of Australia Party Infrastructure".equals(institution)
                || "Libraries Australia".equals(institution)) {
            institution = id.get("institution");
        }
    }
}

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  w w  w.  java  2s. c  om
            // 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;/*  w  w  w.  j av a 2  s . c om*/

        // 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>/*w w  w .ja  va2s .com*/
 * 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 nlaGetRecordById(String id) {
    Node node = nlaGetRecordNodeById(id);

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

    if ("xml".equals(responsePacking)) {
        return node.asXML();
    } else {
        return node.getText();
    }
}

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

License:Open Source License

/**
 * <p>//from ww  w . j a v  a2  s  . co  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;
}