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