View Javadoc

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