View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.taglibs.util;
35  
36  import info.magnolia.cms.beans.config.ContentRepository;
37  import info.magnolia.cms.beans.config.ServerConfiguration;
38  import info.magnolia.cms.core.Content;
39  import info.magnolia.cms.core.HierarchyManager;
40  import info.magnolia.cms.core.NodeData;
41  import info.magnolia.cms.taglibs.Resource;
42  import info.magnolia.context.MgnlContext;
43  
44  import java.io.IOException;
45  import java.util.ArrayList;
46  import java.util.Iterator;
47  import java.util.List;
48  
49  import javax.jcr.PropertyType;
50  import javax.jcr.RepositoryException;
51  import javax.servlet.jsp.JspWriter;
52  import javax.servlet.jsp.tagext.BodyTagSupport;
53  
54  import org.apache.commons.lang.StringUtils;
55  import org.apache.commons.lang.exception.NestableRuntimeException;
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  
60  /**
61   * Utility tag which can be used to print out a link based on the value of a node data or tries to find the first page
62   * with a specific template name, starting from a specific page.
63   * @jsp.tag name="aHref" body-content="JSP"
64   * @jsp.tag-example <cmsu:aHref ... />
65   *
66   * @author Marcel Salathe
67   * @author Fabrizio Giustina
68   * @version $Revision $ ($Author $)
69   */
70  public class AHref extends BodyTagSupport {
71  
72      /**
73       * Stable serialVersionUID.
74       */
75      private static final long serialVersionUID = 222L;
76  
77      /**
78       * Logger.
79       */
80      private static Logger log = LoggerFactory.getLogger(AHref.class);
81  
82      /**
83       * href part that is added before the nodeData content.
84       */
85      private String preHref;
86  
87      /**
88       * href part that is added after the nodeData content.
89       */
90      private String postHref;
91  
92      /**
93       * level from where to start the template search.
94       */
95      private int level;
96  
97      /**
98       * template name to search for.
99       */
100     private String templateName;
101 
102     /**
103      * name of nodeData to evaluate.
104      */
105     private String nodeDataName;
106 
107     /**
108      * link attributes, added using child tags.
109      */
110     private transient List attributes;
111 
112     /**
113      * @param name name of nodeData to evaluate
114      * @deprecated use the nodeDataName attribute instead.
115      * @jsp.attribute required="false" rtexprvalue="true"
116      */
117     public void setAtomName(String name) {
118         this.setNodeDataName(name);
119     }
120 
121     /**
122      * node containing the link information
123      * @jsp.attribute required="false" rtexprvalue="true"
124      */
125     public void setNodeDataName(String name) {
126         this.nodeDataName = name;
127     }
128 
129     /**
130      * href part that is added before the nodeData content.
131      * @jsp.attribute required="false" rtexprvalue="true"
132      */
133     public void setPreHref(String preHref) {
134         this.preHref = preHref;
135     }
136 
137     /**
138      * href part that is added after the nodeData content.
139      * @jsp.attribute required="false" rtexprvalue="true"
140      */
141     public void setPostHref(String postHref) {
142         this.postHref = postHref;
143     }
144 
145     /**
146      * template name to search for.
147      * @jsp.attribute required="false" rtexprvalue="true"
148      */
149     public void setTemplateName(String templateName) {
150         this.templateName = templateName;
151     }
152 
153     /**
154      * level from where to start the template search.
155      * @jsp.attribute required="false" rtexprvalue="true" type="int"
156      */
157     public void setLevel(int level) {
158         this.level = level;
159     }
160 
161     /**
162      * Adds a link parameter.
163      * @param name name of attribute to add to the a element
164      * @param value value of attribute to add to the a element
165      */
166     public void setAttribute(String name, String value) {
167         if (attributes == null) {
168             attributes = new ArrayList();
169         }
170         String[] attributeArray = new String[]{name, value};
171         attributes.add(attributeArray);
172     }
173 
174     /**
175      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
176      */
177     public int doEndTag() {
178         if (StringUtils.isEmpty(this.templateName)) {
179             if (this.nodeDataName == null) {
180                 this.writeLink(StringUtils.EMPTY);
181                 return EVAL_BODY_BUFFERED;
182             }
183             Content contentNode = Resource.getLocalContentNode();
184             if (contentNode == null) {
185                 contentNode = Resource.getGlobalContentNode();
186                 if (contentNode == null) {
187                     this.writeLink(StringUtils.EMPTY);
188                     return EVAL_BODY_BUFFERED;
189                 }
190             }
191 
192             NodeData nodeData = contentNode.getNodeData(this.nodeDataName);
193 
194             if ((nodeData == null) || !nodeData.isExist()) {
195                 this.writeLink(StringUtils.EMPTY);
196                 return EVAL_BODY_BUFFERED;
197             }
198             int type = nodeData.getType();
199             if (type == PropertyType.STRING) {
200                 if (StringUtils.isEmpty(nodeData.getString())) {
201                     this.writeLink(StringUtils.EMPTY);
202                 }
203                 else {
204                     this.writeLink(nodeData.getString());
205                 }
206             }
207         }
208         else {
209             Content startPage;
210             try {
211                 startPage = Resource.getCurrentActivePage().getAncestor(this.level);
212                 HierarchyManager hm = MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
213                 Content resultPage = hm.getPage(startPage.getHandle(), this.templateName);
214                 this.writeLink(resultPage.getHandle());
215             }
216             catch (RepositoryException e) {
217                 log.error(e.getMessage());
218                 this.writeLink(StringUtils.EMPTY);
219             }
220         }
221         return EVAL_BODY_BUFFERED;
222     }
223 
224     /**
225      * Write a link.
226      * @param path link path
227      */
228     private void writeLink(String path) {
229         JspWriter out = pageContext.getOut();
230         try {
231             if (StringUtils.isNotEmpty(path)) {
232 
233                 out.print("<a href=\""); //$NON-NLS-1$
234                 if (this.preHref != null) {
235                     out.print(this.preHref);
236                 }
237                 out.print(path);
238                 if (MgnlContext.getHierarchyManager(ContentRepository.WEBSITE).isPage(path)) {
239                     out.print("."); //$NON-NLS-1$
240                     out.print(ServerConfiguration.getInstance().getDefaultExtension());
241                 }
242                 if (this.postHref != null) {
243                     out.print(this.postHref);
244                 }
245                 out.print("\""); //$NON-NLS-1$
246                 if ((attributes != null) && (attributes.size() > 0)) {
247                     Iterator i = attributes.iterator();
248                     while (i.hasNext()) {
249                         String[] s = (String[]) i.next();
250                         out.print(" "); //$NON-NLS-1$
251                         out.print(s[0]);
252                         out.print("=\""); //$NON-NLS-1$
253                         out.print(s[1]);
254                         out.print("\""); //$NON-NLS-1$
255                     }
256                 }
257                 out.print(">"); //$NON-NLS-1$
258             }
259             out.print(getBodyContent().getString());
260             if (StringUtils.isNotEmpty(path)) {
261                 out.print("</a>"); //$NON-NLS-1$
262             }
263         }
264         catch (RepositoryException e) {
265             log.error(e.getMessage(), e);
266         }
267         catch (IOException e) {
268             throw new NestableRuntimeException(e);
269         }
270         attributes = null;
271     }
272 
273     /**
274      * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
275      */
276     public void release() {
277         this.preHref = null;
278         this.postHref = null;
279         this.level = 0;
280         this.templateName = null;
281         this.nodeDataName = null;
282         this.attributes = null;
283         super.release();
284     }
285 
286 }