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 javax.inject.Named;
22  import javax.inject.Singleton;
23  import javax.xml.stream.XMLStreamException;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.io.Writer;
29  import java.nio.file.Files;
30  import java.util.Map;
31  import java.util.Objects;
32  
33  import org.apache.maven.settings.Settings;
34  import org.apache.maven.settings.v4.SettingsStaxWriter;
35  
36  /**
37   * Handles serialization of settings into the default textual format.
38   *
39   */
40  @Named
41  @Singleton
42  public class DefaultSettingsWriter implements SettingsWriter {
43  
44      @Override
45      public void write(File output, Map<String, Object> options, Settings settings) throws IOException {
46          Objects.requireNonNull(output, "output cannot be null");
47          Objects.requireNonNull(settings, "settings cannot be null");
48  
49          output.getParentFile().mkdirs();
50  
51          write(Files.newOutputStream(output.toPath()), options, settings);
52      }
53  
54      @Override
55      public void write(Writer output, Map<String, Object> options, Settings settings) throws IOException {
56          Objects.requireNonNull(output, "output cannot be null");
57          Objects.requireNonNull(settings, "settings cannot be null");
58  
59          try (Writer out = output) {
60              new SettingsStaxWriter().write(out, settings.getDelegate());
61          } catch (XMLStreamException e) {
62              throw new IOException("Error writing settings", e);
63          }
64      }
65  
66      @Override
67      public void write(OutputStream output, Map<String, Object> options, Settings settings) throws IOException {
68          Objects.requireNonNull(output, "output cannot be null");
69          Objects.requireNonNull(settings, "settings cannot be null");
70  
71          try (OutputStream out = output) {
72              new SettingsStaxWriter().write(out, settings.getDelegate());
73          } catch (XMLStreamException e) {
74              throw new IOException("Error writing settings", e);
75          }
76      }
77  }