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