1   package org.apache.maven.model.io;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  
38  
39  
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              
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  }