View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.settings.io;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  import java.util.Map;
27  import java.util.Objects;
28  import javax.inject.Named;
29  import javax.inject.Singleton;
30  import org.apache.maven.api.settings.Settings;
31  import org.apache.maven.settings.v4.SettingsXpp3Writer;
32  import org.codehaus.plexus.util.WriterFactory;
33  
34  /**
35   * Handles serialization of settings into the default textual format.
36   *
37   * @author Benjamin Bentmann
38   */
39  @Named
40  @Singleton
41  public class DefaultSettingsWriter implements SettingsWriter {
42  
43      @Override
44      public void write(File output, Map<String, Object> options, Settings settings) throws IOException {
45          Objects.requireNonNull(output, "output cannot be null");
46          Objects.requireNonNull(settings, "settings cannot be null");
47  
48          output.getParentFile().mkdirs();
49  
50          write(WriterFactory.newXmlWriter(output), options, settings);
51      }
52  
53      @Override
54      public void write(Writer output, Map<String, Object> options, Settings settings) throws IOException {
55          Objects.requireNonNull(output, "output cannot be null");
56          Objects.requireNonNull(settings, "settings cannot be null");
57  
58          try (Writer out = output) {
59              new SettingsXpp3Writer().write(out, settings);
60          }
61      }
62  
63      @Override
64      public void write(OutputStream output, Map<String, Object> options, Settings settings) throws IOException {
65          Objects.requireNonNull(output, "output cannot be null");
66          Objects.requireNonNull(settings, "settings cannot be null");
67  
68          String encoding = settings.getModelEncoding();
69          // TODO Use StringUtils here
70          if (encoding == null || encoding.length() <= 0) {
71              encoding = "UTF-8";
72          }
73  
74          try (Writer out = new OutputStreamWriter(output, encoding)) {
75              write(out, options, settings);
76          }
77      }
78  }