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.commons.lang3.Validate;
30 import org.apache.maven.settings.Settings;
31 import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
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 = SettingsWriter.class )
42 public class DefaultSettingsWriter
43 implements SettingsWriter
44 {
45
46 @Override
47 public void write( File output, Map<String, Object> options, Settings settings )
48 throws IOException
49 {
50 Validate.notNull( output, "output cannot be null" );
51 Validate.notNull( settings, "settings cannot be null" );
52
53 output.getParentFile().mkdirs();
54
55 write( WriterFactory.newXmlWriter( output ), options, settings );
56 }
57
58 @Override
59 public void write( Writer output, Map<String, Object> options, Settings settings )
60 throws IOException
61 {
62 Validate.notNull( output, "output cannot be null" );
63 Validate.notNull( settings, "settings cannot be null" );
64
65 try
66 {
67 SettingsXpp3Writer w = new SettingsXpp3Writer();
68 w.write( output, settings );
69 }
70 finally
71 {
72 IOUtil.close( output );
73 }
74 }
75
76 @Override
77 public void write( OutputStream output, Map<String, Object> options, Settings settings )
78 throws IOException
79 {
80 Validate.notNull( output, "output cannot be null" );
81 Validate.notNull( settings, "settings cannot be null" );
82
83 try
84 {
85 String encoding = settings.getModelEncoding();
86
87 if ( encoding == null || encoding.length() <= 0 )
88 {
89 encoding = "UTF-8";
90 }
91 write( new OutputStreamWriter( output, encoding ), options, settings );
92 }
93 finally
94 {
95 IOUtil.close( output );
96 }
97 }
98
99 }