Coverage Report - org.apache.maven.plugin.war.PropertyUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyUtils
92% 
100% 
4
 
 1  
 package org.apache.maven.plugin.war;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.codehaus.plexus.util.IOUtil;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.FileInputStream;
 23  
 import java.io.FileNotFoundException;
 24  
 import java.io.IOException;
 25  
 import java.util.Enumeration;
 26  
 import java.util.Properties;
 27  
 
 28  
 /**
 29  
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
 30  
  * @version $Id: PropertyUtils.java 390314 2006-03-31 04:04:35Z brett $
 31  
  * @todo this is duplicated from the resources plugin - migrate to plexus-utils
 32  
  */
 33  
 public final class PropertyUtils
 34  
 {
 35  
     private PropertyUtils()
 36  0
     {
 37  
         // prevent instantiation
 38  0
     }
 39  
 
 40  
     /**
 41  
      * Reads a property file, resolving all internal variables.
 42  
      *
 43  
      * @param propfile       The property file to load
 44  
      * @param fail           wheter to throw an exception when the file cannot be loaded or to return null
 45  
      * @param useSystemProps wheter to incorporate System.getProperties settings into the returned Properties object.
 46  
      * @return the loaded and fully resolved Properties object
 47  
      */
 48  
     public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps )
 49  
         throws IOException
 50  
     {
 51  12
         Properties props = new Properties();
 52  
 
 53  12
         if ( useSystemProps )
 54  
         {
 55  6
             props = new Properties( System.getProperties() );
 56  
         }
 57  
 
 58  12
         if ( propfile.exists() )
 59  
         {
 60  9
             FileInputStream inStream = new FileInputStream( propfile );
 61  
             try
 62  
             {
 63  9
                 props.load( inStream );
 64  
             }
 65  
             finally
 66  
             {
 67  9
                 IOUtil.close( inStream );
 68  9
             }
 69  9
         }
 70  3
         else if ( fail )
 71  
         {
 72  3
             throw new FileNotFoundException( propfile.toString() );
 73  
         }
 74  
 
 75  9
         for ( Enumeration n = props.propertyNames(); n.hasMoreElements(); )
 76  
         {
 77  336
             String k = (String) n.nextElement();
 78  336
             props.setProperty( k, PropertyUtils.getPropertyValue( k, props ) );
 79  336
         }
 80  
 
 81  9
         return props;
 82  
     }
 83  
 
 84  
 
 85  
     /**
 86  
      * Retrieves a property value, replacing values like ${token}
 87  
      * using the Properties to look them up.
 88  
      * <p/>
 89  
      * It will leave unresolved properties alone, trying for System
 90  
      * properties, and implements reparsing (in the case that
 91  
      * the value of a property contains a key), and will
 92  
      * not loop endlessly on a pair like
 93  
      * test = ${test}.
 94  
      */
 95  
     private static String getPropertyValue( String k, Properties p )
 96  
     {
 97  
         // This can also be done using InterpolationFilterReader,
 98  
         // but it requires reparsing the file over and over until
 99  
         // it doesn't change.
 100  
 
 101  336
         String v = p.getProperty( k );
 102  336
         String ret = "";
 103  
         int idx, idx2;
 104  
 
 105  345
         while ( ( idx = v.indexOf( "${" ) ) >= 0 )
 106  
         {
 107  
             // append prefix to result
 108  9
             ret += v.substring( 0, idx );
 109  
 
 110  
             // strip prefix from original
 111  9
             v = v.substring( idx + 2 );
 112  
 
 113  
             // if no matching } then bail
 114  9
             if ( ( idx2 = v.indexOf( '}' ) ) < 0 )
 115  
             {
 116  0
                 break;
 117  
             }
 118  
 
 119  
             // strip out the key and resolve it
 120  
             // resolve the key/value for the ${statement}
 121  9
             String nk = v.substring( 0, idx2 );
 122  9
             v = v.substring( idx2 + 1 );
 123  9
             String nv = p.getProperty( nk );
 124  
 
 125  
             // try global environment..
 126  9
             if ( nv == null )
 127  
             {
 128  3
                 nv = System.getProperty( nk );
 129  
             }
 130  
 
 131  
             // if the key cannot be resolved,
 132  
             // leave it alone ( and don't parse again )
 133  
             // else prefix the original string with the
 134  
             // resolved property ( so it can be parsed further )
 135  
             // taking recursion into account.
 136  9
             if ( nv == null || nv.equals( k ) )
 137  
             {
 138  3
                 ret += "${" + nk + "}";
 139  3
             }
 140  
             else
 141  
             {
 142  6
                 v = nv + v;
 143  
             }
 144  9
         }
 145  336
         return ret + v;
 146  
     }
 147  
 }