Coverage report

  %line %branch
org.apache.commons.jelly.TagLibrary
49% 
75% 

 1  
 /*
 2  
  * Copyright 2002,2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.commons.jelly;
 18  
 
 19  
 import java.io.File;
 20  
 import java.util.HashMap;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.apache.commons.beanutils.ConvertUtils;
 24  
 import org.apache.commons.beanutils.Converter;
 25  
 
 26  
 import org.apache.commons.jelly.expression.CompositeExpression;
 27  
 import org.apache.commons.jelly.expression.ConstantExpression;
 28  
 import org.apache.commons.jelly.expression.Expression;
 29  
 import org.apache.commons.jelly.expression.ExpressionFactory;
 30  
 import org.apache.commons.jelly.impl.TagFactory;
 31  
 import org.apache.commons.jelly.impl.TagScript;
 32  
 
 33  
 import org.xml.sax.Attributes;
 34  
 
 35  
 /** <p><code>Taglib</code> represents the metadata for a Jelly custom tag library.</p>
 36  
   *
 37  
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 38  
   * @version $Revision: 155420 $
 39  
   */
 40  
 
 41  
 public abstract class TagLibrary {
 42  
 
 43  988
     private Map tags = new HashMap();
 44  
 
 45  
     static {
 46  
 
 47  
         // register standard converters
 48  
 
 49  273
         ConvertUtils.register(
 50  
             new Converter() {
 51  
                 public Object convert(Class type, Object value) {
 52  
                     if ( value instanceof File ) {
 53  
                         return (File) value;
 54  
                     }
 55  
                     else if ( value != null ) {
 56  
                         String text = value.toString();
 57  
                         return new File( text );
 58  
                     }
 59  
                     return null;
 60  
                 }
 61  
             },
 62  273
             File.class
 63  
         );
 64  273
     }
 65  
 
 66  988
     public TagLibrary() {
 67  988
     }
 68  
 
 69  
     /** Creates a new script to execute the given tag name and attributes */
 70  
     public TagScript createTagScript(String name, Attributes attributes)
 71  
         throws JellyException {
 72  
 
 73  19721
         Object value = tags.get(name);
 74  19721
         if (value instanceof Class) {
 75  19721
             Class type = (Class) value;
 76  19721
             return TagScript.newInstance(type);
 77  
         }
 78  0
         else if (value instanceof TagFactory) {
 79  0
             return new TagScript( (TagFactory) value );
 80  
         }
 81  0
         return null;
 82  
 
 83  
     }
 84  
 
 85  
     /** Creates a new Tag for the given tag name and attributes */
 86  
     public Tag createTag(String name, Attributes attributes)
 87  
         throws JellyException {
 88  
 
 89  0
         Object value = tags.get(name);
 90  0
         if (value instanceof Class) {
 91  0
             Class type = (Class) value;
 92  
             try {
 93  0
                 return (Tag) type.newInstance();
 94  0
             } catch (InstantiationException e) {
 95  0
                 throw new JellyException(e.toString());
 96  0
             } catch (IllegalAccessException e) {
 97  0
                 throw new JellyException(e.toString());
 98  
             }
 99  
         }
 100  0
         else if (value instanceof TagFactory) {
 101  0
             TagFactory factory = (TagFactory) value;
 102  0
             return factory.createTag(name, attributes);
 103  
         }
 104  0
         return null;
 105  
     }
 106  
 
 107  
     /** Allows taglibs to use their own expression evaluation mechanism */
 108  
     public Expression createExpression(
 109  
         ExpressionFactory factory,
 110  
         TagScript tagScript,
 111  
         String attributeName,
 112  
         String attributeValue)
 113  
         throws JellyException {
 114  
 
 115  26026
         ExpressionFactory myFactory = getExpressionFactory();
 116  26026
         if (myFactory == null) {
 117  26026
             myFactory = factory;
 118  
         }
 119  26026
         if (myFactory != null) {
 120  26026
             return CompositeExpression.parse(attributeValue, myFactory);
 121  
         }
 122  
 
 123  
         // will use a constant expression instead
 124  0
         return new ConstantExpression(attributeValue);
 125  
     }
 126  
 
 127  
 
 128  
     // Implementation methods
 129  
     //-------------------------------------------------------------------------
 130  
 
 131  
     /**
 132  
      * Registers a tag implementation Class for a given tag name
 133  
      */
 134  
     protected void registerTag(String name, Class type) {
 135  31395
         tags.put(name, type);
 136  31395
     }
 137  
 
 138  
     /**
 139  
      * Registers a tag factory for a given tag name
 140  
      */
 141  
     protected void registerTagFactory(String name, TagFactory tagFactory) {
 142  0
         tags.put(name, tagFactory);
 143  0
     }
 144  
 
 145  
     /** Allows derived tag libraries to use their own factory */
 146  
     protected ExpressionFactory getExpressionFactory() {
 147  26026
         return null;
 148  
     }
 149  
 
 150  
     protected Map getTagClasses() {
 151  0
         return tags;
 152  
     }
 153  
 
 154  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.