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.asqatasun.rules.rgaa30.Rgaa30Rule090104.java

@Override
protected void select(SSPHandler sspHandler) {
    super.select(sspHandler);
    Iterator<Element> elementsIterator = getElements().get().iterator();
    while (elementsIterator.hasNext()) {
        Element element = elementsIterator.next();
        if (element.hasAttr("aria-level")) {
            if (!PATTERN.matcher(element.attr("aria-level")).matches()) {
                elementsIterator.remove();
            }//from  www .  j  a va 2s .  c om
        }
    }
}

From source file:org.asqatasun.rules.rgaa30.Rgaa30Rule110101.java

@Override
protected void select(SSPHandler sspHandler) {

    // Selection of all the input form elements of the page
    ElementSelector elementSelector = new SimpleElementSelector(FORM_ELEMENT_CSS_LIKE_QUERY);
    elementSelector.selectElements(sspHandler, inputFormHandler);

    // the selection of the input form elements without label is initialised
    // with all the elements of the page, some elements will be removed later
    inputFormWithoutLabelHandler.addAll(inputFormHandler.get());

    // selection of the input form elements with explicit label
    ElementHandler<Element> inputFormLabelHandler = new ElementHandlerImpl();
    ElementSelector explicitLabelSelector = new InputFormElementWithExplicitLabelSelector(inputFormHandler);
    explicitLabelSelector.selectElements(sspHandler, inputFormLabelHandler);

    // remove all the input form elements with explicit label from 
    // the selection of the input form elements without label
    inputFormWithoutLabelHandler.removeAll(inputFormLabelHandler.get());

    // selection of the input form with inplicit label
    ElementSelector inplicitLabelSelector = new InputFormElementWithInplicitLabelSelector(inputFormHandler);
    inplicitLabelSelector.selectElements(sspHandler, inputFormLabelHandler);

    // remove all the input form elements with inplicit label from 
    // the selection of the input form elements without label
    inputFormWithoutLabelHandler.removeAll(inputFormLabelHandler.get());

    // selection of the input form elements with explicit label
    ElementHandler<Element> inputFormWithAttrHandler = new ElementHandlerImpl();
    for (Element el : inputFormWithoutLabelHandler.get()) {
        if (el.hasAttr(TITLE_ATTR) || el.hasAttr(ARIA_LABEL_ATTR) || el.hasAttr(ARIA_LABELLEDBY_ATTR)) {
            inputFormWithAttrHandler.add(el);
        }/*w  ww.jav  a2  s  .c om*/
    }

    // remove all the input form elements with title, aria-label, or 
    // aria-labelledby attributes from the selection of the input form 
    // elements without label
    inputFormWithoutLabelHandler.removeAll(inputFormWithAttrHandler.get());
}

From source file:org.asqatasun.rules.rgaa30.Rgaa30Rule110102.java

/**
 * This method linked each input on a page to its form in a map.
 *///from  w  w w.j  ava 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_ELEMENT)) {
                    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.asqatasun.rules.textbuilder.CompleteTextElementBuilder.java

/**
 * The textual content of an element can be composed with :
 * <ul>/*from  w w  w.j  av a  2 s. com*/
 *     <li> The text of the element</li>
 *     <li> The text of the alt attribute of the element</li>
 *     <li> The text of the title attribute of the element</li>
 *     <li> The text of the summary attribute of the element</li>
 *     <li> The text of the value attribute of the element</li>
 *     <li> The text of the content attribute of the element when 
 *          the value of the name attribute is "description"
 *      </li>
 * </ul>
 * 
 * @param element
 * @return the textual content of an element
 */
@Override
public String buildTextFromElement(Element element) {
    StringBuilder strb = new StringBuilder();
    if (StringUtils.isNotBlank(element.ownText())) {
        strb.append(SPACER);
        strb.append(element.ownText().trim());
    }

    strb.append(getTextualContentOfAttribute(element, AttributeStore.ALT_ATTR));
    strb.append(getTextualContentOfAttribute(element, AttributeStore.TITLE_ATTR));
    strb.append(getTextualContentOfAttribute(element, AttributeStore.SUMMARY_ATTR));
    strb.append(getTextualContentOfAttribute(element, AttributeStore.VALUE_ATTR));

    if (element.hasAttr(AttributeStore.CONTENT_ATTR) && element.hasAttr(AttributeStore.NAME_ATTR)
            && StringUtils.equalsIgnoreCase(element.attr(AttributeStore.NAME_ATTR), "description")
            && StringUtils.isNotBlank(element.attr(AttributeStore.CONTENT_ATTR))) {
        strb.append(SPACER);
        strb.append(getTextualContentOfAttribute(element, AttributeStore.CONTENT_ATTR));
    }
    return StringUtils.trim(strb.toString());
}

From source file:org.asqatasun.rules.textbuilder.CompleteTextElementBuilder.java

/**
 * //w ww  .  j av a  2  s.c  o m
 * @param element
 * @param attributeName
 * @return the textual content of an attribute
 */
private String getTextualContentOfAttribute(Element element, String attributeName) {
    if (element.hasAttr(attributeName)) {
        return SPACER + StringUtils.trim(element.attr(attributeName));
    }
    return "";
}

From source file:org.asqatasun.rules.textbuilder.DeepTextElementBuilder.java

@Override
public String buildTextFromElement(Element element) {
    StringBuilder elementText = new StringBuilder();
    if (element.hasAttr(ALT_ATTR)) {
        elementText.append(SPACER);//from w ww.ja va 2s . com
        elementText.append(altAttrTextBuilder.buildTextFromElement(element));
    }
    for (Node child : element.childNodes()) {
        if (child instanceof TextNode && !((TextNode) child).isBlank()) {
            elementText.append(SPACER);
            elementText.append(StringUtils.trim(((TextNode) child).text()));
        } else if (child instanceof Element) {
            elementText.append(SPACER);
            elementText.append(buildTextFromElement((Element) child));
        }
    }
    return StringUtils.trim(elementText.toString());
}

From source file:org.asqatasun.rules.textbuilder.TextAttributeOfElementBuilder.java

/**
 * /*from   w w  w . j a  v a  2s.c  om*/
 * @param element
 * @return the content of the attribute when it exits, null instead.
 */
@Override
public String buildTextFromElement(Element element) {
    if (CollectionUtils.isEmpty(attributeNames)) {
        return null;
    }
    boolean elementHasAttr = false;
    StringBuilder strb = new StringBuilder();
    for (String attributeName : attributeNames) {
        if (element.hasAttr(attributeName)) {
            elementHasAttr = true;
            strb.append(element.attr(attributeName));
            strb.append(SPACER);
        }
    }
    if (!elementHasAttr) {
        return null;
    }
    return strb.toString().trim();
}

From source file:org.norvelle.addressdiscoverer.parse.structured.StructuredPageWebContactLink.java

/**
 * Attempt to find a URL-type link associated with the given Jsoup Element,
 * by looking at all the HREF attributes of the various subelements.
 * /*from ww w.j  av  a  2s. co  m*/
 * @param element
 * @throws DoesNotContainContactLinkException
 * @throws MultipleContactLinksOfSameTypeFoundException 
 */
public StructuredPageWebContactLink(Element element)
        throws DoesNotContainContactLinkException, MultipleContactLinksOfSameTypeFoundException {
    super(element);
    ArrayList<String> hrefs = new ArrayList();
    Elements elements = element.getAllElements();
    for (Element child : elements) {
        if (child.hasAttr("href")) {
            String href = child.attr("href");
            if (!href.startsWith("mailto:"))
                hrefs.add(href);
        }
    }

    if (hrefs.isEmpty())
        throw new DoesNotContainContactLinkException();
    else if (hrefs.size() > 1)
        throw new MultipleContactLinksOfSameTypeFoundException("Multiple web links");
    this.address = hrefs.get(0);
}

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

/**
 * This methods checks whether a given attribute is present for a set of 
 * elements/* w ww .j  a v a2 s.c o  m*/
 * 
 * @param elements
 * @param testSolutionHandler
 */
private void checkAttributePresence(Elements elements, TestSolutionHandler testSolutionHandler) {

    if (elements.isEmpty()) {
        testSolutionHandler.addTestSolution(TestSolution.NOT_APPLICABLE);
        return;
    }

    TestSolution testSolution = TestSolution.PASSED;

    for (Element el : elements) {
        if (!el.hasAttr(attributeName)) {

            testSolution = setTestSolution(testSolution, getFailureSolution());

            if (StringUtils.isNotBlank(messageCodeOnAttrNotDetected)) {
                createSourceCodeRemark(getFailureSolution(), el, messageCodeOnAttrNotDetected);

            }

        } else if (StringUtils.isNotBlank(messageCodeOnAttrDetected)) {

            testSolution = setTestSolution(testSolution, getSuccessSolution());

            createSourceCodeRemark(getSuccessSolution(), el, messageCodeOnAttrDetected);

        }
    }

    testSolutionHandler.addTestSolution(testSolution);

}

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

/**
 * This methods checks whether a given attribute is present for a set of
 * elements//from   w  w w .  ja va2 s .c  om
 *
 * @param elements
 * @param testSolutionHandler
 */
private void checkAttributeWithValuePresence(Elements elements, TestSolutionHandler testSolutionHandler) {

    if (elements.isEmpty()) {
        testSolutionHandler.addTestSolution(TestSolution.NOT_APPLICABLE);
        return;
    }

    TestSolution testSolution = TestSolution.PASSED;

    for (Element el : elements) {
        if (!el.hasAttr(attributeName)
                || (el.hasAttr(attributeName) && !el.attr(attributeName).equals(attributeValue))) {

            testSolution = setTestSolution(testSolution, getFailureSolution());

            if (StringUtils.isNotBlank(messageCodeOnAttrNotDetected)) {
                createSourceCodeRemark(getFailureSolution(), el, messageCodeOnAttrNotDetected);

            }
        } else if (StringUtils.isNotBlank(messageCodeOnAttrDetected)) {
            testSolution = setTestSolution(testSolution, getSuccessSolution());

            createSourceCodeRemark(getSuccessSolution(), el, messageCodeOnAttrDetected);
        }
    }

    testSolutionHandler.addTestSolution(testSolution);

}