1 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 ; 42 import java.io.Writer ; 43 import java.text.MessageFormat ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 47 54 55 public class WeblogMacro extends SnipMacro { 56 private SnipSpace space; 57 58 public WeblogMacro() { 59 space = SnipSpaceFactory.getInstance(); 60 } 61 62 public String getName() { 63 return "weblog"; 64 } 65 66 public String getDescription() { 67 return ResourceManager.getString("i18n.messages", "macro.weblog.description"); 68 } 69 70 public String [] getParamDescription() { 71 return ResourceManager.getString("i18n.messages", "macro.weblog.params").split(";"); 72 } 73 74 public void execute(Writer writer, SnipMacroParameter params) 75 throws IllegalArgumentException , IOException { 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 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 ("unknown weblog "+name); 94 } 95 List posts = blog.getPosts(count); 98 100 104 116 int NAME_INDEX = 0; 117 int DAY_INDEX = 1; 118 int COUNT_INDEX = 2; 119 120 String lastDay = ""; 121 Iterator iterator = posts.iterator(); 122 while (iterator.hasNext()) { 123 Object object = iterator.next(); 124 Snip entry = (Snip) object; 126 127 String [] entryName = StringUtil.split(entry.getName(), "/"); 128 int slashOffset = entryName.length - 3; 129 String day = (entryName.length > 1 ? entryName[slashOffset + DAY_INDEX] : entryName[0]); 130 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 mf = new MessageFormat (ResourceManager.getString("i18n.messages", "macro.anchor.permalink")); 146 writer.write(mf.format(new Object []{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 |