1 package org.apache.maven.toolchain.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 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
40
41
42
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 }