001package org.apache.maven.scm.provider.svn;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.codehaus.plexus.util.IOUtil;
023import org.codehaus.plexus.util.Os;
024import org.codehaus.plexus.util.StringUtils;
025
026import java.io.BufferedReader;
027import java.io.File;
028import java.io.FileReader;
029import java.io.IOException;
030import java.util.ArrayList;
031import java.util.Iterator;
032import java.util.List;
033
034/**
035 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
036 *
037 */
038public class SvnConfigFileReader
039{
040    private File configDirectory;
041
042    public File getConfigDirectory()
043    {
044        if ( configDirectory == null )
045        {
046            if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
047            {
048                configDirectory = new File( System.getenv( "APPDATA" ), "Subversion" );
049            }
050            else
051            {
052                configDirectory = new File( System.getProperty( "user.home" ), ".subversion" );
053            }
054        }
055
056        return configDirectory;
057    }
058
059    public void setConfigDirectory( File configDirectory )
060    {
061        this.configDirectory = configDirectory;
062    }
063
064    public String getProperty( String group, String propertyName )
065    {
066        List<String> lines = getConfLines();
067
068        boolean inGroup = false;
069        for ( Iterator<String> i = lines.iterator(); i.hasNext(); )
070        {
071            String line = i.next().trim();
072
073            if ( !inGroup )
074            {
075                if ( ( "[" + group + "]" ).equals( line ) )
076                {
077                    inGroup = true;
078                }
079            }
080            else
081            {
082                if ( line.startsWith( "[" ) && line.endsWith( "]" ) )
083                {
084                    //a new group start
085                    return null;
086                }
087
088                if ( line.startsWith( "#" ) )
089                {
090                    continue;
091                }
092                if ( line.indexOf( '=' ) < 0 )
093                {
094                    continue;
095                }
096
097                String property = line.substring( 0, line.indexOf( '=' ) ).trim();
098
099                if ( !property.equals( propertyName ) )
100                {
101                    continue;
102                }
103
104                String value = line.substring( line.indexOf( '=' ) + 1 );
105                return value.trim();
106            }
107        }
108
109        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        List<String> lines = new ArrayList<String>();
120
121        BufferedReader reader = null;
122
123        try
124        {
125            if ( getConfigDirectory().exists() )
126            {
127                reader = new BufferedReader( new FileReader( new File( getConfigDirectory(), "config" ) ) );
128                String line;
129                while ( ( line = reader.readLine() ) != null )
130                {
131                    if ( !line.startsWith( "#" ) && StringUtils.isNotEmpty( line ) )
132                    {
133                        lines.add( line );
134                    }
135                }
136            }
137        }
138        catch ( IOException e )
139        {
140            lines.clear();
141        }
142        finally
143        {
144            IOUtil.close( reader );
145            reader = null;
146        }
147
148        return lines;
149    }
150}