View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.io;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.Reader;
29  import java.nio.file.Path;
30  import java.util.Map;
31  import java.util.Objects;
32  
33  import org.apache.maven.model.InputSource;
34  import org.apache.maven.model.Model;
35  import org.apache.maven.model.building.ModelSourceTransformer;
36  import org.apache.maven.model.building.TransformerContext;
37  import org.apache.maven.model.v4.MavenXpp3Reader;
38  import org.apache.maven.model.v4.MavenXpp3ReaderEx;
39  import org.codehaus.plexus.util.ReaderFactory;
40  import org.codehaus.plexus.util.xml.XmlStreamReader;
41  import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
42  import org.codehaus.plexus.util.xml.pull.MXParser;
43  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
44  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
45  
46  /**
47   * Handles deserialization of a model from some kind of textual format like XML.
48   *
49   * @author Benjamin Bentmann
50   */
51  @Named
52  @Singleton
53  public class DefaultModelReader implements ModelReader {
54      private final ModelSourceTransformer transformer;
55  
56      @Inject
57      public DefaultModelReader(ModelSourceTransformer transformer) {
58          this.transformer = transformer;
59      }
60  
61      @Override
62      public Model read(File input, Map<String, ?> options) throws IOException {
63          Objects.requireNonNull(input, "input cannot be null");
64  
65          try (XmlStreamReader in = ReaderFactory.newXmlReader(input)) {
66              Model model = read(in, input.toPath(), options);
67  
68              model.setPomFile(input);
69  
70              return model;
71          }
72      }
73  
74      @Override
75      public Model read(Reader input, Map<String, ?> options) throws IOException {
76          Objects.requireNonNull(input, "input cannot be null");
77  
78          try (Reader in = input) {
79              return read(in, null, options);
80          }
81      }
82  
83      @Override
84      public Model read(InputStream input, Map<String, ?> options) throws IOException {
85          Objects.requireNonNull(input, "input cannot be null");
86  
87          try (XmlStreamReader in = ReaderFactory.newXmlReader(input)) {
88              return read(in, null, options);
89          }
90      }
91  
92      private boolean isStrict(Map<String, ?> options) {
93          Object value = (options != null) ? options.get(IS_STRICT) : null;
94          return value == null || Boolean.parseBoolean(value.toString());
95      }
96  
97      private InputSource getSource(Map<String, ?> options) {
98          Object value = (options != null) ? options.get(INPUT_SOURCE) : null;
99          return (InputSource) value;
100     }
101 
102     private TransformerContext getTransformerContext(Map<String, ?> options) {
103         Object value = (options != null) ? options.get(TRANSFORMER_CONTEXT) : null;
104         return (TransformerContext) value;
105     }
106 
107     private Model read(Reader reader, Path pomFile, Map<String, ?> options) throws IOException {
108         try {
109             XmlPullParser parser = new MXParser(EntityReplacementMap.defaultEntityReplacementMap);
110             parser.setInput(reader);
111 
112             TransformerContext context = getTransformerContext(options);
113             XmlPullParser transformingParser =
114                     context != null ? transformer.transform(parser, pomFile, context) : parser;
115 
116             InputSource source = getSource(options);
117             boolean strict = isStrict(options);
118             if (source != null) {
119                 return readModelEx(transformingParser, source, strict);
120             } else {
121                 return readModel(transformingParser, strict);
122             }
123         } catch (XmlPullParserException e) {
124             throw new ModelParseException(e.getMessage(), e.getLineNumber(), e.getColumnNumber(), e);
125         } catch (IOException e) {
126             throw e;
127         } catch (Exception e) {
128             throw new IOException("Unable to transform pom", e);
129         }
130     }
131 
132     private Model readModel(XmlPullParser parser, boolean strict) throws XmlPullParserException, IOException {
133         MavenXpp3Reader mr = new MavenXpp3Reader();
134         return new Model(mr.read(parser, strict));
135     }
136 
137     private Model readModelEx(XmlPullParser parser, InputSource source, boolean strict)
138             throws XmlPullParserException, IOException {
139         MavenXpp3ReaderEx mr = new MavenXpp3ReaderEx();
140         return new Model(mr.read(
141                 parser, strict, new org.apache.maven.api.model.InputSource(source.getModelId(), source.getLocation())));
142     }
143 }