Clover coverage report - Ledge Web - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:20:55 CET
file stats: LOC: 200   Methods: 5
NCLOC: 110   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ComponentTool.java 64.3% 67.5% 80% 67.8%
coverage coverage
 1    //
 2    // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 3    // All rights reserved.
 4    //
 5    // Redistribution and use in source and binary forms, with or without modification,
 6    // are permitted provided that the following conditions are met:
 7    //
 8    // * Redistributions of source code must retain the above copyright notice,
 9    // this list of conditions and the following disclaimer.
 10    // * Redistributions in binary form must reproduce the above copyright notice,
 11    // this list of conditions and the following disclaimer in the documentation
 12    // and/or other materials provided with the distribution.
 13    // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 14    // nor the names of its contributors may be used to endorse or promote products
 15    // derived from this software without specific prior written permission.
 16    //
 17    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 18    // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19    // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 20    // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 21    // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 22    // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 23    // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 24    // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25    // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26    // POSSIBILITY OF SUCH DAMAGE.
 27    //
 28    package org.objectledge.web.mvc.components;
 29   
 30    import java.util.HashMap;
 31    import java.util.Iterator;
 32    import java.util.List;
 33    import java.util.Map;
 34   
 35    import org.objectledge.context.Context;
 36    import org.objectledge.pipeline.ProcessingException;
 37    import org.objectledge.templating.Template;
 38    import org.objectledge.templating.TemplatingContext;
 39    import org.objectledge.utils.CollectionUtils;
 40    import org.objectledge.web.mvc.builders.BuildException;
 41    import org.objectledge.web.mvc.builders.DefaultTemplate;
 42    import org.objectledge.web.mvc.finders.MVCClassFinder;
 43    import org.objectledge.web.mvc.finders.MVCTemplateFinder;
 44    import org.objectledge.web.mvc.security.SecurityHelper;
 45   
 46    /**
 47    * A templating context tool for embedding components in application UI.
 48    *
 49    * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
 50    * @version $Id: ComponentTool.java,v 1.16 2005/07/22 17:25:52 pablo Exp $
 51    */
 52    public class ComponentTool
 53    {
 54    /** The context. */
 55    protected Context context;
 56   
 57    /** The class finder for finding component objects. */
 58    protected MVCClassFinder classFinder;
 59   
 60    /** The template finder for finding component templates. */
 61    protected MVCTemplateFinder templateFinder;
 62   
 63    /** SecurityHelper for access checking. */
 64    protected SecurityHelper securityHelper;
 65   
 66    /** the default component implementation. */
 67    protected Component defaultComponent;
 68   
 69    /** the default template. */
 70    protected Template defaultTemplate;
 71   
 72    /**
 73    * Construct a component tool.
 74    *
 75    * @param context thread's processing context.
 76    * @param classFinder class finder for finding component objects.
 77    * @param templateFinder template finder for finding component templates.
 78    * @param securityHelper security helper for access checking
 79    */
 80  92 public ComponentTool(Context context, MVCClassFinder classFinder,
 81    MVCTemplateFinder templateFinder, SecurityHelper securityHelper)
 82    {
 83  92 this.context = context;
 84  92 this.classFinder = classFinder;
 85  92 this.templateFinder = templateFinder;
 86  92 this.securityHelper = securityHelper;
 87  92 this.defaultComponent = new DefaultComponent(context);
 88  92 this.defaultTemplate = new DefaultTemplate();
 89    }
 90   
 91    /**
 92    * Embeds a component with a givemn name.
 93    *
 94    * @param componentName name of the component to be embedded.
 95    * @return contents of a rendered component
 96    * @throws BuildException on problems with component building
 97    * @throws ProcessingException if processing fails.
 98    */
 99  230 public String embed(String componentName)
 100    throws BuildException, ProcessingException
 101    {
 102  230 return embed(componentName, (Map)null);
 103    }
 104   
 105    /**
 106    * Embeds a component with a givemn name.
 107    *
 108    * @param componentName name of the component to be embedded.
 109    * @param config localy scoped configuration in list form.
 110    * @return contents of a rendered component
 111    * @throws BuildException on problems with component building
 112    * @throws ProcessingException if processing fails.
 113    */
 114  0 public String embed(String componentName, List config)
 115    throws BuildException, ProcessingException
 116    {
 117  0 return embed(componentName, CollectionUtils.listToMap(config));
 118    }
 119   
 120    /**
 121    * Embeds a component with a givemn name.
 122    *
 123    * @param componentName name of the component to be embedded.
 124    * @param config localy scoped configuration.
 125    * @return contents of a rendered component
 126    * @throws BuildException on problems with component building
 127    * @throws ProcessingException if processing fails.
 128    */
 129  230 public String embed(String componentName, Map config)
 130    throws BuildException, ProcessingException
 131    {
 132  230 Component component = classFinder.getComponent(componentName);
 133  230 Template template = templateFinder.getComponentTemplate(componentName);
 134  230 template = resolveTemplate(template);
 135  230 if(component != null)
 136    {
 137  92 Template newTemplate = component.getTemplate();
 138  92 if(newTemplate != null)
 139    {
 140  46 template = newTemplate;
 141    }
 142  92 if(template == null)
 143    {
 144  46 template = defaultTemplate;
 145    }
 146    }
 147    else
 148    {
 149  138 if(template == null)
 150    {
 151  46 throw new IllegalArgumentException("No component nor template with name "
 152    + componentName);
 153    }
 154  92 component = defaultComponent;
 155    }
 156  184 securityHelper.checkSecurity(component, context);
 157  184 String result = null;
 158  184 try
 159    {
 160  184 if(config != null)
 161    {
 162  0 Map<String, Object> store = new HashMap<String, Object>();
 163  0 TemplatingContext tContext =
 164    TemplatingContext.getTemplatingContext(context);
 165  0 Iterator i = config.keySet().iterator();
 166  0 while(i.hasNext())
 167    {
 168  0 String key = (String)i.next();
 169  0 store.put(key, tContext.get(key));
 170  0 tContext.put(key, config.get(key));
 171    }
 172  0 result = component.build(template);
 173  0 i = config.keySet().iterator();
 174  0 while(i.hasNext())
 175    {
 176  0 String key = (String)i.next();
 177  0 tContext.put(key, store.get(key));
 178    }
 179    }
 180    else
 181    {
 182  184 result = component.build(template);
 183    }
 184  138 return result;
 185    }
 186    catch(Exception e)
 187    {
 188  46 throw new ProcessingException("Failed to build component with template: "+template.getName(), e);
 189    }
 190    }
 191   
 192    /**
 193    * Allows overriding of the used template.
 194    * @param template the template to be overriden.
 195    */
 196  230 protected Template resolveTemplate(Template template)
 197    {
 198  230 return template;
 199    }
 200    }