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