View Javadoc
1   package org.apache.maven.internal.impl;
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 javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.io.Reader;
28  import java.io.Writer;
29  import java.util.Objects;
30  
31  import org.apache.maven.api.annotations.Nonnull;
32  import org.apache.maven.api.services.xml.SettingsXmlFactory;
33  import org.apache.maven.api.services.xml.XmlReaderException;
34  import org.apache.maven.api.services.xml.XmlReaderRequest;
35  import org.apache.maven.api.services.xml.XmlWriterException;
36  import org.apache.maven.api.services.xml.XmlWriterRequest;
37  import org.apache.maven.api.model.InputSource;
38  import org.apache.maven.api.settings.Settings;
39  import org.apache.maven.settings.v4.SettingsXpp3Reader;
40  import org.apache.maven.settings.v4.SettingsXpp3Writer;
41  
42  @Named
43  @Singleton
44  public class DefaultSettingsXmlFactory
45          implements SettingsXmlFactory
46  {
47      @Override
48      public Settings read( @Nonnull XmlReaderRequest request ) throws XmlReaderException
49      {
50          Objects.requireNonNull( request, "request can not be null" );
51          Reader reader = request.getReader();
52          InputStream inputStream = request.getInputStream();
53          if ( reader == null && inputStream == null )
54          {
55              throw new IllegalArgumentException( "reader or inputStream must be non null" );
56          }
57          try
58          {
59              InputSource source = null;
60              if ( request.getModelId() != null || request.getLocation() != null )
61              {
62                  source = new InputSource( request.getModelId(), request.getLocation() );
63              }
64              SettingsXpp3Reader xml = new SettingsXpp3Reader();
65              xml.setAddDefaultEntities( request.isAddDefaultEntities() );
66              if ( reader != null )
67              {
68                  return xml.read( reader, request.isStrict() );
69              }
70              else
71              {
72                  return xml.read( inputStream, request.isStrict() );
73              }
74          }
75          catch ( Exception e )
76          {
77              throw new XmlReaderException( "Unable to read settings", e );
78          }
79      }
80  
81      @Override
82      public void write( XmlWriterRequest<Settings> request ) throws XmlWriterException
83      {
84          Objects.requireNonNull( request, "request can not be null" );
85          Settings content = Objects.requireNonNull( request.getContent(), "content can not be null" );
86          OutputStream outputStream = request.getOutputStream();
87          Writer writer = request.getWriter();
88          if ( writer == null && outputStream == null )
89          {
90              throw new IllegalArgumentException( "writer or outputStream must be non null" );
91          }
92          try
93          {
94              if ( writer != null )
95              {
96                  new SettingsXpp3Writer().write( writer, content );
97              }
98              else
99              {
100                 new SettingsXpp3Writer().write( outputStream, content );
101             }
102         }
103         catch ( Exception e )
104         {
105             throw new XmlWriterException( "Unable to write settings", e );
106         }
107     }
108 }