001package 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
022import org.apache.maven.profiles.io.xpp3.ProfilesXpp3Reader;
023import org.codehaus.plexus.component.annotations.Component;
024import org.codehaus.plexus.interpolation.EnvarBasedValueSource;
025import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
026import org.codehaus.plexus.logging.AbstractLogEnabled;
027import org.codehaus.plexus.util.IOUtil;
028import org.codehaus.plexus.util.ReaderFactory;
029import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
030
031import java.io.File;
032import java.io.IOException;
033import java.io.Reader;
034import java.io.StringReader;
035import java.io.StringWriter;
036
037@Deprecated
038@Component( role = MavenProfilesBuilder.class )
039public 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            try ( Reader profileReader = ReaderFactory.newXmlReader( profilesXml );
056                  StringWriter sWriter = new StringWriter() )
057            {
058                IOUtil.copy( profileReader, sWriter );
059
060                String rawInput = sWriter.toString();
061
062                try
063                {
064                    RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
065                    interpolator.addValueSource( new EnvarBasedValueSource() );
066
067                    rawInput = interpolator.interpolate( rawInput, "settings" );
068                }
069                catch ( Exception e )
070                {
071                    getLogger().warn(
072                        "Failed to initialize environment variable resolver. Skipping environment " + "substitution in "
073                            + PROFILES_XML_FILE + "." );
074                    getLogger().debug( "Failed to initialize envar resolver. Skipping resolution.", e );
075                }
076
077                StringReader sReader = new StringReader( rawInput );
078
079                profilesRoot = reader.read( sReader );
080            }
081
082        }
083
084        return profilesRoot;
085    }
086
087}