001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.svn.util;
020
021import java.io.File;
022import java.io.IOException;
023
024import org.apache.maven.scm.providers.svn.settings.Settings;
025import org.apache.maven.scm.providers.svn.settings.io.xpp3.SvnXpp3Reader;
026import org.codehaus.plexus.util.ReaderFactory;
027import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
028
029/**
030 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
031 */
032public class SvnUtil {
033    protected static final String SVN_SETTINGS_FILENAME = "svn-settings.xml";
034
035    public static final File DEFAULT_SETTINGS_DIRECTORY = new File(System.getProperty("user.home"), ".scm");
036
037    private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY;
038
039    private static Settings settings;
040
041    private SvnUtil() {}
042
043    public static Settings getSettings() {
044        if (settings == null) {
045            settings = readSettings();
046        }
047        return settings;
048    }
049
050    public static Settings readSettings() {
051        File settingsFile = getSettingsFile();
052
053        if (settingsFile.exists()) {
054            SvnXpp3Reader reader = new SvnXpp3Reader();
055            try {
056                return reader.read(ReaderFactory.newXmlReader(settingsFile));
057            } catch (IOException e) {
058                // Nothing to do
059            } catch (XmlPullParserException e) {
060                String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
061
062                System.err.println(message);
063            }
064        }
065
066        return new Settings();
067    }
068
069    public static void setSettingsDirectory(File directory) {
070        settingsDirectory = directory;
071        settings = readSettings();
072    }
073
074    public static File getSettingsFile() {
075        return new File(settingsDirectory, SVN_SETTINGS_FILENAME);
076    }
077}