Example usage for org.jsoup.nodes Element children

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

Introduction

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

Prototype

public Elements children() 

Source Link

Document

Get this element's child elements.

Usage

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.j  av a 2 s . c  o  m
                    }
                    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.lang.LangChangeChecker.java

/**
 * //from  w w w .  j a v a2  s.com
 * @param sspHandler
 * @param element
 * @param checkCurrentElement
 * @return 
 */
private TestSolution checkLanguageRelevancyRecursively(SSPHandler sspHandler, Element element,
        @Nullable String currentLangRedefinitionValue) {

    String currentLangRedefinition = currentLangRedefinitionValue;

    TestSolutionHandler tsh = new TestSolutionHandlerImpl();
    // if the current element defines a lang, we extract its value and check
    // it is different from the default one. If it is different, we start
    // the check considering that the lang of the current node and all its 
    // children have to be different from the default one.
    // If it is identical or if the current element does not define a lang, 
    // we start the check considering that the lang of the current node and all its 
    // children have to be identical to the default one.
    if (isLangDefinedForElement(element)) {
        String langDefinition = extractLangDefinitionFromElement(element, sspHandler);
        String currentLang = extractEffectiveLang(langDefinition);
        if (!StringUtils.equalsIgnoreCase(defaultLang, currentLang)) {
            currentLangRedefinition = currentLang;
        } else {
            currentLangRedefinition = null;
        }
    }

    // we extract the textual content of the current element. If not empty 
    // we check the language relevancy 
    String extractedText = extractTextFromElement(element, false);
    if (isTextTestable(extractedText)) {
        newElementTested();
        if (StringUtils.isNotBlank(currentLangRedefinition)) {
            tsh.addTestSolution(
                    checkLanguageDifferentFromDefault(element, extractedText, currentLangRedefinition));
        } else {
            tsh.addTestSolution(checkLanguageIdenticalToDefault(element, extractedText));
        }
    }

    for (Element el : element.children()) {
        tsh.addTestSolution(checkLanguageRelevancyRecursively(sspHandler, el, currentLangRedefinition));
    }
    return tsh.getTestSolution();
}

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

/**
 * //from  w w w.  j a  v  a2s.  c  o m
 * @param element
 * @param extractRecursively
 * @return 
 */
protected String extractTextFromElement(Element element, boolean extractRecursively) {
    if (EXCLUDED_ELEMENTS_LIST.contains(element.tagName())) {
        return null;
    }
    StringBuilder strb = new StringBuilder();
    if (testableTextElementBuilder == null) {
        testableTextElementBuilder = new CompleteTextElementBuilder();
    }
    strb.append(testableTextElementBuilder.buildTextFromElement(element));

    if (extractRecursively) {
        for (Element el : element.children()) {
            if (!isLangDefinedForElement(el) && !EXCLUDED_ELEMENTS_LIST.contains(el.tagName())) {
                strb.append(TextElementBuilder.SPACER);
                strb.append(extractTextFromElement(el, true));
            }
        }
    }
    return strb.toString().replaceAll(" +", " ");
}

From source file:org.asqatasun.rules.elementselector.CompositeLinkElementSelector.java

/**
 * //from w  w  w .j a va 2  s  .  com
 * @param linkElement
 * @return whether the current link element is an image link
 */
protected boolean isImageLink(Element linkElement) {
    if (linkElement.children().isEmpty() || linkElement.children().size() > 1
            || StringUtils.isNotBlank(linkElement.ownText())) {
        return false;
    }
    return !linkElement.children().select(IMAGE_LINK_CHILDREN_CSS_LIKE_QUERY).isEmpty();
}

From source file:org.asqatasun.rules.elementselector.CompositeLinkElementSelector.java

/**
 * /* ww w  . jav a 2 s  .  com*/
 * @param linkElement
 * @return whether the current link element is a svg link
 */
protected boolean isSvgLink(Element linkElement) {
    if (linkElement.children().isEmpty() || linkElement.children().size() > 1
            || StringUtils.isNotBlank(linkElement.ownText())) {
        return false;
    }
    return !linkElement.children().select(HtmlElementStore.SVG_ELEMENT).isEmpty();
}

From source file:org.asqatasun.rules.elementselector.ImageElementSelector.java

/**
 * /*from  w  w w .  ja  v  a2 s  .c  om*/
 * @param imageParent
 * @param image
 * @return whether the current image is an image link
 */
private boolean isImageLink(Element imageParent, Element image) {
    if (imageParent == null || !StringUtils.equals(imageParent.text(), image.text())) {
        return false;
    }
    if (imageParent.children().size() == 1) {
        return isImageLink(imageParent.child(0), image);
    } else if (imageParent.children().isEmpty() && imageParent.equals(image)) {
        return true;
    }
    return false;
}

From source file:org.asqatasun.rules.elementselector.ImageElementSelector.java

/**
 * An link is seen as composite when it is composed with more than one 
 * element. The tested element has at least one image. If the text is different
 * from the one of the child element, the link is composite by definition. 
 * It the text is identical, we check whether the current element has more
 * than 1 child./*from ww  w  .  j  av  a2s . com*/
 * @param imageParent
 * @return whether the current image is a composite link.
 */
private boolean isCompositeLink(Element imageParent, Element image) {
    if (imageParent == null) {
        return false;
    }
    if (!StringUtils.equals(imageParent.text(), image.text())) {
        return true;
    }
    if (imageParent.children().size() == 1) {
        return isCompositeLink(imageParent.child(0), image);
    } else if (imageParent.children().size() > 1) {
        return true;
    }
    return false;
}

From source file:org.dataconservancy.ui.it.UiConfigurationActionBeanIT.java

/**
 * Insures that an XSD schema, composed of a single schema document (no <xsd:include> statements), can be
 * added to the system./*from   w ww.  ja  va  2s .c  o m*/
 *
 * @throws Exception
 */
@Test
public void testAddMavenPomMetadataFormat() throws Exception {
    // Get a count of the current number of metadata formats in the system
    List<UiConfigurationActionBean.MetaDataFormatTransport> mdfts = getMdfs();
    int mdfCount = mdfts.size();

    // Compose the mdft to add
    final UiConfigurationActionBean.MetaDataFormatTransport mdft = new UiConfigurationActionBean()
            .getNewMetadataFormatTransport();
    final AddMetadataFormatRequest req = new AddMetadataFormatRequest(urlConfig);
    // A unique name insures that this Metadata Format doesn't exist yet in the system (but we verify this
    // assumption anyway)
    final String name = UUID.randomUUID().toString();
    final boolean validates = true;
    final String version = this.getClass().getSimpleName() + " Maven 4.0.0 POM";
    final boolean appliesToCollection = false;
    final boolean appliesToProject = false;
    final boolean appliesToItem = true;
    final List<String> disciplineIds = Arrays.asList("dc:discipline:Biology");
    mdft.setName(name);
    mdft.setVersion(version);
    mdft.setSchemaURL(MAVEN_MODEL_4_0_0_SCHEMA_URL);
    mdft.setSchemaSource(MAVEN_MODEL_4_0_0_SCHEMA_URL);
    mdft.setValidates(validates);
    mdft.setAppliesToCollection(appliesToCollection);
    mdft.setAppliesToProject(appliesToProject);
    mdft.setAppliesToItem(appliesToItem);
    mdft.setDisciplineIds(disciplineIds);

    // Insure that the new Metadata Format being added isn't in the list of existing metadata formats
    assertFalse(mdfts.contains(mdft));

    // Add the metadata format
    HttpAssert.ResponseHolder holder = new HttpAssert.ResponseHolder();

    HttpAssert.assertStatus(hc, req.asHttpPost(mdft), 200, holder);

    final String html = IOUtils.toString(holder.getBody());
    assertNotNull(html);
    final Document dom = Jsoup.parse(html);
    assertNotNull(dom);
    Element nameElement = dom.getElementById("schemaName");
    assertNotNull(nameElement);
    String testText = nameElement.text();
    assertTrue(nameElement.text().equalsIgnoreCase("Schema Name: " + name));

    Element versionElement = dom.getElementById("schemaVersion");
    assertNotNull(versionElement);
    assertTrue(versionElement.text().equalsIgnoreCase("Version: " + version));

    Element namespacesElement = dom.getElementById("namespaces");
    //assertEquals(2, namespacesElement.childNodeSize());

    Elements namespaceElements = namespacesElement.children();
    boolean foundPrefixedNamespace = false;
    boolean foundNamespace = false;
    for (Element namespaceElement : namespaceElements) {
        String namespaceText = namespaceElement.text();
        if (namespaceText.contains("Namespace:")) {
            if (namespaceText.contains("Prefix")) {
                assertTrue(namespaceText
                        .equalsIgnoreCase("Namespace: http://www.w3.org/2001/XMLSchema Prefix: xs"));
                foundPrefixedNamespace = true;
            } else {
                assertTrue(namespaceText.equalsIgnoreCase("Namespace: http://maven.apache.org/POM/4.0.0"));
                foundNamespace = true;
            }
        }
    }

    assertTrue(foundPrefixedNamespace);
    assertTrue(foundNamespace);

    // Now we need to persist the format in the system by emulating a click on the "save" button
    HttpAssert.assertStatus(hc, new SaveMetadataFormatRequest(urlConfig).asHttpPost(), 200);

    // insure that the format we've added was added properly (all the values for table columns were
    // persisted properly)
    mdfts = getMdfs();
    assertTrue(mdfts.contains(mdft));
    assertEquals(mdfCount + 1, mdfts.size());
}

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

/**
 * //w w w . j  a va  2s .co m
 * @param linkElement
 * @return whether the current link element is an image link
 */
protected boolean isImageLink(Element linkElement) {
    if (linkElement.children().isEmpty() || linkElement.children().size() > 1
            || StringUtils.isNotBlank(linkElement.ownText())) {
        return false;
    }
    if (!linkElement.children().select(IMAGE_LINK_CHILDREN_CSS_LIKE_QUERY).isEmpty()) {
        return true;
    }
    return false;
}

From source file:org.sbs.goodcrawler.extractor.selector.factory.ElementCssSelectorFactory.java

/**
 * <b>Element??Element??select/*from w  w w  .  j ava2 s . c  om*/
 * @param element
 * @return
 */
@SuppressWarnings("rawtypes")
public static AbstractElementCssSelector create(Element element) {
    String name = element.attr("name");
    String value = element.attr("value");
    String type = element.attr("type");
    String attr = element.attr("attr");
    String pattern = element.attr("pattern");
    String regex = element.attr("regex");
    String required = element.attr("required");
    String sIndex = element.attr("index");
    boolean isRequired = false;
    if (StringUtils.isNotBlank(required)) {
        isRequired = Boolean.parseBoolean(required);
    }
    int index = 0;
    if (StringUtils.isNotBlank(sIndex)) {
        index = Integer.parseInt(sIndex);
    }
    AbstractElementCssSelector selector = ElementCssSelectorFactory.create(name, type, value, attr, isRequired,
            index, regex, pattern);
    // ?
    Elements children = element.children();
    for (Element e : children) {
        if ("action".equals(e.tagName())) {
            SelectorAction action = ActionFactory.create(e, element.attr("type"));
            if (action != null)
                selector.addAction(action);
        }
        // ?Url
        else if ("element".equals(e.tagName())) {
            ((PageElementSelector) selector).addSelector(create(e));
        }
    }
    return selector;
}