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