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