Clover Coverage Report - magnolia-module-templating 4.4.5
Coverage timestamp: Mon Sep 12 2011 16:32:30 CEST
../../../../img/srcFileCovDistChart9.png 5% of files have more coverage
29   124   16   3.62
10   69   0.55   8
8     2  
1    
 
  ParagraphRendererManager       Line # 56 29 0% 16 6 87.2% 0.87234044
 
  (9)
 
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  0 toggle public static ParagraphRendererManager getInstance() {
59  0 return Components.getSingleton(ParagraphRendererManager.class);
60    }
61   
62    private final Map paragraphRenderers;
63   
 
64  9 toggle public ParagraphRendererManager() {
65  9 paragraphRenderers = Collections.synchronizedMap(new LinkedHashMap());
66    }
67   
 
68  12 toggle public ParagraphRenderer getRenderer(String name) {
69  12 final ParagraphRenderer renderer = (ParagraphRenderer) paragraphRenderers.get(name);
70  12 if (renderer == null) {
71  2 throw new IllegalArgumentException("No paragraph renderer registered with name " + name);
72    }
73  10 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  8 toggle public Map getRenderers() {
78  8 return paragraphRenderers;
79    }
80   
 
81  11 toggle protected void onRegister(Content node) {
82  11 final Collection list = node.getChildren(ItemType.CONTENTNODE);
83  11 final Iterator it = list.iterator();
84  29 while (it.hasNext()) {
85  20 final Content paragraphRendererNode = (Content) it.next();
86  20 String name = paragraphRendererNode.getNodeData("name").getString();
87  20 String clazz = paragraphRendererNode.getNodeData("class").getString();
88   
89  20 if (StringUtils.isEmpty(name)) {
90  2 name = paragraphRendererNode.getName();
91    }
92   
93  20 if (StringUtils.isBlank(name) || StringUtils.isBlank(clazz)) {
94  0 log.warn("Can't register template renderer at {}, missing name or class property.", paragraphRendererNode.getHandle());
95  0 continue;
96    }
97   
98  20 try {
99  20 final ParagraphRenderer renderer = Classes.newInstance(clazz);
100  19 registererParagraphRenderer(name, renderer);
101  18 log.debug("Registered template render [{}] with name {}.", clazz, name);
102    } catch (MgnlInstantiationException e) {
103  0 throw newInstanciationException(name, clazz, e);
104    } catch (ClassNotFoundException e) {
105  1 throw newInstanciationException(name, clazz, e);
106    }
107    }
108    }
109   
 
110  19 toggle protected void registererParagraphRenderer(String name, ParagraphRenderer renderer) {
111  19 if (paragraphRenderers.containsKey(name)) {
112  1 throw new IllegalStateException("Duplicate paragraph name \"" + name + "\"");
113    }
114  18 paragraphRenderers.put(name, renderer);
115    }
116   
 
117  1 toggle protected void onClear() {
118  1 paragraphRenderers.clear();
119    }
120   
 
121  1 toggle private IllegalStateException newInstanciationException(String name, String clazz, Exception e) {
122  1 return new IllegalStateException("Can't register paragraph renderer with name [" + name + "] and class [" + clazz + "] : " + e.getClass().getName() + " : " + e.getMessage());
123    }
124    }