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  import java.util.Objects;
29  
30  import javax.inject.Named;
31  import javax.inject.Singleton;
32  
33  import org.apache.maven.settings.Settings;
34  import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer;
35  import org.codehaus.plexus.util.WriterFactory;
36  
37  /**
38   * Handles serialization of settings into the default textual format.
39   *
40   * @author Benjamin Bentmann
41   */
42  @Named
43  @Singleton
44  public class DefaultSettingsWriter
45      implements SettingsWriter
46  {
47  
48      @Override
49      public void write( File output, Map<String, Object> options, Settings settings )
50          throws IOException
51      {
52          Objects.requireNonNull( output, "output cannot be null" );
53          Objects.requireNonNull( settings, "settings cannot be null" );
54  
55          output.getParentFile().mkdirs();
56  
57          write( WriterFactory.newXmlWriter( output ), options, settings );
58      }
59  
60      @Override
61      public void write( Writer output, Map<String, Object> options, Settings settings )
62          throws IOException
63      {
64          Objects.requireNonNull( output, "output cannot be null" );
65          Objects.requireNonNull( settings, "settings cannot be null" );
66  
67          try ( final Writer out = output )
68          {
69              new SettingsXpp3Writer().write( out, settings );
70          }
71      }
72  
73      @Override
74      public void write( OutputStream output, Map<String, Object> options, Settings settings )
75          throws IOException
76      {
77          Objects.requireNonNull( output, "output cannot be null" );
78          Objects.requireNonNull( settings, "settings cannot be null" );
79  
80          String encoding = settings.getModelEncoding();
81          // TODO Use StringUtils here
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, settings );
90          }
91      }
92  
93  }