View Javadoc
1   package org.apache.maven.settings.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 org.apache.commons.lang3.Validate;
29  import org.apache.maven.settings.Settings;
30  import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.util.IOUtil;
33  import org.codehaus.plexus.util.ReaderFactory;
34  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35  
36  /**
37   * Handles deserialization of settings from the default textual format.
38   *
39   * @author Benjamin Bentmann
40   */
41  @Component( role = SettingsReader.class )
42  public class DefaultSettingsReader
43      implements SettingsReader
44  {
45  
46      @Override
47      public Settings read( File input, Map<String, ?> options )
48          throws IOException
49      {
50          Validate.notNull( input, "input cannot be null" );
51  
52          Settings settings = read( ReaderFactory.newXmlReader( input ), options );
53  
54          return settings;
55      }
56  
57      @Override
58      public Settings read( Reader input, Map<String, ?> options )
59          throws IOException
60      {
61          Validate.notNull( input, "input cannot be null" );
62  
63          try
64          {
65              SettingsXpp3Reader r = new SettingsXpp3Reader();
66              return r.read( input, isStrict( options ) );
67          }
68          catch ( XmlPullParserException e )
69          {
70              throw new SettingsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
71          }
72          finally
73          {
74              IOUtil.close( input );
75          }
76      }
77  
78      @Override
79      public Settings read( InputStream input, Map<String, ?> options )
80          throws IOException
81      {
82          Validate.notNull( input, "input cannot be null" );
83  
84          try
85          {
86              SettingsXpp3Reader r = new SettingsXpp3Reader();
87              return r.read( input, isStrict( options ) );
88          }
89          catch ( XmlPullParserException e )
90          {
91              throw new SettingsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
92          }
93          finally
94          {
95              IOUtil.close( input );
96          }
97      }
98  
99      private boolean isStrict( Map<String, ?> options )
100     {
101         Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
102         return value == null || Boolean.parseBoolean( value.toString() );
103     }
104 
105 }