001package org.apache.maven.settings.io;
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 java.io.File;
023import java.io.IOException;
024import java.io.OutputStream;
025import java.io.OutputStreamWriter;
026import java.io.Writer;
027import java.util.Map;
028
029import org.apache.maven.settings.Settings;
030import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
031import org.codehaus.plexus.component.annotations.Component;
032import org.codehaus.plexus.util.IOUtil;
033import org.codehaus.plexus.util.WriterFactory;
034
035/**
036 * Handles serialization of settings into the default textual format.
037 * 
038 * @author Benjamin Bentmann
039 */
040@Component( role = SettingsWriter.class )
041public class DefaultSettingsWriter
042    implements SettingsWriter
043{
044
045    public void write( File output, Map<String, Object> options, Settings settings )
046        throws IOException
047    {
048        if ( output == null )
049        {
050            throw new IllegalArgumentException( "output file missing" );
051        }
052
053        if ( settings == null )
054        {
055            throw new IllegalArgumentException( "settings missing" );
056        }
057
058        output.getParentFile().mkdirs();
059
060        write( WriterFactory.newXmlWriter( output ), options, settings );
061    }
062
063    public void write( Writer output, Map<String, Object> options, Settings settings )
064        throws IOException
065    {
066        if ( output == null )
067        {
068            throw new IllegalArgumentException( "output writer missing" );
069        }
070
071        if ( settings == null )
072        {
073            throw new IllegalArgumentException( "settings missing" );
074        }
075
076        try
077        {
078            SettingsXpp3Writer w = new SettingsXpp3Writer();
079            w.write( output, settings );
080        }
081        finally
082        {
083            IOUtil.close( output );
084        }
085    }
086
087    public void write( OutputStream output, Map<String, Object> options, Settings settings )
088        throws IOException
089    {
090        if ( output == null )
091        {
092            throw new IllegalArgumentException( "output stream missing" );
093        }
094
095        if ( settings == null )
096        {
097            throw new IllegalArgumentException( "settings missing" );
098        }
099
100        try
101        {
102            String encoding = settings.getModelEncoding();
103            if ( encoding == null || encoding.length() <= 0 )
104            {
105                encoding = "UTF-8";
106            }
107            write( new OutputStreamWriter( output, encoding ), options, settings );
108        }
109        finally
110        {
111            IOUtil.close( output );
112        }
113    }
114
115}