1 package org.apache.maven.settings.io;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.maven.settings.Settings;
29 import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
30 import org.codehaus.plexus.component.annotations.Component;
31 import org.codehaus.plexus.util.IOUtil;
32 import org.codehaus.plexus.util.ReaderFactory;
33 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34
35
36
37
38
39
40 @Component( role = SettingsReader.class )
41 public class DefaultSettingsReader
42 implements SettingsReader
43 {
44
45 @Override
46 public Settings read( File input, Map<String, ?> options )
47 throws IOException
48 {
49 if ( input == null )
50 {
51 throw new IllegalArgumentException( "input file missing" );
52 }
53
54 Settings settings = read( ReaderFactory.newXmlReader( input ), options );
55
56 return settings;
57 }
58
59 @Override
60 public Settings read( Reader input, Map<String, ?> options )
61 throws IOException
62 {
63 if ( input == null )
64 {
65 throw new IllegalArgumentException( "input reader missing" );
66 }
67
68 try
69 {
70 SettingsXpp3Reader r = new SettingsXpp3Reader();
71 return r.read( input, isStrict( options ) );
72 }
73 catch ( XmlPullParserException e )
74 {
75 throw new SettingsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
76 }
77 finally
78 {
79 IOUtil.close( input );
80 }
81 }
82
83 @Override
84 public Settings read( InputStream input, Map<String, ?> options )
85 throws IOException
86 {
87 if ( input == null )
88 {
89 throw new IllegalArgumentException( "input stream missing" );
90 }
91
92 try
93 {
94 SettingsXpp3Reader r = new SettingsXpp3Reader();
95 return r.read( input, isStrict( options ) );
96 }
97 catch ( XmlPullParserException e )
98 {
99 throw new SettingsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
100 }
101 finally
102 {
103 IOUtil.close( input );
104 }
105 }
106
107 private boolean isStrict( Map<String, ?> options )
108 {
109 Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
110 return value == null || Boolean.parseBoolean( value.toString() );
111 }
112
113 }