001package org.apache.maven.scm.provider.svn.util;
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.scm.providers.svn.settings.Settings;
023import org.apache.maven.scm.providers.svn.settings.io.xpp3.SvnXpp3Reader;
024import org.codehaus.plexus.util.ReaderFactory;
025import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
026
027import java.io.File;
028import java.io.IOException;
029
030/**
031 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
032 *
033 */
034public class SvnUtil
035{
036    protected static final String SVN_SETTINGS_FILENAME = "svn-settings.xml";
037
038    public static final File DEFAULT_SETTINGS_DIRECTORY = new File( System.getProperty( "user.home" ), ".scm" );
039
040    private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY;
041
042    private static Settings settings;
043
044    private SvnUtil()
045    {
046    }
047
048    public static Settings getSettings()
049    {
050        if ( settings == null )
051        {
052            settings = readSettings();
053        }
054        return settings;
055    }
056
057    public static Settings readSettings()
058    {
059        File settingsFile = getSettingsFile();
060
061        if ( settingsFile.exists() )
062        {
063            SvnXpp3Reader reader = new SvnXpp3Reader();
064            try
065            {
066                return reader.read( ReaderFactory.newXmlReader( settingsFile ) );
067            }
068            catch ( IOException e )
069            {
070                //Nothing to do
071            }
072            catch ( XmlPullParserException e )
073            {
074                String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
075
076                System.err.println( message );
077            }
078        }
079
080        return new Settings();
081    }
082
083    public static void setSettingsDirectory( File directory )
084    {
085        settingsDirectory = directory;
086        settings = readSettings();
087    }
088
089    public static File getSettingsFile()
090    {
091        return new File( settingsDirectory, SVN_SETTINGS_FILENAME );
092    }
093}