001    package org.apache.maven.profiles;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.profiles.io.xpp3.ProfilesXpp3Reader;
023    import org.codehaus.plexus.component.annotations.Component;
024    import org.codehaus.plexus.interpolation.EnvarBasedValueSource;
025    import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
026    import org.codehaus.plexus.logging.AbstractLogEnabled;
027    import org.codehaus.plexus.util.IOUtil;
028    import org.codehaus.plexus.util.ReaderFactory;
029    import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
030    
031    import java.io.File;
032    import java.io.IOException;
033    import java.io.Reader;
034    import java.io.StringReader;
035    import java.io.StringWriter;
036    
037    @Deprecated
038    @Component( role = MavenProfilesBuilder.class )
039    public class DefaultMavenProfilesBuilder
040        extends AbstractLogEnabled
041        implements MavenProfilesBuilder
042    {
043        private static final String PROFILES_XML_FILE = "profiles.xml";
044    
045        public ProfilesRoot buildProfiles( File basedir )
046            throws IOException, XmlPullParserException
047        {
048            File profilesXml = new File( basedir, PROFILES_XML_FILE );
049    
050            ProfilesRoot profilesRoot = null;
051    
052            if ( profilesXml.exists() )
053            {
054                ProfilesXpp3Reader reader = new ProfilesXpp3Reader();
055                Reader profileReader = null;
056                try
057                {
058                    profileReader = ReaderFactory.newXmlReader( profilesXml );
059    
060                    StringWriter sWriter = new StringWriter();
061    
062                    IOUtil.copy( profileReader, sWriter );
063    
064                    String rawInput = sWriter.toString();
065    
066                    try
067                    {
068                        RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
069                        interpolator.addValueSource( new EnvarBasedValueSource() );
070    
071                        rawInput = interpolator.interpolate( rawInput, "settings" );
072                    }
073                    catch ( Exception e )
074                    {
075                        getLogger().warn( "Failed to initialize environment variable resolver. Skipping environment "
076                                              + "substitution in " + PROFILES_XML_FILE + "." );
077                        getLogger().debug( "Failed to initialize envar resolver. Skipping resolution.", e );
078                    }
079    
080                    StringReader sReader = new StringReader( rawInput );
081    
082                    profilesRoot = reader.read( sReader );
083                }
084                finally
085                {
086                    IOUtil.close( profileReader );
087                }
088            }
089    
090            return profilesRoot;
091        }
092    
093    }