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    @Override
046    public void write( File output, Map<String, Object> options, Settings settings )
047        throws IOException
048    {
049        if ( output == null )
050        {
051            throw new IllegalArgumentException( "output file missing" );
052        }
053
054        if ( settings == null )
055        {
056            throw new IllegalArgumentException( "settings missing" );
057        }
058
059        output.getParentFile().mkdirs();
060
061        write( WriterFactory.newXmlWriter( output ), options, settings );
062    }
063
064    @Override
065    public void write( Writer output, Map<String, Object> options, Settings settings )
066        throws IOException
067    {
068        if ( output == null )
069        {
070            throw new IllegalArgumentException( "output writer missing" );
071        }
072
073        if ( settings == null )
074        {
075            throw new IllegalArgumentException( "settings missing" );
076        }
077
078        try
079        {
080            SettingsXpp3Writer w = new SettingsXpp3Writer();
081            w.write( output, settings );
082        }
083        finally
084        {
085            IOUtil.close( output );
086        }
087    }
088
089    @Override
090    public void write( OutputStream output, Map<String, Object> options, Settings settings )
091        throws IOException
092    {
093        if ( output == null )
094        {
095            throw new IllegalArgumentException( "output stream missing" );
096        }
097
098        if ( settings == null )
099        {
100            throw new IllegalArgumentException( "settings missing" );
101        }
102
103        try
104        {
105            String encoding = settings.getModelEncoding();
106            if ( encoding == null || encoding.length() <= 0 )
107            {
108                encoding = "UTF-8";
109            }
110            write( new OutputStreamWriter( output, encoding ), options, settings );
111        }
112        finally
113        {
114            IOUtil.close( output );
115        }
116    }
117
118}