View Javadoc

1   package net.sf.statscm;
2   
3   /*
4    * Copyright 2006 Doug Culnane
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.util.Locale;
21  
22  import net.sf.statcvs.output.ConfigurationException;
23  import net.sf.statcvs.output.ConfigurationOptions;
24  
25  import org.apache.maven.model.Build;
26  import org.apache.maven.model.Developer;
27  import org.apache.maven.project.MavenProject;
28  import org.jfree.util.Log;
29  
30  /**
31   * Central StatSCM Configuration object.
32   * 
33   * 
34   * @author DoCulnan
35   * 
36   */
37  public class StatConf extends ConfigurationOptions
38  {
39  	
40      /**
41       * Name of directory for HTML and xdoc files.
42       */
43      protected static final String STATSCM_DIR_NAME = "statscm";
44  
45      /**
46       * String identifier for Subversion connetion type
47       */
48      protected static final String CONNECTION_TYPE_SVN = "svn";
49  
50      /**
51       * String identifier for CVS connetion type
52       */
53      protected static final String CONNECTION_TYPE_CVS = "cvs";
54  
55      /**
56       * Conveniance variable to deal with the Unix (/) DOS (\) problem. This is a shortcut for
57       * System.getProperty("file.separator").
58       */
59      protected String FILE_SEPARATOR = System.getProperty( "file.separator" );
60  
61      /**
62       * Variable indicating the type of SCM system.
63       */
64      private String connectionType;
65      
66      
67      /** 
68       * Store the locale for this confuguration instance.
69       */
70      private Locale locale = Locale.US;
71  
72      /**
73       * Public constructor.
74       */
75      StatConf()
76      {
77      }
78      
79      public static void setConfigFile(String configFile) {
80      	try {
81  			ConfigurationOptions.setConfigFile(configFile);
82  		} catch (ConfigurationException e) {
83  			Log.error("Error reading Configuration File.", e);
84  		}
85      }
86      
87      /**
88       * Base Directory file.  This is the root folder of the project.
89       */
90      private File baseDirectory = new File( "." );
91  
92      /**
93       * To be called by plugin. If this is not called early in the process then nothing will work.
94       * 
95       * @param project
96       *            Maven Project Object Model (pom.xml).
97       * @throws ConfigurationException
98       *             Throws error if there is any problem with the configuration.
99       */
100     public void configure( MavenProject project, Locale locale ) throws ConfigurationException
101     {
102         this.locale = locale;
103 
104         if ( project.getBasedir() != null )
105         {
106             baseDirectory = project.getBasedir();
107         }
108 
109         if ( project.getScm() == null )
110         {
111             throw new ConfigurationException( "There must be scm section in your pom.xml file." );
112         }
113         
114         // Set output parameters.
115         setOutputFormat( "xdoc" );
116         
117         // ? setDefaultCssFile("maven2-xdoc.css");
118         String buildBaseDirectory = null;
119         Build build = project.getBuild();
120         if ( build != null )
121         {
122             buildBaseDirectory = build.getDirectory();
123         }
124         if ( buildBaseDirectory == null )
125         {
126             buildBaseDirectory = baseDirectory.getAbsolutePath() + FILE_SEPARATOR + "target";
127         }
128         setOutputDir( buildBaseDirectory + FILE_SEPARATOR + "generated-site"
129                         + FILE_SEPARATOR + "xdoc" + FILE_SEPARATOR + STATSCM_DIR_NAME );
130 
131         // Set up connection type
132         connectionType = null;
133         if ( project.getScm().getConnection() != null ) 
134         {
135             connectionType = extractConnectionType( project.getScm().getConnection() );
136         }
137         if ( connectionType == null )
138         {
139             connectionType = extractConnectionType( project.getScm().getDeveloperConnection() );
140         }
141         if ( connectionType == null )
142         {
143             throw new ConfigurationException( "There must be scm connection url section in your pom.xml file for a supported SCM type." );
144         }
145 
146         // Integration With SCM Browser
147         // TODO need to make up some better rules to try and get the type if SCM Browser server application.
148         if ( project.getScm().getUrl() != null )
149         {
150         	if ( project.getScm().getUrl().toLowerCase( Locale.getDefault() ).indexOf( "viewvc" ) > -1 ) 
151         	{
152         		setViewVcURL( project.getScm().getUrl() );
153         	} 
154         	else if ( project.getScm().getUrl().toLowerCase( Locale.getDefault() ).indexOf( "viewcvs" ) > -1 ) 
155         	{
156         		setViewCvsURL("");
157         	} 
158         	else if ( project.getScm().getUrl().toLowerCase( Locale.getDefault() ).indexOf( "jcvs" ) > -1 ) 
159         	{
160         		setJCVSWebURL("");
161         	} 
162         	else if ( project.getScm().getUrl().toLowerCase( Locale.getDefault() ).indexOf( "chora" ) > -1 ) 
163         	{
164         		setChoraURL("");
165         	}
166         }
167 
168         // Configure Issue Management.
169         if ( project.getIssueManagement() != null 
170         		&& project.getIssueManagement().getSystem() != null )
171         {
172         	String bugSystem = project.getIssueManagement().getSystem().toLowerCase( Locale.getDefault() );
173         	if ( "bugzilla".equals( bugSystem ) )  
174         	{
175         		setBugzillaUrl( project.getIssueManagement().getUrl() );
176         	}
177         	else if ( "mantis".equals( bugSystem ) )  
178         	{
179         		setMantisUrl( project.getIssueManagement().getUrl() );
180         	}
181         	else if ( "trac".equals( bugSystem ))
182         	{
183         	    // Can only do this when we can add bug trackers and repos to stat-svn
184         	    // NOTE: Need to account for the IssueManagement url not being the exact
185         	    //  base path where the trac wiki starts. Maybe add a mojo parameter that
186         	    //  either gives the alternate path or an offset to the supplied path.
187         	    // Trac trac = new Trac( project.getIssueManagement().getUrl() );
188         	    // ConfigurationOptions.webBugTracker = trac; 
189         	    // ConfigurationOptions.webRepository = trac;
190         	}
191         }
192         
193         // General Info.
194         if ( project.getName() != null )
195         {
196             setProjectName( project.getName() );
197         }
198         
199         // set developer properties.
200         if (project.getDevelopers() != null) {
201 	        java.util.List developers = project.getDevelopers();
202 	        for ( java.util.Iterator i = developers.iterator(); i.hasNext(); )
203 	        {
204 	        	Developer developer = (Developer) i.next();
205 	        	if (developer.getName() != null) 
206 	        	{
207 	        		 getConfigProperties().setProperty("user." + developer.getId() + ".realName", developer.getName());
208 	        	}
209 	        	if (developer.getUrl() != null) 
210 	        	{
211 	        		getConfigProperties().setProperty("user." + developer.getId() + ".url", developer.getUrl() );
212 	        	}
213         		if (developer.getEmail() != null) 
214 	        	{
215         			getConfigProperties().setProperty("user." + developer.getId() + ".email", developer.getEmail());
216 	        	}
217 	        }
218         }
219 
220         //ConfigurationOptions.setSymbolicNamesPattern(".*");
221     }
222 
223     /**
224      * Returns the type of SCM system.
225      * 
226      * @return String represeting an ID for type of connection.
227      */
228     protected String getConnectionType()
229     {
230         return connectionType;
231     }
232 
233     /**
234      * Is the Maven Project using SVN as a SCM system?
235      * 
236      * @return true if SVN
237      */
238     public boolean isStatSVN()
239     {
240         return CONNECTION_TYPE_SVN.equals( getConnectionType() );
241     }
242 
243     /**
244      * Is the Maven Project using CVS as a SCM system?
245      * 
246      * @return true if CVS
247      */
248     public boolean isStatCVS()
249     {
250         return CONNECTION_TYPE_CVS.equals( getConnectionType() );
251     }
252 
253     /**
254      * Try and extract the connection type for the url from the the Maven SCM Connection URL.
255      * 
256      * @param connectionUrl
257      *            Maven SCM Connection URL.
258      * @return String represeting an ID for type of connection or null if can not be extracted.
259      */
260     protected String extractConnectionType( String connectionUrl )
261     {
262         if ( connectionUrl == null ) 
263         {
264             return null;
265         }
266         else
267         {
268             String con = connectionUrl.toLowerCase( Locale.getDefault() );
269             String[] scmTypes = { CONNECTION_TYPE_SVN, CONNECTION_TYPE_CVS };
270             for ( int i = 0; i < scmTypes.length; i++ )
271             {
272                 if ( con.indexOf( scmTypes[i] + ":" ) > -1 ) 
273                 {
274                     return scmTypes[i];
275                 }
276             }
277             return null;
278         }
279     }
280 
281     /**
282      * Method to access the Absolute location of the log file.
283      * 
284      * @return Absolute path of log file.
285      */
286     public String getSCMLogFileName()
287     {
288         return getOutputDir() + "scm.log";
289     }
290 
291     /**
292      * Getter for the Base Directory.
293      * 
294      * @return root folder for project.
295      */
296     public File getBaseDirectory()
297     {
298         return baseDirectory;
299     }
300 
301     public Locale getLocale()
302     {
303         return locale;
304     }
305 
306     public void setLocale( Locale locale )
307     {
308         this.locale = locale;
309     }
310 
311 }