Example usage for org.jsoup.nodes Element text

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

Introduction

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

Prototype

public String text() 

Source Link

Document

Gets the combined text of this element and all its children.

Usage

From source file:com.licubeclub.zionhs.Schedule.java

private void networkTask() {
    SRL.setRefreshing(true);/*w  ww  . j av  a  2  s . com*/
    ScheduleCacheManager manager = new ScheduleCacheManager();
    NetworkChecker NetCheck = new NetworkChecker(Schedule.this);
    if (NetCheck.isNetworkConnected()) {
        final Handler mHandler = new Handler();

        new Thread() {
            public void run() {

                //Task

                //Notices URL
                try {
                    int skip = 0;
                    boolean skipedboolean = false;
                    schedulearray = new ArrayList<String>();
                    dayarray = new ArrayList<String>();

                    //                    ? ?? ? ??  
                    Document doc = Jsoup.connect(URL).get();

                    Elements rawdaydata = doc.select(".listDay"); //Get contents from the class,"listDay"
                    for (Element el : rawdaydata) {
                        String daydata = el.text();
                        if (daydata.equals("") | daydata == null) {
                            if (skipedboolean) {
                            } else {
                                skip++;
                            }
                        } else {
                            dayarray.add(daydata); // add value to ArrayList
                            skipedboolean = true;
                        }
                    }
                    Log.d("Schedule", "Parsed Day Array" + dayarray);

                    Elements rawscheduledata = doc.select(".listData"); //Get contents from tags,"a" which are in the class,"ellipsis"
                    for (Element el : rawscheduledata) {
                        String scheduledata = el.text();
                        if (skip > 0) {
                            skip--;
                        } else {
                            schedulearray.add(scheduledata); // add value to ArrayList
                        }
                    }
                    Log.d("Schedule", "Parsed Schedule Array" + schedulearray);

                    //                    SRL.setRefreshing(false);
                } catch (IOException e) {
                    e.printStackTrace();
                    //                    SRL.setRefreshing(false);

                }
                mHandler.post(new Runnable() {
                    public void run() {
                        //UI Task
                        adapter = new ListCalendarAdapter(Schedule.this, dayarray, schedulearray);
                        listview.setAdapter(adapter);
                        ScheduleCacheManager manager = new ScheduleCacheManager();
                        manager.updateCache(dayarray, schedulearray);
                        SRL.setRefreshing(false);
                        handler.sendEmptyMessage(0);
                    }
                });

            }
        }.start();

    } else {
        Log.d("Schedule", "Loading from Cache");
        Toast.makeText(Schedule.this, getResources().getString(R.string.network_connection_warning),
                Toast.LENGTH_LONG).show();
        dayarray = manager.loadDateCache();
        schedulearray = manager.loadContentCache();
        adapter = new ListCalendarAdapter(Schedule.this, dayarray, schedulearray);
        listview.setAdapter(adapter);
    }

}

From source file:com.lloydtorres.stately.issues.IssueDecisionActivity.java

/**
 * Process the received page into the Issue and its IssueOptions
 * @param v Activity view/*from   w ww  .  j  a va  2s. c  om*/
 * @param d Document received from NationStates
 */
private void processIssueInfo(View v, Document d) {
    // First check if the issue is still available
    if (d.text().contains(NOT_AVAILABLE)) {
        mSwipeRefreshLayout.setRefreshing(false);
        SparkleHelper.makeSnackbar(v,
                String.format(Locale.US, getString(R.string.issue_unavailable), mNation.name));
        return;
    }

    Element issueInfoContainer = d.select("div#dilemma").first();

    if (issueInfoContainer == null) {
        // safety check
        mSwipeRefreshLayout.setRefreshing(false);
        SparkleHelper.makeSnackbar(v, getString(R.string.login_error_parsing));
        return;
    }

    Elements issueInfoRaw = issueInfoContainer.children();

    String issueText = issueInfoRaw.select("p").first().text();
    // If this is an issue chain, grab the second paragraph instead
    if (d.select("div.dilemmachain").first() != null) {
        issueText = issueInfoRaw.select("p").get(1).text();
        if (d.text().contains(STORY_SO_FAR)) {
            issueText = issueText + "<br><br>" + issueInfoRaw.select("p").get(2).text();
        }
    }
    issue.content = issueText;

    issue.options = new ArrayList<IssueOption>();

    Element optionHolderMain = issueInfoRaw.select("ol.diloptions").first();
    if (optionHolderMain != null) {
        Elements optionsHolder = optionHolderMain.select("li");

        int i = 0;
        for (Element option : optionsHolder) {
            IssueOption issueOption = new IssueOption();
            issueOption.index = i++;

            Element button = option.select("button").first();
            if (button != null) {
                issueOption.header = button.attr("name");
            } else {
                issueOption.header = IssueOption.SELECTED_HEADER;
            }

            Element optionContentHolder = option.select("p").first();
            if (optionContentHolder == null) {
                // safety check
                mSwipeRefreshLayout.setRefreshing(false);
                SparkleHelper.makeSnackbar(v, getString(R.string.login_error_parsing));
                return;
            }

            issueOption.content = optionContentHolder.text();
            issue.options.add(issueOption);
        }
    }

    IssueOption dismissOption = new IssueOption();
    dismissOption.index = -1;
    dismissOption.header = IssueOption.DISMISS_HEADER;
    dismissOption.content = "";
    issue.options.add(dismissOption);

    setRecyclerAdapter(issue);
    mSwipeRefreshLayout.setRefreshing(false);
    mSwipeRefreshLayout.setEnabled(false);
}

From source file:com.lloydtorres.stately.issues.IssuesFragment.java

/**
 * Process the HTML contents of the issues into actual Issue objects
 * @param d/*  ww  w.  jav a2s .co m*/
 */
private void processIssues(View v, Document d) {
    issues = new ArrayList<Object>();

    Element issuesContainer = d.select("ul.dilemmalist").first();

    if (issuesContainer == null) {
        // safety check
        mSwipeRefreshLayout.setRefreshing(false);
        SparkleHelper.makeSnackbar(v, getString(R.string.login_error_parsing));
        return;
    }

    Elements issuesRaw = issuesContainer.children();

    for (Element i : issuesRaw) {
        Issue issueCore = new Issue();

        Elements issueContents = i.children();

        // Get issue ID and name
        Element issueMain = issueContents.select("a").first();

        if (issueMain == null) {
            continue;
        }

        String issueLink = issueMain.attr("href");
        issueCore.id = Integer.valueOf(issueLink.replace("page=show_dilemma/dilemma=", ""));
        Matcher chainMatcher = CHAIN_ISSUE_REGEX.matcher(issueMain.text());
        if (chainMatcher.find()) {
            issueCore.chain = chainMatcher.group(1);
            issueCore.title = chainMatcher.group(2);
        } else {
            issueCore.title = issueMain.text();
        }

        issues.add(issueCore);
    }

    Element nextIssueUpdate = d.select("p.dilemmanextupdate").first();
    if (nextIssueUpdate != null) {
        String nextUpdate = nextIssueUpdate.text();
        issues.add(nextUpdate);
    }

    if (issuesRaw.size() <= 0) {
        String nextUpdate = getString(R.string.no_issues);

        Matcher m = NEXT_ISSUE_REGEX.matcher(d.html());
        if (m.find()) {
            long nextUpdateTime = Long.valueOf(m.group(1)) / 1000L;
            nextUpdate = String.format(Locale.US, getString(R.string.next_issue),
                    SparkleHelper.getReadableDateFromUTC(getContext(), nextUpdateTime));
        }

        issues.add(nextUpdate);
    }

    if (mRecyclerAdapter == null) {
        mRecyclerAdapter = new IssuesRecyclerAdapter(getContext(), issues, mNation);
        mRecyclerView.setAdapter(mRecyclerAdapter);
    } else {
        ((IssuesRecyclerAdapter) mRecyclerAdapter).setIssueCards(issues);
    }
    mSwipeRefreshLayout.setRefreshing(false);
}

From source file:com.lloydtorres.stately.telegrams.TelegramsFragment.java

/**
 * Actually parse through the response sent by NS and build telegram objects.
 * @param d Document containing parsed response.
 * @param direction Direction the user is loading telegrams
 * @param firstRun if first time running this process
 */// w  w  w  .j a v  a  2s  .c om
private void processRawTelegrams(Document d, int direction, boolean firstRun) {
    Element telegramsContainer = d.select("div#tglist").first();
    Element foldersContainer = d.select("select#tgfolder").first();

    if (telegramsContainer == null || foldersContainer == null) {
        // safety check
        mSwipeRefreshLayout.setRefreshing(false);
        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_parsing));
        return;
    }

    // Build list of folders
    folders = new ArrayList<TelegramFolder>();
    Elements rawFolders = foldersContainer.select("option[value]");
    for (Element rf : rawFolders) {
        TelegramFolder telFolder = new TelegramFolder();
        String rfValue = rf.attr("value");
        if (!rfValue.equals("_new")) {
            String rfName = rf.text();
            telFolder.name = rfName;
            telFolder.value = rfValue;
            folders.add(telFolder);
        }
    }

    // Build telegram objects from raw telegrams
    ArrayList<Telegram> scannedTelegrams = MuffinsHelper.processRawTelegrams(telegramsContainer,
            PinkaHelper.getActiveUser(getContext()).nationId);
    switch (direction) {
    case SCAN_FORWARD:
        processTelegramsForward(scannedTelegrams, firstRun);
        break;
    default:
        processTelegramsBackward(scannedTelegrams);
        break;
    }

    mSwipeRefreshLayout.setRefreshing(false);
}

From source file:com.near.chimerarevo.fragments.PostFragment.java

private void parseTitles(Elements tils, int type) {
    for (Element ti : tils)
        addTitle(ti.text().trim(), type);
}

From source file:com.near.chimerarevo.fragments.PostFragment.java

private void parseCodeText(Elements cds) {
    for (Element cd : cds)
        addText(cd.text().trim(), false, Typeface.MONOSPACE);
}

From source file:com.near.chimerarevo.fragments.PostFragment.java

private void parseTables(Elements tbls) {
    TableLayout tl = new TableLayout(getActivity());
    LayoutParams tl_prms = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    tl_prms.gravity = Gravity.CENTER_HORIZONTAL;
    tl_prms.setMargins(10, 10, 10, 0);//  www. j ava 2 s  .c  o m
    tl.setLayoutParams(tl_prms);

    for (Element tbl : tbls) {
        Elements rws = tbl.getElementsByTag("td");
        TableRow row = new TableRow(getActivity());
        for (Element rw : rws) {
            TextView txt = new TextView(getActivity());
            txt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT));
            txt.setText(rw.text());
            row.addView(txt);
        }
        tl.addView(row);
    }
    lay.addView(tl);
}

From source file:com.normalexception.app.rx8club.fragment.category.CategoryFragment.java

/**
 * Grab contents from the forum that the user clicked on
 * @param doc      The document parsed from the link
 * @param id      The id number of the link
 * @param isMarket    True if the link is from a marketplace category
 *//*w w  w  .j  a  v a2  s  .  c o  m*/
public void getCategoryContents(Document doc, String id, boolean isMarket) {

    // Update pagination
    try {
        Elements pageNumbers = doc.select("div[class=pagenav]");
        Elements pageLinks = pageNumbers.first().select("td[class^=vbmenu_control]");
        thisPage = pageLinks.text().split(" ")[1];
        finalPage = pageLinks.text().split(" ")[3];
    } catch (Exception e) {
    }

    // Make sure id contains only numbers
    if (!isNewTopicActivity)
        id = Utils.parseInts(id);

    // Grab each thread
    Elements threadListing = doc.select("table[id=threadslist] > tbody > tr");

    for (Element thread : threadListing) {
        try {
            boolean isSticky = false, isLocked = false, hasAttachment = false, isAnnounce = false,
                    isPoll = false;
            String formattedTitle = "", postCount = "0", views = "0", forum = "", threadUser = "",
                    lastUser = "", threadLink = "", lastPage = "", totalPosts = "0", threadDate = "";

            Elements announcementContainer = thread.select("td[colspan=5]");
            Elements threadTitleContainer = thread.select("a[id^=thread_title]");

            // We could have two different types of threads.  Announcement threads are 
            // completely different than the other types of threads (sticky, locked, etc)
            // so we need to play some games here
            if (announcementContainer != null && !announcementContainer.isEmpty()) {
                Log.d(TAG, "Announcement Thread Found");

                Elements annThread = announcementContainer.select("div > a");
                Elements annUser = announcementContainer.select("div > span[class=smallfont]");
                formattedTitle = "Announcement: " + annThread.first().text();
                threadUser = annUser.last().text();
                threadLink = annThread.attr("href");
                isAnnounce = true;
            } else if (threadTitleContainer != null && !threadTitleContainer.isEmpty()) {
                Element threadLinkEl = thread.select("a[id^=thread_title]").first();
                Element repliesText = thread.select("td[title^=Replies]").first();
                Element threaduser = thread.select("td[id^=td_threadtitle_] div.smallfont").first();
                Element threadicon = thread.select("img[id^=thread_statusicon_]").first();
                Element threadDiv = thread.select("td[id^=td_threadtitle_] > div").first();
                Element threadDateFull = thread.select("td[title^=Replies:] > div").first();

                try {
                    isSticky = threadDiv.text().contains("Sticky:");
                } catch (Exception e) {
                }

                try {
                    isPoll = threadDiv.text().contains("Poll:");
                } catch (Exception e) {
                }

                try {
                    String icSt = threadicon.attr("src");
                    isLocked = (icSt.contains("lock") && icSt.endsWith(".gif"));
                } catch (Exception e) {
                }

                String preString = "";
                try {
                    preString = threadDiv.select("span > b").text();
                } catch (Exception e) {
                }

                try {
                    hasAttachment = !threadDiv.select("a[onclick^=attachments]").isEmpty();
                } catch (Exception e) {
                }

                // Find the last page if it exists
                try {
                    lastPage = threadDiv.select("span").last().select("a").last().attr("href");
                } catch (Exception e) {
                }

                threadDate = threadDateFull.text();
                int findAMPM = threadDate.indexOf("M") + 1;
                threadDate = threadDate.substring(0, findAMPM);

                String totalPostsInThreadTitle = threadicon.attr("alt");

                if (totalPostsInThreadTitle != null && totalPostsInThreadTitle.length() > 0)
                    totalPosts = totalPostsInThreadTitle.split(" ")[2];

                // Remove page from the link
                String realLink = Utils.removePageFromLink(link);

                if (threadLinkEl.attr("href").contains(realLink) || (isNewTopicActivity || isMarket)) {

                    String txt = repliesText.getElementsByClass("alt2").attr("title");
                    String splitter[] = txt.split(" ", 4);

                    postCount = splitter[1].substring(0, splitter[1].length() - 1);
                    views = splitter[3];

                    try {
                        if (this.isNewTopicActivity)
                            forum = thread.select("td[class=alt1]").last().text();
                    } catch (Exception e) {
                    }

                    formattedTitle = String.format("%s%s%s", isSticky ? "Sticky: " : isPoll ? "Poll: " : "",
                            preString.length() == 0 ? "" : preString + " ", threadLinkEl.text());
                }

                threadUser = threaduser.text();
                lastUser = repliesText.select("a[href*=members]").text();
                threadLink = threadLinkEl.attr("href");
            }

            // Add our thread to our list as long as the thread
            // contains a title
            if (!formattedTitle.equals("")) {
                ThreadModel tv = new ThreadModel();
                tv.setTitle(formattedTitle);
                tv.setStartUser(threadUser);
                tv.setLastUser(lastUser);
                tv.setLink(threadLink);
                tv.setLastLink(lastPage);
                tv.setPostCount(postCount);
                tv.setMyPosts(totalPosts);
                tv.setViewCount(views);
                tv.setLocked(isLocked);
                tv.setSticky(isSticky);
                tv.setAnnouncement(isAnnounce);
                tv.setPoll(isPoll);
                tv.setHasAttachment(hasAttachment);
                tv.setForum(forum);
                tv.setLastPostTime(threadDate);
                threadlist.add(tv);
            } else if (thread.text()
                    .contains(MainApplication.getAppContext().getString(R.string.constantNoUpdate))) {
                Log.d(TAG, String.format("Found End of New Threads after %d threads...", threadlist.size()));
                if (threadlist.size() > 0) {
                    ThreadModel ltv = threadlist.get(threadlist.size() - 1);
                    Log.d(TAG, String.format("Last New Thread '%s'", ltv.getTitle()));
                }

                if (!PreferenceHelper.hideOldPosts(MainApplication.getAppContext()))
                    threadlist.add(new ThreadModel(true));
                else {
                    Log.d(TAG, "User Chose To Hide Old Threads");
                    break;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Error Parsing That Thread...", e);
            Log.d(TAG, "Thread may have moved");
        }
    }
}

From source file:com.normalexception.app.rx8club.fragment.HomeFragment.java

/**
 * Get the forum contents as a Map that links the category names
 * to a list of the forums within each category
 * @param root   The full forum document
 * @return      A map of the categories to the forums
 *///from   w  w w  . ja  v  a 2 s  .  c om
private void getCategories(Document root) {
    // Grab each category
    Elements categories = root.select("td[class=tcat][colspan=5]");
    Log.d(TAG, "Category Size: " + categories.size());

    // Now grab each section within a category
    Elements categorySections = root.select("tbody[id^=collapseobj_forumbit_]");

    // These should match in size
    if (categories.size() != categorySections.size()) {
        Log.w(TAG, String.format("Size of Categories (%d) doesn't match Category Sections (%d)",
                categories.size(), categorySections.size()));
        return;
    }

    // Iterate over each category
    int catIndex = 0;
    for (Element category : categorySections) {

        CategoryModel cv = new CategoryModel();
        cv.setTitle(categories.get(catIndex++).text());
        mainList.add(cv);

        Elements forums = category.select("tr[align=center]");
        for (Element forum : forums) {
            cv = new CategoryModel();
            List<SubCategoryModel> scvList = cv.getSubCategories();

            // Each forum object should have 5 columns
            Elements columns = forum.select("tr[align=center] > td");
            try {
                if (columns.size() != 5)
                    continue;

                String forum_name = columns.get(HomeFragment.FORUM_NAME).select("strong").text();
                String forum_href = columns.get(HomeFragment.FORUM_NAME).select("a").attr("href");
                String forum_desc = "";
                try {
                    forum_desc = columns.get(HomeFragment.FORUM_NAME).select("div[class=smallfont]").first()
                            .text();
                } catch (NullPointerException npe) {
                    /* Some might not have a desc */ }
                String threads = columns.get(HomeFragment.THREADS_CNT).text();
                String posts = columns.get(HomeFragment.POSTS_CNT).text();

                // Lets grab each subcategory
                Elements subCats = columns.select("tbody a");
                for (Element subCat : subCats) {
                    SubCategoryModel scv = new SubCategoryModel();
                    scv.setLink(subCat.attr("href"));
                    scv.setTitle(subCat.text().toString());
                    scvList.add(scv);
                }

                cv.setTitle(forum_name);
                cv.setThreadCount(threads);
                cv.setPostCount(posts);
                cv.setLink(forum_href);
                cv.setDescription(forum_desc);
                cv.setSubCategories(scvList);
                mainList.add(cv);
            } catch (Exception e) {
                Log.e(TAG, "Error Parsing Forum", e);
            }
        }
    }

    return;
}

From source file:com.normalexception.app.rx8club.fragment.thread.ThreadFragment.java

/**
 * Grab contents from the forum that the user clicked on
 * @param doc   The document parsed from the link
 * @param id   The id number of the link
 * @return      An arraylist of forum contents
 *///  www.j  a v a  2 s .c  om
public void getThreadContents(Document doc) {
    // Update pagination
    try {
        Elements pageNumbers = doc.select("div[class=pagenav]");
        if (pageNumbers.first() != null) {
            Elements pageLinks = pageNumbers.first().select("td[class^=vbmenu_control]");
            thisPage = pageLinks.text().split(" ")[1];
            finalPage = pageLinks.text().split(" ")[3];
            Log.d(TAG, String.format("This Page: %s, Final Page: %s", thisPage, finalPage));
        } else {
            Log.d(TAG, "Thread only contains one page");
        }
    } catch (Exception e) {
        Log.e(TAG, "We had an error with pagination", e);
    }

    // Is user thread admin??
    Elements threadTools = doc.select("div[id=threadtools_menu] > form > table");
    if (threadTools.text().contains(MODERATION_TOOLS)) {
        Log.d(TAG, "<><> User has administrative rights here! <><>");
    } else {
        //adminContent.setVisibility(View.GONE);
        lv.removeHeaderView(adminContent);
    }

    // Get the user's actual ID, there is a chance they never got it
    // before
    UserProfile.getInstance().setUserId(HtmlFormUtils.getInputElementValueByName(doc, "loggedinuser"));

    // Get Post Number and security token
    securityToken = HtmlFormUtils.getInputElementValueByName(doc, "securitytoken");

    Elements pNumber = doc.select("a[href^=http://www.rx8club.com/newreply.php?do=newreply&noquote=1&p=]");
    String pNumberHref = pNumber.attr("href");
    postNumber = pNumberHref.substring(pNumberHref.lastIndexOf("=") + 1);
    threadNumber = doc.select("input[name=searchthreadid]").attr("value");

    Elements posts = doc.select("div[id=posts]").select("div[id^=edit]");
    Log.v(TAG, String.format("Parsing through %d posts", posts.size()));
    for (Element post : posts) {
        try {
            Elements innerPost = post.select("table[id^=post]");

            // User Control Panel
            Elements userCp = innerPost.select("td[class=alt2]");
            Elements userDetail = userCp.select("div[class=smallfont]");
            Elements userSubDetail = userDetail.last().select("div");
            Elements userAvatar = userDetail.select("img[alt$=Avatar]");

            // User Information
            PostModel pv = new PostModel();
            pv.setUserName(userCp.select("div[id^=postmenu]").text());
            pv.setIsLoggedInUser(LoginFactory.getInstance().isLoggedIn()
                    ? UserProfile.getInstance().getUsername().equals(pv.getUserName())
                    : false);
            pv.setUserTitle(userDetail.first().text());
            pv.setUserImageUrl(userAvatar.attr("src"));
            pv.setPostDate(innerPost.select("td[class=thead]").first().text());
            pv.setPostId(Utils.parseInts(post.attr("id")));
            pv.setRootThreadUrl(currentPageLink);

            // get Likes if any exist
            Elements eLikes = innerPost.select("div[class*=vbseo_liked] > a");
            List<String> likes = new ArrayList<String>();
            for (Element eLike : eLikes)
                likes.add(eLike.text());
            pv.setLikes(likes);

            Iterator<Element> itr = userSubDetail.listIterator();
            while (itr.hasNext()) {
                String txt = itr.next().text();
                if (txt.contains("Location:"))
                    pv.setUserLocation(txt);
                else if (txt.contains("Posts:"))
                    pv.setUserPostCount(txt);
                else if (txt.contains("Join Date:"))
                    pv.setJoinDate(txt);
            }

            // User Post Content
            pv.setUserPost(formatUserPost(innerPost));

            // User signature
            try {
                Element userSig = innerPost.select("div[class=konafilter]").first();
                pv.setUserSignature(userSig.html());
            } catch (NullPointerException npe) {
            }

            Elements postAttachments = innerPost.select("a[id^=attachment]");
            if (postAttachments != null && !postAttachments.isEmpty()) {
                ArrayList<String> attachments = new ArrayList<String>();
                for (Element postAttachment : postAttachments) {
                    attachments.add(postAttachment.attr("href"));
                }
                pv.setAttachments(attachments);
            }

            pv.setSecurityToken(securityToken);

            // Make sure we aren't adding a blank user post
            if (pv.getUserPost() != null)
                postlist.add(pv);
        } catch (Exception e) {
            Log.w(TAG, "Error Parsing Post...Probably Deleted");
        }
    }
}