KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ImageMacro.java,v 1.4 2005/01/07 19:28:00 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.IOException JavaDoc;
29 import java.io.Writer JavaDoc;
30
31 import org.apache.commons.lang.StringUtils;
32 import org.radeox.macro.parameter.MacroParameter;
33
34 /**
35  * A simple macro that shows image. It can be used in two ways: - Show images
36  * from internet referenced by URL. (e.g.
37  * {image:http://www.mysite.com/image.gif} ) - Show images locally uploaded by
38  * the user into blog's intern "images"-directory. For example call:
39  * {image:image.gif} will load some image "image.gif" from "images"-directory of
40  * a current blog.
41  *
42  */

43 public class ImageMacro extends AbstractMacro {
44
45     /*
46      * (non-Javadoc)
47      *
48      * @see org.radeox.macro.Macro#getName()
49      */

50     public String JavaDoc getName() {
51         return "image";
52     }
53
54     /*
55      * (non-Javadoc)
56      *
57      * @see org.radeox.macro.BaseMacro#getDescription()
58      */

59     public String JavaDoc getDescription() {
60         return "Shows image.\n"
61                 + "This macro can be used in two ways:\n"
62                 + "- Show images from internet referenced by URL. (e.g. {image:http://www.mysite.com/image.gif} )\n"
63                 + "- Show images locally uploaded by the user into blog's intern \"images\"-directory.\n"
64                 + " For example call: {image:image.gif} will load some image \"image.gif\" from \"images\"-directory of a current blog.";
65     }
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see org.radeox.macro.BaseMacro#getParamDescription()
71      */

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

82     public void execute(Writer JavaDoc writer, MacroParameter params) throws IllegalArgumentException JavaDoc,
83             IOException JavaDoc {
84
85         if (params != null && params.getLength() == 1) {
86             String JavaDoc url = params.get(0);
87             if (StringUtils.isNotEmpty(url)) {
88
89                 StringBuffer JavaDoc out = new StringBuffer JavaDoc();
90                 if (url.trim().startsWith("http://")) {
91                     out.append("<img SRC=\"");
92                     out.append(url.trim());
93                     out.append("\"/>");
94                 } else {
95
96                     if (blog != null) {
97
98                         out.append("<img SRC=\"");
99                         out.append(webappName + "/blogs/" + blog.getUrlName() + "/images/"
100                                 + url.trim());
101                         out.append("\"/>");
102                     }
103                 }
104                 writer.write(out.toString());
105             }
106         } else
107             throw new IllegalArgumentException JavaDoc("Unknown parameters within macro 'blog' found!");
108     }
109
110 }
Popular Tags