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.maven.toolchain.model.PersistedToolchains;
32  import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.ReaderFactory;
35  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
36  
37  /**
38   * Handles deserialization of toolchains from the default textual format.
39   *
40   * @author Robert Scholte
41   * @since 3.3.0
42   */
43  @Named
44  @Singleton
45  public class DefaultToolchainsReader
46      implements ToolchainsReader
47  {
48  
49      @Override
50      public PersistedToolchains read( File input, Map<String, ?> options )
51          throws IOException
52      {
53          if ( input == null )
54          {
55              throw new IllegalArgumentException( "input file missing" );
56          }
57  
58          return read( ReaderFactory.newXmlReader( input ), options );
59      }
60  
61      @Override
62      public PersistedToolchains read( Reader input, Map<String, ?> options )
63          throws IOException
64      {
65          if ( input == null )
66          {
67              throw new IllegalArgumentException( "input reader missing" );
68          }
69  
70          try
71          {
72              MavenToolchainsXpp3Reader r = new MavenToolchainsXpp3Reader();
73              return r.read( input, isStrict( options ) );
74          }
75          catch ( XmlPullParserException e )
76          {
77              throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
78          }
79          finally
80          {
81              IOUtil.close( input );
82          }
83      }
84  
85      @Override
86      public PersistedToolchains read( InputStream input, Map<String, ?> options )
87          throws IOException
88      {
89          if ( input == null )
90          {
91              throw new IllegalArgumentException( "input stream missing" );
92          }
93  
94          try
95          {
96              MavenToolchainsXpp3Reader r = new MavenToolchainsXpp3Reader();
97              return r.read( input, isStrict( options ) );
98          }
99          catch ( XmlPullParserException e )
100         {
101             throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
102         }
103         finally
104         {
105             IOUtil.close( input );
106         }
107     }
108 
109     private boolean isStrict( Map<String, ?> options )
110     {
111         Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
112         return value == null || Boolean.parseBoolean( value.toString() );
113     }
114 
115 }