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