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