1 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 ; 40 import java.io.Writer ; 41 import java.util.Collection ; 42 import java.util.Iterator ; 43 import java.util.ResourceBundle ; 44 45 51 52 public class HotSnipMacro extends BaseMacro { 53 public HotSnipMacro() { 54 } 55 56 public String [] getParamDescription() { 57 return ResourceManager.getString("i18n.messages", "macro.hotsnip.params").split(";"); 58 } 59 60 public String getName() { 61 return "snips-by-hotness"; 62 } 63 64 public String getDescription() { 65 return ResourceManager.getString("i18n.messages", "macro.hotsnip.description"); 66 } 67 68 public void execute(Writer writer, MacroParameter params) 69 throws IllegalArgumentException , IOException { 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 e) { 81 Logger.warn("HotnessMacro: illegal parameter count='" + params.get("0") + "'"); 82 } 83 } 84 85 if (params.getLength() <= 1) { 86 Collection c = space.getHot(length); 87 Iterator 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 ("Number of arguments does not match"); 108 } 109 } 110 } 111 } | Popular Tags |