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.toolchain.io;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.Reader;
25  import java.util.Map;
26  import java.util.Objects;
27  import javax.inject.Named;
28  import javax.inject.Singleton;
29  import org.apache.maven.api.toolchain.PersistedToolchains;
30  import org.apache.maven.toolchain.v4.MavenToolchainsXpp3Reader;
31  import org.codehaus.plexus.util.ReaderFactory;
32  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33  
34  /**
35   * Handles deserialization of toolchains from the default textual format.
36   *
37   * @author Robert Scholte
38   * @since 3.3.0
39   */
40  @Named
41  @Singleton
42  public class DefaultToolchainsReader implements ToolchainsReader {
43  
44      @Override
45      public PersistedToolchains read(File input, Map<String, ?> options) throws IOException {
46          Objects.requireNonNull(input, "input cannot be null");
47  
48          return read(ReaderFactory.newXmlReader(input), options);
49      }
50  
51      @Override
52      public PersistedToolchains read(Reader input, Map<String, ?> options) throws IOException {
53          Objects.requireNonNull(input, "input cannot be null");
54  
55          try (Reader in = input) {
56              return new MavenToolchainsXpp3Reader().read(in, isStrict(options));
57          } catch (XmlPullParserException e) {
58              throw new ToolchainsParseException(e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e);
59          }
60      }
61  
62      @Override
63      public PersistedToolchains read(InputStream input, Map<String, ?> options) throws IOException {
64          Objects.requireNonNull(input, "input cannot be null");
65  
66          try (InputStream in = input) {
67              return new MavenToolchainsXpp3Reader().read(in, isStrict(options));
68          } catch (XmlPullParserException e) {
69              throw new ToolchainsParseException(e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e);
70          }
71      }
72  
73      private boolean isStrict(Map<String, ?> options) {
74          Object value = (options != null) ? options.get(IS_STRICT) : null;
75          return value == null || Boolean.parseBoolean(value.toString());
76      }
77  }