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