1 25 26 package org.snipsnap.semanticweb.rss; 27 28 import org.radeox.util.logging.Logger; 29 import org.radeox.filter.regex.MatchResult; 30 import org.snipsnap.snip.Snip; 31 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.regex.Pattern ; 36 import java.util.regex.Matcher ; 37 38 45 46 public class Rssify { 47 public static List rssify(Snip snip) { 48 return rssify(snip.getChildrenDateOrder()); 49 } 50 51 public static List rssify(List snips) { 52 List result = new ArrayList (); 53 Pattern pattern = null; 54 MatchResult matchResult; 55 56 try { 57 pattern = Pattern.compile("^[\\p{Space}]*(1(\\.1)*)[\\p{Space}]+(.*?)$", Pattern.DOTALL | Pattern.MULTILINE); 58 } catch (Exception e) { 59 Logger.warn("bad pattern", e); 60 } 61 62 Iterator iterator = snips.iterator(); 63 while (iterator.hasNext() && result.size() <= 10) { 64 Snip snip = (Snip) iterator.next(); 65 66 String content = snip.getContent(); 67 Matcher matcher = pattern.matcher(content); 68 69 int start = 0; 70 String title = ""; 71 while (matcher.find()) { 72 matchResult = new MatchResult(matcher); 73 String post = content.substring(start, matchResult.beginOffset(0)).trim(); 74 if (!("".equals(title) && "".equals(post))) { 75 add(result, snip, post, title); 76 } 77 start = matchResult.endOffset(0); 78 title = matchResult.group(3).trim(); 79 } 80 81 add(result, snip, content.substring(start).trim(), title); 82 83 } 84 return result; 85 } 86 87 private static void add(List list, Snip snip, String content, String title) { 88 if (list.size() < 10) { 89 list.add(createSnip(snip, content, title)); 90 } 91 } 92 93 private static Snip createSnip(Snip snip, String content, String title) { 94 Snip rssSnip = null; 95 if ("".equals(title)) { 96 rssSnip = new RssSnip(snip, content); 97 } else { 98 int anchorIndex = title.indexOf("{anchor:"); 100 if (anchorIndex != -1) { 101 String url = title.substring(anchorIndex + 8, title.indexOf('}', anchorIndex)); 102 title = title.substring(0, anchorIndex).trim(); 103 rssSnip = new RssSnip(snip, content, title, url); 104 } else { 105 rssSnip = new RssSnip(snip, content, title); 106 } 107 } 108 return rssSnip; 109 } 110 } 111 | Popular Tags |