KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > profiles > DefaultMavenProfilesBuilder


1 package org.apache.maven.profiles;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
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 org.apache.maven.profiles.io.xpp3.ProfilesXpp3Reader;
20 import org.codehaus.plexus.logging.AbstractLogEnabled;
21 import org.codehaus.plexus.util.IOUtil;
22 import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource;
23 import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
24 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
25
26 import java.io.File JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.StringReader JavaDoc;
30 import java.io.StringWriter JavaDoc;
31
32 public class DefaultMavenProfilesBuilder
33     extends AbstractLogEnabled
34     implements MavenProfilesBuilder
35 {
36     private static final String JavaDoc PROFILES_XML_FILE = "profiles.xml";
37
38     public ProfilesRoot buildProfiles( File JavaDoc basedir )
39         throws IOException JavaDoc, XmlPullParserException
40     {
41         File JavaDoc profilesXml = new File JavaDoc( basedir, PROFILES_XML_FILE );
42
43         ProfilesRoot profilesRoot = null;
44
45         if ( profilesXml.exists() )
46         {
47             ProfilesXpp3Reader reader = new ProfilesXpp3Reader();
48             FileReader JavaDoc fileReader = null;
49             try
50             {
51                 fileReader = new FileReader JavaDoc( profilesXml );
52                 
53                 StringWriter JavaDoc sWriter = new StringWriter JavaDoc();
54                 
55                 IOUtil.copy( fileReader, sWriter );
56                 
57                 String JavaDoc rawInput = sWriter.toString();
58                 
59                 try
60                 {
61                     RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
62                     interpolator.addValueSource( new EnvarBasedValueSource() );
63                     
64                     rawInput = interpolator.interpolate( rawInput, "settings" );
65                 }
66                 catch ( Exception JavaDoc e )
67                 {
68                     getLogger().warn( "Failed to initialize environment variable resolver. Skipping environment substitution in " + PROFILES_XML_FILE + "." );
69                     getLogger().debug( "Failed to initialize envar resolver. Skipping resolution.", e );
70                 }
71
72                 StringReader JavaDoc sReader = new StringReader JavaDoc( rawInput );
73
74                 profilesRoot = reader.read( sReader );
75             }
76             finally
77             {
78                 IOUtil.close( fileReader );
79             }
80         }
81
82         return profilesRoot;
83     }
84
85 }
86
Popular Tags