View Javadoc
1   package org.apache.maven.toolchain.io;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.Reader;
26  import java.util.Map;
27  
28  import javax.inject.Named;
29  import javax.inject.Singleton;
30  
31  import org.apache.commons.lang3.Validate;
32  import org.apache.maven.toolchain.model.PersistedToolchains;
33  import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
34  import org.codehaus.plexus.util.IOUtil;
35  import org.codehaus.plexus.util.ReaderFactory;
36  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37  
38  /**
39   * Handles deserialization of toolchains from the default textual format.
40   *
41   * @author Robert Scholte
42   * @since 3.3.0
43   */
44  @Named
45  @Singleton
46  public class DefaultToolchainsReader
47      implements ToolchainsReader
48  {
49  
50      @Override
51      public PersistedToolchains read( File input, Map<String, ?> options )
52          throws IOException
53      {
54          Validate.notNull( input, "input cannot be null" );
55  
56          return read( ReaderFactory.newXmlReader( input ), options );
57      }
58  
59      @Override
60      public PersistedToolchains read( Reader input, Map<String, ?> options )
61          throws IOException
62      {
63          Validate.notNull( input, "input cannot be null" );
64  
65          try
66          {
67              MavenToolchainsXpp3Reader r = new MavenToolchainsXpp3Reader();
68              return r.read( input, isStrict( options ) );
69          }
70          catch ( XmlPullParserException e )
71          {
72              throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
73          }
74          finally
75          {
76              IOUtil.close( input );
77          }
78      }
79  
80      @Override
81      public PersistedToolchains read( InputStream input, Map<String, ?> options )
82          throws IOException
83      {
84          Validate.notNull( input, "input cannot be null" );
85  
86          try
87          {
88              MavenToolchainsXpp3Reader r = new MavenToolchainsXpp3Reader();
89              return r.read( input, isStrict( options ) );
90          }
91          catch ( XmlPullParserException e )
92          {
93              throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
94          }
95          finally
96          {
97              IOUtil.close( input );
98          }
99      }
100 
101     private boolean isStrict( Map<String, ?> options )
102     {
103         Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
104         return value == null || Boolean.parseBoolean( value.toString() );
105     }
106 
107 }