Example usage for org.jsoup.nodes Element childNode

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

Introduction

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

Prototype

public Node childNode(int index) 

Source Link

Document

Get a child node by its 0-based index.

Usage

From source file:com.geecko.QuickLyric.lyrics.LyricWiki.java

public static Lyrics fromURL(String url, String artist, String song) {
    if (url.endsWith("action=edit")) {
        return new Lyrics(NO_RESULT);
    }//from   ww  w .  j a va2  s.  c  o m
    String text;
    try {
        //url = URLDecoder.decode(url, "utf-8");
        Document lyricsPage = Jsoup.connect(url).get();
        Element lyricbox = lyricsPage.select("div.lyricBox").get(0);
        lyricbox.after(lyricbox.childNode(0));
        String lyricsHtml = lyricbox.html();
        text = lyricsHtml.substring(0, lyricsHtml.indexOf("<!--")).replaceAll("<.*?>", "").replaceAll("\n",
                "<br />");
        if (text.contains("&#"))
            text = Parser.unescapeEntities(text, true);
    } catch (IndexOutOfBoundsException | IOException e) {
        e.printStackTrace();
        return new Lyrics(ERROR);
    }

    if (artist == null)
        artist = url.substring(24).replace("Gracenote:", "").split(":", 2)[0].replace('_', ' ');
    if (song == null)
        song = url.substring(24).replace("Gracenote:", "").split(":", 2)[1].replace('_', ' ');

    try {
        artist = URLDecoder.decode(artist, "UTF-8");
        song = URLDecoder.decode(song, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    if (text.contains(
            "Unfortunately, we are not licensed to display the full lyrics for this song at the moment.")
            || text.equals("Instrumental <br />")) {
        Lyrics result = new Lyrics(NEGATIVE_RESULT);
        result.setArtist(artist);
        result.setTitle(song);
        return result;
    } else if (text.equals("") || text.length() < 3)
        return new Lyrics(NO_RESULT);
    else {
        Lyrics lyrics = new Lyrics(POSITIVE_RESULT);
        lyrics.setArtist(artist);
        lyrics.setTitle(song);
        lyrics.setText(text);
        lyrics.setSource("LyricsWiki");
        lyrics.setURL(url);
        return lyrics;
    }
}

From source file:com.decker.parkingSearch.receiver.ParkingContentReceiver.java

public void fetch() throws IOException {

    Document doc = Jsoup.connect(this.baseUrl).get();
    Elements detailBox = doc.select("td[style=\"vertical-align:top;\"]");

    for (Element es : detailBox) {
        try {/*  ww  w.j  a va 2 s  .  c om*/
            Park detail = new Park();
            detail.name = es.childNode(0) instanceof Element ? ((Element) es.childNode(0)).text() : "";
            detail.address = "";
            for (int i = 1; i < es.childNodes().size() - 1; i++) {
                String content = (es.childNodes().get(i)).toString();
                if (content.equals((es.childNodes().get(i + 1)).toString())) {
                    break;
                } else {

                    if (!content.equals("<br>")) {
                        detail.address += (StringEscapeUtils.unescapeHtml(content) + " ");
                    }
                }
            }
            if (es.select("span > a").size() == 0) {
                continue;
            }
            String secretContent = StringEscapeUtils
                    .unescapeHtml(es.select("span > a").get(0).attr("href").replaceAll("\"", ""));
            Matcher matcher = Pattern.compile("(?<=javascript\\:count\\().*(?=\\))").matcher(secretContent);
            String[] secretInfoList;
            if (matcher.find()) {
                secretInfoList = matcher.group().split(",");
            } else {
                continue;
            }
            String mobContent = Jsoup
                    .connect(String.format("http://www.goseeaustralia.com.au/statslookup.asp?keyID=%s&StatID=0",
                            secretInfoList[1]))
                    .get().text();
            detail.mobileNumber = StringUtils.isNotBlank(mobContent) ? mobContent : "";
            String phoneContent = Jsoup
                    .connect(String.format("http://www.goseeaustralia.com.au/statslookup.asp?keyID=%s&StatID=1",
                            secretInfoList[1]))
                    .get().text();
            detail.phoneNumber = StringUtils.isNotBlank(phoneContent) ? phoneContent : "";
            String faxContent = Jsoup
                    .connect(String.format("http://www.goseeaustralia.com.au/statslookup.asp?keyID=%s&StatID=2",
                            secretInfoList[1]))
                    .get().text();
            detail.faxNumber = StringUtils.isNotBlank(faxContent) ? faxContent : "";
            detail.email = StringUtils.isNotBlank(secretInfoList[2]) ? secretInfoList[2] : "";
            this.info.parks.add(detail);
        } catch (Exception ex) {
            System.out.printf("Error during fetch %s park with url %s %n", es.childNode(0).toString(),
                    this.baseUrl);
            ex.printStackTrace();
        }
    }

}

From source file:mobi.jenkinsci.ci.client.JenkinsClient.java

private String getCommitIdFromRow(final Element row) {
    final Element fullChangeDesc = row.select("div[class=changeset-message]").first();
    if (fullChangeDesc == null) {
        return null;
    }/*from  ww  w .  j a  va2  s.c  om*/
    final Element message = fullChangeDesc.select("b").first();
    final String messageText = message.childNode(0).toString();
    final Matcher commitMatch = Pattern.compile("Commit ([^ ]+)").matcher(messageText);
    if (commitMatch.find()) {
        return commitMatch.group(1);
    } else {
        return null;
    }
}

From source file:mobi.jenkinsci.alm.assembla.client.AssemblaClient.java

public void login() throws IOException {
    Document pinDoc = Jsoup.parse(getData(String.format(AUTH, appId), false));
    if (getLatestRedirectedUrl().getPath().startsWith(LOGIN)) {
        pinDoc = postLoginForm(pinDoc);// w ww .j  a v a  2  s .  com
    }

    final Element pinBox = pinDoc.select("div[class=box]").first();
    if (pinBox == null) {
        throw new IOException("Missing PIN code from Assembla auth response");
    }
    final Element pinLabel = pinBox.select("p").first();
    final Element pinValue = pinBox.select("h1").first();
    if (pinLabel == null || pinValue == null) {
        throw new IOException("Missing PIN code from Assembla auth response");
    }
    final String pin = pinValue.childNode(0).toString();
    final HttpPost authPost = new HttpPost(
            String.format(ASSEMBLA_SITE_APP_AUTH, appId, appSecret) + String.format(PIN_AUTH, pin));
    final HttpResponse pinResponse = httpClient.execute(authPost);
    try {
        if (pinResponse.getStatusLine().getStatusCode() != HttpURLConnection.HTTP_OK) {
            throw new IOException(
                    "Post " + authPost.getURI() + " for a PIN failed: " + pinResponse.getStatusLine());
        }
        accessToken = gson.fromJson(
                new JsonReader(new InputStreamReader(pinResponse.getEntity().getContent(), "UTF-8")),
                AssemblaAccessToken.class);
    } finally {
        authPost.releaseConnection();
    }
}

From source file:ca.appvelopers.mcgillmobile.model.retrofit.CourseResultConverter.java

@Override
public List<CourseResult> convert(ResponseBody value) throws IOException {
    String html = value.string();
    List<CourseResult> courses = new ArrayList<>();
    Document document = Jsoup.parse(html, "UTF-8");
    //Parse the response body into a list of rows
    Elements rows = document.getElementsByClass("dddefault");

    // Parse the term from the page header
    Element header = document.getElementsByClass("staticheaders").get(0);
    Term term = Term.parseTerm(header.childNode(2).toString());

    // Get the table in the form of a set of rows
    Element table = document.getElementsByClass("datadisplaytable").get(0).select("tbody").get(0);

    // Go through the rows in the table
    for (Element row : table.select("tr")) {
        // Check that there at least 19 elements in the row
        Elements rowElements = row.select("td");
        if (rowElements.size() < 19) {
            // If there aren't, it must not be a course row
            continue;
        }/* www.j a  v  a 2 s .  c  o m*/

        // Create a new course object with the default values
        double credits = 99;
        String subject = null;
        String number = null;
        String title = "";
        String type = "";
        List<DayOfWeek> days = new ArrayList<>();
        int crn = 0;
        String instructor = "";
        String location = "";
        //So that the rounded start time will be 0
        LocalTime startTime = ScheduleConverter.getDefaultStartTime();
        LocalTime endTime = ScheduleConverter.getDefaultEndTime();
        int capacity = 0;
        int seatsRemaining = 0;
        int waitlistRemaining = 0;
        LocalDate startDate = LocalDate.now();
        LocalDate endDate = LocalDate.now();

        try {
            for (int i = 0; i < rowElements.size(); i++) {
                if (rowElements.get(i).toString().contains("&nbsp;")) {
                    // Empty row: continue
                    continue;
                }
                String rowString = rowElements.get(i).text();

                switch (i) {
                // CRN
                case 1:
                    crn = Integer.parseInt(rowString);
                    break;
                // Subject
                case 2:
                    subject = rowString;
                    break;
                // Number
                case 3:
                    number = rowString;
                    break;
                // Type
                case 5:
                    type = rowString;
                    break;
                // Number of credits
                case 6:
                    credits = Double.parseDouble(rowString);
                    break;
                // Course title
                case 7:
                    //Remove the extra period at the end of the course title
                    title = rowString.substring(0, rowString.length() - 1);
                    break;
                // Days of the week
                case 8:
                    if (rowString.equals("TBA")) {
                        // TBA Stuff: no time associated so skip the next one
                        // and add a dummy to keep the index correct
                        rowElements.add(9, null);
                        i++;
                    } else {
                        // Day Parsing
                        rowString = rowString.replace('\u00A0', ' ').trim();
                        for (int k = 0; k < rowString.length(); k++) {
                            days.add(DayUtils.getDay(rowString.charAt(k)));
                        }
                    }
                    break;
                // Time
                case 9:
                    String[] times = rowString.split("-");
                    try {
                        int startHour = Integer.parseInt(times[0].split(" ")[0].split(":")[0]);
                        int startMinute = Integer.parseInt(times[0].split(" ")[0].split(":")[1]);
                        int endHour = Integer.parseInt(times[1].split(" ")[0].split(":")[0]);
                        int endMinute = Integer.parseInt(times[1].split(" ")[0].split(":")[1]);

                        //If it's PM, then add 12 hours to the hours for 24 hours format
                        //Make sure it isn't noon
                        String startPM = times[0].split(" ")[1];
                        if (startPM.equals("PM") && startHour != 12) {
                            startHour += 12;
                        }

                        String endPM = times[1].split(" ")[1];
                        if (endPM.equals("PM") && endHour != 12) {
                            endHour += 12;
                        }

                        startTime = LocalTime.of(startHour, startMinute);
                        endTime = LocalTime.of(endHour, endMinute);
                    } catch (NumberFormatException e) {
                        //Courses sometimes don't have assigned times
                        startTime = ScheduleConverter.getDefaultStartTime();
                        endTime = ScheduleConverter.getDefaultEndTime();
                    }
                    break;
                // Capacity
                case 10:
                    capacity = Integer.parseInt(rowString);
                    break;
                // Seats remaining
                case 12:
                    seatsRemaining = Integer.parseInt(rowString);
                    break;
                // Waitlist remaining
                case 15:
                    waitlistRemaining = Integer.parseInt(rowString);
                    break;
                // Instructor
                case 16:
                    instructor = rowString;
                    break;
                // Start/end date
                case 17:
                    Pair<LocalDate, LocalDate> dates = parseDateRange(term, rowString);
                    startDate = dates.first;
                    endDate = dates.second;
                    break;
                // Location
                case 18:
                    location = rowString;
                    break;
                }
            }
        } catch (Exception e) {
            Timber.e(e, "Course Results Parser Error");
        }

        // Don't add any courses with errors
        if (subject != null && number != null) {
            // Create a new course object and add it to list
            // TODO Should we be parsing the course section?
            courses.add(new CourseResult(term, subject, number, title, crn, "", startTime, endTime, days, type,
                    location, instructor, credits, startDate, endDate, capacity, seatsRemaining,
                    waitlistRemaining));
        }
    }

    return courses;
}

From source file:org.bonitasoft.web.designer.visitors.HtmlBuilderVisitorTest.java

@Test
public void should_add_dimension_to_component() throws Exception {
    Element element = CustomAssertions.toBody(visitor.visit(aComponent("pbWidget").withDimension(3).build()));

    assertThatHtmlBody(element.childNode(1).outerHtml()).hasClass("col-xs-3");
}