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  
32  /**
33   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
34   *
35   */
36  public class SvnConfigFileReader {
37      private File configDirectory;
38  
39      public File getConfigDirectory() {
40          if (configDirectory == null) {
41              if (Os.isFamily(Os.FAMILY_WINDOWS)) {
42                  configDirectory = new File(System.getenv("APPDATA"), "Subversion");
43              } else {
44                  configDirectory = new File(System.getProperty("user.home"), ".subversion");
45              }
46          }
47  
48          return configDirectory;
49      }
50  
51      public void setConfigDirectory(File configDirectory) {
52          this.configDirectory = configDirectory;
53      }
54  
55      public String getProperty(String group, String propertyName) {
56          List<String> lines = getConfLines();
57  
58          boolean inGroup = false;
59          for (Iterator<String> i = lines.iterator(); i.hasNext(); ) {
60              String line = i.next().trim();
61  
62              if (!inGroup) {
63                  if (("[" + group + "]").equals(line)) {
64                      inGroup = true;
65                  }
66              } else {
67                  if (line.startsWith("[") && line.endsWith("]")) {
68                      // a new group start
69                      return null;
70                  }
71  
72                  if (line.startsWith("#")) {
73                      continue;
74                  }
75                  if (line.indexOf('=') < 0) {
76                      continue;
77                  }
78  
79                  String property = line.substring(0, line.indexOf('=')).trim();
80  
81                  if (!property.equals(propertyName)) {
82                      continue;
83                  }
84  
85                  String value = line.substring(line.indexOf('=') + 1);
86                  return value.trim();
87              }
88          }
89  
90          return null;
91      }
92  
93      /**
94       * Load the svn config file
95       *
96       * @return the list of all lines
97       */
98      private List<String> getConfLines() {
99          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 }