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.WriterFactory;
34
35
36
37
38
39
40 @Component( role = SettingsWriter.class )
41 public class DefaultSettingsWriter
42 implements SettingsWriter
43 {
44
45 @Override
46 public void write( File output, Map<String, Object> options, Settings settings )
47 throws IOException
48 {
49 Validate.notNull( output, "output cannot be null" );
50 Validate.notNull( settings, "settings cannot be null" );
51
52 output.getParentFile().mkdirs();
53
54 write( WriterFactory.newXmlWriter( output ), options, settings );
55 }
56
57 @Override
58 public void write( Writer output, Map<String, Object> options, Settings settings )
59 throws IOException
60 {
61 Validate.notNull( output, "output cannot be null" );
62 Validate.notNull( settings, "settings cannot be null" );
63
64 try ( final Writer out = output )
65 {
66 new SettingsXpp3Writer().write( out, settings );
67 }
68 }
69
70 @Override
71 public void write( OutputStream output, Map<String, Object> options, Settings settings )
72 throws IOException
73 {
74 Validate.notNull( output, "output cannot be null" );
75 Validate.notNull( settings, "settings cannot be null" );
76
77 String encoding = settings.getModelEncoding();
78
79 if ( encoding == null || encoding.length() <= 0 )
80 {
81 encoding = "UTF-8";
82 }
83
84 try ( final Writer out = new OutputStreamWriter( output, encoding ) )
85 {
86 write( out, options, settings );
87 }
88 }
89
90 }