Example usage for org.jdom2 Element getDescendants

List of usage examples for org.jdom2 Element getDescendants

Introduction

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

Prototype

@Override
public <F extends Content> IteratorIterable<F> getDescendants(final Filter<F> filter) 

Source Link

Document

Returns an iterator that walks over all descendants in document order applying the Filter to return only content that match the filter rule.

Usage

From source file:appmain.AppMain.java

private List<String> readXMLData(File xml) {
    try {//w  w w  .j  a  va  2  s. c om
        List<String> transactions = new ArrayList<>();

        Document xmlDoc = jdomBuilder.build(xml);
        Element report = xmlDoc.getRootElement();

        ElementFilter filter = new ElementFilter("G_TR");
        Iterator<Element> c = report.getDescendants(filter);
        while (c.hasNext()) {
            Element e = c.next();
            transactions.add(processTransactionElement(e));
        }
        return transactions;
    } catch (JDOMException | IOException ex) {
        JOptionPane.showMessageDialog(null, "Hiba az XML fjl feldolgozsa kzben!\n"
                + ExceptionUtils.getStackTrace(ex) + "Fjl: " + xml.getAbsolutePath(), "Hiba",
                JOptionPane.ERROR_MESSAGE);
    }
    return null;
}

From source file:com.c4om.utils.xmlutils.XPathUtils.java

License:Apache License

/**
 * It returns a path to a single element. The element (and its ancestors) is distinguished via its 
 * attributes set./*from  w  ww .j a va  2  s. c o  m*/
 * @param currentElement the element whose path is to be calculated
 * @param contextElement the element to which a relative path will be calculated (null for an absolute path).
 * @return the path.
 * @throws IllegalArgumentException if the currentElement is not descendant of the contextElement
 * 
 */
public static String generateAttributeBasedPath(Element currentElement, Element contextElement) {
    StringBuilder builder = new StringBuilder();
    Element parent = currentElement.getParentElement();
    if (contextElement != null) {
        IteratorIterable<Element> descendants = contextElement.getDescendants(Filters.element());
        Set<Element> descendantsSet = ImmutableSet.copyOf((Iterable<Element>) descendants);
        if (!descendantsSet.contains(currentElement)) {
            throw new IllegalArgumentException("The provided element is not descendant of the stopElement");
        }
    }
    if (parent != contextElement) {
        String pathToParent = generateAttributeBasedPath(parent, contextElement);
        builder.append(pathToParent);
    } else if (contextElement != null) {
        builder.insert(0, ".");
    }
    builder.append("/");
    builder.append(currentElement.getQualifiedName());
    String filter = "[" + generateAttributesFilter(currentElement.getAttributes()) + "]";
    builder.append(filter);
    String result = builder.toString();
    return result;
}

From source file:com.musala.cron.RefreshDbTask.java

License:Apache License

@Scheduled(fixedRate = 300000)
@Transactional/*from w  ww. j av a  2s .c o  m*/
public void printMe() {
    logger.info("Cron task is started");

    for (Site rssFeedSite : siteService.findAll()) {
        logger.info("--------------> Reading information for site: " + rssFeedSite.getRssLink());
        SAXBuilder builder = new SAXBuilder();
        Document doc = null;

        try {
            doc = builder.build(new URL(rssFeedSite.getRssLink()));
            if (rssFeedSite.getLastVisitDateTag() != null) {
                Element root = doc.getRootElement();
                ElementFilter filter = new ElementFilter(rssFeedSite.getLastVisitDateTag());

                String currentLastVisitedDate = null;

                for (Element c : root.getDescendants(filter)) {
                    currentLastVisitedDate = c.getTextNormalize();
                }

                if (currentLastVisitedDate != null
                        && currentLastVisitedDate.equals(rssFeedSite.getLastVisitDate())) {
                    logger.info("--------------> Rss {} is not changed", rssFeedSite.getRssLink());
                } else {
                    logger.info("--------------> Rss {} is changed", rssFeedSite.getRssLink());
                    rssFeedSite.setLastVisitDate(currentLastVisitedDate);
                    subject.processRss(rssFeedSite);
                }
            }
        } catch (JDOMException | IOException e) {
            logger.warn("Error occurred during reading of rss", e);
        }
    }
}

From source file:com.seleniumtests.util.squashta.TaScriptGenerator.java

License:Apache License

/**
 * Read a test element in an XML testNG file
 * @param test         the test element to read
 * @param testDefs      list of test definitions to update
 * @param testngFile   testNgFile read/*w  ww . ja va  2 s. c o m*/
 */
private void readTestTag(Element test, List<SquashTaTestDef> testDefs, File testngFile) {
    boolean cucumberTest = false;
    String cucumberNamedTest = "";
    boolean exclude = false;

    // search cucumber parameters among test parameters
    // does test specifies precise cucumber properties (cucumberTests / cucumberTags)
    for (Element param : test.getChildren("parameter")) {
        if ("cucumberTests".equals(param.getAttributeValue("name"))
                || "cucumberTags".equals(param.getAttributeValue("name"))) {
            cucumberTest = true;
            cucumberNamedTest = param.getAttributeValue("value");

            if (!cucumberNamedTest.isEmpty()) {
                break;
            }
        }
    }

    for (Element param : test.getChildren("parameter")) {
        if (XML_EXCLUDE.equals(param.getAttributeValue("name"))) {
            exclude = true;
        }
    }

    // is this test a cucumber test ? (calling specific runner)
    for (Element pack : test.getDescendants(new ElementFilter("package"))) {
        if (pack.getAttributeValue("name").contains("com.seleniumtests.core.runner")) {
            cucumberTest = true;
        }
    }

    if (!exclude) {
        if (cucumberTest) {
            testDefs.add(
                    new SquashTaTestDef(testngFile, test.getAttributeValue("name"), true, cucumberNamedTest));
        } else {
            testDefs.add(new SquashTaTestDef(testngFile, test.getAttributeValue("name"), false, ""));
        }
    }
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.OperatorNormalizer.java

License:Apache License

private void normalizeUnicode(final Element ancestor, final Normalizer.Form form) {
    assert ancestor != null && form != null;
    final List<Text> texts = new ArrayList<Text>();
    final ContentFilter textFilter = new ContentFilter(ContentFilter.TEXT);
    for (Content text : ancestor.getContent(textFilter)) {
        texts.add((Text) text);//from w w  w  . ja  v  a 2s  .c o m
    }
    for (Element element : ancestor.getDescendants(new ElementFilter())) {
        for (Content text : element.getContent(textFilter)) {
            texts.add((Text) text);
        }
    }
    for (Text text : texts) {
        if (Normalizer.isNormalized(text.getText(), form)) {
            continue;
        }
        final String normalizedString = Normalizer.normalize(text.getText(), form);
        LOGGER.log(Level.FINE, "Text ''{0}'' normalized to ''{1}''",
                new Object[] { text.getText(), normalizedString });
        text.setText(normalizedString);
        assert Normalizer.isNormalized(text.getText(), form);
    }
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.OperatorNormalizer.java

License:Apache License

private void replaceOperators(final Element element, final Map<String, String> replacements) {
    assert element != null && replacements != null;
    List<Element> operatorsToReplace = new ArrayList<Element>();
    for (Element operator : element.getDescendants(new ElementFilter(OPERATOR))) {
        if (replacements.containsKey(operator.getTextTrim())) {
            operatorsToReplace.add(operator);
        }/*from   w  ww  .j  a  va  2s  .  c  om*/
    }
    for (Element operator : operatorsToReplace) {
        final String oldOperator = operator.getTextTrim();
        final String newOperator = replacements.get(oldOperator);
        operator.setText(newOperator);
        LOGGER.log(Level.FINE, "Operator ''{0}'' was replaced by ''{1}''",
                new Object[] { oldOperator, newOperator });
    }
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.OperatorNormalizer.java

License:Apache License

private void replaceIdentifiers(final Element ancestor, final Set<String> operators) {
    assert ancestor != null && operators != null;
    final List<Element> toReplace = new ArrayList<Element>();
    for (Element element : ancestor.getDescendants(new ElementFilter(IDENTIFIER))) {
        // TODO: control whole ranges of symbols rather than listed ones
        if (operators.contains(element.getTextTrim())) {
            toReplace.add(element);/*  w ww  . j a  va 2s. c om*/
        }
    }
    for (Element element : toReplace) {
        LOGGER.log(Level.FINE, "Creating an operator from {0}", element.getText());
        replaceElement(element, OPERATOR);
    }
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.ScriptNormalizer.java

License:Apache License

private void replaceDescendants(final Element ancestor, final Map<String, String> map) {
    assert ancestor != null && map != null;
    final List<Element> toReplace = new ArrayList<Element>();
    for (Element element : ancestor.getDescendants(new ElementFilter())) {
        if (map.containsKey(element.getName())) {
            toReplace.add(element);//from  w ww.j av a 2s .com
        }
    }
    for (Element element : toReplace) {
        replaceElement(element, map.get(element.getName()));
    }
}

From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java

License:Apache License

/**
 * This method sets a given namespace to the given element and any of its descendants whose 
 * name is a provided one and have no namespace.
 * @param name the name of the searched elements
 * @param rootElement the root element to begin the search at
 * @param namespace the namespace to set
 *//*from  ww  w  .ja  v  a 2 s.  co m*/
private void setNamespaceToElementsByName(String name, Element rootElement, Namespace namespace) {
    IteratorIterable<Element> descendants = rootElement
            .getDescendants(Filters.element(name, Namespace.NO_NAMESPACE));
    for (Element descendant : descendants) {
        descendant.setNamespace(namespace);
    }
    if (rootElement.getName().equals(name) && rootElement.getNamespace().equals(Namespace.NO_NAMESPACE)) {
        rootElement.setNamespace(namespace);
    }
}

From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.JSONTypesExtractorImpl.java

License:Apache License

/**
 * This method undoes what addPrefixToJSONKeysEndingsRecursive method did at the original JSON: It looks 
 * for elements whose name ends at a desired key ending preceeded by a prefix to remove and removes it.
 * @param rootElement the root element./*  ww w .  j a  v  a2 s.  co m*/
 * @param desiredKeyEnding the desired ending.
 * @param prefixToRemove the prefix to remove.
 */
private void removePrefixToElementNameEndings(Element rootElement, String desiredKeyEnding,
        String prefixToRemove) {
    String keyToSearch = prefixToRemove + desiredKeyEnding;
    for (Element element : rootElement.getDescendants(Filters.element())) {
        if (!(element.getName().endsWith(keyToSearch)
                && element.getNamespace().equals(Namespace.NO_NAMESPACE))) {
            continue;
        }
        String name = element.getName();
        String newName = name.replaceAll(Pattern.quote(keyToSearch) + "$", desiredKeyEnding);
        element.setName(newName);
    }
}