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 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.model.Model;
31 import org.apache.maven.model.v4.MavenXpp3Writer;
32 import org.codehaus.plexus.util.WriterFactory;
33
34
35
36
37
38
39 @Named
40 @Singleton
41 public class DefaultModelWriter implements ModelWriter {
42
43 @Override
44 public void write(File output, Map<String, Object> options, Model model) throws IOException {
45 Objects.requireNonNull(output, "output cannot be null");
46 Objects.requireNonNull(model, "model cannot be null");
47
48 output.getParentFile().mkdirs();
49
50 write(WriterFactory.newXmlWriter(output), options, model);
51 }
52
53 @Override
54 public void write(Writer output, Map<String, Object> options, Model model) throws IOException {
55 Objects.requireNonNull(output, "output cannot be null");
56 Objects.requireNonNull(model, "model cannot be null");
57
58 try (Writer out = output) {
59 new MavenXpp3Writer().write(out, model);
60 }
61 }
62
63 @Override
64 public void write(OutputStream output, Map<String, Object> options, Model model) throws IOException {
65 Objects.requireNonNull(output, "output cannot be null");
66 Objects.requireNonNull(model, "model cannot be null");
67
68 String encoding = model.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, model);
76 }
77 }
78
79 @Override
80 public void write(File output, Map<String, Object> options, org.apache.maven.model.Model model) throws IOException {
81 write(output, options, model.getDelegate());
82 }
83
84 @Override
85 public void write(Writer output, Map<String, Object> options, org.apache.maven.model.Model model)
86 throws IOException {
87 write(output, options, model.getDelegate());
88 }
89
90 @Override
91 public void write(OutputStream output, Map<String, Object> options, org.apache.maven.model.Model model)
92 throws IOException {
93 write(output, options, model.getDelegate());
94 }
95 }