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 import java.util.Objects;
28
29 import javax.inject.Named;
30 import javax.inject.Singleton;
31
32 import org.apache.maven.toolchain.model.PersistedToolchains;
33 import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
34 import org.codehaus.plexus.util.ReaderFactory;
35 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
36
37
38
39
40
41
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 Objects.requireNonNull( input, "input cannot be null" );
54
55 return read( ReaderFactory.newXmlReader( input ), options );
56 }
57
58 @Override
59 public PersistedToolchains read( Reader input, Map<String, ?> options )
60 throws IOException
61 {
62 Objects.requireNonNull( input, "input cannot be null" );
63
64 try ( final Reader in = input )
65 {
66 return new MavenToolchainsXpp3Reader().read( in, isStrict( options ) );
67 }
68 catch ( XmlPullParserException e )
69 {
70 throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
71 }
72 }
73
74 @Override
75 public PersistedToolchains read( InputStream input, Map<String, ?> options )
76 throws IOException
77 {
78 Objects.requireNonNull( input, "input cannot be null" );
79
80 try ( final InputStream in = input )
81 {
82 return new MavenToolchainsXpp3Reader().read( in, isStrict( options ) );
83 }
84 catch ( XmlPullParserException e )
85 {
86 throw new ToolchainsParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
87 }
88 }
89
90 private boolean isStrict( Map<String, ?> options )
91 {
92 Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
93 return value == null || Boolean.parseBoolean( value.toString() );
94 }
95
96 }