1 25 26 package com.j2biz.blogunity.render.macro; 27 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.io.Writer ; 31 32 import org.apache.commons.lang.StringUtils; 33 import org.radeox.macro.parameter.MacroParameter; 34 35 import com.j2biz.blogunity.BlogunityManager; 36 import com.j2biz.blogunity.util.ResourceUtils; 37 38 47 public class FileAttachmentMacro extends AbstractMacro { 48 49 54 public String getName() { 55 return "file"; 56 } 57 58 63 public String getDescription() { 64 return "Shows link to file-attachment.\n" 65 + "This macro can be used in two ways:\n" 66 + "- Show attachments to the files referenced by URL. (e.g. {file:http://www.mysite.com/bla.zip} )\n" 67 + "- Show attachments locally uploaded by the user into blog's intern \"files\"-directory.\n" 68 + " For example call: {file:xxx.zip} will show download-link to file \"xxx.zip\" located in \"files\"-directory of the current blog."; 69 } 70 71 76 public String [] getParamDescription() { 77 return new String []{"1: file (relative or absolute path )"}; 78 } 79 80 86 public void execute(Writer writer, MacroParameter params) throws IllegalArgumentException , 87 IOException { 88 89 if (params != null && params.getLength() == 1) { 90 String url = params.get(0); 91 if (StringUtils.isNotEmpty(url)) { 92 93 StringBuffer out = new StringBuffer (); 94 if (url.trim().startsWith("http://")) { 95 out.append("<a HREF=\""); 96 out.append(url.trim()); 97 out.append("\">"); 98 out.append(url.trim()); 99 out.append("</a>"); 100 } else { 101 102 if (blog != null) { 103 104 String path = blog.getUrlName() + "/files/" + url.trim(); 105 106 out.append("<img SRC=\""); 107 out.append(BlogunityManager.getBase()); 108 out.append("/images/file.png"); 109 out.append("\">"); 110 out.append("<a HREF=\""); 111 out.append(webappName + "/blogs/" + path); 112 out.append("\">"); 113 out.append(url.trim()); 114 115 File file = new File (BlogunityManager.getSystemConfiguration() 116 .getBlogsDirectory(), path); 117 118 if (file.exists() && file.isFile()) { 119 out.append(" <code><font style=\"color: #C0C0C0; font-size:11px;\">("); 120 out.append(ResourceUtils.getPreformattedFilesize(file.length())); 121 out.append(")</font></code>"); 122 } else { 123 out.append(" (file not found)"); 124 } 125 126 out.append("</a>"); 127 } 128 } 129 writer.write(out.toString()); 130 } 131 } else 132 throw new IllegalArgumentException ("Unknown parameters within macro 'file' found!"); 133 } 134 135 } | Popular Tags |