KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > render > macro > FileAttachmentMacro


1 /*
2  * $Id: FileAttachmentMacro.java,v 1.2 2005/01/09 12:26:48 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.render.macro;
27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.Writer JavaDoc;
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 /**
39  * A simple macro that shows image. It can be used in two ways: - Show images
40  * from internet referenced by URL. (e.g.
41  * {image:http://www.mysite.com/image.gif} ) - Show images locally uploaded by
42  * the user into blog's intern "images"-directory. For example call:
43  * {image:image.gif} will load some image "image.gif" from "images"-directory of
44  * a current blog.
45  *
46  */

47 public class FileAttachmentMacro extends AbstractMacro {
48
49     /*
50      * (non-Javadoc)
51      *
52      * @see org.radeox.macro.Macro#getName()
53      */

54     public String JavaDoc getName() {
55         return "file";
56     }
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see org.radeox.macro.BaseMacro#getDescription()
62      */

63     public String JavaDoc 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     /*
72      * (non-Javadoc)
73      *
74      * @see org.radeox.macro.BaseMacro#getParamDescription()
75      */

76     public String JavaDoc[] getParamDescription() {
77         return new String JavaDoc[]{"1: file (relative or absolute path )"};
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see org.radeox.macro.Macro#execute(java.io.Writer,
84      * org.radeox.macro.parameter.MacroParameter)
85      */

86     public void execute(Writer JavaDoc writer, MacroParameter params) throws IllegalArgumentException JavaDoc,
87             IOException JavaDoc {
88
89         if (params != null && params.getLength() == 1) {
90             String JavaDoc url = params.get(0);
91             if (StringUtils.isNotEmpty(url)) {
92
93                 StringBuffer JavaDoc out = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc file = new File JavaDoc(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 JavaDoc("Unknown parameters within macro 'file' found!");
133     }
134
135 }
Popular Tags