Example usage for org.jsoup.nodes Element hasText

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

Introduction

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

Prototype

public boolean hasText() 

Source Link

Document

Test if this element has any text content (that is not just whitespace).

Usage

From source file:Main.java

public static String getText(final Element element) {
    Element textTag = element;

    while (!textTag.hasText() && textTag.children().size() > 0) {
        textTag = textTag.children().get(0);
    }//from   w  w  w .j a va  2 s.  c o  m

    return textTag.text();
}

From source file:Main.java

public static String getScrapeText(Elements elements, String query) {
    String resultString = "";
    for (Element element : elements.select(query))
        if (element.hasText())
            resultString += element.text().trim() + "\n";
    return resultString.trim();
}

From source file:org.shareok.data.plosdata.PlosUtil.java

public static String getPlosAck(String html) {

    String ack = "";
    Document doc = Jsoup.parse(html.toString());
    Elements ackLinks = doc.select("a[id=ack]");
    if (!ackLinks.isEmpty()) {
        Element ackDiv = ackLinks.first().parent();
        if (null != ackDiv) {
            Elements ackParagraphs = ackDiv.select("p");
            if (!ackParagraphs.isEmpty()) {
                for (Element element : ackParagraphs) {
                    if (element.hasText())
                        ack += element.text();
                }//from   www .j  ava2  s .co  m
            }
            //System.out.println("the ack = "+ack+"\n\n");
        }
    }

    return ack;
}

From source file:info.smartkit.hairy_batman.query.SogouSearchQuery.java

public void parseWxUserId() {
    Document doc;//from  w ww .  jav  a 2  s . co  m
    try {

        // need http protocol
        doc = Jsoup.connect(GlobalConsts.SOGOU_SEARCH_URL_BASE + wxFoo.getSubscribeId()).get();

        // get all "?:" value of html <span>
        Elements openIdSpans = doc.select(GlobalConsts.SOGOU_SEARCH_WX_USER_ID_HTML_ELEMENTS);
        //
        for (Element openIdSpan : openIdSpans) {
            if (openIdSpan.hasText()) {
                if (openIdSpan.text().contains(GlobalConsts.SOGOU_SEARCH_WX_USER_ID_KEYWORDS)) {
                    // get the value from href attribute
                    LOG.info("openId span text : " + openIdSpan.text());
                    // FIXME:????
                    if (this.wxFoo.getUserId() == null) {
                        this.wxFoo.setOpenId(
                                openIdSpan.text().split(GlobalConsts.SOGOU_SEARCH_WX_USER_ID_KEYWORDS)[1]);
                        LOG.info("saved wxUserId value: " + this.wxFoo.getUserId());
                        GlobalVariables.wxFooListWithUserId.add(this.wxFoo);
                    }
                }
            }
        }

    } catch (IOException e) {
        // e.printStackTrace();
        LOG.error(e.toString());
    }
}

From source file:com.spd.ukraine.lucenewebsearch1.web.IndexingController.java

private boolean prevElementContainsElementText(Element prevElement, Element element) {
    return (prevElement.hasText() && element.hasText() && prevElement.text().contains(element.text()));
}

From source file:mml.handler.post.MMLPostHTMLHandler.java

/**
 * Parse a codeblock/*from  ww w.  ja va2 s . c  o m*/
 * @param elem the element to parse
 * @throws a JSON exception
 */
private void parsePre(Element elem) throws JSONException {
    if (elem.hasText()) {
        int offset = sb.length();
        String name = elem.attr("class");
        if (name == null || name.length() == 0)
            name = "pre";
        Range r = new Range(name, offset, 0);
        stil.add(r);
        if (elem.hasAttr("class")) {
            List<Node> children = elem.childNodes();
            for (Node child : children) {
                if (child instanceof Element) {
                    if (child.nodeName().equals("span"))
                        parseSpan((Element) child);
                    else
                        parseOtherElement((Element) child);
                } else if (child instanceof TextNode)
                    sb.append(((TextNode) child).getWholeText());
            }
        } else
            sb.append(elem.text());
        this.stil.updateLen(r, sb.length() - offset);
    }
    prevWasMilestone = false;
    ensure(1, false);
}

From source file:mml.handler.post.MMLPostHTMLHandler.java

/**
 * Parse a span with a class or not/* www .j a  v a  2 s. co  m*/
 * @param span the span in HTML
 */
private void parseSpan(Element span) throws JSONException {
    if (span.hasText()) {
        int offset = sb.length();
        String name = span.attr("class");
        Range r = new Range(name, offset, 0);
        if (name == null || name.length() == 0)
            name = "span";
        if (isMilestone(name)) {
            pages.add(r);
            sb.append(span.text());
            sb.append("\n");
            pages.updateLen(r, sb.length() - offset);
            prevWasMilestone = true;
        } else if (name.equals("soft-hyphen")) {
            stil.add(r);
            // get previous word
            int i = sb.length() - 1;
            while (i > 0 && !Character.isWhitespace(sb.charAt(i)))
                i--;
            if (i > 0)
                i++;
            String prev = clean(sb.substring(i), true);
            // get next word
            String next = clean(nextWord(span), false);
            if (this.speller.isHardHyphen(prev, next))
                r.name = "hard-hyphen";
            sb.append(span.text());
            stil.updateLen(r, sb.length() - offset);
        } else // span may contain other spans
        {
            stil.add(r);
            List<Node> children = span.childNodes();
            for (Node child : children) {
                if (child instanceof Element) {
                    String nName = child.nodeName().toLowerCase();
                    if (nName.equals("span"))
                        parseSpan((Element) child);
                    else
                        parseOtherElement((Element) child);
                } else if (child instanceof TextNode) {
                    TextNode tn = (TextNode) child;
                    sb.append(tn.text());
                }
            }
            if (isLineFormat(name))
                ensure(1, false);
            stil.updateLen(r, sb.length() - offset);
        }
    }
    // else strangely no text: ignore it
}

From source file:com.adarshahd.indianrailinfo.donate.TrainDetails.java

private void createTableLayoutTrainAvailability() {

    if (mPage.contains("SORRY")) {
        TextView textViewTrnDtls = new TextView(mActivity);
        textViewTrnDtls.setText("Not a valid class, Please select a different class and try again.");
        textViewTrnDtls.setTextAppearance(mActivity, android.R.style.TextAppearance_DeviceDefault_Large);
        textViewTrnDtls.setPadding(10, 10, 10, 10);
        textViewTrnDtls.setTextColor(Color.RED);
        mFrameLayout.removeAllViews();//from   www  . j av a2 s. co  m
        mFrameLayout.addView(textViewTrnDtls);
        if (mDialog.isShowing()) {
            mDialog.cancel();
        }
        return;
    }

    if (mPage.contains("ISL Of")) {
        TextView textViewTrnDtls = new TextView(mActivity);
        textViewTrnDtls.setText("Station is not in ISL Of the Train. \nPlease modify the source/destination!");
        textViewTrnDtls.setTextAppearance(mActivity, android.R.style.TextAppearance_DeviceDefault_Large);
        textViewTrnDtls.setPadding(10, 10, 10, 10);
        textViewTrnDtls.setTextColor(Color.RED);
        mFrameLayout.removeAllViews();
        mFrameLayout.addView(textViewTrnDtls);
        if (mDialog.isShowing()) {
            mDialog.cancel();
        }
        return;
    }

    if (mPage.contains("ERROR")) {
        TextView textViewTrnDtls = new TextView(mActivity);
        textViewTrnDtls.setText("Your request resulted in an error.\nPlease check!");
        textViewTrnDtls.setTextAppearance(mActivity, android.R.style.TextAppearance_DeviceDefault_Large);
        textViewTrnDtls.setPadding(10, 10, 10, 10);
        textViewTrnDtls.setTextColor(Color.RED);
        mFrameLayout.removeAllViews();
        mFrameLayout.addView(textViewTrnDtls);
        if (mDialog.isShowing()) {
            mDialog.cancel();
        }
        return;
    }

    if (mPage.contains("Network Connectivity")) {
        TextView textViewTrnDtls = new TextView(mActivity);
        textViewTrnDtls.setText("Looks like the server is busy.\nPlease try later!");
        textViewTrnDtls.setTextAppearance(mActivity, android.R.style.TextAppearance_DeviceDefault_Large);
        textViewTrnDtls.setPadding(10, 10, 10, 10);
        textViewTrnDtls.setTextColor(Color.RED);
        mFrameLayout.removeAllViews();
        mFrameLayout.addView(textViewTrnDtls);
        if (mDialog.isShowing()) {
            mDialog.cancel();
        }
        return;
    }

    if (mPage.contains("unavailable")) {
        TextView textViewTrnDtls = new TextView(mActivity);
        textViewTrnDtls.setText(
                "Response from server:\n\nYour request could not be processed now.\nPlease try again later!");
        textViewTrnDtls.setTextAppearance(mActivity, android.R.style.TextAppearance_DeviceDefault_Large);
        textViewTrnDtls.setPadding(10, 10, 10, 10);
        textViewTrnDtls.setTextColor(Color.RED);
        mFrameLayout.removeAllViews();
        mFrameLayout.addView(textViewTrnDtls);
        if (mDialog.isShowing()) {
            mDialog.cancel();
        }
        return;
    }

    if (mDetails == null || !mDetails.getTrainNumber().equals(mTrainNumber)) {
        Iterator iterator = null;
        try {
            iterator = mElements.first().parent().parent().parent().getElementsByTag("tr").iterator();
        } catch (Exception e) {
            Log.i("TrainDetails", mPage);
            e.printStackTrace();
            return;
        }
        mListAv = new ArrayList<List<String>>();
        List<String> list;
        Element tmp;
        tmp = (Element) iterator.next();
        list = new ArrayList<String>();
        list.add(tmp.select("th").get(0).text());
        list.add("Date");
        list.add(tmp.select("th").get(2).text());
        //list.add(tmp.select("th").get(3).text());
        mListAv.add(list);
        while (iterator.hasNext()) {
            tmp = (Element) iterator.next();
            if (!tmp.hasText()) {
                continue;
            }
            list = new ArrayList<String>();
            list.add(tmp.select("td").get(0).text());
            list.add(tmp.select("td").get(1).text());
            list.add(tmp.select("td").get(2).text());
            //list.add(tmp.select("td").get(3).text());
            mListAv.add(list);
        }
        mDetails = new Details(mListAv, TrainEnquiry.AVAILABILITY, mTrainNumber);
    } else {
        mListAv = mDetails.getList();
    }
    mTblLayoutAv = new TableLayout(mActivity);
    TableRow row;
    TextView tv1, tv2, tv3;
    mTblLayoutAv.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    for (int i = 0; i < mListAv.size(); i++) {
        row = new TableRow(mActivity);
        tv1 = new TextView(mActivity);
        tv2 = new TextView(mActivity);
        tv3 = new TextView(mActivity);
        //tv4 = new TextView(mActivity);

        tv1.setText("   " + mListAv.get(i).get(0));
        tv2.setText("   " + mListAv.get(i).get(1));
        tv3.setText("   " + mListAv.get(i).get(2));
        //tv4.setText("   " + mListAv.get(i).get(3));

        tv1.setTextAppearance(mActivity, android.R.style.TextAppearance_DeviceDefault_Medium);
        tv2.setTextAppearance(mActivity, android.R.style.TextAppearance_DeviceDefault_Medium);
        tv3.setTextAppearance(mActivity, android.R.style.TextAppearance_DeviceDefault_Medium);
        //tv4.setTextAppearance(mActivity,android.R.style.TextAppearance_DeviceDefault_Medium);

        tv1.setPadding(5, 5, 5, 5);
        tv2.setPadding(5, 5, 5, 5);
        tv3.setPadding(5, 5, 5, 5);
        //tv4.setPadding(5,5,5,5);

        /*tv2.setBackgroundResource(R.drawable.card_divider);
        tv3.setBackgroundResource(R.drawable.card_divider);
        tv4.setBackgroundResource(R.drawable.card_divider);*/

        row.addView(tv1);
        row.addView(tv2);
        row.addView(tv3);
        //row.addView(tv4);

        row.setBackgroundResource(R.drawable.button_selector);
        row.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        row.setOnClickListener(this);
        mTblLayoutAv.addView(row);
    }
    LinearLayout ll = new LinearLayout(mActivity);
    ScrollView scrollView = new ScrollView(mActivity);
    TextView textViewTrnDtls = new TextView(mActivity);
    textViewTrnDtls.setText("Availability details:");
    textViewTrnDtls.setTextAppearance(mActivity, android.R.style.TextAppearance_DeviceDefault_Large);
    textViewTrnDtls.setPadding(10, 10, 10, 10);
    ll.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.addView(textViewTrnDtls);
    ll.addView(mTblLayoutAv);
    scrollView.addView(ll);
    mFrameLayout.removeAllViews();
    mFrameLayout.addView(scrollView);
    if (mDialog.isShowing()) {
        mDialog.cancel();
    }
}

From source file:org.lockss.extractor.JsoupTagExtractor.java

/**
 * extract the values for the as defined by the selectors and store them in
 * article. These can be selectors or they can be css/jquery selection strings
 * metadata/*from w  ww  .j a v a  2  s  .c o  m*/
 * @param doc the jsoup parsed doc
 * @param articleMeta the ArticleMetadata in which to store the selector/value(s)
 */
void extractSelectors(Document doc, ArticleMetadata articleMeta) {

    // if we don't have any selectors there is nothing to do, so we return
    if (m_selectors == null || m_selectors.isEmpty())
        return;

    for (String selector : m_selectors) {
        String val;
        Elements elements = doc.select(selector);
        for (Element element : elements) {
            if (element.hasText()) {
                if (m_isHtml) {
                    val = processHtml(selector, element.text());
                } else {
                    val = processXml(selector, element.text());
                }
                if (theLog.isDebug3())
                    theLog.debug3("Add: " + selector + " = " + val);
                articleMeta.putRaw(selector, val);
            }
        }
    }
}

From source file:org.lockss.extractor.JsoupXmlTagExtractor.java

/**
 * extract the values for the desired tags and store them in article metadata
 * @param doc the jsoup parsed doc//from  w w  w  . j  av  a 2  s .  c om
 * @param articleMeta the ArticleMetadata in which to store the tag/value(s)
 */
void extractTags(Document doc, ArticleMetadata articleMeta) {
    // if we don't have any tags, there is nothing to do so we return
    if (m_tags == null || m_tags.isEmpty())
        return;
    for (String tag : m_tags) {
        String value;
        Elements tag_elements = doc.select(tag);
        for (Element tag_el : tag_elements) {
            if (tag_el.hasText()) {
                value = processXml(tag, tag_el.text());
                articleMeta.putRaw(tag, value);
            }
        }
    }
}