Coverage report

  %line %branch
org.apache.commons.jelly.impl.StaticTagScript
70% 
85% 

 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  
 package org.apache.commons.jelly.impl;
 17  
 
 18  
 import java.net.URL;
 19  
 import java.util.Iterator;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.apache.commons.jelly.DynaTag;
 23  
 import org.apache.commons.jelly.JellyContext;
 24  
 import org.apache.commons.jelly.JellyException;
 25  
 import org.apache.commons.jelly.JellyTagException;
 26  
 import org.apache.commons.jelly.Tag;
 27  
 import org.apache.commons.jelly.TagLibrary;
 28  
 import org.apache.commons.jelly.XMLOutput;
 29  
 import org.apache.commons.jelly.expression.Expression;
 30  
 import org.xml.sax.SAXException;
 31  
 
 32  
 /**
 33  
  * <p><code>StaticTagScript</code> is a script that evaluates a StaticTag, a piece of static XML
 34  
  * though its attributes or element content may contain dynamic expressions.
 35  
  * The first time this tag evaluates, it may have become a dynamic tag, so it will check that
 36  
  * a new dynamic tag has not been generated.</p>
 37  
  *
 38  
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 39  
  * @version $Revision: 1.8 $
 40  
  */
 41  
 public class StaticTagScript extends TagScript {
 42  
 
 43  0
     public StaticTagScript() {
 44  0
     }
 45  
 
 46  
     public StaticTagScript(TagFactory tagFactory) {
 47  69
         super(tagFactory);
 48  69
     }
 49  
 
 50  
 
 51  
     // Script interface
 52  
     //-------------------------------------------------------------------------
 53  
     public void run(JellyContext context, XMLOutput output) throws JellyTagException {
 54  
         try {
 55  48
             startNamespacePrefixes(output);
 56  48
         } catch (SAXException e) {
 57  0
             throw new JellyTagException("could not start namespace prefixes",e);
 58  
         }
 59  
 
 60  48
         Tag tag = null;
 61  
         try {
 62  48
             tag = getTag();
 63  
 
 64  
             // lets see if we have a dynamic tag
 65  48
             if (tag instanceof StaticTag) {
 66  48
                 tag = findDynamicTag(context, (StaticTag) tag);
 67  
             }
 68  
 
 69  48
             setTag(tag);
 70  48
         } catch (JellyException e) {
 71  0
             throw new JellyTagException(e);
 72  
         }
 73  
 
 74  48
         URL rootURL = context.getRootURL();
 75  48
         URL currentURL = context.getCurrentURL();
 76  
         try {
 77  48
             if ( tag == null ) {
 78  0
                 return;
 79  
             }
 80  48
             tag.setContext(context);
 81  48
             setContextURLs(context);
 82  
 
 83  48
             DynaTag dynaTag = (DynaTag) tag;
 84  
 
 85  
             // ### probably compiling this to 2 arrays might be quicker and smaller
 86  138
             for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
 87  54
                 Map.Entry entry = (Map.Entry) iter.next();
 88  42
                 String name = (String) entry.getKey();
 89  42
                 Expression expression = (Expression) entry.getValue();
 90  
 
 91  42
                 Object value = null;
 92  
 
 93  42
                 if ( Expression.class.isAssignableFrom( dynaTag.getAttributeType(name) ) ) {
 94  0
                     value = expression;
 95  
                 } else {
 96  42
                     value = expression.evaluate(context);
 97  
                 }
 98  
 
 99  42
                 dynaTag.setAttribute(name, value);
 100  
             }
 101  
 
 102  48
             tag.doTag(output);
 103  48
         }
 104  
         catch (JellyTagException e) {
 105  0
             handleException(e);
 106  0
         }
 107  
         catch (RuntimeException e) {
 108  0
             handleException(e);
 109  0
         } finally {
 110  0
             context.setCurrentURL(currentURL);
 111  48
             context.setRootURL(rootURL);
 112  
         }
 113  
 
 114  
         try {
 115  48
             endNamespacePrefixes(output);
 116  48
         } catch (SAXException e) {
 117  0
             throw new JellyTagException("could not end namespace prefixes",e);
 118  
         }
 119  48
     }
 120  
 
 121  
     /**
 122  
      * Attempts to find a dynamically created tag that has been created since this
 123  
      * script was compiled
 124  
      */
 125  
     protected Tag findDynamicTag(JellyContext context, StaticTag tag) throws JellyException {
 126  
         // lets see if there's a tag library for this URI...
 127  48
         TagLibrary taglib = context.getTagLibrary( tag.getUri() );
 128  48
         if ( taglib != null ) {
 129  18
             Tag newTag = taglib.createTag( tag.getLocalName(), getSaxAttributes() );
 130  18
             if ( newTag != null ) {
 131  0
                 newTag.setParent( tag.getParent() );
 132  0
                 newTag.setBody( tag.getBody() );
 133  0
                 return newTag;
 134  
             }
 135  
         }
 136  48
         return tag;
 137  
     }
 138  
 }

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