Coverage Report - org.apache.maven.scm.provider.svn.SvnConfigFileReader
 
Classes in this File Line Coverage Branch Coverage Complexity
SvnConfigFileReader
0 %
0/41
0 %
0/28
5,25
 
 1  
 package org.apache.maven.scm.provider.svn;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.codehaus.plexus.util.IOUtil;
 23  
 import org.codehaus.plexus.util.Os;
 24  
 import org.codehaus.plexus.util.StringUtils;
 25  
 
 26  
 import java.io.BufferedReader;
 27  
 import java.io.File;
 28  
 import java.io.FileReader;
 29  
 import java.io.IOException;
 30  
 import java.util.ArrayList;
 31  
 import java.util.Iterator;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 36  
  * @version $Id: SvnConfigFileReader.java 1058775 2011-01-13 22:52:38Z olamy $
 37  
  */
 38  0
 public class SvnConfigFileReader
 39  
 {
 40  
     private File configDirectory;
 41  
 
 42  
     public File getConfigDirectory()
 43  
     {
 44  0
         if ( configDirectory == null )
 45  
         {
 46  0
             if ( Os.isFamily( "windows" ) )
 47  
             {
 48  0
                 configDirectory = new File( System.getProperty( "user.home" ), "Application Data/Subversion" );
 49  
             }
 50  
             else
 51  
             {
 52  0
                 configDirectory = new File( System.getProperty( "user.home" ), ".subversion" );
 53  
             }
 54  
         }
 55  
 
 56  0
         return configDirectory;
 57  
     }
 58  
 
 59  
     public void setConfigDirectory( File configDirectory )
 60  
     {
 61  0
         this.configDirectory = configDirectory;
 62  0
     }
 63  
 
 64  
     public String getProperty( String group, String propertyName )
 65  
     {
 66  0
         List<String> lines = getConfLines();
 67  
 
 68  0
         boolean inGroup = false;
 69  0
         for ( Iterator<String> i = lines.iterator(); i.hasNext(); )
 70  
         {
 71  0
             String line = i.next().trim();
 72  
 
 73  0
             if ( !inGroup )
 74  
             {
 75  0
                 if ( ( "[" + group + "]" ).equals( line ) )
 76  
                 {
 77  0
                     inGroup = true;
 78  
                 }
 79  
             }
 80  
             else
 81  
             {
 82  0
                 if ( line.startsWith( "[" ) && line.endsWith( "]" ) )
 83  
                 {
 84  
                     //a new group start
 85  0
                     return null;
 86  
                 }
 87  
 
 88  0
                 if ( line.startsWith( "#" ) )
 89  
                 {
 90  0
                     continue;
 91  
                 }
 92  0
                 if ( line.indexOf( '=' ) < 0 )
 93  
                 {
 94  0
                     continue;
 95  
                 }
 96  
 
 97  0
                 String property = line.substring( 0, line.indexOf( '=' ) ).trim();
 98  
 
 99  0
                 if ( !property.equals( propertyName ) )
 100  
                 {
 101  0
                     continue;
 102  
                 }
 103  
 
 104  0
                 String value = line.substring( line.indexOf( '=' ) + 1 );
 105  0
                 return value.trim();
 106  
             }
 107  0
         }
 108  
 
 109  0
         return null;
 110  
     }
 111  
 
 112  
     /**
 113  
      * Load the svn config file
 114  
      *
 115  
      * @return the list of all lines
 116  
      */
 117  
     private List<String> getConfLines()
 118  
     {
 119  0
         List<String> lines = new ArrayList<String>();
 120  
 
 121  0
         BufferedReader reader = null;
 122  
 
 123  
         try
 124  
         {
 125  0
             if ( getConfigDirectory().exists() )
 126  
             {
 127  0
                 reader = new BufferedReader( new FileReader( new File( getConfigDirectory(), "config" ) ) );
 128  
                 String line;
 129  0
                 while ( ( line = reader.readLine() ) != null )
 130  
                 {
 131  0
                     if ( !line.startsWith( "#" ) && StringUtils.isNotEmpty( line ) )
 132  
                     {
 133  0
                         lines.add( line );
 134  
                     }
 135  
                 }
 136  
             }
 137  
         }
 138  0
         catch ( IOException e )
 139  
         {
 140  0
             lines.clear();
 141  
         }
 142  
         finally
 143  
         {
 144  0
             IOUtil.close( reader );
 145  0
             reader = null;
 146  0
         }
 147  
 
 148  0
         return lines;
 149  
     }
 150  
 }