1 package org.apache.maven.scm.provider.svn;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.util.IOUtil;
23 import org.codehaus.plexus.util.Os;
24 import org.codehaus.plexus.util.StringUtils;
25
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.List;
33
34
35
36
37
38 public class SvnConfigFileReader
39 {
40 private File configDirectory;
41
42 public File getConfigDirectory()
43 {
44 if ( configDirectory == null )
45 {
46 if ( Os.isFamily( "windows" ) )
47 {
48 configDirectory = new File( System.getProperty( "user.home" ), "Application Data/Subversion" );
49 }
50 else
51 {
52 configDirectory = new File( System.getProperty( "user.home" ), ".subversion" );
53 }
54 }
55
56 return configDirectory;
57 }
58
59 public void setConfigDirectory( File configDirectory )
60 {
61 this.configDirectory = configDirectory;
62 }
63
64 public String getProperty( String group, String propertyName )
65 {
66 List<String> lines = getConfLines();
67
68 boolean inGroup = false;
69 for ( Iterator<String> i = lines.iterator(); i.hasNext(); )
70 {
71 String line = i.next().trim();
72
73 if ( !inGroup )
74 {
75 if ( ( "[" + group + "]" ).equals( line ) )
76 {
77 inGroup = true;
78 }
79 }
80 else
81 {
82 if ( line.startsWith( "[" ) && line.endsWith( "]" ) )
83 {
84
85 return null;
86 }
87
88 if ( line.startsWith( "#" ) )
89 {
90 continue;
91 }
92 if ( line.indexOf( '=' ) < 0 )
93 {
94 continue;
95 }
96
97 String property = line.substring( 0, line.indexOf( '=' ) ).trim();
98
99 if ( !property.equals( propertyName ) )
100 {
101 continue;
102 }
103
104 String value = line.substring( line.indexOf( '=' ) + 1 );
105 return value.trim();
106 }
107 }
108
109 return null;
110 }
111
112
113
114
115
116
117 private List<String> getConfLines()
118 {
119 List<String> lines = new ArrayList<String>();
120
121 BufferedReader reader = null;
122
123 try
124 {
125 if ( getConfigDirectory().exists() )
126 {
127 reader = new BufferedReader( new FileReader( new File( getConfigDirectory(), "config" ) ) );
128 String line;
129 while ( ( line = reader.readLine() ) != null )
130 {
131 if ( !line.startsWith( "#" ) && StringUtils.isNotEmpty( line ) )
132 {
133 lines.add( line );
134 }
135 }
136 }
137 }
138 catch ( IOException e )
139 {
140 lines.clear();
141 }
142 finally
143 {
144 IOUtil.close( reader );
145 reader = null;
146 }
147
148 return lines;
149 }
150 }