Coverage report

  %line %branch
org.apache.commons.jelly.tags.util.PropertiesTag
65% 
97% 

 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.tags.util;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileInputStream;
 21  
 import java.io.FileNotFoundException;
 22  
 import java.io.InputStream;
 23  
 import java.io.IOException;
 24  
 import java.util.Enumeration;
 25  
 import java.util.Properties;
 26  
 
 27  
 import org.apache.commons.jelly.JellyTagException;
 28  
 import org.apache.commons.jelly.TagSupport;
 29  
 import org.apache.commons.jelly.XMLOutput;
 30  
 
 31  
 /**
 32  
  * A tag which loads a properties file from a given file name or URI
 33  
  * which are loaded into the current context.
 34  
  *
 35  
  * @author Jim Birchfield
 36  
  * @version $Revision: 1.6 $
 37  
  */
 38  
 public class PropertiesTag extends TagSupport {
 39  
     private String file;
 40  
     private String uri;
 41  
     private String var;
 42  
 
 43  2
     public PropertiesTag() {
 44  2
     }
 45  
 
 46  
     // Tag interface
 47  
     //-------------------------------------------------------------------------
 48  
     public void doTag(final XMLOutput output) throws JellyTagException {
 49  2
         if (file == null && uri == class="keyword">null) {
 50  0
             throw new JellyTagException("This tag must define a 'file' or 'uri' attribute");
 51  
         }
 52  2
         InputStream is = null;
 53  2
         if (file != null) {
 54  0
             File f = new File(file);
 55  0
             if (!f.exists()) {
 56  0
                 throw new JellyTagException("file: " + file + " does not exist!");
 57  
             }
 58  
 
 59  
             try {
 60  0
                 is = new FileInputStream(f);
 61  0
             } catch (FileNotFoundException e) {
 62  0
                 throw new JellyTagException(e);
 63  0
             }
 64  
         }
 65  
         else {
 66  2
             is = context.getResourceAsStream(uri);
 67  2
             if (is == null) {
 68  0
                 throw new JellyTagException( "Could not find: " + uri );
 69  
             }
 70  
         }
 71  2
         Properties props = new Properties();
 72  
 
 73  
         try {
 74  2
             props.load(is);
 75  2
         } catch (IOException e) {
 76  0
             throw new JellyTagException("properties tag could not load from file",e);
 77  
         }
 78  
 
 79  2
         if (var != null) {
 80  1
             context.setVariable(var, props);
 81  
         }
 82  
         else {
 83  1
             Enumeration propsEnum = props.propertyNames();
 84  4
             while (propsEnum.hasMoreElements()) {
 85  2
                 String key = (String) propsEnum.nextElement();
 86  2
                 String value = props.getProperty(key);
 87  
 
 88  
                 // @todo we should parse the value in case its an Expression
 89  2
                 context.setVariable(key, value);
 90  
             }
 91  
         }
 92  
 
 93  2
     }
 94  
 
 95  
     // Properties
 96  
     //-------------------------------------------------------------------------
 97  
 
 98  
     /**
 99  
      * Sets the file name to be used to load the properties file.
 100  
      */
 101  
     public void setFile(String file) {
 102  0
         this.file = file;
 103  0
     }
 104  
 
 105  
     /**
 106  
      * Sets the URI of the properties file to use. This can be a full URL or a relative URI
 107  
      * or an absolute URI to the root context of this JellyContext.
 108  
      */
 109  
     public void setUri(String uri) {
 110  2
         this.uri = uri;
 111  2
     }
 112  
 
 113  
     /**
 114  
      * If this is defined then a Properties object containing all the
 115  
      * properties will be created and exported, otherwise the current variable
 116  
      * scope will be set to the value of the properties.
 117  
      *
 118  
      * @param var The var to set
 119  
      */
 120  
     public void setVar(String var) {
 121  1
         this.var = class="keyword">var;
 122  1
     }
 123  
 
 124  
 }

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