View Javadoc
1   package org.apache.maven.model.io;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Handles deserialization of a model from some kind of textual format like XML.
40   *
41   * @author Benjamin Bentmann
42   */
43  @Component( role = ModelReader.class )
44  public class DefaultModelReader
45      implements ModelReader
46  {
47  
48      @Override
49      public Model read( File input, Map<String, ?> options )
50          throws IOException
51      {
52          if ( input == null )
53          {
54              throw new IllegalArgumentException( "input file missing" );
55          }
56  
57          Model model = read( new FileInputStream( input ), options );
58  
59          model.setPomFile( input );
60  
61          return model;
62      }
63  
64      @Override
65      public Model read( Reader input, Map<String, ?> options )
66          throws IOException
67      {
68          if ( input == null )
69          {
70              throw new IllegalArgumentException( "input reader missing" );
71          }
72  
73          try
74          {
75              return read( input, isStrict( options ), getSource( options ) );
76          }
77          finally
78          {
79              IOUtil.close( input );
80          }
81      }
82  
83      @Override
84      public Model 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              return read( ReaderFactory.newXmlReader( input ), isStrict( options ), getSource( options ) );
95          }
96          finally
97          {
98              IOUtil.close( input );
99          }
100     }
101 
102     private boolean isStrict( Map<String, ?> options )
103     {
104         Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
105         return value == null || Boolean.parseBoolean( value.toString() );
106     }
107 
108     private InputSource getSource( Map<String, ?> options )
109     {
110         Object value = ( options != null ) ? options.get( INPUT_SOURCE ) : null;
111         return (InputSource) value;
112     }
113 
114     private Model read( Reader reader, boolean strict, InputSource source )
115         throws IOException
116     {
117         try
118         {
119             if ( source != null )
120             {
121                 return new MavenXpp3ReaderEx().read( reader, strict, source );
122             }
123             else
124             {
125                 return new MavenXpp3Reader().read( reader, strict );
126             }
127         }
128         catch ( XmlPullParserException e )
129         {
130             throw new ModelParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
131         }
132     }
133 
134 }