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.commons.lang3.Validate;
030import org.apache.maven.model.Model;
031import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
032import org.codehaus.plexus.component.annotations.Component;
033import org.codehaus.plexus.util.IOUtil;
034import org.codehaus.plexus.util.WriterFactory;
035
036/**
037 * Handles serialization of a model into some kind of textual format like XML.
038 *
039 * @author Benjamin Bentmann
040 */
041@Component( role = ModelWriter.class )
042public class DefaultModelWriter
043    implements ModelWriter
044{
045
046    @Override
047    public void write( File output, Map<String, Object> options, Model model )
048        throws IOException
049    {
050        Validate.notNull( output, "output cannot be null" );
051        Validate.notNull( model, "model cannot be null" );
052
053        output.getParentFile().mkdirs();
054
055        write( WriterFactory.newXmlWriter( output ), options, model );
056    }
057
058    @Override
059    public void write( Writer output, Map<String, Object> options, Model model )
060        throws IOException
061    {
062        Validate.notNull( output, "output cannot be null" );
063        Validate.notNull( model, "model cannot be null" );
064
065        try
066        {
067            MavenXpp3Writer w = new MavenXpp3Writer();
068            w.write( output, model );
069        }
070        finally
071        {
072            IOUtil.close( output );
073        }
074    }
075
076    @Override
077    public void write( OutputStream output, Map<String, Object> options, Model model )
078        throws IOException
079    {
080        Validate.notNull( output, "output cannot be null" );
081        Validate.notNull( model, "model cannot be null" );
082
083        try
084        {
085            String encoding = model.getModelEncoding();
086            // TODO Use StringUtils here
087            if ( encoding == null || encoding.length() <= 0 )
088            {
089                encoding = "UTF-8";
090            }
091            write( new OutputStreamWriter( output, encoding ), options, model );
092        }
093        finally
094        {
095            IOUtil.close( output );
096        }
097    }
098
099}