View Javadoc
1   package org.apache.maven.model.io;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.io.OutputStreamWriter;
26  import java.io.Writer;
27  import java.util.Map;
28  
29  import org.apache.commons.lang3.Validate;
30  import org.apache.maven.model.Model;
31  import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.WriterFactory;
35  
36  /**
37   * Handles serialization of a model into some kind of textual format like XML.
38   *
39   * @author Benjamin Bentmann
40   */
41  @Component( role = ModelWriter.class )
42  public class DefaultModelWriter
43      implements ModelWriter
44  {
45  
46      @Override
47      public void write( File output, Map<String, Object> options, Model model )
48          throws IOException
49      {
50          Validate.notNull( output, "output cannot be null" );
51          Validate.notNull( model, "model cannot be null" );
52  
53          output.getParentFile().mkdirs();
54  
55          write( WriterFactory.newXmlWriter( output ), options, model );
56      }
57  
58      @Override
59      public void write( Writer output, Map<String, Object> options, Model model )
60          throws IOException
61      {
62          Validate.notNull( output, "output cannot be null" );
63          Validate.notNull( model, "model cannot be null" );
64  
65          try
66          {
67              MavenXpp3Writer w = new MavenXpp3Writer();
68              w.write( output, model );
69          }
70          finally
71          {
72              IOUtil.close( output );
73          }
74      }
75  
76      @Override
77      public void write( OutputStream output, Map<String, Object> options, Model model )
78          throws IOException
79      {
80          Validate.notNull( output, "output cannot be null" );
81          Validate.notNull( model, "model cannot be null" );
82  
83          try
84          {
85              String encoding = model.getModelEncoding();
86              // TODO Use StringUtils here
87              if ( encoding == null || encoding.length() <= 0 )
88              {
89                  encoding = "UTF-8";
90              }
91              write( new OutputStreamWriter( output, encoding ), options, model );
92          }
93          finally
94          {
95              IOUtil.close( output );
96          }
97      }
98  
99  }