1 package org.apache.maven.model.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.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.Reader;
27 import java.util.Map;
28
29 import org.apache.commons.lang3.Validate;
30 import org.apache.maven.model.InputSource;
31 import org.apache.maven.model.Model;
32 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
33 import org.apache.maven.model.io.xpp3.MavenXpp3ReaderEx;
34 import org.codehaus.plexus.component.annotations.Component;
35 import org.codehaus.plexus.util.ReaderFactory;
36 import org.codehaus.plexus.util.xml.XmlStreamReader;
37 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
38
39
40
41
42
43
44 @Component( role = ModelReader.class )
45 public class DefaultModelReader
46 implements ModelReader
47 {
48
49 @Override
50 public Model read( File input, Map<String, ?> options )
51 throws IOException
52 {
53 Validate.notNull( input, "input cannot be null" );
54
55 Model model = read( new FileInputStream( input ), options );
56
57 model.setPomFile( input );
58
59 return model;
60 }
61
62 @Override
63 public Model read( Reader input, Map<String, ?> options )
64 throws IOException
65 {
66 Validate.notNull( input, "input cannot be null" );
67
68 try ( final Reader in = input )
69 {
70 return read( in, isStrict( options ), getSource( options ) );
71 }
72 }
73
74 @Override
75 public Model read( InputStream input, Map<String, ?> options )
76 throws IOException
77 {
78 Validate.notNull( input, "input cannot be null" );
79
80 try ( final XmlStreamReader in = ReaderFactory.newXmlReader( input ) )
81 {
82 return read( in, isStrict( options ), getSource( options ) );
83 }
84 }
85
86 private boolean isStrict( Map<String, ?> options )
87 {
88 Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
89 return value == null || Boolean.parseBoolean( value.toString() );
90 }
91
92 private InputSource getSource( Map<String, ?> options )
93 {
94 Object value = ( options != null ) ? options.get( INPUT_SOURCE ) : null;
95 return (InputSource) value;
96 }
97
98 private Model read( Reader reader, boolean strict, InputSource source )
99 throws IOException
100 {
101 try
102 {
103 if ( source != null )
104 {
105 return new MavenXpp3ReaderEx().read( reader, strict, source );
106 }
107 else
108 {
109 return new MavenXpp3Reader().read( reader, strict );
110 }
111 }
112 catch ( XmlPullParserException e )
113 {
114 throw new ModelParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
115 }
116 }
117
118 }