1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
95
96
97
98 private List<String> getConfLines() {
99 List<String> lines = new ArrayList<>();
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 }