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.module.templating;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.objectfactory.Components;
39  
40  import java.io.IOException;
41  import java.io.Writer;
42  
43  import javax.servlet.jsp.PageContext;
44  
45  
46  /**
47   * The central component to call when rendering paragraphs.
48   * @author gjoseph
49   * @version $Revision: $ ($Author: $)
50   * @deprecated since 4.3, use the {@link RenderingEngine} instead
51   */
52  public class ParagraphRenderingFacade {
53  
54      private final ParagraphRendererManager rendererManager;
55  
56      private final ParagraphManager paragraphManager;
57  
58      public static ParagraphRenderingFacade getInstance() {
59          return Components.getSingleton(ParagraphRenderingFacade.class);
60      }
61  
62      public ParagraphRenderingFacade() {
63          this(ParagraphRendererManager.getInstance(), ParagraphManager.getInstance());
64      }
65  
66      // currently only used for tests, but one day, we'll go IOC instead of using singletons !
67      ParagraphRenderingFacade(ParagraphRendererManager rendererManager, ParagraphManager paragraphManager) {
68          this.rendererManager = rendererManager;
69          this.paragraphManager = paragraphManager;
70      }
71  
72      /**
73       * Renders the given node to the given Writer, using the appropriate Paragraph (and the appropriate ParagraphRender
74       * as mandated by this Paragraph).
75       */
76      public void render(Content content, Writer out) throws RenderException, IOException {
77          render(content, out, null);
78      }
79  
80      /**
81       * Renders the given node to the given Writer, using the appropriate Paragraph (and the appropriate ParagraphRender
82       * as mandated by this Paragraph).
83       */
84      public void render(Content content, Writer out, PageContext pageContext) throws RenderException, IOException {
85          final String paragraphName = content.getMetaData().getTemplate();
86          final Paragraph paragraph = paragraphManager.getParagraphDefinition(paragraphName);
87          if (paragraph == null) {
88              throw new IllegalStateException("Paragraph " + paragraphName + " not found for page " + content.getHandle());
89          }
90          render(content, paragraph, out, pageContext);
91      }
92  
93      /**
94       * Renders the given node to the given Writer, using the given Paragraph (and the appropriate ParagraphRender as
95       * mandated by this Paragraph). Use with care.
96       */
97      public void render(Content content, Paragraph paragraph, Writer out) throws RenderException, IOException {
98          render(content, paragraph, out, null);
99      }
100 
101     /**
102      * Renders the given node to the given Writer, using the given Paragraph (and the appropriate ParagraphRender as
103      * mandated by this Paragraph). Use with care.
104      * TODO only used locally, should be protected ?
105      */
106     public void render(Content content, Paragraph paragraph, Writer out, PageContext pageContext) throws RenderException, IOException{
107         setPageContext(pageContext);
108         final String paragraphType = paragraph.getType();
109 
110         try {
111             final ParagraphRenderer renderer = rendererManager.getRenderer(paragraphType);
112             renderer.render(content, paragraph, out);
113         } finally {
114             setPageContext(null);
115         }
116 
117     }
118 
119     private void setPageContext(PageContext pageContext) {
120         if (pageContext != null && MgnlContext.isWebContext()) {
121             MgnlContext.getWebContext().setPageContext(pageContext);
122         }
123     }
124 
125 }