Example usage for org.jsoup.select Elements hasAttr

List of usage examples for org.jsoup.select Elements hasAttr

Introduction

In this page you can find the example usage for org.jsoup.select Elements hasAttr.

Prototype

public boolean hasAttr(String attributeKey) 

Source Link

Document

Checks if any of the matched elements have this attribute defined.

Usage

From source file:io.sightly.tck.html.HTMLExtractor.java

/**
 * Checks if an element matched by the {@code selector} contains or not the attribute {@code attributeName},
 * depending on the value of the {@code exists} flag. Additionally, the attribute's value can be checked against {@code attributeValue}.
 *
 * @param url            the url that identifies the markup
 * @param markup         the markup//from w w  w . j a  va2 s  .co m
 * @param selector       the selector used for retrieval
 * @param exists         flag that defines if the attribute is expected to exist or not
 * @param attributeName  the attribute's name
 * @param attributeValue the attribute's value
 * @return {@code true} if the attribute matches the defined conditions, {@code false} otherwise
 */
public static boolean hasAttribute(String url, String markup, String selector, boolean exists,
        String attributeName, String attributeValue) {
    ensureMarkup(url, markup);
    Document document = documents.get(url);
    Elements elements = document.select(selector);
    if (elements.size() > 0) {
        if (exists) {
            if (StringUtils.isNotEmpty(attributeValue)) {
                String value = elements.attr(attributeName);
                return attributeValue.equals(value);
            }
            return true;
        } else {
            return elements.hasAttr(attributeName);
        }
    }
    return false;
}

From source file:org.asqatasun.rules.elementchecker.attribute.IdUnicityChecker.java

/**
 * This methods checks whether a given id is unique for a set of 
 * elements//  ww w.java2 s .  c  om
 * 
 * @param sspHandler
 * @param elements
 * @param testSolutionHandler
 */
private void checkIdUnicity(SSPHandler sspHandler, Elements elements, TestSolutionHandler testSolutionHandler) {

    if (elements.isEmpty() || !elements.hasAttr(ID_ATTR)) {
        testSolutionHandler.addTestSolution(TestSolution.NOT_APPLICABLE);
        return;
    }

    TestSolution testSolution = getSuccessSolution();

    for (Element el : elements) {
        if (el.hasAttr(ID_ATTR) && StringUtils.isNotBlank(el.id())
                && getIdPresenceCounter(sspHandler, el.id()) > 1) {
            testSolution = getFailureSolution();
            addSourceCodeRemark(getFailureSolution(), el, getFailureMsgCode());
        }
    }

    testSolutionHandler.addTestSolution(testSolution);
}

From source file:org.opens.tanaguru.rules.elementchecker.attribute.IdUnicityChecker.java

/**
 * This methods checks whether a given id is unique for a set of 
 * elements//from w w  w  .jav  a  2 s. c o  m
 * 
 * @param sspHandler
 * @param elements
 * @param testSolutionHandler
 */
private void checkIdUnicity(SSPHandler sspHandler, Elements elements, TestSolutionHandler testSolutionHandler) {

    if (elements.isEmpty() || !elements.hasAttr(ID_ATTR)) {
        testSolutionHandler.addTestSolution(TestSolution.NOT_APPLICABLE);
        return;
    }

    TestSolution testSolution = getSuccessSolution();

    for (Element el : elements) {
        if (el.hasAttr(ID_ATTR) && StringUtils.isNotBlank(el.id())
                && getIdPresenceCounter(sspHandler, el.id()) > 1) {
            testSolution = getFailureSolution();
            if (StringUtils.isNotBlank(messageCodeOnIdNotUnique)) {

                addSourceCodeRemark(getFailureSolution(), el, messageCodeOnIdNotUnique);
            }
        }
    }

    testSolutionHandler.addTestSolution(testSolution);

}