View Javadoc
1   package org.apache.maven.model.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 org.apache.maven.model.Model;
31  import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.util.WriterFactory;
34  
35  /**
36   * Handles serialization of a model into some kind of textual format like XML.
37   *
38   * @author Benjamin Bentmann
39   */
40  @Component( role = ModelWriter.class )
41  public class DefaultModelWriter
42      implements ModelWriter
43  {
44  
45      @Override
46      public void write( File output, Map<String, Object> options, Model model )
47          throws IOException
48      {
49          Objects.requireNonNull( output, "output cannot be null" );
50          Objects.requireNonNull( model, "model cannot be null" );
51  
52          output.getParentFile().mkdirs();
53  
54          write( WriterFactory.newXmlWriter( output ), options, model );
55      }
56  
57      @Override
58      public void write( Writer output, Map<String, Object> options, Model model )
59          throws IOException
60      {
61          Objects.requireNonNull( output, "output cannot be null" );
62          Objects.requireNonNull( model, "model cannot be null" );
63  
64          try ( final Writer out = output )
65          {
66              new MavenXpp3Writer().write( out, model );
67          }
68      }
69  
70      @Override
71      public void write( OutputStream output, Map<String, Object> options, Model model )
72          throws IOException
73      {
74          Objects.requireNonNull( output, "output cannot be null" );
75          Objects.requireNonNull( model, "model cannot be null" );
76  
77          String encoding = model.getModelEncoding();
78          // TODO Use StringUtils here
79          if ( encoding == null || encoding.length() <= 0 )
80          {
81              encoding = "UTF-8";
82          }
83  
84          try ( final Writer out = new OutputStreamWriter( output, encoding ) )
85          {
86              write( out, options, model );
87          }
88      }
89  
90  }