KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.engine.context.RenderContext;
29 import org.radeox.macro.Macro;
30 import org.radeox.macro.BaseMacro;
31 import org.radeox.macro.parameter.MacroParameter;
32 import org.radeox.util.logging.Logger;
33 import org.radeox.util.i18n.ResourceManager;
34 import org.snipsnap.render.context.SnipRenderContext;
35 import org.snipsnap.snip.Snip;
36 import org.snipsnap.snip.SnipLink;
37 import org.snipsnap.snip.SnipSpace;
38
39 import java.io.IOException JavaDoc;
40 import java.io.Writer JavaDoc;
41 import java.util.Collection JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.ResourceBundle JavaDoc;
44
45 /*
46  * Macro that displays the hottest snips, currently the most viewed.
47  *
48  * @author stephan
49  * @version $Id: HotSnipMacro.java 1606 2004-05-17 10:56:18Z leo $
50  */

51
52 public class HotSnipMacro extends BaseMacro {
53   public HotSnipMacro() {
54   }
55
56   public String JavaDoc[] getParamDescription() {
57     return ResourceManager.getString("i18n.messages", "macro.hotsnip.params").split(";");
58   }
59
60   public String JavaDoc getName() {
61     return "snips-by-hotness";
62   }
63
64   public String JavaDoc getDescription() {
65     return ResourceManager.getString("i18n.messages", "macro.hotsnip.description");
66   }
67
68   public void execute(Writer JavaDoc writer, MacroParameter params)
69       throws IllegalArgumentException JavaDoc, IOException JavaDoc {
70
71     RenderContext context = params.getContext();
72     if (context instanceof SnipRenderContext) {
73       SnipSpace space = ((SnipRenderContext) context).getSpace();
74
75       int length = 10;
76       boolean showSize = false;
77       if (params.getLength() > 0) {
78         try {
79           length = Integer.parseInt(params.get("0"));
80         } catch (NumberFormatException JavaDoc e) {
81           Logger.warn("HotnessMacro: illegal parameter count='" + params.get("0") + "'");
82         }
83       }
84
85       if (params.getLength() <= 1) {
86         Collection JavaDoc c = space.getHot(length);
87         Iterator JavaDoc iterator = c.iterator();
88         writer.write("<div class=\"list\"><div class=\"list-title\">");
89         ResourceManager.getString("i18n.messages", "macro.hotsnip.viewed");
90         if (showSize) {
91           writer.write(" (");
92           writer.write("" + length);
93           writer.write(")");
94         }
95         writer.write("</div><ul>");
96         while (iterator.hasNext()) {
97           Snip hotSnip = (Snip) iterator.next();
98           writer.write("<li><span class=\"count\">");
99           writer.write("" + hotSnip.getViewCount());
100           writer.write("</span>");
101           writer.write("<span class=\"content\">");
102           SnipLink.appendLink(writer, hotSnip);
103           writer.write("</span></li>");
104         }
105         writer.write("</ul></div>");
106       } else {
107         throw new IllegalArgumentException JavaDoc("Number of arguments does not match");
108       }
109     }
110   }
111 }
Popular Tags