Search in sources :

Example 46 with Element

use of org.jsoup.nodes.Element in project LeMondeRssReader by MBach.

the class ArticleActivity method extractComments.

private List<Model> extractComments(Element doc, boolean loadMoreComments) {
    List<Model> commentList = new ArrayList<>();
    // Extract header
    if (!loadMoreComments) {
        Elements header = doc.select("[itemprop='InteractionCount']");
        if (atLeastOneChild(header)) {
            TextView commentHeader = new TextView(getBaseContext());
            commentHeader.setText(String.format("Commentaires %s", header.text()));
            commentHeader.setTypeface(null, Typeface.BOLD);
            commentHeader.setTextColor(Color.WHITE);
            commentHeader.setPadding(0, 0, 0, Constants.PADDING_COMMENT_ANSWER);
            commentList.add(new Model(commentHeader, 0));
        }
    }
    // Extract comments
    Elements comments = doc.select("[itemprop='commentText']");
    for (Element comment : comments) {
        Elements refs = comment.select("p.references");
        if (atLeastOneChild(refs)) {
            // Clear date
            refs.select("span").remove();
            TextView author = new TextView(getBaseContext());
            author.setTypeface(null, Typeface.BOLD);
            author.setText(refs.text());
            author.setTextColor(Color.WHITE);
            Elements commentComment = refs.next();
            if (atLeastOneChild(commentComment)) {
                TextView content = new TextView(getBaseContext());
                content.setText(commentComment.first().text());
                content.setTextColor(Color.WHITE);
                if (comment.hasClass("reponse")) {
                    author.setPadding(Constants.PADDING_COMMENT_ANSWER, 0, 0, 12);
                    content.setPadding(Constants.PADDING_COMMENT_ANSWER, 0, 0, 16);
                } else {
                    author.setPadding(0, 0, 0, 12);
                    content.setPadding(0, 0, 0, 16);
                }
                Integer commentId = Integer.valueOf(comment.attr("data-reaction_id"));
                commentList.add(new Model(author, commentId));
                commentList.add(new Model(content, commentId));
            }
        }
    }
    // Extract full comments page URI
    Elements div = doc.select("div.reactions");
    if (atLeastOneChild(div)) {
        Element fullComments = div.first().nextElementSibling();
        Elements next = fullComments.select("a");
        if (atLeastOneChild(next)) {
            commentsURI = next.first().attr("href");
        }
    }
    return commentList;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) TextView(android.widget.TextView) Elements(org.jsoup.select.Elements)

Example 47 with Element

use of org.jsoup.nodes.Element in project anton-pavlovich-bot by wyvie.

the class SlovnikCommand method parseHtml.

private List<List<String>> parseHtml(String source) {
    List<List<String>> blocks = new ArrayList<>();
    Document document = Jsoup.parse(source);
    Elements results = document.select("div#results");
    // #results > div > h1 - subject itself or nothing have been found message
    List<String> headFastDefLine = new ArrayList<>();
    headFastDefLine.add(String.format("<b>%s</b>", results.select("h1").text()));
    results.select("#fastMeanings a").forEach(element -> {
        String hrefNew = BASE_SLOVNIK_URL + element.attr("href");
        element = element.attr("href", hrefNew);
        headFastDefLine.add(element.toString());
    });
    // extended grammatics
    List<String> extDefs = new ArrayList<>();
    results.select("ol li dl").forEach(dl -> {
        Element dt = dl.selectFirst("dt");
        String def = dt.text();
        String aTagsDefs = dt.select("a").stream().map(e -> {
            String hrefNew = BASE_SLOVNIK_URL + e.attr("href");
            return e.attr("href", hrefNew).toString();
        }).collect(Collectors.joining(","));
        extDefs.add(String.format("%s %s", aTagsDefs, def));
        dl.select("dd").forEach(dd -> {
            extDefs.add(dd.wholeText());
        });
    });
    // additional definitions links
    List<String> moreDefs = new ArrayList<>();
    results.select("ul.moreResults li").forEach(li -> {
        Element aTag = li.selectFirst("a");
        li.selectFirst("a").remove();
        String hrefNew = BASE_SLOVNIK_URL + aTag.attr("href");
        aTag = aTag.attr("href", hrefNew);
        String liText = li.text();
        moreDefs.add(String.format("%s <i>%s</i>", aTag, liText));
    });
    // synonyms/antonyms
    List<String> synDefs = new ArrayList<>();
    results.select("div.other-meaning a").forEach(a -> {
        String hrefNew = BASE_SLOVNIK_URL + a.attr("href");
        synDefs.add(a.attr("href", hrefNew).toString());
    });
    // additional definitions links
    List<String> addDefs = new ArrayList<>();
    results.select("#fulltext li").forEach(li -> {
        Element aTag = li.selectFirst("a");
        li.selectFirst("a").remove();
        String hrefNew = BASE_SLOVNIK_URL + aTag.attr("href");
        aTag = aTag.attr("href", hrefNew);
        String liText = li.text();
        addDefs.add(String.format("%s <i>%s</i>", aTag, liText));
    });
    blocks.add(headFastDefLine);
    blocks.add(extDefs);
    blocks.add(synDefs);
    blocks.add(moreDefs);
    blocks.add(addDefs);
    return blocks;
}
Also used : Arrays(java.util.Arrays) UrlHelper(org.wyvie.chehov.bot.commands.helper.UrlHelper) Logger(org.slf4j.Logger) ParseMode(com.pengrad.telegrambot.model.request.ParseMode) User(com.pengrad.telegrambot.model.User) SendMessage(com.pengrad.telegrambot.request.SendMessage) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SendResponse(com.pengrad.telegrambot.response.SendResponse) List(java.util.List) Message(com.pengrad.telegrambot.model.Message) Service(org.springframework.stereotype.Service) Document(org.jsoup.nodes.Document) Element(org.jsoup.nodes.Element) Qualifier(org.springframework.beans.factory.annotation.Qualifier) CommandHandler(org.wyvie.chehov.bot.commands.CommandHandler) Jsoup(org.jsoup.Jsoup) Elements(org.jsoup.select.Elements) TelegramBot(com.pengrad.telegrambot.TelegramBot) StringUtils(org.springframework.util.StringUtils) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 48 with Element

use of org.jsoup.nodes.Element in project flow by vaadin.

the class BootstrapHandlerTest method no_body_size_or_page_configurator_still_adds_margin_for_body.

// 2344
@Test
public void no_body_size_or_page_configurator_still_adds_margin_for_body() throws InvalidRouteConfigurationException {
    initUI(testUI, createVaadinRequest(), Collections.singleton(RootNavigationTarget.class));
    Document page = BootstrapHandler.getBootstrapPage(new BootstrapContext(request, null, session, testUI));
    Elements allElements = page.head().getAllElements();
    Optional<Element> styleTag = allElements.stream().filter(element -> element.tagName().equals("style")).findFirst();
    Assert.assertTrue("Expected a style element in head.", styleTag.isPresent());
    Assert.assertTrue("The first style tag should start with body style containing margin", styleTag.get().toString().startsWith("<style type=\"text/css\">body {margin:0;}"));
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) Inline(com.vaadin.flow.component.page.Inline) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) TargetElement(com.vaadin.flow.component.page.TargetElement) Registration(com.vaadin.flow.shared.Registration) PageTitle(com.vaadin.flow.router.PageTitle) Router(com.vaadin.flow.router.Router) Route(com.vaadin.flow.router.Route) RouteAlias(com.vaadin.flow.router.RouteAlias) Theme(com.vaadin.flow.theme.Theme) Assert.assertThat(org.junit.Assert.assertThat) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) After(org.junit.After) UI(com.vaadin.flow.component.UI) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) BodySize(com.vaadin.flow.component.page.BodySize) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Elements(org.jsoup.select.Elements) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Matchers(org.mockito.Matchers) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Before(org.junit.Before) Text(com.vaadin.flow.component.Text) RouterLayout(com.vaadin.flow.router.RouterLayout) Html(com.vaadin.flow.component.Html) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) InlineTemplate(com.vaadin.flow.template.angular.InlineTemplate) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Mockito(org.mockito.Mockito) Assert(org.junit.Assert) Collections(java.util.Collections) Viewport(com.vaadin.flow.component.page.Viewport) ParentLayout(com.vaadin.flow.router.ParentLayout) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) TargetElement(com.vaadin.flow.component.page.TargetElement) Element(org.jsoup.nodes.Element) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Example 49 with Element

use of org.jsoup.nodes.Element in project flow by vaadin.

the class BootstrapHandlerTest method es6NotSupported_webcomponentsPolyfillBasePresent_polyfillsLoaded.

@Test
public void es6NotSupported_webcomponentsPolyfillBasePresent_polyfillsLoaded() {
    Mockito.when(browser.isEs6Supported()).thenReturn(false);
    Element head = initTestUI();
    checkInlinedScript(head, "es6-collections.js", true);
    checkInlinedScript(head, "babel-helpers.min.js", true);
}
Also used : TargetElement(com.vaadin.flow.component.page.TargetElement) Element(org.jsoup.nodes.Element) Test(org.junit.Test)

Example 50 with Element

use of org.jsoup.nodes.Element in project flow by vaadin.

the class BootstrapHandler method createInlineJavaScriptElement.

private static Element createInlineJavaScriptElement(String javaScriptContents) {
    // defer makes no sense without src:
    // https://developer.mozilla.org/en/docs/Web/HTML/Element/script
    Element wrapper = createJavaScriptElement(null, false);
    wrapper.appendChild(new DataNode(javaScriptContents, wrapper.baseUri()));
    return wrapper;
}
Also used : DataNode(org.jsoup.nodes.DataNode) TargetElement(com.vaadin.flow.component.page.TargetElement) Element(org.jsoup.nodes.Element)

Aggregations

Element (org.jsoup.nodes.Element)1237 Document (org.jsoup.nodes.Document)559 Elements (org.jsoup.select.Elements)529 ArrayList (java.util.ArrayList)316 IOException (java.io.IOException)220 Test (org.junit.Test)144 ElementHandlerImpl (org.asqatasun.ruleimplementation.ElementHandlerImpl)90 File (java.io.File)87 URL (java.net.URL)82 Matcher (java.util.regex.Matcher)73 List (java.util.List)60 HashMap (java.util.HashMap)57 Pattern (java.util.regex.Pattern)54 Node (org.jsoup.nodes.Node)50 TextNode (org.jsoup.nodes.TextNode)48 InputStream (java.io.InputStream)38 JSONException (org.json.JSONException)36 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)35 Map (java.util.Map)34 JSONObject (org.json.JSONObject)34