View Javadoc

1   /**
2    * This file Copyright (c) 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.templating.elements;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.context.WebContext;
39  import info.magnolia.jcr.inheritance.InheritanceNodeWrapper;
40  import info.magnolia.registry.RegistrationException;
41  import info.magnolia.rendering.context.RenderingContext;
42  import info.magnolia.rendering.engine.AppendableOnlyOutputProvider;
43  import info.magnolia.rendering.engine.RenderException;
44  import info.magnolia.rendering.engine.RenderingEngine;
45  import info.magnolia.rendering.template.TemplateDefinition;
46  import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
47  
48  import java.io.IOException;
49  import java.util.HashMap;
50  import java.util.Map;
51  
52  import javax.inject.Inject;
53  import javax.jcr.Node;
54  
55  import org.apache.commons.lang.StringUtils;
56  
57  /**
58   * Renders a piece of content.
59   *
60   * @version $Id$
61   */
62  public class ComponentElement extends AbstractContentTemplatingElement {
63  
64      private Map<String, Object> contextAttributes = new HashMap<String, Object>();
65      private final RenderingEngine renderingEngine;
66      private Node content;
67      private TemplateDefinitionAssignment templateDefinitionAssignment;
68  
69      private String dialog;
70  
71      @Inject
72      public ComponentElement(ServerConfiguration server, RenderingContext renderingContext, RenderingEngine renderingEngine, TemplateDefinitionAssignment templateDefinitionAssignment ) {
73          super(server, renderingContext);
74          this.renderingEngine = renderingEngine;
75          this.templateDefinitionAssignment = templateDefinitionAssignment;
76      }
77  
78      @Override
79      public void begin(Appendable out) throws IOException, RenderException {
80  
81          TemplateDefinition componentDefinition = null;
82          content = getPassedContent();
83  
84          if(content == null) {
85              throw new RenderException("The 'content' or 'workspace' and 'path' attribute have to be set to rendre a component.");
86          }
87  
88          if(isAdmin()){
89              MarkupHelper helper = new MarkupHelper(out);
90  
91              helper.openComment("cms:component");
92  
93  
94              helper.attribute("content", getNodePath(content));
95  
96              if(content instanceof InheritanceNodeWrapper) {
97                  if (((InheritanceNodeWrapper) content).isInherited()) {
98                      helper.attribute("inherited", "true");
99                  }
100             }
101 
102             try {
103                 componentDefinition = templateDefinitionAssignment.getAssignedTemplateDefinition(content);
104             } catch (RegistrationException e) {
105                 throw new RenderException("No template definition found for the current content", e);
106             }
107             if(StringUtils.isEmpty(dialog)) {
108                 dialog = resolveDialog(componentDefinition);
109             }
110             helper.attribute("dialog", dialog);
111             helper.append(" -->\n");
112         }
113 
114         // TODO not sure how to pass editable
115 
116         WebContext webContext = MgnlContext.getWebContext();
117         webContext.push(webContext.getRequest(), webContext.getResponse());
118         setAttributesInWebContext(contextAttributes, WebContext.LOCAL_SCOPE);
119 
120 
121         try {
122             if(componentDefinition != null) {
123                 renderingEngine.render(content, componentDefinition, new HashMap<String, Object>(), new AppendableOnlyOutputProvider(out));
124             } else {
125                 renderingEngine.render(content, new AppendableOnlyOutputProvider(out));
126             }
127         } finally {
128             webContext.pop();
129             webContext.setPageContext(null);
130             restoreAttributesInWebContext(contextAttributes, WebContext.LOCAL_SCOPE);
131         }
132     }
133 
134     @Override
135     public void end(Appendable out) throws IOException, RenderException {
136         if(isAdmin()){
137             new MarkupHelper(out).closeComment("cms:component");
138         }
139     }
140 
141     public Map<String, Object> getContextAttributes() {
142         return contextAttributes;
143     }
144 
145     public void setContextAttributes(Map<String, Object> contextAttributes) {
146         this.contextAttributes = contextAttributes;
147     }
148 
149     private String resolveDialog(TemplateDefinition component) {
150         if (StringUtils.isNotEmpty(this.dialog)) {
151             return this.dialog;
152         }
153         String dialog = component.getDialog();
154         if (StringUtils.isNotEmpty(dialog)) {
155             return dialog;
156         }
157         return null;
158     }
159 
160     public void setDialog(String dialog) {
161         this.dialog = dialog;
162     }
163 }