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:com.astamuse.asta4d.render.RenderUtil.java

/**
 * Find out all the snippet in the passed Document and execute them. The Containing embed tag of the passed Document will be exactly
 * mixed in here too. <br>//from w  ww .java  2  s  . co m
 * Recursively contained snippets will be executed from outside to inside, thus the inner snippets will not be executed until all of
 * their outer snippets are finished. Also, the dynamically created snippets and embed tags will comply with this rule too.
 * 
 * @param doc
 *            the Document to apply snippets
 * @throws SnippetNotResovlableException
 * @throws SnippetInvokeException
 * @throws TemplateException
 */
public final static void applySnippets(Document doc) throws SnippetNotResovlableException,
        SnippetInvokeException, TemplateException, TemplateNotFoundException {
    if (doc == null) {
        return;
    }

    applyClearAction(doc, false);

    // retrieve ready snippets
    String selector = SelectorUtil.attr(ExtNodeConstants.SNIPPET_NODE_TAG_SELECTOR,
            ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS, ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS_READY);
    List<Element> snippetList = new ArrayList<>(doc.select(selector));
    int readySnippetCount = snippetList.size();
    int blockedSnippetCount = 0;
    for (int i = readySnippetCount - 1; i >= 0; i--) {
        // if parent snippet has not been executed, the current snippet will
        // not be executed too.
        if (isBlockedByParentSnippet(doc, snippetList.get(i))) {
            snippetList.remove(i);
            blockedSnippetCount++;
        }
    }
    readySnippetCount = readySnippetCount - blockedSnippetCount;

    String renderDeclaration;
    Renderer renderer;
    Context context = Context.getCurrentThreadContext();
    Configuration conf = Configuration.getConfiguration();
    final SnippetInvoker invoker = conf.getSnippetInvoker();

    String refId;
    String currentTemplatePath;
    Element renderTarget;
    for (Element element : snippetList) {
        if (!conf.isSkipSnippetExecution()) {
            // for a faked snippet node which is created by template
            // analyzing process, the render target element should be its
            // child.
            if (element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_TYPE)
                    .equals(ExtNodeConstants.SNIPPET_NODE_ATTR_TYPE_FAKE)) {
                renderTarget = element.children().first();
                // the hosting element of this faked snippet has been removed by outer a snippet
                if (renderTarget == null) {
                    element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS,
                            ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS_FINISHED);
                    continue;
                }
            } else {
                renderTarget = element;
            }

            // we have to reset the ref of current snippet at every time to make sure the ref is always unique(duplicated snippet ref
            // could be created by list rendering)
            TemplateUtil.resetSnippetRefs(element);

            context.setCurrentRenderingElement(renderTarget);
            renderDeclaration = element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_RENDER);

            refId = element.attr(ExtNodeConstants.ATTR_SNIPPET_REF);
            currentTemplatePath = element.attr(ExtNodeConstants.ATTR_TEMPLATE_PATH);

            context.setCurrentRenderingElement(renderTarget);
            context.setData(TRACE_VAR_TEMPLATE_PATH, currentTemplatePath);

            try {
                if (element.hasAttr(ExtNodeConstants.SNIPPET_NODE_ATTR_PARALLEL)) {
                    ConcurrentRenderHelper crHelper = ConcurrentRenderHelper.getInstance(context, doc);
                    final Context newContext = context.clone();
                    final String declaration = renderDeclaration;
                    crHelper.submitWithContext(newContext, declaration, refId, new Callable<Renderer>() {
                        @Override
                        public Renderer call() throws Exception {
                            return invoker.invoke(declaration);
                        }
                    });
                    element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS,
                            ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS_WAITING);
                } else {
                    renderer = invoker.invoke(renderDeclaration);
                    applySnippetResultToElement(doc, refId, element, renderTarget, renderer);
                }
            } catch (SnippetNotResovlableException | SnippetInvokeException e) {
                throw e;
            } catch (Exception e) {
                SnippetInvokeException se = new SnippetInvokeException(
                        "Error occured when executing rendering on [" + renderDeclaration + "]:"
                                + e.getMessage(),
                        e);
                throw se;
            }

            context.setData(TRACE_VAR_TEMPLATE_PATH, null);
            context.setCurrentRenderingElement(null);
        } else {// if skip snippet
            element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS,
                    ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS_FINISHED);
        }
    }

    // load embed nodes which blocking parents has finished
    List<Element> embedNodeList = doc.select(ExtNodeConstants.EMBED_NODE_TAG_SELECTOR);
    int embedNodeListCount = embedNodeList.size();
    Iterator<Element> embedNodeIterator = embedNodeList.iterator();
    Element embed;
    Element embedContent;
    while (embedNodeIterator.hasNext()) {
        embed = embedNodeIterator.next();
        if (isBlockedByParentSnippet(doc, embed)) {
            embedNodeListCount--;
            continue;
        }
        embedContent = TemplateUtil.getEmbedNodeContent(embed);
        TemplateUtil.mergeBlock(doc, embedContent);
        embed.before(embedContent);
        embed.remove();
    }

    if ((readySnippetCount + embedNodeListCount) > 0) {
        TemplateUtil.regulateElement(null, doc);
        applySnippets(doc);
    } else {
        ConcurrentRenderHelper crHelper = ConcurrentRenderHelper.getInstance(context, doc);
        String delcaration = null;
        if (crHelper.hasUnCompletedTask()) {
            delcaration = null;
            try {
                FutureRendererHolder holder = crHelper.take();
                delcaration = holder.getRenderDeclaration();
                String ref = holder.getSnippetRefId();
                String reSelector = SelectorUtil.attr(ExtNodeConstants.SNIPPET_NODE_TAG_SELECTOR,
                        ExtNodeConstants.ATTR_SNIPPET_REF, ref);
                Element element = doc.select(reSelector).get(0);// must have
                Element target;
                if (element.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_TYPE)
                        .equals(ExtNodeConstants.SNIPPET_NODE_ATTR_TYPE_FAKE)) {
                    target = element.children().first();
                } else {
                    target = element;
                }
                applySnippetResultToElement(doc, ref, element, target, holder.getRenderer());
                applySnippets(doc);
            } catch (InterruptedException | ExecutionException e) {
                throw new SnippetInvokeException("Concurrent snippet invocation failed"
                        + (delcaration == null ? "" : " on [" + delcaration + "]"), e);
            }
        }
    }
}

From source file:io.github.carlomicieli.footballdb.starter.parsers.DraftParser.java

private Stream<Element> extractTableRows(Element table) {
    Predicate<Element> withoutCssClass = e -> e.className().equals("");

    return table.children().stream().filter(withoutCssClass);
}

From source file:fr.arlefebvre.pronostics.controller.EuroMatchListController.java

@RequestMapping("/euro2016/matches")
public List<Match> matches() {
    if (pseudoCache != null && !pseudoCache.isEmpty())
        return pseudoCache;
    ArrayList<Match> result = new ArrayList<Match>();
    String uri = "http://www.lequipe.fr/Football/Euro/Saison-2016/calendrier-resultats.html";

    //On se connecte au site et on charge le document html

    Document doc;/*from  w  w w.ja v  a2 s.c o m*/
    try {
        doc = Jsoup.connect(uri).get();

        Elements elements = doc.getElementsByClass("mainDate");
        for (Element element : elements) {
            Element title = element.getElementsByClass("title").first();
            String date = title.text();

            Element tbody = element.getElementsByTag("tbody").first();
            for (Element matchElement : tbody.children()) {
                String groupe = matchElement.getElementsByClass("date").first().text();
                String home = matchElement.getElementsByClass("domicile").first().text();
                String away = matchElement.getElementsByClass("exterieur").first().text();

                Match m = new Match();
                m.setDate(date);
                m.setHomeTeamId(home);
                m.setAwayTeamId(away);
                m.setGroup(groupe);
                result.add(m);
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    if (pseudoCache == null)
        pseudoCache = result;
    return result;
}

From source file:fr.arlefebvre.pronostics.controller.UEFATeamsController.java

@RequestMapping("/uefa/teams")
public List<Team> teams() {
    if (pseudoCache != null && !pseudoCache.isEmpty())
        return pseudoCache;
    ArrayList<Team> result = new ArrayList<Team>();
    String uri = "http://fr.fifa.com/fifa-world-ranking/ranking-table/men/uefa.html";

    //On se connecte au site et on charge le document html

    Document doc;/*w  ww  .  j a  v a 2 s. c  o m*/
    try {
        doc = Jsoup.connect(uri).get();
        Elements elements = doc.getElementsByClass("table");
        for (Element element : elements) {
            Element tbody = element.getElementsByTag("tbody").first();
            for (Element child : tbody.children()) {
                Element teamNameElement = child.getElementsByClass("tbl-teamname").first();
                String name = teamNameElement.text();
                String countryCode = child.getElementsByClass("tbl-countrycode").first().text();
                String imgUrl = teamNameElement.select("img").first().absUrl("src");
                Team team = new Team();
                team.setName(name);
                team.setCountryCode(countryCode);
                team.setImgUrl(imgUrl);
                team.setNationalTeam(true);
                result.add(team);
            }
        }

        //String titre =  element.text();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //        RestTemplate restTemplate = new RestTemplate();
    //        ResponseEntity<ChampionListDto> response = restTemplate.getForEntity(
    //                uri,
    //                ChampionListDto.class);
    //
    //        List<ChampionDto> champions = response.getBody().getChampions();
    //        return champions.stream().map(c -> getChampionById(c.getId()).getName()).collect(Collectors.toList());
    result.sort((t1, t2) -> t1.getName().compareTo(t2.getName()));
    if (pseudoCache == null)
        pseudoCache = result;
    return result;
}

From source file:gov.medicaid.screening.dao.impl.AccreditedBirthCentersLicenseDAOBean.java

/**
 * Retrieves all results from the source site.
 * //from   w w  w  . j a v  a  2  s .  c om
 * @return the birth centers matched
 * @throws URISyntaxException
 *             if the URL could not be correctly constructed
 * @throws IOException
 *             for any I/O related errors
 * @throws ServiceException
 *             for any other errors encountered
 */
private List<AccreditedBirthCenter> getAllResults() throws URISyntaxException, IOException, ServiceException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new LaxRedirectStrategy());

    HttpGet getFrontPage = new HttpGet(new URIBuilder(getSearchURL()).build());
    HttpResponse response = client.execute(getFrontPage);

    verifyAndAuditCall(getSearchURL(), response);

    Document page = Jsoup.parse(EntityUtils.toString(response.getEntity()));

    List<AccreditedBirthCenter> allCenters = new ArrayList<AccreditedBirthCenter>();
    Elements rows = page.select("table#wp-table-reloaded-id-1-no-1 tbody tr");
    for (Element row : rows) {
        AccreditedBirthCenter center = parseCenter(row.children());
        if (center != null) {
            allCenters.add(center);
        }
    }
    return allCenters;
}

From source file:de.geeksfactory.opacclient.apis.TouchPoint.java

static List<ReservedItem> parse_reslist(Document doc) {
    List<ReservedItem> reservations = new ArrayList<>();
    Elements copytrs = doc.select(".data tr");
    int trs = copytrs.size();
    if (trs <= 1) {
        return null;
    }// w  ww . ja v a  2 s  . c om
    for (int i = 1; i < trs; i++) {
        Element tr = copytrs.get(i);
        ReservedItem item = new ReservedItem();

        if (tr.text().contains("keine Daten") || tr.children().size() == 1) {
            return null;
        }

        item.setTitle(tr.child(2).select("b, strong").text().trim());
        try {
            String[] rowsplit2 = tr.child(2).html().split("<br[ /]*>");
            String[] rowsplit3 = tr.child(3).html().split("<br[ /]*>");
            if (rowsplit2.length > 1)
                item.setAuthor(rowsplit2[1].trim());
            if (rowsplit3.length > 2)
                item.setBranch(rowsplit3[2].trim());
            if (rowsplit3.length > 2) {
                item.setStatus(rowsplit3[0].trim() + " (" + rowsplit3[1].trim() + ")");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        reservations.add(item);
    }
    return reservations;
}

From source file:io.github.carlomicieli.footballdb.starter.parsers.SeasonGamesParser.java

private Stream<Element> extractRows(Element tbody) {
    Predicate<Element> withoutCssClass = e -> e.className().equals("");
    Predicate<Element> gameRow = e -> !e.child(2).text().equals("Playoffs");

    return tbody.children().stream().filter(withoutCssClass).filter(gameRow);
}

From source file:com.liato.bankdroid.banking.banks.MinPension.java

private Account updateAccount(String URL, String selector, String name) throws IOException {
    String response = urlopen.open(URL);
    Document dResponse = Jsoup.parse(response);
    List<Transaction> transactions = new ArrayList<>();
    String institute = "";
    String subInstitute = "";
    for (Element e : dResponse.select(selector)) {
        if (e.hasClass("GroupRow")) {
            institute = e.children().first().text();
        } else if (e.hasClass("GroupMemberRow") || e.hasClass("SubRow")) {
            Elements elements = e.children();
            if (elements.size() == 6) { //Special case for "Allmn pension"
                if (elements.get(2).text().isEmpty()) {
                    //   subInstitute =  "  " + elements.get(1).text(); /* Doesn't fit atm. */
                } else {
                    transactions.add(new Transaction(elements.get(5).text(),
                            institute + subInstitute + "\n  " + elements.get(1).text(),
                            Helpers.parseBalance(elements.get(2).text())));
                    subInstitute = "";
                }/*from www .  j  a  v a2s .c  om*/
            } else if (elements.size() >= 7) {
                transactions.add(
                        new Transaction(elements.get(6).text(), institute + "\n  " + elements.get(1).text(),
                                Helpers.parseBalance(elements.get(4).text())));
            }
        }
    }

    balance = BigDecimal.ZERO;
    for (Transaction t : transactions) {
        balance = balance.add(t.getAmount());
    }
    Account account = new Account(name, balance, name, Account.REGULAR, "");
    account.setTransactions(transactions);
    return account;
}

From source file:com.obnsoft.ptcm3.MyApplication.java

private void parseCommandHtml() {
    mCommands = new ArrayList<Command>();
    mCategories = new ArrayList<String>();
    int categoryId = -1;
    try {/*from ww  w .  ja va 2 s  .com*/
        InputStream in = openFileInput(FNAME_CMD_HTML);
        Document document = Jsoup.parse(in, "UTF-8", URL_CMD_HTML);
        in.close();
        Element divContentArea = document.getElementById(ID_CONTENTAREA);
        for (Element e : divContentArea.children()) {
            if (e.tagName().equals(TAG_TABLE)) {
                if (e.className().equals("")) {
                    mCommands.add(new Command(e, categoryId));
                }
            } else if (e.tagName().equals(TAG_H3)) {
                mCategories.add(e.text());
                categoryId++;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        mCommands = null;
        mCategories = null;
    }
}

From source file:eu.masconsult.bgbanking.banks.dskbank.DskClient.java

private RawBankAccount obtainBankAccountFromHtmlTableRow(Element row) {
    // skip title rows
    if (row.children().size() != 4) {
        return null;
    }//from  w  ww.j a v  a2 s  .c  om
    // skip header
    if (row.hasClass("td-header")) {
        return null;
    }

    String onclick = row.child(0).child(0).attr("onclick");
    Matcher matcher = PATTERN_MATCH_BANK_ACCOUNT_ID.matcher(onclick);
    if (!matcher.find()) {
        throw new ParseException("can't find bank account id in " + onclick);
    }

    return new RawBankAccount().setServerId(matcher.group(1)).setName(row.child(0).text())
            .setIBAN(row.child(1).text()).setCurrency(row.child(2).text())
            .setBalance(Convert.strToFloat(row.child(3).text()))
            .setAvailableBalance(Convert.strToFloat(row.child(3).text()));
}