package 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. import java.util.Locale; import java.util.StringTokenizer; import votorola.a.diff.harvest.cache.HarvestCache; /** * Helpers for harvesting. This covers string functions to * ease summarizing at the moment. */ public class HarvestUtil { // avoid instantiation private HarvestUtil() { } /** * Simple helper function to get a shortened version of the message body. * You might need to do cleanup yourself depending on how the body is * formatted before using this function. At the moment this removes links * {@linkplain #removeLinks(String)} and calls * {@linkplain #cutSummaryInWords(String)}. * * @param toSummarize * Message body or content. * @return Summary of the message body. */ public static String summarize(final String toSummarize) { final String noLinks = removeLinks(toSummarize); return cutSummaryInWords(noLinks); } /** * Cuts the summary in words until #CHARLIMIT is reached. * * @param source * Source string for the summary. * @return Summary consisting of all words before CHARLIMIT is reached. */ public static String cutSummaryInWords(final String source) { final StringTokenizer st = new StringTokenizer(source); final StringBuilder sb = new StringBuilder(); while (st.hasMoreTokens() && sb.length() < HarvestCache.CHARLIMIT) { sb.append(st.nextToken() + " "); } return sb.toString(); } /** * Remove links from the source string (message content). It does not remove * html tags like "a", but only the href-targets. * * @param source * Source string which may contain links. * @return Source string with stripped out hypertext references. */ public static String removeLinks(final String source) { return source.replaceAll("(http|https|ftp)://\\S+", ""); } }