1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.settings.io;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26 import java.util.Map;
27 import java.util.Objects;
28 import javax.inject.Named;
29 import javax.inject.Singleton;
30 import org.apache.maven.api.settings.Settings;
31 import org.apache.maven.settings.v4.SettingsXpp3Writer;
32 import org.codehaus.plexus.util.WriterFactory;
33
34
35
36
37
38
39 @Named
40 @Singleton
41 public class DefaultSettingsWriter implements SettingsWriter {
42
43 @Override
44 public void write(File output, Map<String, Object> options, Settings settings) throws IOException {
45 Objects.requireNonNull(output, "output cannot be null");
46 Objects.requireNonNull(settings, "settings cannot be null");
47
48 output.getParentFile().mkdirs();
49
50 write(WriterFactory.newXmlWriter(output), options, settings);
51 }
52
53 @Override
54 public void write(Writer output, Map<String, Object> options, Settings settings) throws IOException {
55 Objects.requireNonNull(output, "output cannot be null");
56 Objects.requireNonNull(settings, "settings cannot be null");
57
58 try (Writer out = output) {
59 new SettingsXpp3Writer().write(out, settings);
60 }
61 }
62
63 @Override
64 public void write(OutputStream output, Map<String, Object> options, Settings settings) throws IOException {
65 Objects.requireNonNull(output, "output cannot be null");
66 Objects.requireNonNull(settings, "settings cannot be null");
67
68 String encoding = settings.getModelEncoding();
69
70 if (encoding == null || encoding.length() <= 0) {
71 encoding = "UTF-8";
72 }
73
74 try (Writer out = new OutputStreamWriter(output, encoding)) {
75 write(out, options, settings);
76 }
77 }
78 }