KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > semanticweb > rss > Rssify


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
36 import java.util.regex.Matcher JavaDoc;
37
38 /**
39  * Split snips into sub-units for e.g. RSS. By default header titles
40  * are used as seperators.
41  *
42  * @author Stephan J. Schmidt
43  * @version $Id: Rssify.java 1256 2003-12-11 13:24:57Z leo $
44  */

45
46 public class Rssify {
47   public static List JavaDoc rssify(Snip snip) {
48     return rssify(snip.getChildrenDateOrder());
49   }
50
51   public static List JavaDoc rssify(List JavaDoc snips) {
52     List JavaDoc result = new ArrayList JavaDoc();
53     Pattern JavaDoc 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 JavaDoc e) {
59       Logger.warn("bad pattern", e);
60     }
61
62     Iterator JavaDoc iterator = snips.iterator();
63     while (iterator.hasNext() && result.size() <= 10) {
64       Snip snip = (Snip) iterator.next();
65
66       String JavaDoc content = snip.getContent();
67       Matcher JavaDoc matcher = pattern.matcher(content);
68
69       int start = 0;
70       String JavaDoc title = "";
71       while (matcher.find()) {
72         matchResult = new MatchResult(matcher);
73         String JavaDoc 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 JavaDoc list, Snip snip, String JavaDoc content, String JavaDoc title) {
88     if (list.size() < 10) {
89       list.add(createSnip(snip, content, title));
90     }
91   }
92
93   private static Snip createSnip(Snip snip, String JavaDoc content, String JavaDoc title) {
94     Snip rssSnip = null;
95     if ("".equals(title)) {
96       rssSnip = new RssSnip(snip, content);
97     } else {
98       // does the header contain an {anchor} macro? then use the url instead of the Snip url
99
int anchorIndex = title.indexOf("{anchor:");
100       if (anchorIndex != -1) {
101         String JavaDoc 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