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.internal.impl;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.io.Reader;
27  import java.io.Writer;
28  import java.util.Objects;
29  
30  import org.apache.maven.api.annotations.Nonnull;
31  import org.apache.maven.api.model.InputSource;
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.toolchain.PersistedToolchains;
38  import org.apache.maven.toolchain.v4.MavenToolchainsXpp3Reader;
39  import org.apache.maven.toolchain.v4.MavenToolchainsXpp3Writer;
40  
41  @Named
42  @Singleton
43  public class DefaultToolchainsXmlFactory implements ToolchainsXmlFactory {
44      @Override
45      public PersistedToolchains read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
46          Objects.requireNonNull(request, "request can not be null");
47          Reader reader = request.getReader();
48          InputStream inputStream = request.getInputStream();
49          if (reader == null && inputStream == null) {
50              throw new IllegalArgumentException("reader or inputStream must be non null");
51          }
52          try {
53              InputSource source = null;
54              if (request.getModelId() != null || request.getLocation() != null) {
55                  source = new InputSource(request.getModelId(), request.getLocation());
56              }
57              MavenToolchainsXpp3Reader xml = new MavenToolchainsXpp3Reader();
58              xml.setAddDefaultEntities(request.isAddDefaultEntities());
59              if (reader != null) {
60                  return xml.read(reader, request.isStrict());
61              } else {
62                  return xml.read(inputStream, request.isStrict());
63              }
64          } catch (Exception e) {
65              throw new XmlReaderException("Unable to read toolchains", e);
66          }
67      }
68  
69      @Override
70      public void write(XmlWriterRequest<PersistedToolchains> request) throws XmlWriterException {
71          Objects.requireNonNull(request, "request can not be null");
72          PersistedToolchains content = Objects.requireNonNull(request.getContent(), "content can not be null");
73          OutputStream outputStream = request.getOutputStream();
74          Writer writer = request.getWriter();
75          if (writer == null && outputStream == null) {
76              throw new IllegalArgumentException("writer or outputStream must be non null");
77          }
78          try {
79              if (writer != null) {
80                  new MavenToolchainsXpp3Writer().write(writer, content);
81              } else {
82                  new MavenToolchainsXpp3Writer().write(outputStream, content);
83              }
84          } catch (Exception e) {
85              throw new XmlWriterException("Unable to write toolchains", e);
86          }
87      }
88  }