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.maven.model.InputSource;
30  import org.apache.maven.model.Model;
31  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
32  import org.apache.maven.model.io.xpp3.MavenXpp3ReaderEx;
33  import org.codehaus.plexus.component.annotations.Component;
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  @Component( role = ModelReader.class )
44  public class DefaultModelReader
45      implements ModelReader
46  {
47  
48      public Model read( File input, Map<String, ?> options )
49          throws IOException
50      {
51          if ( input == null )
52          {
53              throw new IllegalArgumentException( "input file missing" );
54          }
55  
56          Model model = read( new FileInputStream( input ), options );
57  
58          model.setPomFile( input );
59  
60          return model;
61      }
62  
63      public Model read( Reader input, Map<String, ?> options )
64          throws IOException
65      {
66          if ( input == null )
67          {
68              throw new IllegalArgumentException( "input reader missing" );
69          }
70  
71          try
72          {
73              return read( input, isStrict( options ), getSource( options ) );
74          }
75          finally
76          {
77              IOUtil.close( input );
78          }
79      }
80  
81      public Model read( InputStream input, Map<String, ?> options )
82          throws IOException
83      {
84          if ( input == null )
85          {
86              throw new IllegalArgumentException( "input stream missing" );
87          }
88  
89          try
90          {
91              return read( ReaderFactory.newXmlReader( input ), isStrict( options ), getSource( options ) );
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     private InputSource getSource( Map<String, ?> options )
106     {
107         Object value = ( options != null ) ? options.get( INPUT_SOURCE ) : null;
108         return (InputSource) value;
109     }
110 
111     private Model read( Reader reader, boolean strict, InputSource source )
112         throws IOException
113     {
114         try
115         {
116             if ( source != null )
117             {
118                 return new MavenXpp3ReaderEx().read( reader, strict, source );
119             }
120             else
121             {
122                 return new MavenXpp3Reader().read( reader, strict );
123             }
124         }
125         catch ( XmlPullParserException e )
126         {
127             throw new ModelParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
128         }
129     }
130 
131 }