KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > macro > WeblogMacro


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.render.macro;
27
28 import org.radeox.util.i18n.ResourceManager;
29 import org.snipsnap.app.Application;
30 import org.snipsnap.config.Configuration;
31 import org.snipsnap.render.filter.links.BackLinks;
32 import org.snipsnap.render.macro.parameter.SnipMacroParameter;
33 import org.snipsnap.snip.Blog;
34 import org.snipsnap.snip.Snip;
35 import org.snipsnap.snip.SnipLink;
36 import org.snipsnap.snip.SnipSpace;
37 import org.snipsnap.snip.SnipSpaceFactory;
38 import org.snipsnap.snip.SnipUtil;
39 import org.snipsnap.util.StringUtil;
40
41 import java.io.IOException JavaDoc;
42 import java.io.Writer JavaDoc;
43 import java.text.MessageFormat JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46
47 /*
48  * Macro that displays a weblog. All subsnips are read and
49  * displayed in reverse chronological order.
50  *
51  * @author stephan
52  * @version $Id: WeblogMacro.java 1653 2004-06-11 20:04:56Z leo $
53  */

54
55 public class WeblogMacro extends SnipMacro {
56   private SnipSpace space;
57
58   public WeblogMacro() {
59     space = SnipSpaceFactory.getInstance();
60   }
61
62   public String JavaDoc getName() {
63     return "weblog";
64   }
65
66   public String JavaDoc getDescription() {
67     return ResourceManager.getString("i18n.messages", "macro.weblog.description");
68   }
69
70   public String JavaDoc[] getParamDescription() {
71     return ResourceManager.getString("i18n.messages", "macro.weblog.params").split(";");
72   }
73
74   public void execute(Writer JavaDoc writer, SnipMacroParameter params)
75     throws IllegalArgumentException JavaDoc, IOException JavaDoc {
76
77     int count = 0;
78     if (params.getLength() > 0) {
79       count = Integer.parseInt(params.get("count", 0));
80     } else {
81       count = 10;
82     }
83
84     String JavaDoc name;
85     if(params.getLength() > 1) {
86       name = params.get("snip", 1);
87     } else {
88       name = params.getSnipRenderContext().getSnip().getName();
89     }
90     Blog blog = space.getBlog(name);
91
92     if(null == blog) {
93       throw new IllegalArgumentException JavaDoc("unknown weblog "+name);
94     }
95     // order by name
96
// with correct ending /1,/2,/3,...,/11,/12
97
List JavaDoc posts = blog.getPosts(count);
98     //System.out.println("Weblog Posts for '"+name+"': "+posts.size());
99

100     // Convert
101
// - all Snips with start parent -> rename start/2003-05-02
102
// - comments?
103

104     // start/2002-03-01
105
// start/2002-05-06
106
// start/2002-05-06/1
107
// start/2002-05-06/2
108
//
109
// 1. Group by day
110
// - iterate
111
// - cut "name/"
112
// - get day
113
// - if day changes, render new day
114
// 2. Render each day
115

116     int NAME_INDEX = 0;
117     int DAY_INDEX = 1;
118     int COUNT_INDEX = 2;
119
120     String JavaDoc lastDay = "";
121     Iterator JavaDoc iterator = posts.iterator();
122     while (iterator.hasNext()) {
123       Object JavaDoc object = iterator.next();
124       // System.err.println("Class="+object.getClass());
125
Snip entry = (Snip) object;
126
127       String JavaDoc[] entryName = StringUtil.split(entry.getName(), "/");
128       int slashOffset = entryName.length - 3;
129       String JavaDoc day = (entryName.length > 1 ? entryName[slashOffset + DAY_INDEX] : entryName[0]);
130       // New Day?
131
//System.err.println("entryName="+Arrays.asList(entryName));
132
if (!lastDay.equals(day)) {
133         writer.write("<div class=\"blog-date\">");
134         writer.write(SnipUtil.toDate(day));
135         lastDay = day;
136         writer.write("</div>");
137       }
138
139       Configuration conf = Application.get().getConfiguration();
140
141       writer.write(entry.getXMLContent());
142       writer.write(" <a HREF=\"");
143       SnipLink.appendUrl(writer, entry.getName());
144       writer.write("\" title=\"");
145       MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "macro.anchor.permalink"));
146       writer.write(mf.format(new Object JavaDoc[]{entry.getName()}));
147       writer.write("\">");
148       SnipLink.appendImage(writer, "Icon-Permalink", "PermaLink");
149       writer.write("</a>");
150
151       writer.write("<div class=\"snip-post-comments\">");
152       writer.write(entry.getComments().getCommentString());
153       writer.write(" | ");
154       writer.write(entry.getComments().getPostString());
155       writer.write("</div>\n\n");
156
157       if ("true".equals(conf.getFeatureReferrerShow())) {
158         writer.write("<div class=\"snip-backlinks\">");
159         BackLinks.appendTo(writer, entry.getAccess().getBackLinks(), 5);
160         writer.write("</div>");
161       }
162     }
163   }
164 }
165
Popular Tags