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.beans.config.ContentRepository;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.NodeData;
39  import info.magnolia.cms.i18n.I18nContentSupportFactory;
40  import info.magnolia.cms.i18n.I18nContentSupport;
41  import info.magnolia.cms.util.ContentUtil;
42  
43  import info.magnolia.context.MgnlContext;
44  import javax.jcr.RepositoryException;
45  import javax.servlet.jsp.tagext.TagSupport;
46  
47  import org.apache.commons.lang.StringUtils;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  
52  /**
53   * @author fgiust
54   * @version $Revision: 32667 $ ($Author: gjoseph $)
55   */
56  public abstract class BaseContentTag extends TagSupport {
57      private final static Logger log = LoggerFactory.getLogger(BaseContentTag.class);
58  
59      private String nodeDataName;
60  
61      private Content contentNode;
62  
63      private String contentNodeName;
64  
65      private String contentNodeCollectionName;
66  
67      private String uuid;
68  
69      private String path;
70  
71      private String repository = ContentRepository.WEBSITE;
72  
73      private boolean inherit;
74  
75      /**
76       * This is historically. Meaning that we work on the page node itself even when we are in an iterator.
77       */
78      private boolean actpage;
79  
80      /**
81       * Set the node data name, e.g. "mainText".
82       * @jsp.attribute required="false" rtexprvalue="true"
83       */
84      public void setNodeDataName(String name) {
85          this.nodeDataName = name;
86      }
87  
88      /**
89       * Inside a "contentNodeIterator": if not set, the current content node is taken. If set empty
90       * (contentNodeName=""), the top level content is taken. If specified, the named content node is taken. Outside
91       * a "contentNodeIterator": if not set or empty: the top level content is taken. If specified, the named content
92       * node is taken.
93       *
94       * @jsp.attribute required="false" rtexprvalue="true"
95       */
96      public void setContentNodeName(String name) {
97          this.contentNodeName = name;
98      }
99  
100     /**
101      * Name of the collection holding the content node, e.g. "mainColumnParagraphs".
102      * @jsp.attribute required="false" rtexprvalue="true"
103      */
104     public void setContentNodeCollectionName(String name) {
105         this.contentNodeCollectionName = name;
106     }
107 
108     /**
109      * Inherit the value from parent pages, if not set in the current one.
110      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
111      */
112     public void setInherit(boolean inherit) {
113         this.inherit = inherit;
114     }
115 
116     /**
117      * @deprecated Use {@link #getFirstMatchingNode()} instead
118      */
119     protected Content getFirtMatchingNode() {
120         return getFirstMatchingNode();
121     }
122 
123     /**
124      * Get the first matching node containing a NodeData named <code>nodeDataName</code>.
125      * @return the active node, or the first matching one if nodedata is null and inherit is set.
126      */
127     protected Content getFirstMatchingNode() {
128         if (actpage) {
129             return getCurrentPage();
130         }
131 
132         Content contentNode = null;
133         if (this.getContentNode() != null) {
134             contentNode = this.getContentNode();
135         }
136         if (StringUtils.isNotEmpty(this.getUuid())) {
137             contentNode = ContentUtil.getContentByUUID(this.getRepository(), this.getUuid());
138         }
139 
140         if (StringUtils.isNotEmpty(this.getPath())) {
141             contentNode = ContentUtil.getContent(this.getRepository(), this.getPath());
142         }
143 
144         if (contentNode == null) {
145             Content currentPage = getCurrentPage();
146             contentNode = resolveNode(currentPage);
147 
148             try {
149                 while (inherit && currentPage.getLevel() > 0 && contentNode == null) {
150                     currentPage = currentPage.getParent();
151                     contentNode = resolveNode(currentPage);
152                 }
153             }
154             catch (RepositoryException e) {
155                 log.error(e.getMessage(), e);
156             }
157         }
158 
159         if (contentNode == null) {
160             return null;
161         }
162 
163         final I18nContentSupport i18nSupport = I18nContentSupportFactory.getI18nSupport();
164         if (StringUtils.isNotEmpty(this.nodeDataName)) {
165             Content currentPage = getCurrentPage();
166             NodeData nodeData = i18nSupport.getNodeData(contentNode, this.nodeDataName);
167             try {
168                 while (inherit && currentPage.getLevel() > 0 && !nodeData.isExist()) {
169                     currentPage = currentPage.getParent();
170                     contentNode = resolveNode(currentPage);
171                     if (contentNode != null) {
172                         nodeData = i18nSupport.getNodeData(contentNode, this.nodeDataName);
173                     }
174                 }
175             }
176             catch (RepositoryException e) {
177                 log.error(e.getMessage(), e);
178             }
179         }
180 
181         return contentNode;
182     }
183 
184     protected Content getCurrentPage() {
185         return Resource.getCurrentActivePage();
186     }
187 
188     protected Content resolveNode(Content currentPage) {
189         Content currentParagraph = MgnlContext.getAggregationState().getCurrentContent();
190 
191         try {
192             if (StringUtils.isNotEmpty(contentNodeName)) {
193                 // contentNodeName is defined
194                 if (StringUtils.isEmpty(contentNodeCollectionName)) {
195                     // e.g. <cms:out nodeDataName="title" contentNodeName="footer"/>
196                     return currentPage.getContent(contentNodeName);
197                 }
198 
199                 // e.g. <cms:out nodeDataName="title" contentNodeName="01" contentNodeCollectionName="mainPars"/>
200                 // e.g. <cms:out nodeDataName="title" contentNodeName="footer" contentNodeCollectionName=""/>
201                 return currentPage.getContent(contentNodeCollectionName).getContent(contentNodeName);
202             } else  if (currentParagraph == null || currentParagraph.getHandle().equals(MgnlContext.getAggregationState().getMainContent().getHandle())) {
203                 // outside collection iterator
204                 if (StringUtils.isEmpty(contentNodeCollectionName)) {
205                     // e.g. <cms:out nodeDataName="title"/>
206                     // e.g. <cms:out nodeDataName="title" contentNodeName=""/>
207                     // e.g. <cms:out nodeDataName="title" contentNodeCollectionName=""/>
208                     return currentPage;
209                 }
210                 // ERROR: no content node assignable because contentNodeName is empty
211                 // e.g. <cms:out nodeDataName="title" contentNodeCollectionName="mainPars"/>
212 
213                 // but in this case we return contentNodeCollection if existent
214                 if (currentPage.hasContent(contentNodeCollectionName)) {
215                     return currentPage.getContent(contentNodeCollectionName);
216                 }
217 
218             } else {
219                 // inside collection iterator
220                 if (contentNodeName == null && contentNodeCollectionName == null) {
221                     // e.g. <cms:out nodeDataName="title"/>
222                     return currentParagraph;
223                 } else if ((contentNodeName != null && StringUtils.isEmpty(contentNodeName))
224                     || (contentNodeCollectionName != null && StringUtils.isEmpty(contentNodeCollectionName))) {
225                     // empty collection name -> use actpage
226                     // e.g. <cms:out nodeDataName="title" contentNodeCollectionName=""/>
227                     return currentPage;
228                 } else if (contentNodeCollectionName != null && !StringUtils.isEmpty(contentNodeCollectionName)) {
229                     return currentParagraph.getContent(contentNodeCollectionName);
230                 }
231             }
232         }
233         catch (RepositoryException re) {
234             log.debug(re.getMessage(), re);
235         }
236         return null;
237     }
238 
239     public void release() {
240         super.release();
241 
242         this.nodeDataName = null;
243         this.contentNodeName = null;
244         this.contentNodeCollectionName = null;
245         this.contentNode = null;
246         this.inherit = false;
247         this.actpage = false;
248         this.uuid = null;
249         this.path = null;
250         this.repository = ContentRepository.WEBSITE;
251     }
252 
253     public boolean isActpage() {
254         return this.actpage;
255     }
256 
257     /**
258      * If true we work on the current active page instead of any other node.
259      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
260      * @deprecated
261      */
262     public void setActpage(boolean actpage) {
263         this.actpage = actpage;
264     }
265 
266     public String getRepository() {
267         return this.repository;
268     }
269 
270     /**
271      * Used if the uuid or path attribute is set. Defaults to "website".
272      * @jsp.attribute required="false" rtexprvalue="true"
273      */
274     public void setRepository(String repository) {
275         this.repository = repository;
276     }
277 
278     public String getUuid() {
279         return this.uuid;
280     }
281 
282     /**
283      * The uuid to use for finding the content.
284      * You must define the repository attribute if the content is not stored in the website repository.
285      * @jsp.attribute required="false" rtexprvalue="true"
286      */
287     public void setUuid(String uuid) {
288         this.uuid = uuid;
289     }
290 
291     public String getPath() {
292         return this.path;
293     }
294 
295     /**
296      * The absolute path to the content.
297      * You must define the repository attribute if the content is not stored in the website repository.
298      * @jsp.attribute required="false" rtexprvalue="true"
299      */
300     public void setPath(String path) {
301         this.path = path;
302     }
303 
304     public Content getContentNode() {
305         return this.contentNode;
306     }
307 
308     /**
309      * The content object to use.
310      * @jsp.attribute required="false" rtexprvalue="true"
311      */
312     public void setContentNode(Content content) {
313         this.contentNode = content;
314     }
315 
316     protected String getNodeDataName() {
317         return nodeDataName;
318     }
319 
320     protected String getContentNodeName() {
321         return contentNodeName;
322     }
323 
324     protected String getContentNodeCollectionName() {
325         return contentNodeCollectionName;
326     }
327 }