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.model.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.model.Model;
31  import org.apache.maven.model.v4.MavenXpp3Writer;
32  import org.codehaus.plexus.util.WriterFactory;
33  
34  /**
35   * Handles serialization of a model into some kind of textual format like XML.
36   *
37   * @author Benjamin Bentmann
38   */
39  @Named
40  @Singleton
41  public class DefaultModelWriter implements ModelWriter {
42  
43      @Override
44      public void write(File output, Map<String, Object> options, Model model) throws IOException {
45          Objects.requireNonNull(output, "output cannot be null");
46          Objects.requireNonNull(model, "model cannot be null");
47  
48          output.getParentFile().mkdirs();
49  
50          write(WriterFactory.newXmlWriter(output), options, model);
51      }
52  
53      @Override
54      public void write(Writer output, Map<String, Object> options, Model model) throws IOException {
55          Objects.requireNonNull(output, "output cannot be null");
56          Objects.requireNonNull(model, "model cannot be null");
57  
58          try (Writer out = output) {
59              new MavenXpp3Writer().write(out, model);
60          }
61      }
62  
63      @Override
64      public void write(OutputStream output, Map<String, Object> options, Model model) throws IOException {
65          Objects.requireNonNull(output, "output cannot be null");
66          Objects.requireNonNull(model, "model cannot be null");
67  
68          String encoding = model.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, model);
76          }
77      }
78  
79      @Override
80      public void write(File output, Map<String, Object> options, org.apache.maven.model.Model model) throws IOException {
81          write(output, options, model.getDelegate());
82      }
83  
84      @Override
85      public void write(Writer output, Map<String, Object> options, org.apache.maven.model.Model model)
86              throws IOException {
87          write(output, options, model.getDelegate());
88      }
89  
90      @Override
91      public void write(OutputStream output, Map<String, Object> options, org.apache.maven.model.Model model)
92              throws IOException {
93          write(output, options, model.getDelegate());
94      }
95  }