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.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.IOUtil;
36  import org.codehaus.plexus.util.ReaderFactory;
37  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
38  
39  /**
40   * Handles deserialization of a model from some kind of textual format like XML.
41   *
42   * @author Benjamin Bentmann
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
69          {
70              return read( input, isStrict( options ), getSource( options ) );
71          }
72          finally
73          {
74              IOUtil.close( input );
75          }
76      }
77  
78      @Override
79      public Model read( InputStream input, Map<String, ?> options )
80          throws IOException
81      {
82          Validate.notNull( input, "input cannot be null" );
83  
84          try
85          {
86              return read( ReaderFactory.newXmlReader( input ), isStrict( options ), getSource( options ) );
87          }
88          finally
89          {
90              IOUtil.close( input );
91          }
92      }
93  
94      private boolean isStrict( Map<String, ?> options )
95      {
96          Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
97          return value == null || Boolean.parseBoolean( value.toString() );
98      }
99  
100     private InputSource getSource( Map<String, ?> options )
101     {
102         Object value = ( options != null ) ? options.get( INPUT_SOURCE ) : null;
103         return (InputSource) value;
104     }
105 
106     private Model read( Reader reader, boolean strict, InputSource source )
107         throws IOException
108     {
109         try
110         {
111             if ( source != null )
112             {
113                 return new MavenXpp3ReaderEx().read( reader, strict, source );
114             }
115             else
116             {
117                 return new MavenXpp3Reader().read( reader, strict );
118             }
119         }
120         catch ( XmlPullParserException e )
121         {
122             throw new ModelParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
123         }
124     }
125 
126 }