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.File;
22  import java.io.IOException;
23  import java.io.UncheckedIOException;
24  import java.nio.charset.StandardCharsets;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.stream.Collectors;
31  
32  import org.codehaus.plexus.util.Os;
33  
34  /**
35   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
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 = getConfigLines();
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 non-comment, non-empty lines in the config file
98       */
99      private List<String> getConfigLines() {
100         Path configPath = getConfigDirectory().toPath().resolve("config");
101         if (Files.exists(configPath)) {
102             try {
103                 return Files.lines(configPath, StandardCharsets.UTF_8)
104                         .filter(line -> !line.isEmpty())
105                         .filter(line -> !line.startsWith("#"))
106                         .collect(Collectors.toCollection(ArrayList::new));
107             } catch (UncheckedIOException | IOException e) {
108                 return new ArrayList<>();
109             }
110         }
111         return new ArrayList<>();
112     }
113 }