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.IOException;
24  import java.io.InputStream;
25  import java.io.Reader;
26  import java.nio.file.Path;
27  import java.util.Map;
28  import java.util.Objects;
29  
30  import javax.inject.Inject;
31  import javax.inject.Named;
32  import javax.inject.Singleton;
33  
34  import org.apache.maven.api.model.InputSource;
35  import org.apache.maven.api.model.Model;
36  import org.apache.maven.model.building.ModelSourceTransformer;
37  import org.apache.maven.model.building.TransformerContext;
38  import org.apache.maven.model.v4.MavenXpp3Reader;
39  import org.apache.maven.model.v4.MavenXpp3ReaderEx;
40  import org.codehaus.plexus.util.ReaderFactory;
41  import org.codehaus.plexus.util.xml.XmlStreamReader;
42  import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
43  import org.codehaus.plexus.util.xml.pull.MXParser;
44  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
45  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
46  
47  /**
48   * Handles deserialization of a model from some kind of textual format like XML.
49   *
50   * @author Benjamin Bentmann
51   */
52  @Named
53  @Singleton
54  public class DefaultModelReader
55      implements ModelReader
56  {
57      private final ModelSourceTransformer transformer;
58  
59      @Inject
60      public DefaultModelReader( ModelSourceTransformer transformer )
61      {
62          this.transformer = transformer;
63      }
64  
65      @Override
66      public Model read( File input, Map<String, ?> options )
67          throws IOException
68      {
69          Objects.requireNonNull( input, "input cannot be null" );
70  
71          try ( XmlStreamReader in = ReaderFactory.newXmlReader( input ) )
72          {
73              Model model = read( in, input.toPath(), options );
74  
75              return model.withPomFile( input.toPath() );
76          }
77      }
78  
79      @Override
80      public Model read( Reader input, Map<String, ?> options )
81          throws IOException
82      {
83          Objects.requireNonNull( input, "input cannot be null" );
84  
85          try ( Reader in = input )
86          {
87              return read( in, null, options );
88          }
89      }
90  
91      @Override
92      public Model read( InputStream input, Map<String, ?> options )
93          throws IOException
94      {
95          Objects.requireNonNull( input, "input cannot be null" );
96  
97          try ( XmlStreamReader in = ReaderFactory.newXmlReader( input ) )
98          {
99              return read( in, null, options );
100         }
101     }
102 
103     private boolean isStrict( Map<String, ?> options )
104     {
105         Object value = ( options != null ) ? options.get( IS_STRICT ) : null;
106         return value == null || Boolean.parseBoolean( value.toString() );
107     }
108 
109     private InputSource getSource( Map<String, ?> options )
110     {
111         Object value = ( options != null ) ? options.get( INPUT_SOURCE ) : null;
112         if ( value instanceof org.apache.maven.model.InputSource )
113         {
114             org.apache.maven.model.InputSource src = ( org.apache.maven.model.InputSource ) value;
115             return new InputSource( src.getModelId(), src.getLocation() );
116         }
117         return (InputSource) value;
118     }
119 
120     private TransformerContext getTransformerContext( Map<String, ?> options )
121     {
122         Object value = ( options != null ) ? options.get( TRANSFORMER_CONTEXT ) : null;
123         return (TransformerContext) value;
124     }
125 
126     private Model read( Reader reader, Path pomFile, Map<String, ?> options )
127         throws IOException
128     {
129         try
130         {
131             XmlPullParser parser = new MXParser( EntityReplacementMap.defaultEntityReplacementMap );
132             parser.setInput( reader );
133 
134             TransformerContext context = getTransformerContext( options );
135             XmlPullParser transformingParser = context != null
136                     ? transformer.transform( parser, pomFile, context ) : parser;
137 
138             InputSource source = getSource( options );
139             boolean strict = isStrict( options );
140             if ( source != null )
141             {
142                 return readModelEx( transformingParser, source, strict );
143             }
144             else
145             {
146                 return readModel( transformingParser, strict );
147             }
148         }
149         catch ( XmlPullParserException e )
150         {
151             throw new ModelParseException( e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e );
152         }
153         catch ( IOException e )
154         {
155             throw e;
156         }
157         catch ( Exception e )
158         {
159             throw new IOException( "Unable to transform pom", e );
160         }
161     }
162 
163     private Model readModel( XmlPullParser parser, boolean strict )
164             throws XmlPullParserException, IOException
165     {
166         MavenXpp3Reader mr = new MavenXpp3Reader();
167         return mr.read( parser, strict );
168     }
169 
170     private Model readModelEx( XmlPullParser parser, InputSource source, boolean strict )
171             throws XmlPullParserException, IOException
172     {
173         MavenXpp3ReaderEx mr = new MavenXpp3ReaderEx();
174         return mr.read( parser, strict, source );
175     }
176 
177 }