1   package org.apache.maven.settings.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.maven.settings.Settings;
30  import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.WriterFactory;
34  
35  
36  
37  
38  
39  
40  @Component( role = SettingsWriter.class )
41  public class DefaultSettingsWriter
42      implements SettingsWriter
43  {
44  
45      public void write( File output, Map<String, Object> options, Settings settings )
46          throws IOException
47      {
48          if ( output == null )
49          {
50              throw new IllegalArgumentException( "output file missing" );
51          }
52  
53          if ( settings == null )
54          {
55              throw new IllegalArgumentException( "settings missing" );
56          }
57  
58          output.getParentFile().mkdirs();
59  
60          write( WriterFactory.newXmlWriter( output ), options, settings );
61      }
62  
63      public void write( Writer output, Map<String, Object> options, Settings settings )
64          throws IOException
65      {
66          if ( output == null )
67          {
68              throw new IllegalArgumentException( "output writer missing" );
69          }
70  
71          if ( settings == null )
72          {
73              throw new IllegalArgumentException( "settings missing" );
74          }
75  
76          try
77          {
78              SettingsXpp3Writer w = new SettingsXpp3Writer();
79              w.write( output, settings );
80          }
81          finally
82          {
83              IOUtil.close( output );
84          }
85      }
86  
87      public void write( OutputStream output, Map<String, Object> options, Settings settings )
88          throws IOException
89      {
90          if ( output == null )
91          {
92              throw new IllegalArgumentException( "output stream missing" );
93          }
94  
95          if ( settings == null )
96          {
97              throw new IllegalArgumentException( "settings missing" );
98          }
99  
100         try
101         {
102             String encoding = settings.getModelEncoding();
103             if ( encoding == null || encoding.length() <= 0 )
104             {
105                 encoding = "UTF-8";
106             }
107             write( new OutputStreamWriter( output, encoding ), options, settings );
108         }
109         finally
110         {
111             IOUtil.close( output );
112         }
113     }
114 
115 }