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