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:tr.edu.gsu.nerwip.retrieval.reader.wikipedia.WikipediaReader.java

/**
 * Retrieve the text located in //from  w  w  w.  jav  a 2 s  . c  o  m
 * a hyperlink (A) HTML element.
 * <br/>
 * We ignore all external links,
 * as well as linked images.
 * 
 * @param element
 *       Element to be processed.
 * @param rawStr
 *       Current raw text string.
 * @param linkedStr
 *       Current text with hyperlinks.
 * @return
 *       {@code true} iff the element was processed.
 */
private boolean processHyperlinkElement(Element element, StringBuilder rawStr, StringBuilder linkedStr) {
    boolean result;
    String eltClass = element.attr(XmlNames.ATT_CLASS);

    //      if(eltClass==null)
    {
        result = true;

        // simple text
        String str = element.text();
        if (!str.isEmpty()) {
            rawStr.append(str);

            //if(str.contains("Philadelphia, Pa."))   //debug stuff
            //   System.out.print("");

            // hyperlink
            String eltTitle = element.attr(XmlNames.ATT_TITLE);
            if ((eltClass == null || (!eltClass.contains(CLASS_IMAGE) && !eltClass.contains(CLASS_EXTERNAL)))
                    && (eltTitle == null || (!eltTitle.contains(TITLE_LISTEN)))) {
                String href = element.attr(XmlNames.ATT_HREF);
                String code = "<" + XmlNames.ELT_A + " " + XmlNames.ATT_HREF + "=\"" + href + "\">" + str + "</"
                        + XmlNames.ELT_A + ">";
                linkedStr.append(code);
            } else
                linkedStr.append(str);
        }
    }

    //      else
    //         result = false;

    return result;
}

From source file:gr.scify.newsum.ui.ViewActivity.java

@Override
public void run() {
    // take the String from the TopicActivity
    Bundle extras = getIntent().getExtras();
    Category = extras.getString(CATEGORY_INTENT_VAR);

    // Make sure we have updated the data source
    NewSumUiActivity.setDataSource(this);

    // Get user sources
    String sUserSources = Urls.getUserVisibleURLsAsString(ViewActivity.this);

    // get Topics from TopicActivity (avoid multiple server calls)
    TopicInfo[] tiTopics = TopicActivity.getTopics(sUserSources, Category, this);

    // Also get Topic Titles, to display to adapter
    final String[] saTopicTitles = new String[tiTopics.length];
    // Also get Topic IDs
    final String[] saTopicIDs = new String[tiTopics.length];
    // Also get Dates, in order to show in summary title
    final String[] saTopicDates = new String[tiTopics.length];
    // DeHTML titles
    for (int iCnt = 0; iCnt < tiTopics.length; iCnt++) {
        // update Titles Array
        saTopicTitles[iCnt] = Html.fromHtml(tiTopics[iCnt].getTitle()).toString();
        // update IDs Array
        saTopicIDs[iCnt] = tiTopics[iCnt].getID();
        // update Date Array
        saTopicDates[iCnt] = tiTopics[iCnt].getPrintableDate(NewSumUiActivity.getDefaultLocale());
    }//from   w  w  w.  j  av  a 2 s.  c om
    // get the value of the TopicIDs list size (to use in swipe)
    saTopicIDsLength = saTopicIDs.length;
    final TextView title = (TextView) findViewById(R.id.title);
    // Fill topic spinner
    final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,
            android.R.layout.simple_spinner_item, saTopicTitles);

    final TextView tx = (TextView) findViewById(R.id.textView1);
    //      final float minm = tx.getTextSize();
    //      final float maxm = (minm + 24);

    // Get active topic
    int iTopicNum;
    // If we have returned from a pause
    if (iPrvSelectedItem >= 0)
        // use previous selection before pause
        iTopicNum = iPrvSelectedItem;
    // else
    else
        // use selection from topic page
        iTopicNum = extras.getInt(TOPIC_ID_INTENT_VAR);
    final int num = iTopicNum;

    // create an invisible spinner just to control the summaries of the
    // category (i will use it later on Swipe)
    final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            spinner.setAdapter(adapter);

            // Scroll view init
            final ScrollView scroll = (ScrollView) findViewById(R.id.scrollView1);
            final String[] saTopicTitlesArg = saTopicTitles;
            final String[] saTopicIDsArg = saTopicIDs;
            final String[] SaTopicDatesArg = saTopicDates;

            // Add selection event
            spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    // Changing summary
                    loading = true;
                    showWaitingDialog();
                    // Update visibility of rating bar
                    final RatingBar rb = (RatingBar) findViewById(R.id.ratingBar);
                    rb.setRating(0.0f);
                    rb.setVisibility(View.VISIBLE);
                    final TextView rateLbl = (TextView) findViewById(R.id.rateLbl);
                    rateLbl.setVisibility(View.VISIBLE);
                    scroll.scrollTo(0, 0);

                    String UserSources = Urls.getUserVisibleURLsAsString(ViewActivity.this);

                    String[] saTopicIDs = saTopicIDsArg;

                    // track summary views per category and topic title
                    if (getAnalyticsPref()) {
                        EasyTracker.getTracker().sendEvent(VIEW_SUMMARY_ACTION, Category,
                                saTopicTitlesArg[arg2], 0l);
                    }

                    if (sCustomCategory.trim().length() > 0) {
                        if (Category.equals(sCustomCategory)) {
                            Context ctxCur = NewSumUiActivity.getAppContext(ViewActivity.this);
                            String sCustomCategoryURL = ctxCur.getResources()
                                    .getString(R.string.custom_category_url);
                            // Check if specific element needs to be read
                            String sElementID = ctxCur.getResources()
                                    .getString(R.string.custom_category_elementId);
                            // If an element needs to be selected
                            if (sElementID.trim().length() > 0) {
                                try {
                                    // Check if specific element needs to be read
                                    String sViewOriginalPage = ctxCur.getResources()
                                            .getString(R.string.custom_category_visit_source);
                                    // Init text by a link to the original page
                                    sText = "<p><a href='" + sCustomCategoryURL + "'>" + sViewOriginalPage
                                            + "</a></p>";
                                    // Get document
                                    Document doc = Jsoup.connect(sCustomCategoryURL).get();
                                    // If a table
                                    Element eCur = doc.getElementById(sElementID);
                                    if (eCur.tagName().equalsIgnoreCase("table")) {
                                        // Get table rows
                                        Elements eRows = eCur.select("tr");

                                        // For each row
                                        StringBuffer sTextBuf = new StringBuffer();
                                        for (Element eCurRow : eRows) {
                                            // Append content
                                            // TODO: Use HTML if possible. Now problematic (crashes when we click on link)
                                            sTextBuf.append("<p>" + eCurRow.text() + "</p>");
                                        }
                                        // Return as string
                                        sText = sText + sTextBuf.toString();
                                    } else
                                        // else get text
                                        sText = eCur.text();

                                } catch (IOException e) {
                                    // Show unavailable text
                                    sText = ctxCur.getResources()
                                            .getString(R.string.custom_category_unavailable);
                                    e.printStackTrace();
                                }

                            } else
                                sText = Utils.getFromHttp(sCustomCategoryURL, false);
                        }

                    } else {
                        // call getSummary with (sTopicID, sUserSources). Use "All" for
                        // all Sources
                        String[] Summary = NewSumServiceClient.getSummary(saTopicIDs[arg2], UserSources);
                        // check if Summary exists, otherwise display message
                        if (Summary.length == 0) { // DONE APPLICATION HANGS, DOES NOT
                            // WORK. Updated: Probably OK
                            nothingFound = true;
                            AlertDialog.Builder al = new AlertDialog.Builder(ViewActivity.this);
                            al.setMessage(R.string.shouldReloadSummaries);
                            al.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface arg0, int arg1) {
                                    // Reset cache
                                    CacheController.clearCache();
                                    // Restart main activity
                                    startActivity(new Intent(getApplicationContext(), NewSumUiActivity.class)
                                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                                }
                            });
                            al.setCancelable(false);
                            al.show();
                            // Return to home activity
                            loading = false;
                            return;
                        }
                        // Generate Summary text for normal categories
                        sText = generateSummaryText(Summary, ViewActivity.this);
                        pText = generatesummarypost(Summary, ViewActivity.this);
                    }

                    // Update HTML
                    tx.setText(Html.fromHtml(sText));
                    // Allow links to be followed into browser
                    tx.setMovementMethod(LinkMovementMethod.getInstance());
                    // Also Add Date to Topic Title inside Summary
                    title.setText(saTopicTitlesArg[arg2] + " : " + SaTopicDatesArg[arg2]);

                    // Update size
                    updateTextSize();

                    // Update visited topics
                    TopicActivity.addVisitedTopicID(saTopicIDs[arg2]);
                    // Done
                    loading = false;
                    closeWaitingDialog();
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                }

            });

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // Get active topic
                    spinner.setSelection(num);
                }
            });

        }
    });

    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            showHelpDialog();
        }
    });

    closeWaitingDialog();
}

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

private void parseDropdown(Element dropdownElement, List<SearchField> fields) throws JSONException {
    Elements options = dropdownElement.select("option");
    DropdownSearchField dropdown = new DropdownSearchField();
    if (dropdownElement.parent().select("input[type=hidden]").size() > 0) {
        dropdown.setId(dropdownElement.parent().select("input[type=hidden]").attr("value"));
        dropdown.setData(new JSONObject("{\"restriction\": true}"));
    } else {/*from   ww  w  . ja v a  2  s.  com*/
        dropdown.setId(dropdownElement.attr("name"));
        dropdown.setData(new JSONObject("{\"restriction\": false}"));
    }
    for (Element option : options) {
        dropdown.addDropdownValue(option.attr("value"), option.text());
    }
    dropdown.setDisplayName(dropdownElement.parent().select("label").text());
    fields.add(dropdown);
}

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

protected void parse_reslist(String type, List<ReservedItem> reservations, Document doc, int offset) {
    Elements copytrs = doc.select(".data tr");
    doc.setBaseUri(opac_url);//from w  ww.jav a 2  s .  c o  m
    int trs = copytrs.size();
    if (trs == 1) {
        return;
    }
    assert (trs > 0);
    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;
        }

        item.setTitle(tr.child(1).select("strong").text().trim());
        try {
            String[] rowsplit1 = tr.child(1).html().split("<br[ /]*>");
            String[] rowsplit2 = tr.child(2).html().split("<br[ /]*>");
            if (rowsplit1.length > 1)
                item.setAuthor(rowsplit1[1].trim());
            if (rowsplit2.length > 2)
                item.setBranch(rowsplit2[2].trim());
            if (rowsplit2.length > 2)
                item.setStatus(rowsplit2[0].trim());

            if (tr.select("a").size() == 1) {
                item.setCancelData(type + "$" + offset + "$" + tr.select("a").attr("abs:href").split("\\?")[1]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        reservations.add(item);
    }
    assert (reservations.size() == trs - 1);
}

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

public List<SearchField> getSearchFields() throws IOException, JSONException {
    if (!initialised) {
        start();/*from  w ww .j  a  va 2 s  . c  o m*/
    }

    String html = httpGet(opac_url + "/search.do?methodToCall=switchSearchPage&SearchType=2", ENCODING);
    Document doc = Jsoup.parse(html);
    List<SearchField> fields = new ArrayList<>();

    Elements options = doc.select("select[name=searchCategories[0]] option");
    for (Element option : options) {
        TextSearchField field = new TextSearchField();
        field.setDisplayName(option.text());
        field.setId(option.attr("value"));
        field.setHint("");
        fields.add(field);
    }

    for (Element dropdown : doc.select("#tab-content select")) {
        parseDropdown(dropdown, fields);
    }

    return fields;
}

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

protected void parse_medialist(List<LentItem> media, Document doc, int offset) {
    Elements copytrs = doc.select(".data tr");
    doc.setBaseUri(opac_url);//from  w ww  .ja va 2  s.c  o m

    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN);

    int trs = copytrs.size();
    if (trs == 1) {
        return;
    }
    assert (trs > 0);
    for (int i = 1; i < trs; i++) {
        Element tr = copytrs.get(i);
        LentItem item = new LentItem();

        if (tr.text().contains("keine Daten")) {
            return;
        }

        item.setTitle(tr.child(1).select("strong").text().trim());
        try {
            item.setAuthor(tr.child(1).html().split("<br[ /]*>")[1].trim());

            String[] col2split = tr.child(2).html().split("<br[ /]*>");
            String deadline = col2split[0].trim();
            if (deadline.contains("-")) {
                deadline = deadline.split("-")[1].trim();
            }
            try {
                item.setDeadline(fmt.parseLocalDate(deadline).toString());
            } catch (IllegalArgumentException e1) {
                e1.printStackTrace();
            }

            if (col2split.length > 1) {
                item.setHomeBranch(col2split[1].trim());
            }

            if (tr.select("a").size() > 0) {
                for (Element link : tr.select("a")) {
                    String href = link.attr("abs:href");
                    Map<String, String> hrefq = getQueryParamsFirst(href);
                    if (hrefq.get("methodToCall").equals("renewalPossible")) {
                        item.setProlongData(offset + "$" + href.split("\\?")[1]);
                        item.setRenewable(true);
                        break;
                    }
                }
            } else if (tr.select(".textrot, .textgruen, .textdunkelblau").size() > 0) {
                item.setProlongData("" + tr.select(".textrot, .textgruen, .textdunkelblau").text());
                item.setRenewable(false);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        media.add(item);
    }
    assert (media.size() == trs - 1);

}

From source file:com.astamuse.asta4d.web.form.field.impl.AbstractRadioAndCheckboxRenderer.java

protected Renderer retrieveAndCreateValueMap(final String editTargetSelector,
        final String displayTargetSelector) {
    Renderer render = Renderer.create();
    if (PrepareRenderingDataUtil.retrieveStoredDataFromContextBySelector(editTargetSelector) == null) {

        final List<Pair<String, String>> inputList = new LinkedList<>();

        final List<OptionValuePair> optionList = new LinkedList<>();

        render.add(editTargetSelector, new ElementSetter() {
            @Override//from w w  w  .  jav  a  2 s  .  co  m
            public void set(Element elem) {
                inputList.add(Pair.of(elem.id(), elem.attr("value")));
            }
        });

        render.add(":root", new Renderable() {
            @Override
            public Renderer render() {
                Renderer render = Renderer.create();
                for (Pair<String, String> input : inputList) {
                    String id = input.getLeft();
                    final String value = input.getRight();
                    if (StringUtils.isEmpty(id)) {
                        if (allowNonIdItems()) {
                            optionList.add(new OptionValuePair(value, value));
                        } else {
                            String msg = "The target item[%s] must have id specified.";
                            throw new IllegalArgumentException(String.format(msg, editTargetSelector));
                        }
                    } else {
                        render.add(SelectorUtil.attr("for", id), Renderer.create("label", new ElementSetter() {
                            @Override
                            public void set(Element elem) {
                                optionList.add(new OptionValuePair(value, elem.text()));
                            }
                        }));
                        render.add(":root", new Renderable() {
                            @Override
                            public Renderer render() {
                                PrepareRenderingDataUtil.storeDataToContextBySelector(editTargetSelector,
                                        displayTargetSelector, new OptionValueMap(optionList));
                                return Renderer.create();
                            }
                        });
                    }
                } // end for loop
                return render;
            }
        });
    }
    return render;
}

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

@Override
public ProlongAllResult prolongAll(Account account, int useraction, String selection) throws IOException {
    if (!initialised) {
        start();/*  w w  w .j av  a 2  s  . c o  m*/
    }
    if (System.currentTimeMillis() - logged_in > SESSION_LIFETIME || logged_in_as == null) {
        try {
            account(account);
        } catch (JSONException e) {
            e.printStackTrace();
            return new ProlongAllResult(MultiStepResult.Status.ERROR);
        } catch (OpacErrorException e) {
            return new ProlongAllResult(MultiStepResult.Status.ERROR, e.getMessage());
        }
    } else if (logged_in_as.getId() != account.getId()) {
        try {
            account(account);
        } catch (JSONException e) {
            e.printStackTrace();
            return new ProlongAllResult(MultiStepResult.Status.ERROR);
        } catch (OpacErrorException e) {
            return new ProlongAllResult(MultiStepResult.Status.ERROR, e.getMessage());
        }
    }

    // We have to call the page we originally found the link on first...
    String html = httpGet(opac_url + "/userAccount.do?methodToCall=renewalPossible&renewal=account", ENCODING);
    Document doc = Jsoup.parse(html);

    if (doc.select("table.data").size() > 0) {
        List<Map<String, String>> result = new ArrayList<>();
        for (Element td : doc.select("table.data tr td")) {
            Map<String, String> line = new HashMap<>();
            if (!td.text().contains("Titel") || !td.text().contains("Status")) {
                continue;
            }
            String nextNodeIs = "";
            for (Node n : td.childNodes()) {
                String text;
                if (n instanceof Element) {
                    text = ((Element) n).text();
                } else if (n instanceof TextNode) {
                    text = ((TextNode) n).text();
                } else {
                    continue;
                }
                if (text.trim().length() == 0) {
                    continue;
                }
                if (text.contains("Titel:")) {
                    nextNodeIs = ProlongAllResult.KEY_LINE_TITLE;
                } else if (text.contains("Verfasser:")) {
                    nextNodeIs = ProlongAllResult.KEY_LINE_AUTHOR;
                } else if (text.contains("Leihfristende:")) {
                    nextNodeIs = ProlongAllResult.KEY_LINE_NEW_RETURNDATE;
                } else if (text.contains("Status:")) {
                    nextNodeIs = ProlongAllResult.KEY_LINE_MESSAGE;
                } else if (text.contains("Mediennummer:") || text.contains("Signatur:")) {
                    nextNodeIs = "";
                } else if (nextNodeIs.length() > 0) {
                    line.put(nextNodeIs, text.trim());
                    nextNodeIs = "";
                }
            }
            result.add(line);
        }
        return new ProlongAllResult(MultiStepResult.Status.OK, result);
    }

    return new ProlongAllResult(MultiStepResult.Status.ERROR,
            stringProvider.getString(StringProvider.COULD_NOT_LOAD_ACCOUNT));
}

From source file:ExtractorContentTest.java

private boolean isEmpty(Element row) {
    for (Element header : row.select("th")) {
        String headerV = header.text();
        if (!headerV.isEmpty())
            return false;
    }//from w w w .j av a 2s .  c  o  m
    // all empty
    return true;
}

From source file:ExtractorContentTest.java

private Catalog treatRows(Element table, Tree<String> structuralInformation, List<Header> rHeaders,
        boolean sortable) {
    int I = 0;/*www  .  j  ava  2  s .com*/
    Catalog product = new Catalog(structuralInformation, rHeaders);
    for (Element row : table.select("tr")) {

        Elements lines;
        if (sortable) {
            lines = row.select("th"); // first entry is a header in sortable table
            lines.addAll(row.select("td"));
        } else {
            lines = row.select("td");
        }

        Product p = new Product("product_" + I, structuralInformation, rHeaders);
        int J = 0;
        for (Element line : lines) {
            p.add(J, line.text());
            J++;
        }

        // necessarily a tr with a td
        if (!lines.isEmpty()) {
            if (sortable && (I == 0)) {
                // header (first entry) is not a product
            } else
                product.add(p);
            I++;
        }

    }
    return product;
}