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.ToolchainsXmlFactory;
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.toolchain.PersistedToolchains;
39  import org.apache.maven.toolchain.v4.MavenToolchainsXpp3Reader;
40  import org.apache.maven.toolchain.v4.MavenToolchainsXpp3Writer;
41  
42  
43  @Named
44  @Singleton
45  public class DefaultToolchainsXmlFactory
46          implements ToolchainsXmlFactory
47  {
48      @Override
49      public PersistedToolchains read( @Nonnull XmlReaderRequest request ) throws XmlReaderException
50      {
51          Objects.requireNonNull( request, "request can not be null" );
52          Reader reader = request.getReader();
53          InputStream inputStream = request.getInputStream();
54          if ( reader == null && inputStream == null )
55          {
56              throw new IllegalArgumentException( "reader or inputStream must be non null" );
57          }
58          try
59          {
60              InputSource source = null;
61              if ( request.getModelId() != null || request.getLocation() != null )
62              {
63                  source = new InputSource( request.getModelId(), request.getLocation() );
64              }
65              MavenToolchainsXpp3Reader xml = new MavenToolchainsXpp3Reader();
66              xml.setAddDefaultEntities( request.isAddDefaultEntities() );
67              if ( reader != null )
68              {
69                  return xml.read( reader, request.isStrict() );
70              }
71              else
72              {
73                  return xml.read( inputStream, request.isStrict() );
74              }
75          }
76          catch ( Exception e )
77          {
78              throw new XmlReaderException( "Unable to read toolchains", e );
79          }
80      }
81  
82      @Override
83      public void write( XmlWriterRequest<PersistedToolchains> request ) throws XmlWriterException
84      {
85          Objects.requireNonNull( request, "request can not be null" );
86          PersistedToolchains content = Objects.requireNonNull( request.getContent(), "content can not be null" );
87          OutputStream outputStream = request.getOutputStream();
88          Writer writer = request.getWriter();
89          if ( writer == null && outputStream == null )
90          {
91              throw new IllegalArgumentException( "writer or outputStream must be non null" );
92          }
93          try
94          {
95              if ( writer != null )
96              {
97                  new MavenToolchainsXpp3Writer().write( writer, content );
98              }
99              else
100             {
101                 new MavenToolchainsXpp3Writer().write( outputStream, content );
102             }
103         }
104         catch ( Exception e )
105         {
106             throw new XmlWriterException( "Unable to write toolchains", e );
107         }
108     }
109 }