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