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  import javax.xml.stream.Location;
25  import javax.xml.stream.XMLInputFactory;
26  import javax.xml.stream.XMLStreamException;
27  import javax.xml.stream.XMLStreamReader;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.Reader;
33  import java.nio.file.Files;
34  import java.nio.file.Path;
35  import java.util.Map;
36  import java.util.Objects;
37  
38  import org.apache.maven.model.InputSource;
39  import org.apache.maven.model.Model;
40  import org.apache.maven.model.building.ModelSourceTransformer;
41  import org.apache.maven.model.v4.MavenStaxReader;
42  
43  /**
44   * Handles deserialization of a model from some kind of textual format like XML.
45   *
46   */
47  @Named
48  @Singleton
49  public class DefaultModelReader implements ModelReader {
50      private final ModelSourceTransformer transformer;
51  
52      @Inject
53      public DefaultModelReader(ModelSourceTransformer transformer) {
54          this.transformer = transformer;
55      }
56  
57      @Override
58      public Model read(File input, Map<String, ?> options) throws IOException {
59          Objects.requireNonNull(input, "input cannot be null");
60  
61          try (InputStream in = Files.newInputStream(input.toPath())) {
62              Model model = read(in, input.toPath(), options);
63  
64              model.setPomFile(input);
65  
66              return model;
67          }
68      }
69  
70      @Override
71      public Model read(Reader input, Map<String, ?> options) throws IOException {
72          Objects.requireNonNull(input, "input cannot be null");
73  
74          try (Reader in = input) {
75              return read(in, null, options);
76          }
77      }
78  
79      @Override
80      public Model read(InputStream input, Map<String, ?> options) throws IOException {
81          Objects.requireNonNull(input, "input cannot be null");
82  
83          try (InputStream in = input) {
84              return read(input, null, options);
85          }
86      }
87  
88      private boolean isStrict(Map<String, ?> options) {
89          Object value = (options != null) ? options.get(IS_STRICT) : null;
90          return value == null || Boolean.parseBoolean(value.toString());
91      }
92  
93      private InputSource getSource(Map<String, ?> options) {
94          Object value = (options != null) ? options.get(INPUT_SOURCE) : null;
95          return (InputSource) value;
96      }
97  
98      private Path getRootDirectory(Map<String, ?> options) {
99          Object value = (options != null) ? options.get(ROOT_DIRECTORY) : null;
100         return (Path) value;
101     }
102 
103     private Model read(InputStream input, Path pomFile, Map<String, ?> options) throws IOException {
104         try {
105             XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory();
106             factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
107             XMLStreamReader parser = factory.createXMLStreamReader(input);
108 
109             InputSource source = getSource(options);
110             boolean strict = isStrict(options);
111             MavenStaxReader mr = new MavenStaxReader();
112             mr.setAddLocationInformation(source != null);
113             Model model = new Model(mr.read(parser, strict, source != null ? source.toApiSource() : null));
114             return model;
115         } catch (XMLStreamException e) {
116             Location location = e.getLocation();
117             throw new ModelParseException(
118                     e.getMessage(),
119                     location != null ? location.getLineNumber() : -1,
120                     location != null ? location.getColumnNumber() : -1,
121                     e);
122         } catch (Exception e) {
123             throw new IOException("Unable to transform pom", e);
124         }
125     }
126 
127     private Model read(Reader reader, Path pomFile, Map<String, ?> options) throws IOException {
128         try {
129             XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory();
130             factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
131             XMLStreamReader parser = factory.createXMLStreamReader(reader);
132 
133             InputSource source = getSource(options);
134             boolean strict = isStrict(options);
135             MavenStaxReader mr = new MavenStaxReader();
136             mr.setAddLocationInformation(source != null);
137             Model model = new Model(mr.read(parser, strict, source != null ? source.toApiSource() : null));
138             return model;
139         } catch (XMLStreamException e) {
140             Location location = e.getLocation();
141             throw new ModelParseException(
142                     e.getMessage(),
143                     location != null ? location.getLineNumber() : -1,
144                     location != null ? location.getColumnNumber() : -1,
145                     e);
146         } catch (Exception e) {
147             throw new IOException("Unable to transform pom", e);
148         }
149     }
150 }