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 javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.io.OutputStream;
27  import java.io.OutputStreamWriter;
28  import java.io.Writer;
29  import java.util.Map;
30  import java.util.Objects;
31  
32  import org.apache.maven.model.Model;
33  import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
34  import org.codehaus.plexus.util.WriterFactory;
35  
36  /**
37   * Handles serialization of a model into some kind of textual format like XML.
38   *
39   * @deprecated use {@link org.apache.maven.model.v4.MavenStaxWriter} instead
40   */
41  @Named
42  @Singleton
43  @Deprecated(since = "4.0.0")
44  public class DefaultModelWriter implements ModelWriter {
45  
46      @Override
47      public void write(File output, Map<String, Object> options, Model model) throws IOException {
48          Objects.requireNonNull(output, "output cannot be null");
49          Objects.requireNonNull(model, "model cannot be null");
50  
51          output.getParentFile().mkdirs();
52  
53          write(WriterFactory.newXmlWriter(output), options, model);
54      }
55  
56      @Override
57      public void write(Writer output, Map<String, Object> options, Model model) throws IOException {
58          Objects.requireNonNull(output, "output cannot be null");
59          Objects.requireNonNull(model, "model cannot be null");
60  
61          try (Writer out = output) {
62              new MavenXpp3Writer().write(out, model);
63          }
64      }
65  
66      @Override
67      public void write(OutputStream output, Map<String, Object> options, Model model) throws IOException {
68          Objects.requireNonNull(output, "output cannot be null");
69          Objects.requireNonNull(model, "model cannot be null");
70  
71          String encoding = model.getModelEncoding();
72          // TODO Use StringUtils here
73          if (encoding == null || encoding.length() <= 0) {
74              encoding = "UTF-8";
75          }
76  
77          try (Writer out = new OutputStreamWriter(output, encoding)) {
78              write(out, options, model);
79          }
80      }
81  }