View Javadoc
1   package org.apache.maven.scm.provider.svn;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
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                      //a new group start
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      * Load the svn config file
114      *
115      * @return the list of all lines
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 }