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
43 @Named
44 @Singleton
45 @Deprecated(since = "4.0.0")
46 public class DefaultModelWriter implements ModelWriter {
47
48 @Override
49 public void write(File output, Map<String, Object> options, Model model) throws IOException {
50 Objects.requireNonNull(output, "output cannot be null");
51 Objects.requireNonNull(model, "model cannot be null");
52
53 output.getParentFile().mkdirs();
54
55 write(new XmlStreamWriter(Files.newOutputStream(output.toPath())), options, model);
56 }
57
58 @Override
59 public void write(Writer output, Map<String, Object> options, Model model) throws IOException {
60 Objects.requireNonNull(output, "output cannot be null");
61 Objects.requireNonNull(model, "model cannot be null");
62
63 try (Writer out = output) {
64 new MavenStaxWriter().write(out, model);
65 } catch (XMLStreamException e) {
66 throw new IOException(e);
67 }
68 }
69
70 @Override
71 public void write(OutputStream output, Map<String, Object> options, Model model) throws IOException {
72 Objects.requireNonNull(output, "output cannot be null");
73 Objects.requireNonNull(model, "model cannot be null");
74
75 String encoding = model.getModelEncoding();
76 if (encoding == null || encoding.isEmpty()) {
77 encoding = "UTF-8";
78 }
79
80 try (Writer out = new OutputStreamWriter(output, encoding)) {
81 write(out, options, model);
82 }
83 }
84
85 @Override
86 public void write(File output, Map<String, Object> options, org.apache.maven.model.Model model) throws IOException {
87 write(output, options, model.getDelegate());
88 }
89
90 @Override
91 public void write(Writer output, Map<String, Object> options, org.apache.maven.model.Model model)
92 throws IOException {
93 write(output, options, model.getDelegate());
94 }
95
96 @Override
97 public void write(OutputStream output, Map<String, Object> options, org.apache.maven.model.Model model)
98 throws IOException {
99 write(output, options, model.getDelegate());
100 }
101 }