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;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.i18n.I18nContentWrapper;
38  import info.magnolia.context.MgnlContext;
39  
40  import javax.servlet.jsp.PageContext;
41  
42  /**
43   * Exposes a content node to the pagecontext as a Map of nodeData, in order to access the exposed object using JSTL.
44   * Since JSTL doesn't allow calling a method like <code>Content.getNodeData(String)</code> the <code>Content</code>
45   * is wrapped into a <code>NodeMapWrapper</code> which exposes NodeData using a map interface. This tag can be useful
46   * in similar situations: (see @jsp.tag-example)
47   *
48   * @jsp.tag name="setNode" body-content="empty"
49   * @jsp.tag-example
50   * <cms:setNode var="currentNode"/>
51   * <c:if test="${!empty currentNode.title}">
52   *   <c:out value="${currentNode.title}"/>
53   * </c:if>
54   *
55   * @author Fabrizio Giustina
56   * @version $Revision: 34388 $ ($Author: fgiust $)
57   */
58  public class SetNode extends BaseContentTag {
59  
60      /**
61       * Tag attribute. Variable name: the content node will be added to the pagecontext with this name.
62       */
63      private String var;
64  
65      /**
66       * Tag attribute. Scope for the declared variable. Can be <code>page</code>, <code>request</code>,
67       * <code>session</code> or <code>application</code><code></code>.
68       */
69      private int scope = PageContext.PAGE_SCOPE;
70  
71      /**
72       * The content node will be added to the pagecontext with this name.
73       * @jsp.attribute required="true" rtexprvalue="true"
74       */
75      public void setVar(String var) {
76          this.var = var;
77      }
78  
79      /**
80       * @deprecated use the contentNode attribute
81       * @jsp.attribute required="false" rtexprvalue="true"
82       */
83      public void setContent(Content node){
84          this.setContentNode(node);
85      }
86  
87      /**
88       * Scope for the declared variable. Can be "page" (default), "request", "session" or "application".
89       * @jsp.attribute required="false" rtexprvalue="true"
90       */
91      public void setScope(String scope) {
92          if ("request".equalsIgnoreCase(scope)) { //$NON-NLS-1$
93              this.scope = PageContext.REQUEST_SCOPE;
94          }
95          else if ("session".equalsIgnoreCase(scope)) { //$NON-NLS-1$
96              this.scope = PageContext.SESSION_SCOPE;
97          }
98          else if ("application".equalsIgnoreCase(scope)) { //$NON-NLS-1$
99              this.scope = PageContext.APPLICATION_SCOPE;
100         }
101         else {
102             // default
103             this.scope = PageContext.PAGE_SCOPE;
104         }
105     }
106 
107     /**
108      * @jsp.attribute description="nodeDataName is not supported in this tag !" required="false" rtexprvalue="false"
109      */
110     public void setNodeDataName(String name) {
111         throw new UnsupportedOperationException("nodeDataName not supported in this tag");
112     }
113 
114     /**
115      * Set contentNode in pagecontext and continue evaluating jsp.
116      * @return int
117      */
118     public int doEndTag() {
119         // Evaluated content node.
120         Content contentNode = getFirstMatchingNode();
121 
122         // set attribute
123         if (contentNode != null) { 
124             Content mainContent = MgnlContext.getAggregationState().getMainContent();
125             if (mainContent == null) {
126                 mainContent = contentNode;
127             }
128             info.magnolia.cms.util.NodeMapWrapper nmw = new info.magnolia.cms.util.NodeMapWrapper(
129                 new I18nContentWrapper(contentNode),
130                 mainContent.getHandle());
131             
132             pageContext.setAttribute(this.var, nmw, this.scope);
133         }
134         else {
135             pageContext.removeAttribute(this.var);
136         }
137 
138         return EVAL_PAGE;
139     }
140 
141     public void release() {
142         super.release();
143         this.var = null;
144         this.scope = PageContext.PAGE_SCOPE;
145     }
146 
147     /**
148      * Wrapper for a content Node which exposes a Map interface, used to access its content using jstl.
149      * @author fgiust
150      * @version $Revision: 34388 $ ($Author: fgiust $)
151      * @deprecated use info.magnolia.cms.util.NodeMapWrapper instead
152      */
153     public class NodeMapWrapper extends info.magnolia.cms.util.NodeMapWrapper {
154 
155         public NodeMapWrapper(Content node, Content actPage) {
156             super(node, actPage.getHandle());
157         }
158     }
159 
160 }