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