KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > util > AHref


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs.util;
14
15 import info.magnolia.cms.beans.config.Server;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.HierarchyManager;
18 import info.magnolia.cms.core.NodeData;
19 import info.magnolia.cms.security.SessionAccessControl;
20 import info.magnolia.cms.util.Resource;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.jcr.PropertyType;
28 import javax.jcr.RepositoryException;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.jsp.JspWriter JavaDoc;
31 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
32
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.commons.lang.exception.NestableRuntimeException;
35 import org.apache.log4j.Logger;
36
37
38 /**
39  * Utility tag which can be used to print out a link based on the value of a node data.
40  * @author Marcel Salathe
41  * @author Fabrizio Giustina
42  * @version $Revision $ ($Author $)
43  */

44 public class AHref extends BodyTagSupport JavaDoc {
45
46     /**
47      * Stable serialVersionUID.
48      */

49     private static final long serialVersionUID = 222L;
50
51     /**
52      * Logger.
53      */

54     private static Logger log = Logger.getLogger(AHref.class);
55
56     /**
57      * href part that is added before the nodeData content.
58      */

59     private String JavaDoc preHref;
60
61     /**
62      * href part that is added after the nodeData content.
63      */

64     private String JavaDoc postHref;
65
66     /**
67      * level from where to start the template search.
68      */

69     private int level;
70
71     /**
72      * template name to search for.
73      */

74     private String JavaDoc templateName;
75
76     /**
77      * name of nodeData to evaluate.
78      */

79     private String JavaDoc nodeDataName;
80
81     /**
82      * link attributes, added using child tags.
83      */

84     private transient List JavaDoc attributes;
85
86     /**
87      * @param name name of nodeData to evaluate
88      * @deprecated nodeDataName
89      */

90     public void setAtomName(String JavaDoc name) {
91         this.setNodeDataName(name);
92     }
93
94     /**
95      * Setter for the <code>nodeDataName</code> tag attribute.
96      * @param name name of nodeData to evaluate
97      */

98     public void setNodeDataName(String JavaDoc name) {
99         this.nodeDataName = name;
100     }
101
102     /**
103      * Setter for the <code>preHref</code> tag attribute.
104      * @param preHref href part that is added before the nodeData content
105      */

106     public void setPreHref(String JavaDoc preHref) {
107         this.preHref = preHref;
108     }
109
110     /**
111      * Setter for the <code>postHref</code> tag attribute.
112      * @param postHref href part that is added after the nodeData content
113      */

114     public void setPostHref(String JavaDoc postHref) {
115         this.postHref = postHref;
116     }
117
118     /**
119      * Setter for the <code>templateName</code> tag attribute.
120      * @param templateName template name to search for
121      */

122     public void setTemplateName(String JavaDoc templateName) {
123         this.templateName = templateName;
124     }
125
126     /**
127      * Setter for the <code>level</code> tag attribute.
128      * @param level level from where to start the template search
129      */

130     public void setLevel(int level) {
131         this.level = level;
132     }
133
134     /**
135      * Adds a link parameter.
136      * @param name name of attribute to add to the a element
137      * @param value value of attribute to add to the a element
138      */

139     public void setAttribute(String JavaDoc name, String JavaDoc value) {
140         if (attributes == null) {
141             attributes = new ArrayList JavaDoc();
142         }
143         String JavaDoc[] attributeArray = new String JavaDoc[]{name, value};
144         attributes.add(attributeArray);
145     }
146
147     /**
148      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
149      */

150     public int doEndTag() {
151         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
152         if (StringUtils.isEmpty(this.templateName)) {
153             if (this.nodeDataName == null) {
154                 this.writeLink(StringUtils.EMPTY);
155                 return EVAL_BODY_BUFFERED;
156             }
157             Content contentNode = Resource.getLocalContentNode(req);
158             if (contentNode == null) {
159                 contentNode = Resource.getGlobalContentNode(req);
160                 if (contentNode == null) {
161                     this.writeLink(StringUtils.EMPTY);
162                     return EVAL_BODY_BUFFERED;
163                 }
164             }
165
166             NodeData nodeData = contentNode.getNodeData(this.nodeDataName);
167
168             if ((nodeData == null) || !nodeData.isExist()) {
169                 this.writeLink(StringUtils.EMPTY);
170                 return EVAL_BODY_BUFFERED;
171             }
172             int type = nodeData.getType();
173             if (type == PropertyType.STRING) {
174                 if (StringUtils.isEmpty(nodeData.getString())) {
175                     this.writeLink(StringUtils.EMPTY);
176                 }
177                 else {
178                     this.writeLink(nodeData.getString());
179                 }
180             }
181         }
182         else {
183             Content startPage;
184             try {
185                 startPage = Resource.getCurrentActivePage(req).getAncestor(this.level);
186                 HierarchyManager hm = SessionAccessControl.getHierarchyManager(req);
187                 Content resultPage = hm.getPage(startPage.getHandle(), this.templateName);
188                 this.writeLink(resultPage.getHandle());
189             }
190             catch (RepositoryException e) {
191                 log.error(e.getMessage());
192                 this.writeLink(StringUtils.EMPTY);
193             }
194         }
195         return EVAL_BODY_BUFFERED;
196     }
197
198     /**
199      * Write a link.
200      * @param path link path
201      */

202     private void writeLink(String JavaDoc path) {
203         JspWriter JavaDoc out = pageContext.getOut();
204         try {
205             if (StringUtils.isNotEmpty(path)) {
206
207                 out.print("<a HREF=\""); //$NON-NLS-1$
208
if (this.preHref != null) {
209                     out.print(this.preHref);
210                 }
211                 out.print(path);
212                 if (SessionAccessControl
213                     .getHierarchyManager((HttpServletRequest JavaDoc) pageContext.getRequest())
214                     .isPage(path)) {
215                     out.print("."); //$NON-NLS-1$
216
out.print(Server.getDefaultExtension());
217                 }
218                 if (this.postHref != null) {
219                     out.print(this.postHref);
220                 }
221                 out.print("\""); //$NON-NLS-1$
222
if ((attributes != null) && (attributes.size() > 0)) {
223                     Iterator JavaDoc i = attributes.iterator();
224                     while (i.hasNext()) {
225                         String JavaDoc[] s = (String JavaDoc[]) i.next();
226                         out.print(" "); //$NON-NLS-1$
227
out.print(s[0]);
228                         out.print("=\""); //$NON-NLS-1$
229
out.print(s[1]);
230                         out.print("\""); //$NON-NLS-1$
231
}
232                 }
233                 out.print(">"); //$NON-NLS-1$
234
}
235             out.print(getBodyContent().getString());
236             if (StringUtils.isNotEmpty(path)) {
237                 out.print("</a>"); //$NON-NLS-1$
238
}
239         }
240         catch (RepositoryException e) {
241             log.error(e.getMessage(), e);
242         }
243         catch (IOException JavaDoc e) {
244             throw new NestableRuntimeException(e);
245         }
246         attributes = null;
247     }
248
249     /**
250      * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
251      */

252     public void release() {
253         this.preHref = null;
254         this.postHref = null;
255         this.level = 0;
256         this.templateName = null;
257         this.nodeDataName = null;
258         this.attributes = null;
259         super.release();
260     }
261
262 }
263
Popular Tags