001package org.apache.maven.model.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.model.Model;
030import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
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 a model into some kind of textual format like XML.
037 *
038 * @author Benjamin Bentmann
039 */
040@Component( role = ModelWriter.class )
041public class DefaultModelWriter
042    implements ModelWriter
043{
044
045    @Override
046    public void write( File output, Map<String, Object> options, Model model )
047        throws IOException
048    {
049        if ( output == null )
050        {
051            throw new IllegalArgumentException( "output file missing" );
052        }
053
054        if ( model == null )
055        {
056            throw new IllegalArgumentException( "model missing" );
057        }
058
059        output.getParentFile().mkdirs();
060
061        write( WriterFactory.newXmlWriter( output ), options, model );
062    }
063
064    @Override
065    public void write( Writer output, Map<String, Object> options, Model model )
066        throws IOException
067    {
068        if ( output == null )
069        {
070            throw new IllegalArgumentException( "output writer missing" );
071        }
072
073        if ( model == null )
074        {
075            throw new IllegalArgumentException( "model missing" );
076        }
077
078        try
079        {
080            MavenXpp3Writer w = new MavenXpp3Writer();
081            w.write( output, model );
082        }
083        finally
084        {
085            IOUtil.close( output );
086        }
087    }
088
089    @Override
090    public void write( OutputStream output, Map<String, Object> options, Model model )
091        throws IOException
092    {
093        if ( output == null )
094        {
095            throw new IllegalArgumentException( "output stream missing" );
096        }
097
098        if ( model == null )
099        {
100            throw new IllegalArgumentException( "model missing" );
101        }
102
103        try
104        {
105            String encoding = model.getModelEncoding();
106            if ( encoding == null || encoding.length() <= 0 )
107            {
108                encoding = "UTF-8";
109            }
110            write( new OutputStreamWriter( output, encoding ), options, model );
111        }
112        finally
113        {
114            IOUtil.close( output );
115        }
116    }
117
118}