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  
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  
42  /**
43   * Set contentNode in resource.
44   * @jsp.tag name="set" body-content="empty"
45   *
46   * @author Sameer Charles
47   * @version $Revision: 32667 $ ($Author: gjoseph $)
48   */
49  public class Set extends BaseContentTag {
50  
51      public static final String SCOPE_GLOBAL = "global";
52      
53      public static final String SCOPE_LOCAL = "local";
54  
55      public static final String SCOPE_PARAGRAPH = "paragraph";
56  
57      public static final String SCOPE_CURRENT = "current";
58  
59      public static final String SCOPE_PAGE = "page";
60  
61      private String scope = SCOPE_GLOBAL;
62      
63      /**
64       * Reset the former status after executing the body.
65       */
66      private boolean forBodyOnly = false;
67  
68      /**
69       * If forBodyOnly is true we have to reset the former status.
70       */
71      private Content previousNode;
72      
73      /**
74       * @deprecated use the contentNode attribute
75       * @jsp.attribute required="false" rtexprvalue="true" type="info.magnolia.cms.core.Content"
76       */
77      public void setContainer(Content contentNode) {
78          this.setContentNode(contentNode);
79      }
80  
81      /**
82       * @deprecated use the contentNodeName attribute
83       * @jsp.attribute required="false" rtexprvalue="true"
84       */
85      public void setContainerName(String name) {
86          this.setContentNodeName(name);
87      }
88  
89      /**
90       * @jsp.attribute description="nodeDataName is not supported in this tag !" required="false" rtexprvalue="false"
91       */
92      public void setNodeDataName(String name) {
93          throw new UnsupportedOperationException("nodeDataName not supported in this tag");
94      }
95  
96      public int doStartTag() {
97          Content node = getFirstMatchingNode();
98  
99          if(isForBodyOnly()){
100             saveCurrentNode();
101         }
102 
103         setNode(node);
104         
105         return EVAL_BODY_INCLUDE;
106     }
107 
108     protected void saveCurrentNode() {
109         if(SCOPE_GLOBAL.equals(this.getScope())){
110             previousNode = Resource.getGlobalContentNode();
111         }
112         else if (SCOPE_LOCAL.equals(this.getScope()) || SCOPE_PARAGRAPH.equals(this.getScope())){
113             previousNode = Resource.getLocalContentNode();
114         }
115         else if (SCOPE_CURRENT.equals(this.getScope()) || SCOPE_PAGE.equals(this.getScope())){
116             previousNode = Resource.getCurrentActivePage();
117         }
118     }
119 
120     protected void setNode(Content node) {
121         if(SCOPE_GLOBAL.equals(this.getScope())){
122             Resource.setGlobalContentNode(node);
123         }
124         else if (SCOPE_LOCAL.equals(this.getScope()) || SCOPE_PARAGRAPH.equals(this.getScope())){
125             Resource.setLocalContentNode(node);
126         }
127         else if (SCOPE_CURRENT.equals(this.getScope()) || SCOPE_PAGE.equals(this.getScope())){
128             Resource.setCurrentActivePage(node);
129         }
130     }
131 
132     public int doEndTag() {
133         if(isForBodyOnly()){
134             setNode(previousNode);
135         }
136         return EVAL_PAGE;
137     }
138 
139     public void release() {
140         super.release();
141         this.scope = SCOPE_GLOBAL;
142         this.forBodyOnly = false;
143         this.previousNode = null;
144     }
145     
146     public String getScope() {
147         return this.scope;
148     }
149 
150     /**
151      * Attention this is not the jstl scope but the magnolia scope! Values are
152      * <ul>
153      * <li>local: same as paragraph</li>
154      * <li>paragraph: the current paragraph</li>
155      * <li>global: a globally use node (default value)</li>
156      * <li>page: same as page</li>
157      * <li>current: the current page</li>
158      * </ul>
159      * @jsp.attribute required="false" rtexprvalue="true"
160      */
161     public void setScope(String scope) {
162         this.scope = scope;
163     }
164     
165     public boolean isForBodyOnly() {
166         return this.forBodyOnly;
167     }
168 
169     /**
170      * If true the node is unset after the end tag. Default is false.
171      * @jsp.attribute required="false" rtexprvalue="true"
172      */
173     public void setForBodyOnly(boolean forBodyOnly) {
174         this.forBodyOnly = forBodyOnly;
175     }
176 }