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.cms.core.ItemType;
38  import info.magnolia.cms.beans.config.ObservedManager;
39  import info.magnolia.objectfactory.Classes;
40  import info.magnolia.objectfactory.Components;
41  import info.magnolia.objectfactory.MgnlInstantiationException;
42  import org.apache.commons.lang.StringUtils;
43  
44  import java.util.Collection;
45  import java.util.Collections;
46  import java.util.Iterator;
47  import java.util.LinkedHashMap;
48  import java.util.Map;
49  
50  /**
51   * Manages the paragraph renderers on the system. Modules can register the nodes where
52   * the paragraph renderers are defined.
53   * @author gjoseph
54   * @version $Revision: $ ($Author: $)
55   */
56  public class ParagraphRendererManager extends ObservedManager {
57  
58      public static ParagraphRendererManager getInstance() {
59          return Components.getSingleton(ParagraphRendererManager.class);
60      }
61  
62      private final Map paragraphRenderers;
63  
64      public ParagraphRendererManager() {
65          paragraphRenderers = Collections.synchronizedMap(new LinkedHashMap());
66      }
67  
68      public ParagraphRenderer getRenderer(String name) {
69          final ParagraphRenderer renderer = (ParagraphRenderer) paragraphRenderers.get(name);
70          if (renderer == null) {
71              throw new IllegalArgumentException("No paragraph renderer registered with name " + name);
72          }
73          return renderer;
74      }
75  
76      // TODO : this should allow util pages to get info about the renderer's nodes path and configuration instead of the renderers' impls.
77      public Map getRenderers() {
78          return paragraphRenderers;
79      }
80  
81      protected void onRegister(Content node) {
82          final Collection list = node.getChildren(ItemType.CONTENTNODE);
83          final Iterator it = list.iterator();
84          while (it.hasNext()) {
85              final Content paragraphRendererNode = (Content) it.next();
86              String name = paragraphRendererNode.getNodeData("name").getString();
87              String clazz = paragraphRendererNode.getNodeData("class").getString();
88  
89              if (StringUtils.isEmpty(name)) {
90                  name = paragraphRendererNode.getName();
91              }
92  
93              if (StringUtils.isBlank(name) || StringUtils.isBlank(clazz)) {
94                  log.warn("Can't register template renderer at {}, missing name or class property.", paragraphRendererNode.getHandle());
95                  continue;
96              }
97  
98              try {
99                  final ParagraphRenderer renderer = Classes.newInstance(clazz);
100                 registererParagraphRenderer(name, renderer);
101                 log.debug("Registered template render [{}] with name {}.", clazz, name);
102             } catch (MgnlInstantiationException e) {
103                 throw newInstanciationException(name, clazz, e);
104             } catch (ClassNotFoundException e) {
105                 throw newInstanciationException(name, clazz, e);
106             }
107         }
108     }
109 
110     protected void registererParagraphRenderer(String name, ParagraphRenderer renderer) {
111         if (paragraphRenderers.containsKey(name)) {
112             throw new IllegalStateException("Duplicate paragraph name \"" + name + "\"");
113         }
114         paragraphRenderers.put(name, renderer);
115     }
116 
117     protected void onClear() {
118         paragraphRenderers.clear();
119     }
120 
121     private IllegalStateException newInstanciationException(String name, String clazz, Exception e) {
122         return new IllegalStateException("Can't register paragraph renderer with name [" + name + "] and class [" + clazz + "] : " + e.getClass().getName() + " : " + e.getMessage());
123     }
124 }