1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
46
47
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 }