View Javadoc
1   package org.apache.maven.settings.io;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.settings.Settings;
30  import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
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   * Handles serialization of settings into the default textual format.
37   *
38   * @author Benjamin Bentmann
39   */
40  @Component( role = SettingsWriter.class )
41  public class DefaultSettingsWriter
42      implements SettingsWriter
43  {
44  
45      @Override
46      public void write( File output, Map<String, Object> options, Settings settings )
47          throws IOException
48      {
49          if ( output == null )
50          {
51              throw new IllegalArgumentException( "output file missing" );
52          }
53  
54          if ( settings == null )
55          {
56              throw new IllegalArgumentException( "settings missing" );
57          }
58  
59          output.getParentFile().mkdirs();
60  
61          write( WriterFactory.newXmlWriter( output ), options, settings );
62      }
63  
64      @Override
65      public void write( Writer output, Map<String, Object> options, Settings settings )
66          throws IOException
67      {
68          if ( output == null )
69          {
70              throw new IllegalArgumentException( "output writer missing" );
71          }
72  
73          if ( settings == null )
74          {
75              throw new IllegalArgumentException( "settings missing" );
76          }
77  
78          try
79          {
80              SettingsXpp3Writer w = new SettingsXpp3Writer();
81              w.write( output, settings );
82          }
83          finally
84          {
85              IOUtil.close( output );
86          }
87      }
88  
89      @Override
90      public void write( OutputStream output, Map<String, Object> options, Settings settings )
91          throws IOException
92      {
93          if ( output == null )
94          {
95              throw new IllegalArgumentException( "output stream missing" );
96          }
97  
98          if ( settings == null )
99          {
100             throw new IllegalArgumentException( "settings missing" );
101         }
102 
103         try
104         {
105             String encoding = settings.getModelEncoding();
106             if ( encoding == null || encoding.length() <= 0 )
107             {
108                 encoding = "UTF-8";
109             }
110             write( new OutputStreamWriter( output, encoding ), options, settings );
111         }
112         finally
113         {
114             IOUtil.close( output );
115         }
116     }
117 
118 }