Example usage for org.jdom2 Element getText

List of usage examples for org.jdom2 Element getText

Introduction

In this page you can find the example usage for org.jdom2 Element getText.

Prototype

public String getText() 

Source Link

Document

Returns the textual content directly held under this element as a string.

Usage

From source file:com.tactfactory.harmony.updater.impl.XmlAndroid.java

License:Open Source License

@Override
public String addElement(String key, String value) {
    String result = value;//from w ww  . j av  a 2s .c  om

    final Element newNode = new Element(XML_ELEMENT_STRING);

    // Add name to element
    newNode.setAttribute(XML_ELEMENT_NAME, key, this.namespace);

    // Set values
    newNode.setText(value);

    // If not found Node, create it
    if (!XMLUtils.addValue(newNode, XML_ELEMENT_NAME, rootNode)) {
        result = newNode.getText();
    }

    return result;
}

From source file:com.tactfactory.harmony.utils.XMLUtils.java

License:Open Source License

/**
 * Add a node to the XML node if it doesn't contains it already.
 * @param newNode The node to add// w  w  w .  j a  v a  2 s .  c om
 * @param id The attribute name to compare
 * @param baseNode The node to add the new node to.
 * @return True if the node was added successfully.
 * False if the node already exists before.
 */
public static boolean addValue(final Element newNode, final String id, final Element baseNode) {

    Element findElement = null;

    // Debug Log
    ConsoleUtils.displayDebug("Update String : " + newNode.getAttributeValue(id));

    findElement = findNode(baseNode, newNode, id);

    // If not found Node, create it
    if (findElement == null) {
        baseNode.addContent(newNode);
        return true;
    } else {
        newNode.setText(findElement.getText());
        return false;
    }
}

From source file:com.thoughtworks.go.config.GoConfigFieldWriter.java

License:Apache License

public void setValueIfNotNull(Element e, Object o) {
    configField.setAccessible(true);/*from   w  w  w. j ava 2 s.  c  o m*/
    if (isSubtag()) {
        setFieldIfNotNull(configField, o, parseSubtag(e, configField));
    } else if (isAttribute()) {
        setValueFromElementIfAppropriate(e, o);
        bombIfNullAndNotAllowed(configField, o, configField.getAnnotation(ConfigAttribute.class));
    } else if (isConfigValue()) {
        setFieldIfNotNull(configField, o, e.getText());
    }
}

From source file:com.thoughtworks.go.domain.materials.mercurial.HgModificationSplitter.java

License:Apache License

private List<File> parseFiles(Element filesElement, String fileType) {
    List files = filesElement.getChild(fileType).getChildren("file");
    List<File> modifiedFiles = new ArrayList<>();
    for (Iterator iterator = files.iterator(); iterator.hasNext();) {
        Element node = (Element) iterator.next();
        modifiedFiles.add(new File(org.apache.commons.lang3.StringEscapeUtils.unescapeXml(node.getText())));
    }/*from   ww w.  j a va  2 s.com*/
    return modifiedFiles;
}

From source file:com.thoughtworks.go.util.SvnLogXmlParser.java

License:Apache License

private Modification parseLogEntry(Element logEntry, String path) throws ParseException {
    Element logEntryPaths = logEntry.getChild("paths");
    if (logEntryPaths == null) {
        /* Path-based access control forbids us from learning
         * details of this log entry, so skip it. */
        return null;
    }/* w w  w . j  a v  a  2 s.c  om*/

    Date modifiedTime = convertDate(logEntry.getChildText("date"));
    String author = logEntry.getChildText("author");
    String comment = logEntry.getChildText("msg");
    String revision = logEntry.getAttributeValue("revision");

    Modification modification = new Modification(author, comment, null, modifiedTime, revision);

    List paths = logEntryPaths.getChildren("path");
    for (Iterator iterator = paths.iterator(); iterator.hasNext();) {
        Element node = (Element) iterator.next();
        if (underPath(path, node.getText())) {
            ModifiedAction action = convertAction(node.getAttributeValue("action"));
            modification.createModifiedFile(node.getText(), null, action);
        }
    }

    return modification;
}

From source file:com.versionmaintain.files.LastVersionInfosParser.java

License:Apache License

public List<VersionInfo> getVersionInfo() {
    SAXBuilder builder = new SAXBuilder();
    List<VersionInfo> infos = new ArrayList<VersionInfo>();
    try {//www . j  ava 2s.  c o m
        Document doc = builder.build(new File(System.getProperty("user.dir") + File.separator + FILE_NAME));
        Element root = doc.getRootElement();
        List<Element> softEles = root.getChildren("software");
        for (Element softEle : softEles) {
            String appName = softEle.getAttribute("name").getValue();
            String versionCode = softEle.getChildText("latest-version-code");
            String versionName = softEle.getChildText("latest-version");

            Element detailEles = softEle.getChild("latest-version-detail");
            List<Element> detailItemEles = detailEles.getChildren("item");
            List<VersionInfoDetail> details = new ArrayList<VersionInfoDetail>();
            for (Element detailItem : detailItemEles) {
                String title = detailItem.getAttributeValue("name");
                List<Element> detailEleList = detailItem.getChildren("detail");
                List<String> detailList = new ArrayList<String>();
                for (Element detailEle : detailEleList) {
                    String strDetail = detailEle.getText();
                    detailList.add(strDetail);
                }
                details.add(new VersionInfoDetail(title, detailList));
            }

            VersionInfo versionInfo = new VersionInfo();
            versionInfo.setAppName(appName);
            versionInfo.setVersion(versionName);
            versionInfo.setVersionCode(Integer.parseInt(versionCode));
            versionInfo.setDetails(details);
            infos.add(versionInfo);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return infos;
}

From source file:com.webfront.app.utils.PDFImporter.java

@Override
public void doImport(BufferedReader reader) throws IOException, ParseException {
    PDFTextStripper pdfStripper = new PDFTextStripper();
    BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(fileName));
    try (PDDocument document = PDDocument.load(inStream)) {
        txtOutput = pdfStripper.getText(document);
        try (FileWriter writer = new FileWriter(cfg.getTmpDir() + cfg.getFileSep() + "pdfOut.txt")) {
            writer.write(txtOutput);//from   www. ja va2  s . com
            writer.close();
            txtReader = new BufferedReader(new FileReader(cfg.getTmpDir() + cfg.getFileSep() + "pdfOut.txt"));
            String text = "";
            while (text != null) {
                text = txtReader.readLine();
                buffer.put(currentLine++, text);
            }
        }
        getConfig();
        currentLine = 0;
        Element root = xmlDoc.getRootElement();
        int maxLines = buffer.size() - 3;
        int markedLine = 0;
        // Scan the output and mark the start of each section
        for (Element el : root.getChildren()) {
            for (Element section : el.getChildren()) {
                String sectionName = section.getAttributeValue("content");
                Element startElement = section.getChild("start");
                Element endElement = section.getChild("end");
                if (startElement != null) {
                    boolean endHasBounds = true;
                    if (endElement.getAttribute("bounded") != null) {
                        String bounds = endElement.getAttributeValue("bounded");
                        if (bounds.equals("false")) {
                            endHasBounds = false;
                        }
                    }
                    Pattern linePattern = Pattern.compile(startElement.getText());
                    String text = "";
                    boolean elementFound = false;
                    while (currentLine < maxLines) {
                        text = buffer.get(currentLine++);
                        if (linePattern.matcher(text).matches()) {
                            markedLine = currentLine - 1;
                            markers.put(sectionName, markedLine);
                            elementFound = true;
                            if (!endHasBounds) {
                                currentLine--;
                            }
                            break;
                        }
                    }
                    if (!elementFound) {
                        currentLine = markedLine;
                    }
                }
            }
        }

        ArrayList<Integer> lineNumbers = new ArrayList<>(markers.values());
        lineNumbers.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        sectionMarks = new TreeSet(markers.values());
        currentLine = 0;
        for (Element element : root.getChildren()) {
            int lines = 0;
            if (element.getAttribute("lines") != null) {
                lines = element.getAttribute("lines").getIntValue();
            }
            for (Element section : element.getChildren()) {
                String contentDesc;
                contentDesc = (section.getAttribute("content") == null ? ""
                        : section.getAttributeValue("content"));
                if (markers.containsKey(contentDesc)) {
                    currentLine = markers.get(contentDesc);
                    processSection(section);
                }
            }
        }
    } catch (DataConversionException ex) {
        Logger.getLogger(PDFImporter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ElementNotFoundException ex) {
        Logger.getLogger(PDFImporter.class.getName()).log(Level.SEVERE, null, ex);
    }
    entries.sort(LedgerEntry.LedgerComparator);
    for (LedgerEntry item : entries) {
        java.util.Date date = new java.util.Date(DateConvertor.toLong(item.getDate(), "MM/dd/yyyy"));
        String amountString = item.getAmount();
        boolean isCredit = true;
        if (item.getAmount().startsWith("-")) {
            isCredit = false;
            String amt = item.getAmount().replaceFirst("-", "");
            item.setAmount(amt);
        }
        float amount = Float.parseFloat(amountString);
        if (isCredit) {
            lastBalance += amount;
            totalDeposits += amount;
        } else {
            lastBalance -= amount;
            totalWithdrawals += amount;
        }
        Ledger ledger = new Ledger(null, date, amount, lastBalance, accountId);
        if (item.getDescription().length() > 120) {
            item.setDescription(item.getDescription().substring(0, 119));
        }
        if (item.getCheckNumber() != null && !item.getCheckNumber().isEmpty()) {
            ledger.setCheckNum(item.getCheckNumber());
            totalChecks -= amount;
        }
        ledger.setTransDesc(item.getDescription());
        getItemList().add(ledger);
    }
}

From source file:com.webfront.app.utils.PDFImporter.java

public void processSection(Element section) throws ElementNotFoundException {
    String name = section.getName();
    boolean hasEntry = false;
    String contentDesc = (section.getAttribute("content") == null ? "" : section.getAttributeValue("content"));
    int lines = 1;
    int maxLines = 0;
    int linesProcessed = 0;
    boolean endHasBounds = true;
    Element startElement = section.getChild("start");
    Element endElement = section.getChild("end");
    Element dataDefinition = section.getChild("data");
    Element lineDefinition = section.getChild("line");
    Pattern dataPattern = null;//from   w  ww. ja v a  2 s  .c om
    Pattern linePattern = null;
    String transType = (section.getAttribute("type") == null ? "info" : section.getAttributeValue("type"));
    String prevLine = "";
    int nextSection = buffer.size() - 1;
    if (sectionMarks.higher(currentLine) != null) {
        nextSection = (int) sectionMarks.higher(currentLine);
    }

    if (startElement.getAttributeValue("lines") != null) {
        lines = Integer.parseInt(startElement.getAttributeValue("lines"));
        currentLine += lines;
    }
    if (lineDefinition != null) {
        linePattern = Pattern.compile(lineDefinition.getText());
        if (lineDefinition.getAttribute("lines") != null) {
            String l = lineDefinition.getAttributeValue("lines");
            if (!l.equals("+")) {
                maxLines = Integer.parseInt(lineDefinition.getAttributeValue("lines"));
            }
        }
    }
    if (endElement.getAttribute("bounded") != null) {
        String bounds = endElement.getAttributeValue("bounded");
        if (bounds.equals("false")) {
            endHasBounds = false;
        }
    }
    Pattern endPattern = Pattern.compile(endElement.getText());
    while (currentLine < nextSection) {
        prevLine = buffer.get(currentLine - 1);
        String text = buffer.get(currentLine++);
        Matcher lineMatcher = null;
        if (lineDefinition != null) {
            lineMatcher = linePattern.matcher(text);
        }
        Matcher endMatcher = endPattern.matcher(text);
        if (endMatcher.matches()) {
            if (!endHasBounds) {
                currentLine -= 1;
            }
            break;
        } else {
            if (currentLine >= buffer.size()) {
                throw new ElementNotFoundException("Not found");
            }
        }
        if (lineMatcher != null && lineMatcher.matches()) {
            if (!contentDesc.equals("discard")) {
                //                    System.out.println(text);
            }
            hasEntry = false;
            if (dataDefinition != null) {
                LedgerEntry entry = new LedgerEntry();
                for (Element dataLine : dataDefinition.getChildren()) {
                    String tag = dataLine.getAttributeValue("content");
                    String regex = dataLine.getText();
                    Matcher matcher = Pattern.compile(regex).matcher(text);
                    String value = "";
                    if (matcher.find()) {
                        value = matcher.group();
                    }
                    switch (tag) {
                    case "beginningBalance":
                        beginningBalance = Float.parseFloat(value.replaceAll(",", ""));
                        break;
                    case "totalDeposits":
                        value.replaceAll(",", "");
                        totalDeposits = Float.parseFloat(value.replaceAll(",", ""));
                        break;
                    case "totalWithdrawals":
                        value.replaceAll(",", "");
                        totalWithdrawals = Float.parseFloat(value.replaceAll(",", ""));
                        break;
                    case "endingBalance":
                        value.replaceAll(",", "");
                        endingBalance = Float.parseFloat(value.replaceAll(",", ""));
                        break;
                    case "accountNumber":
                        summary.put("accountNumber", value);
                        break;
                    case "periodFrom":
                        summary.put("startDate", value);
                        break;
                    case "periodTo":
                        summary.put("endDate", value);
                        break;
                    case "date":
                        entry.setDate(value);
                        text = matcher.replaceFirst("");
                        break;
                    case "check":
                        entry.setCheckNumber(value);
                        break;
                    case "amount":
                        if (transType.equals("debit")) {
                            value = "-" + value;
                        }
                        entry.setAmount(value);
                        text = matcher.replaceFirst("");
                        if (section.getAttributeValue("content").equals("fees")) {
                            String amt = entry.getAmount();
                            amt = amt.replaceAll("-", "");
                            amt = amt.replaceAll(",", "");
                            totalFees += Float.parseFloat(amt);
                        }
                        break;
                    case "description":
                        entry.setDescription(value);
                        break;
                    }
                }
                if (maxLines > 0 && ++linesProcessed == maxLines) {
                    return;
                }
                if (!contentDesc.equals("dailyBalance") && !contentDesc.endsWith("Info")) {
                    entries.add(entry);
                    hasEntry = true;
                }
            }
        } else {
            if (linePattern.matcher(prevLine).matches() && hasEntry) {
                if (!contentDesc.equals("dailyBalance")) {
                    int lastEntry = entries.size() - 1;
                    LedgerEntry entry = entries.get(lastEntry);
                    entry.setDescription(entry.getDescription() + " " + text);
                    entries.set(lastEntry, entry);
                    hasEntry = false;
                }
            }
        }

    }
}

From source file:com.webfront.app.utils.PDFImporter.java

private void getConfig() {
    jdomBuilder = new SAXBuilder();
    String xmlSource = cfg.getInstallDir() + cfg.getFileSep() + "pnc.xml";
    try {/*from  w  w w  .j  a v a2  s .com*/
        xmlDoc = jdomBuilder.build(xmlSource);
        Element root = xmlDoc.getRootElement();
        List<Content> configContent = root.getContent();
        List<Element> childElements = root.getChildren();
        Element header = root.getChild("header");
        for (Element e : header.getChildren()) {
            String content = e.getAttributeValue("content");
            String format = e.getAttributeValue("format");
            String text = e.getText();
        }
    } catch (JDOMException ex) {
        Logger.getLogger(PDFImporter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(PDFImporter.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.xebialabs.overcast.support.libvirt.JDomUtil.java

License:Apache License

public static String getElementText(Element parent, String localName, Namespace ns) {
    if (parent == null) {
        throw new IllegalArgumentException("parent element not found");
    }//from  w  w  w  .ja v a  2s.  com
    Element child = parent.getChild(localName, ns);
    if (child == null) {
        throw new IllegalArgumentException(String.format("child element '%s' not found", localName));
    }
    return child.getText();
}