001package votorola.a.diff.harvest; // Copyright 2012. Christian Weilbach. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. 002 003import java.util.Locale; 004import java.util.StringTokenizer; 005 006import votorola.a.diff.harvest.cache.HarvestCache; 007 008/** 009 * Helpers for harvesting. This covers string functions to 010 * ease summarizing at the moment. 011 */ 012public class HarvestUtil { 013 // avoid instantiation 014 private HarvestUtil() { } 015 016 /** 017 * Simple helper function to get a shortened version of the message body. 018 * You might need to do cleanup yourself depending on how the body is 019 * formatted before using this function. At the moment this removes links 020 * {@linkplain #removeLinks(String)} and calls 021 * {@linkplain #cutSummaryInWords(String)}. 022 * 023 * @param toSummarize 024 * Message body or content. 025 * @return Summary of the message body. 026 */ 027 public static String summarize(final String toSummarize) { 028 final String noLinks = removeLinks(toSummarize); 029 return cutSummaryInWords(noLinks); 030 } 031 032 /** 033 * Cuts the summary in words until #CHARLIMIT is reached. 034 * 035 * @param source 036 * Source string for the summary. 037 * @return Summary consisting of all words before CHARLIMIT is reached. 038 */ 039 public static String cutSummaryInWords(final String source) { 040 final StringTokenizer st = new StringTokenizer(source); 041 final StringBuilder sb = new StringBuilder(); 042 while (st.hasMoreTokens() && sb.length() < HarvestCache.CHARLIMIT) { 043 sb.append(st.nextToken() + " "); 044 } 045 return sb.toString(); 046 } 047 048 /** 049 * Remove links from the source string (message content). It does not remove 050 * html tags like "a", but only the href-targets. 051 * 052 * @param source 053 * Source string which may contain links. 054 * @return Source string with stripped out hypertext references. 055 */ 056 public static String removeLinks(final String source) { 057 return source.replaceAll("(http|https|ftp)://\\S+", ""); 058 } 059 060}