Example usage for org.jsoup.nodes Element hasAttr

List of usage examples for org.jsoup.nodes Element hasAttr

Introduction

In this page you can find the example usage for org.jsoup.nodes Element hasAttr.

Prototype

public boolean hasAttr(String attributeKey) 

Source Link

Document

Test if this element has an attribute.

Usage

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/*www .j  a v  a 2s . com*/
 * 
 * @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);

}

From source file:org.opens.tanaguru.rules.elementchecker.helper.RuleCheckHelper.java

/**
 * This methods checks whether a given attribute is present for a set of 
 * elements. These elements may be out of scope, and a pre-result is 
 * computed./* w  ww  . j  a v  a  2  s  .  c  o  m*/
 * 
 * @param elements
 * @param attributeName
 * @param messageCode
 * @param eeAttributeName
 * @return 
 */
public static TestSolution checkAttributePresenceWithPreComputedResult(Elements elements, String attributeName,
        String messageCode, String eeAttributeName) {
    for (Element el : elements) {
        Collection<EvidenceElement> eeList = new ArrayList<EvidenceElement>();
        if (!el.hasAttr(attributeName)) {
            eeList.add(prs.getEvidenceElement(TEST_RESULT_EVIDENCE_ELEMENT, TestSolution.FAILED.name()));
            addEvidenceElementToCollection(eeList, el, eeAttributeName);
            prs.addSourceCodeRemarkOnElement(TestSolution.NEED_MORE_INFO, el, messageCode, eeList);
        } else {
            eeList.add(prs.getEvidenceElement(TEST_RESULT_EVIDENCE_ELEMENT, TestSolution.PASSED.name()));
            addEvidenceElementToCollection(eeList, el, eeAttributeName);
            prs.addSourceCodeRemarkOnElement(TestSolution.NEED_MORE_INFO, el, messageCode, eeList);
        }
    }
    return TestSolution.NEED_MORE_INFO;
}

From source file:org.opens.tanaguru.rules.elementchecker.helper.RuleCheckHelper.java

/**
 * /*from   ww  w.java  2s  .  c om*/
 * @param element
 * @param attributeName
 * @param isExternalLink
 * @return 
 */
private static String buildAttributeValue(Element element, String attributeName, boolean isExternalResource) {
    if (!element.hasAttr(attributeName)) {
        return ABSENT_ATTRIBUTE_VALUE;
    } else if (isExternalResource && !element.attr("abs:" + attributeName).isEmpty()) {
        return element.absUrl(attributeName);
    } else {
        return element.attr(attributeName);
    }
}

From source file:org.opens.tanaguru.rules.elementselector.LinkElementSelector.java

/**
 * //from   w w  w  . j av  a 2 s. co  m
 * @param linkElement
 * @param linkText
 * @return whether the current link have a context
 */
protected boolean doesLinkHaveContext(Element linkElement, String linkText) {
    // does the current link have a title attribute? 
    if (considerTitleAsContext && linkElement.hasAttr(TITLE_ATTR)
            && !StringUtils.equals(linkElement.attr(TITLE_ATTR), linkText)) {
        return true;
    }
    // does the parent of the current link have some text?
    if (StringUtils.isNotBlank(linkElement.parent().ownText())) {
        return true;
    }
    // does the current element have a previous sibling of heading type?
    if (isOneOfPrecedingSiblingofHeadingType(linkElement)) {
        return true;
    }
    // does one of the parent of the current element have a previous sibling 
    // of heading type or is found in the PARENT_CONTEXT_ELEMENTS list?
    for (Element parent : linkElement.parents()) {
        if (PARENT_CONTEXT_ELEMENTS.contains(parent.tagName())
                || isOneOfPrecedingSiblingofHeadingType(parent)) {
            return true;
        }
    }
    return false;
}

From source file:org.opens.tanaguru.rules.rgaa22.Rgaa22Rule03101.java

@Override
protected void select(SSPHandler sspHandler, ElementHandler<Element> elementHandler) {
    ELEMENT_SELECTOR.selectElements(sspHandler, elementHandler);
    if (elementHandler.isEmpty()) {
        return;/* ww  w. j  av  a  2 s.  c  o  m*/
    }
    // From the selected form elements, only keep the one without id
    // or with an id not unique on the page
    for (Element el : elementHandler.get()) {
        if (!el.hasAttr(ID_ATTR) || StringUtils.isEmpty(el.id().trim()) || CssLikeSelectorBuilder
                .getNumberOfElements(sspHandler, CssLikeSelectorBuilder.buildSelectorFromId(el.id())) >= 2) {
            elementsWithoutUniqueId.add(el);
        }
    }
}

From source file:org.opens.tanaguru.rules.rgaa30.Rgaa30Rule110102.java

/**
 * This method linked each input on a page to its form in a map.
 *///from  ww w .  j  a v  a 2 s . c om
private void putInputElementHandlerIntoTheMap() {
    for (Element el : inputElementHandler.get()) {
        if (!el.hasAttr(TITLE_ATTR) && !el.hasAttr(ARIA_LABEL_ATTR) && !el.hasAttr(ARIA_LABELLEDBY_ATTR)) {
            Element tmpElement = el.parent();
            while (StringUtils.isNotBlank(tmpElement.tagName())) {
                if (tmpElement.tagName().equals(FORM_TAG)) {
                    if (inputFormMap.containsKey(tmpElement)) {
                        inputFormMap.get(tmpElement).add(el);
                    } else {
                        ElementHandler<Element> inputElement = new ElementHandlerImpl();
                        inputElement.add(el);
                        inputFormMap.put(tmpElement, inputElement);
                    }
                    break;
                }
                tmpElement = tmpElement.parent();
            }
        }
    }
}

From source file:org.opens.tanaguru.rules.textbuilder.LinkTextElementBuilder.java

@Override
public String buildTextFromElement(Element element) {
    StringBuilder linkText = new StringBuilder();
    if (element.hasAttr(ALT_ATTR)) {
        linkText.append(SPACER);/*  w  w  w .  j  av  a2s .  c  o m*/
        linkText.append(altAttrTextBuilder.buildTextFromElement(element));
    }
    for (Node child : element.childNodes()) {
        if (child instanceof TextNode && !((TextNode) child).isBlank()) {
            linkText.append(SPACER);
            linkText.append(StringUtils.trim(((TextNode) child).text()));
        } else if (child instanceof Element) {
            linkText.append(SPACER);
            linkText.append(buildTextFromElement((Element) child));
        }
    }
    return StringUtils.trim(linkText.toString());
}

From source file:org.xwiki.validator.HTML5DutchWebGuidelinesValidator.java

private void validateRpd1s3AboutForms() {
    // Form validation
    Elements formElements = getElements("form");

    for (Element formElement : formElements) {
        // Look for either a submit input or an image input with the 'alt' attribute specified.
        // See http://www.w3.org/TR/WCAG10-HTML-TECHS/#forms-graphical-buttons
        boolean hasSubmit = false;
        boolean hasButtonSubmit = false;
        for (Element input : formElement.getElementsByTag(ELEM_INPUT)) {
            String type = input.attr(ATTR_TYPE);
            if (SUBMIT.equals(type) || (IMAGE.equals(type) && !StringUtils.isEmpty(input.attr(ATTR_ALT)))) {
                hasSubmit = true;/*from  ww w .j a  va2  s.c o  m*/
                break;
            }
        }
        for (Element button : formElement.getElementsByTag("button")) {
            if (!button.hasAttr(ATTR_TYPE) || SUBMIT.equals(button.attr(ATTR_TYPE))) {
                hasButtonSubmit = true;
                break;
            }
        }
        assertTrue(Type.ERROR, "rpd1s3.formSubmit", hasSubmit || hasButtonSubmit);
    }
}

From source file:org.xwiki.validator.HTML5DutchWebGuidelinesValidator.java

/**
 * @param table Table to analyze/*from   w  w w.j  av a 2  s.co m*/
 * @return true if the table contains th with ids and td
 */
private boolean hasTableHeadersAndIds(Element table) {
    for (Element td : table.getElementsByTag("td")) {
        if (!td.hasAttr("headers")) {
            return false;
        }
    }

    for (Element td : table.getElementsByTag("th")) {
        if (!td.hasAttr("id")) {
            return false;
        }
    }

    return true;
}

From source file:org.xwiki.validator.HTML5DutchWebGuidelinesValidator.java

/**
 * Avoid automatic redirection during interaction with forms.
 */// w  ww . ja v a2 s  .  c  o  m
public void validateRpd13s4() {
    for (Element form : getElements(ELEM_FORM)) {
        boolean hasSubmit = false;
        boolean hasDynamicSelect = false;

        for (Element input : form.getElementsByTag(ELEM_INPUT)) {
            String type = input.attr(ATTR_TYPE);
            if ("submit".equals(type) || "image".equals(type)) {
                hasSubmit = true;
                break;
            }
        }
        assertTrue(Type.ERROR, "rpd13s4.submit", hasSubmit);

        for (Element select : form.getElementsByTag("select")) {
            if (select.hasAttr("onchange")) {
                hasDynamicSelect = true;
                break;
            }
        }

        if (hasDynamicSelect) {
            addError(Type.WARNING, -1, -1, "rpd13s4.select");
        }
    }
}