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:com.near.chimerarevo.fragments.PostFragment.java

private void parseYoutubeVideos(Elements vids) {
    for (Element vid : vids) {
        if (vid.hasAttr("src")) {
            if (vid.attr("src").contains("youtube")) {
                if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("load_vid_pref",
                        true))/*from   w  w w  .  ja  v  a  2s  .co m*/
                    addYoutubeVideo(vid.attr("src"));
            } else
                addWebObject(vid.attr("width"), vid.attr("height"), vid.attr("src"));
        }
    }
}

From source file:org.arb.extractor.DomTreeWalker.java

private void collectIdsOnElement(Element element, AbstractCodeUnit codeUnit) {
    if (element.hasAttr("id")) {
        elementIdSet.add(element.attr("id"));
    }/*from   w  w  w .  j  av  a 2  s .  c o  m*/
    if (element.hasAttr("arb:id")) {
        arbIdSet.add(element.attr("arb:id"));
    }
    for (int i = 0; i < element.children().size(); i++) {
        collectIdsOnElement(element.child(i), codeUnit);
    }
}

From source file:org.arb.extractor.DomTreeWalker.java

/**
 * Get the existing resource id from the element. Resource id can be specified with either
 * arb:id or id, with arb:id taking priority.
 * /*from ww  w. jav  a2 s. c o  m*/
 * @param elem the element to be checked.
 * @return resource id or null.
 */
private String getElementResourceId(Element elem) {
    if (elem.hasAttr("arb:id")) {
        return elem.attr("arb:id");
    }
    if (elem.hasAttr("id")) {
        return elem.attr("id");
    }
    return null;
}

From source file:org.asqatasun.rules.accessiweb22.Aw22Rule06031.java

/**
 * //  w  w w  .j a  v  a2 s .  c  o  m
 * @param sspHandler
 * @param el
 * @param linkText
 * @return 
 */
private TestSolution testTitleAttributeLink(SSPHandler sspHandler, Element el, String linkText) {
    // if the current has no title or has an empty title or has a title 
    // content identical to the link text, returns not applicable.
    if (!el.hasAttr(TITLE_ATTR)) {
        return TestSolution.NOT_APPLICABLE;
    }
    String attrValue = el.attr(TITLE_ATTR);
    if (StringUtils.isBlank(attrValue)) {
        return TestSolution.NOT_APPLICABLE;
    }
    if (StringUtils.equalsIgnoreCase(attrValue, linkText)) {
        return TestSolution.NOT_APPLICABLE;
    }
    ElementHandler<Element> elHandler = new ElementHandlerImpl(el);
    TestSolutionHandler tsHandler = new TestSolutionHandlerImpl();
    titlePertinenceElementChecker.check(sspHandler, elHandler, tsHandler);
    return tsHandler.getTestSolution();
}

From source file:org.asqatasun.rules.doc.utils.rga33.extractor.Rgaa3Extractor.java

private static void createTestcaseFiles() throws IOException {
    File srcDir = new File(RGAA3_TESTCASE_PATH);
    for (File file : srcDir.listFiles()) {
        String fileName = file.getName().replace("Rgaa30Rule", "").replace(".java", "");
        String theme = fileName.substring(0, 2);
        String crit = fileName.substring(2, 4);
        String test = fileName.substring(4, 6);
        String testKey = Integer.valueOf(theme).toString() + "-" + Integer.valueOf(crit).toString() + "-"
                + Integer.valueOf(test).toString();
        String wrongKey = theme + "." + crit + "." + test;
        for (File testcase : file.listFiles()) {
            if (testcase.isFile() && testcase.getName().contains("html")) {
                Document doc = Jsoup.parse(FileUtils.readFileToString(testcase));
                Element detail = doc.select(".test-detail").first();
                if (detail == null) {
                    System.out.println(doc.outerHtml());
                } else {
                    detail.tagName("div");
                    detail.text("");
                    for (Element el : detail.children()) {
                        el.remove();/*ww w.  ja v  a 2  s  . com*/
                    }
                    if (!detail.hasAttr("lang")) {
                        detail.attr("lang", "fr");
                    }
                    detail.append("\n" + RGAA3.get(testKey).ruleRawHtml + "\n");
                    doc.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
                    doc.outputSettings().outline(false);
                    doc.outputSettings().indentAmount(4);
                    String outputHtml = doc.outerHtml();
                    if (outputHtml.contains(wrongKey)) {
                        outputHtml = outputHtml.replaceAll(wrongKey, RGAA3.get(testKey).getRuleDot());
                    }
                    FileUtils.writeStringToFile(testcase, outputHtml);
                }
            }
        }
    }
}

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

/**
 * This methods checks whether a given attribute is present for a set of 
 * elements/*from w w  w  . j  a v a  2 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());
            createSourceCodeRemark(getFailureSolution(), el, getFailureMsgCode());

        } else {
            testSolution = setTestSolution(testSolution, getSuccessSolution());
            createSourceCodeRemark(getSuccessSolution(), el, getSuccessMsgCode());

        }
    }

    testSolutionHandler.addTestSolution(testSolution);

}

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

/**
 * This methods checks whether a given attribute is present for a set of
 * elements/*  ww  w  .j  av  a 2s.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());
            createSourceCodeRemark(getFailureSolution(), el, getFailureMsgCode());

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

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

    testSolutionHandler.addTestSolution(testSolution);

}

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

/**
 * This methods checks whether a given id is unique for a set of 
 * elements//from www.  j  a  v  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();
            addSourceCodeRemark(getFailureSolution(), el, getFailureMsgCode());
        }
    }

    testSolutionHandler.addTestSolution(testSolution);
}

From source file:org.asqatasun.rules.elementchecker.ElementCheckerImpl.java

/**
 * //from  w w  w.ja v  a2 s  . co m
 * @param element
 * @param attributeName
 * @param isExternalResource
 * @return the text content of an attribute
 */
protected String buildAttributeContent(Element element, String attributeName, boolean isExternalResource) {
    if (!element.hasAttr(attributeName)) {
        return ABSENT_ATTRIBUTE_VALUE;
    } else if (isExternalResource && !element.attr(ABS_URL_PREFIX + attributeName).isEmpty()) {
        return element.absUrl(attributeName).trim();
    } else {
        return element.attr(attributeName).trim();
    }
}

From source file:org.asqatasun.rules.elementchecker.lang.LangChecker.java

/**
 * //from w ww .  ja va2s.  c om
 * @param element
 * @return whether the current element is defined with a "lang" attribute
 * or a "xml:lang" attribute
 */
protected boolean isLangDefinedForElement(Element element) {
    return element.hasAttr(AttributeStore.LANG_ATTR) || element.hasAttr(AttributeStore.XML_LANG_ATTR);
}