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