|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ParagraphRendererManager | Line # 56 | 29 | 0% | 16 | 6 | 87.2% |
0.87234044
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
(9) | |||
Result | |||
0.7234042
|
info.magnolia.module.templating.ParagraphRendererManagerTest.testParagraphRenderersAreAddedProperly info.magnolia.module.templating.ParagraphRendererManagerTest.testParagraphRenderersAreAddedProperly | 1 PASS | |
0.68085104
|
info.magnolia.module.templating.ParagraphRendererManagerTest.testNotSpecifyingTheNamePropertyShouldMeanTheNodeNameIsUsedInstead info.magnolia.module.templating.ParagraphRendererManagerTest.testNotSpecifyingTheNamePropertyShouldMeanTheNodeNameIsUsedInstead | 1 PASS | |
0.68085104
|
info.magnolia.module.templating.ParagraphRendererManagerTest.testCanNotAddParagraphRenderersWithDuplicateNames info.magnolia.module.templating.ParagraphRendererManagerTest.testCanNotAddParagraphRenderersWithDuplicateNames | 1 PASS | |
0.68085104
|
info.magnolia.module.templating.ParagraphRendererManagerTest.testUnknownParagraphRendererNamesThrowsException info.magnolia.module.templating.ParagraphRendererManagerTest.testUnknownParagraphRendererNamesThrowsException | 1 PASS | |
0.63829786
|
info.magnolia.module.templating.ParagraphRendererManagerTest.testRenderersNamePropertyHasPriorityOverNodeName info.magnolia.module.templating.ParagraphRendererManagerTest.testRenderersNamePropertyHasPriorityOverNodeName | 1 PASS | |
0.59574467
|
info.magnolia.module.templating.ParagraphRenderingFacadeTest.testSetsJspPageContext info.magnolia.module.templating.ParagraphRenderingFacadeTest.testSetsJspPageContext | 1 PASS | |
0.59574467
|
info.magnolia.module.templating.ParagraphRenderingFacadeTest.testUsesTheAppropriateParagraphWhenNotExplicitelyPassed info.magnolia.module.templating.ParagraphRenderingFacadeTest.testUsesTheAppropriateParagraphWhenNotExplicitelyPassed | 1 PASS | |
0.59574467
|
info.magnolia.module.templating.ParagraphRenderingFacadeTest.testRenderCallsTheAppropriateRenderer info.magnolia.module.templating.ParagraphRenderingFacadeTest.testRenderCallsTheAppropriateRenderer | 1 PASS | |
0.40425533
|
info.magnolia.module.templating.ParagraphRendererManagerTest.testRenderersWithFaultyClassNamesShouldNotBeIgnored info.magnolia.module.templating.ParagraphRendererManagerTest.testRenderersWithFaultyClassNamesShouldNotBeIgnored | 1 PASS | |
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 | public static ParagraphRendererManager getInstance() { |
59 | 0 | return Components.getSingleton(ParagraphRendererManager.class); |
60 | } | |
61 | ||
62 | private final Map paragraphRenderers; | |
63 | ||
64 | 9 | public ParagraphRendererManager() { |
65 | 9 | paragraphRenderers = Collections.synchronizedMap(new LinkedHashMap()); |
66 | } | |
67 | ||
68 | 12 | 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 | public Map getRenderers() { |
78 | 8 | return paragraphRenderers; |
79 | } | |
80 | ||
81 | 11 | 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 | 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 | protected void onClear() { |
118 | 1 | paragraphRenderers.clear(); |
119 | } | |
120 | ||
121 | 1 | 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 | } |
|