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 import javax.xml.stream.XMLStreamException;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.io.OutputStreamWriter;
29 import java.io.Writer;
30 import java.nio.file.Files;
31 import java.util.Map;
32 import java.util.Objects;
33
34 import org.apache.maven.api.model.Model;
35 import org.apache.maven.model.v4.MavenStaxWriter;
36 import org.codehaus.plexus.util.xml.XmlStreamWriter;
37
38
39
40
41
42 @Named
43 @Singleton
44 public class DefaultModelWriter implements ModelWriter {
45
46 @Override
47 public void write(File output, Map<String, Object> options, Model model) throws IOException {
48 Objects.requireNonNull(output, "output cannot be null");
49 Objects.requireNonNull(model, "model cannot be null");
50
51 output.getParentFile().mkdirs();
52
53 write(new XmlStreamWriter(Files.newOutputStream(output.toPath())), options, model);
54 }
55
56 @Override
57 public void write(Writer output, Map<String, Object> options, Model model) throws IOException {
58 Objects.requireNonNull(output, "output cannot be null");
59 Objects.requireNonNull(model, "model cannot be null");
60
61 try (Writer out = output) {
62 new MavenStaxWriter().write(out, model);
63 } catch (XMLStreamException e) {
64 throw new IOException(e);
65 }
66 }
67
68 @Override
69 public void write(OutputStream output, Map<String, Object> options, Model model) throws IOException {
70 Objects.requireNonNull(output, "output cannot be null");
71 Objects.requireNonNull(model, "model cannot be null");
72
73 String encoding = model.getModelEncoding();
74 if (encoding == null || encoding.isEmpty()) {
75 encoding = "UTF-8";
76 }
77
78 try (Writer out = new OutputStreamWriter(output, encoding)) {
79 write(out, options, model);
80 }
81 }
82
83 @Override
84 public void write(File output, Map<String, Object> options, org.apache.maven.model.Model model) throws IOException {
85 write(output, options, model.getDelegate());
86 }
87
88 @Override
89 public void write(Writer output, Map<String, Object> options, org.apache.maven.model.Model model)
90 throws IOException {
91 write(output, options, model.getDelegate());
92 }
93
94 @Override
95 public void write(OutputStream output, Map<String, Object> options, org.apache.maven.model.Model model)
96 throws IOException {
97 write(output, options, model.getDelegate());
98 }
99 }