Example usage for org.jsoup.safety Whitelist simpleText

List of usage examples for org.jsoup.safety Whitelist simpleText

Introduction

In this page you can find the example usage for org.jsoup.safety Whitelist simpleText.

Prototype

public static Whitelist simpleText() 

Source Link

Document

This whitelist allows only simple text formatting: b, em, i, strong, u.

Usage

From source file:com.test.shopping.view.ProductDetailFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /*/*from ww  w.  j  av  a  2  s .  co m*/
     * Inflate the layout based on the current product details.
     * Fetch the product associated with the current position in product id list
     */
    ProductDataModel product = CacheUtil.getInstance(getActivity()).getProduct(mPosition);

    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.product_detail, container, false);

    TextView productName = (TextView) rootView.findViewById(R.id.product_name);
    productName.setText(StringEscapeUtils.unescapeJava(product.getProductName()));

    String longDes = product.getLongDescription();
    TextView longDescLabel = (TextView) rootView.findViewById(R.id.long_description_label);
    TextView longDesc = (TextView) rootView.findViewById(R.id.long_description);
    if (longDes != null && longDes.length() > 0) {
        longDesc.setText(Jsoup.clean(longDes, Whitelist.simpleText()));
        longDesc.setVisibility(View.VISIBLE);
        longDescLabel.setVisibility(View.VISIBLE);
    } else {
        longDesc.setVisibility(View.INVISIBLE);
        longDescLabel.setVisibility(View.INVISIBLE);
    }

    TextView price = (TextView) rootView.findViewById(R.id.price);
    price.setText(product.getPrice());

    ImageView imageView = (ImageView) rootView.findViewById(R.id.image);
    ImageLoader loader = ConnectionUtil.getInstance(sContext).getImageLoader();
    loader.get(product.getProductImage(),
            ImageLoader.getImageListener(imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher));

    TextView ratingCountView = (TextView) rootView.findViewById(R.id.rating_count);

    ratingCountView.setText("(" + String.valueOf(product.getReviewCount() + ")"));

    RatingBar bar = (RatingBar) rootView.findViewById(R.id.ratingBar);

    bar.setRating((int) product.getReviewRating());
    bar.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });

    TextView inStock = (TextView) rootView.findViewById(R.id.inStock);
    if (product.isInStock()) {
        inStock.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
        inStock.setText(R.string.in_stock_label);
    } else {
        inStock.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
        inStock.setText(R.string.out_of_stock_label);
    }

    return rootView;
}

From source file:com.metadot.book.connectr.server.domain.StreamItem.java

/**
 * convert a feed entry to a StreamItem/*from   ww  w . j  av  a 2  s  .  co m*/
 */
@SuppressWarnings("unchecked")
private static StreamItem buildItem(String urlstring, SyndFeed sf, SyndEntry entry, Set<Long> ukeys) {

    StreamItem item = null;
    String title = "", url = "", description = "", feedDescription = "", feedTitle = "", author = "",
            imageUrl = "";
    Date date = null;
    String descr = null;

    try {
        // gather all the information.....
        url = entry.getLink();
        date = entry.getPublishedDate();
        feedTitle = sf.getTitle();
        feedDescription = sf.getDescription();
        title = Jsoup.clean(entry.getTitle(), Whitelist.simpleText());
        // for twitter, first remove the leading author string
        if (url.contains("twitter.com")) {
            int cindex = title.indexOf(":");
            if (cindex > 0) {
                title = title.substring(cindex + 1);
            }
        }
        if (entry.getDescription() != null) {
            descr = entry.getDescription().getValue();
        }
        if (descr == null) {
            if (entry.getContents().size() > 0) {
                SyndContent content = (SyndContent) entry.getContents().get(0);
                if (content.getType().equalsIgnoreCase("html")) {
                    descr = content.getValue();
                }
            }
        }
        if (descr != null) {
            // sanitize the content 
            description = Jsoup.clean(descr, Whitelist.basicWithImages());
        }

        List<SyndPerson> sauthors = entry.getAuthors();
        List<String> auths = new ArrayList<String>();
        for (SyndPerson auth : sauthors) {
            auths.add(auth.getName());
        }
        author = StringUtils.join(auths, ", ");
        SyndImage eImg = sf.getImage();
        if (eImg != null) {
            imageUrl = eImg.getUrl();
        } else {
            // if twitter link... 
            if (url.contains("twitter.com")) {
                // then see if a second link is available-- if so, it should be the link to the image
                List<SyndLink> links = entry.getLinks();
                if (links.size() >= 2) {
                    SyndLink imgl = links.get(1);
                    imageUrl = imgl.getHref();
                }
            }
        }
        item = new StreamItem(title, description, feedDescription, date, feedTitle, author, url, urlstring,
                imageUrl, ukeys);
        item.buildDescrSummary();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return item;
}

From source file:info.mikaelsvensson.devtools.sitesearch.SiteSearchPlugin.java

private IndexEntry createIndexEntry(final File file) {
    try {// ww w  .  j  a v a  2s. c o m
        Document document = Jsoup.parse(file, "UTF-8", "http://invalid.host");
        Element contentEl = document.getElementById("contentBox");
        if (contentEl == null) {
            contentEl = document.body();
        }
        if (contentEl != null) {
            String text = Jsoup.clean(contentEl.html(), Whitelist.simpleText());
            Collection<WordCount> wordCount = getWordCount(text);
            Collection<WordCount> filteredWordCount = filterWordCount(wordCount);
            return new IndexEntry(document.title(), getRelativePath(getSiteOutputFolder(), file),
                    filteredWordCount);
        }
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    return null;
}

From source file:com.lloydtorres.stately.helpers.SparkleHelper.java

/**
 * Transform NationStates' BBCode-formatted content into HTML
 * @param c App context/* www.j a v a2  s  . c  o m*/
 * @param t TextView
 * @param content Target content
 * @param fm FragmentManager to show spoiler dialogs in
 */
public static void setBbCodeFormatting(Context c, TextView t, String content, FragmentManager fm) {
    if (content == null || content.length() < 0) {
        return;
    }

    String holder = content.trim();
    holder = holder.replace("\n", "<br>");
    holder = holder.replace("&amp;#39;", "'");
    holder = holder.replace("&amp;", "&");
    holder = Jsoup.clean(holder, Whitelist.simpleText().addTags("br"));

    // Replace raw NS nation and region links with Stately versions
    holder = linkifyHelper(c, t, holder, NS_RAW_NATION_LINK, ExploreActivity.EXPLORE_NATION);
    holder = linkifyHelper(c, t, holder, NS_RAW_REGION_LINK, ExploreActivity.EXPLORE_REGION);
    holder = linkifyHelper(c, t, holder, NS_RAW_REGION_LINK_TG, ExploreActivity.EXPLORE_REGION);
    holder = regexReplace(holder, NS_BBCODE_URL_NATION,
            "[url=" + ExploreActivity.EXPLORE_TARGET + "%s/" + ExploreActivity.EXPLORE_NATION + "]");
    holder = regexReplace(holder, NS_BBCODE_URL_REGION,
            "[url=" + ExploreActivity.EXPLORE_TARGET + "%s/" + ExploreActivity.EXPLORE_REGION + "]");

    // Basic BBcode processing
    holder = holder.replaceAll("(?i)\\[hr\\]", "<br>");

    // Process lists first (they're problematic!)
    TextProcessor processor = BBProcessorFactory.getInstance()
            .create(c.getResources().openRawResource(R.raw.bbcode));
    holder = processor.process(holder);
    holder = holder.replace("&lt;", "<");
    holder = holder.replace("&gt;", ">");
    holder = holder.replace("[*]", "<li>");
    holder = Jsoup.clean(holder, Whitelist.relaxed());

    // Q: Why don't you use the BBCode parser instead of doing this manually? :(
    // A: Because it misses some tags for some reason, so it's limited to lists for now.
    holder = regexReplace(holder, BBCODE_B, "<b>%s</b>");
    holder = regexReplace(holder, BBCODE_I, "<i>%s</i>");
    holder = regexReplace(holder, BBCODE_U, "<u>%s</u>");
    holder = regexReplace(holder, BBCODE_PRE, "<code>%s</code>");
    holder = regexDoubleReplace(holder, BBCODE_PROPOSAL, "<a href=\"" + Resolution.PATH_PROPOSAL + "\">%s</a>");
    holder = regexResolutionFormat(holder);
    holder = regexExtract(holder, BBCODE_RESOLUTION_GENERIC);
    holder = regexDoubleReplace(holder, BBCODE_COLOR, "<font color=\"%s\">%s</font>");
    holder = regexDoubleReplace(holder, BBCODE_INTERNAL_URL, "<a href=\"" + BASE_URI_NOSLASH + "/%s\">%s</a>");
    holder = regexGenericUrlFormat(c, holder);
    holder = regexQuoteFormat(c, t, holder);

    // Extract and replace spoilers
    List<Spoiler> spoilers = getSpoilerReplacePairs(c, holder);
    for (int i = 0; i < spoilers.size(); i++) {
        Spoiler s = spoilers.get(i);
        holder = holder.replace(s.raw, s.replacer);
    }

    // Linkify nations and regions
    holder = linkifyHelper(c, t, holder, NS_BBCODE_NATION, ExploreActivity.EXPLORE_NATION);
    holder = linkifyHelper(c, t, holder, NS_BBCODE_NATION_2, ExploreActivity.EXPLORE_NATION);
    holder = linkifyHelper(c, t, holder, NS_BBCODE_NATION_3, ExploreActivity.EXPLORE_NATION);
    holder = linkifyHelper(c, t, holder, NS_BBCODE_REGION, ExploreActivity.EXPLORE_REGION);
    holder = linkifyHelper(c, t, holder, NS_BBCODE_REGION_2, ExploreActivity.EXPLORE_REGION);

    // In case there are no nations or regions to linkify, set and style TextView here too
    setStyledTextView(c, t, holder, spoilers, fm);
}

From source file:org.orcid.utils.OrcidStringUtils.java

public static String stripHtml(String s) {
    String output = Jsoup.clean(s, "", Whitelist.simpleText(), outputSettings);
    // According to
    // http://jsoup.org/apidocs/org/jsoup/nodes/Entities.EscapeMode.html#xhtml
    // jsoup scape lt, gt, amp, apos, and quot for xhtml
    // So we want to restore them
    output = output.replace(LT, DECODED_LT);
    output = output.replace(GT, DECODED_GT);
    output = output.replace(AMP, DECODED_AMP);
    output = output.replace(APOS, DECODED_APOS);
    output = output.replace(QUOT, DECODED_QUOT);
    return output;
}