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
29 import org.apache.maven.model.Model;
30 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
31 import org.codehaus.plexus.component.annotations.Component;
32 import org.codehaus.plexus.util.IOUtil;
33 import org.codehaus.plexus.util.WriterFactory;
34
35
36
37
38
39
40 @Component( role = ModelWriter.class )
41 public class DefaultModelWriter
42 implements ModelWriter
43 {
44
45 @Override
46 public void write( File output, Map<String, Object> options, Model model )
47 throws IOException
48 {
49 if ( output == null )
50 {
51 throw new IllegalArgumentException( "output file missing" );
52 }
53
54 if ( model == null )
55 {
56 throw new IllegalArgumentException( "model missing" );
57 }
58
59 output.getParentFile().mkdirs();
60
61 write( WriterFactory.newXmlWriter( output ), options, model );
62 }
63
64 @Override
65 public void write( Writer output, Map<String, Object> options, Model model )
66 throws IOException
67 {
68 if ( output == null )
69 {
70 throw new IllegalArgumentException( "output writer missing" );
71 }
72
73 if ( model == null )
74 {
75 throw new IllegalArgumentException( "model missing" );
76 }
77
78 try
79 {
80 MavenXpp3Writer w = new MavenXpp3Writer();
81 w.write( output, model );
82 }
83 finally
84 {
85 IOUtil.close( output );
86 }
87 }
88
89 @Override
90 public void write( OutputStream output, Map<String, Object> options, Model model )
91 throws IOException
92 {
93 if ( output == null )
94 {
95 throw new IllegalArgumentException( "output stream missing" );
96 }
97
98 if ( model == null )
99 {
100 throw new IllegalArgumentException( "model missing" );
101 }
102
103 try
104 {
105 String encoding = model.getModelEncoding();
106 if ( encoding == null || encoding.length() <= 0 )
107 {
108 encoding = "UTF-8";
109 }
110 write( new OutputStreamWriter( output, encoding ), options, model );
111 }
112 finally
113 {
114 IOUtil.close( output );
115 }
116 }
117
118 }