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