1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.building;
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.ArrayList;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Objects;
35 import java.util.Optional;
36
37 import org.apache.maven.api.model.Model;
38 import org.apache.maven.api.spi.ModelParser;
39 import org.apache.maven.api.spi.ModelParserException;
40 import org.apache.maven.building.Source;
41 import org.apache.maven.model.io.ModelParseException;
42 import org.apache.maven.model.io.ModelReader;
43 import org.apache.maven.model.locator.ModelLocator;
44 import org.eclipse.sisu.Typed;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 @Named("core-default")
72 @Singleton
73 @Typed(ModelProcessor.class)
74 public class DefaultModelProcessor implements ModelProcessor {
75
76 private final Collection<ModelParser> modelParsers;
77 private final ModelLocator modelLocator;
78 private final ModelReader modelReader;
79
80 @Inject
81 public DefaultModelProcessor(List<ModelParser> modelParsers, ModelLocator modelLocator, ModelReader modelReader) {
82 this.modelParsers = modelParsers;
83 this.modelLocator = modelLocator;
84 this.modelReader = modelReader;
85 }
86
87 @Deprecated
88 @Override
89 public File locatePom(File projectDirectory) {
90 return locatePom(projectDirectory.toPath()).toFile();
91 }
92
93 @Override
94 public Path locatePom(Path projectDirectory) {
95
96
97 Path pom = modelParsers.stream()
98 .map(m -> m.locate(projectDirectory)
99 .map(org.apache.maven.api.services.Source::getPath)
100 .orElse(null))
101 .filter(Objects::nonNull)
102 .findFirst()
103 .orElseGet(() -> modelLocator.locatePom(projectDirectory));
104 if (!pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
105 throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
106 }
107 return pom;
108 }
109
110 @Deprecated
111 @Override
112 public File locateExistingPom(File projectDirectory) {
113 Path path = locateExistingPom(projectDirectory.toPath());
114 return path != null ? path.toFile() : null;
115 }
116
117 @Override
118 public Path locateExistingPom(Path projectDirectory) {
119
120
121 Path pom = modelParsers.stream()
122 .map(m -> m.locate(projectDirectory)
123 .map(org.apache.maven.api.services.Source::getPath)
124 .orElse(null))
125 .filter(Objects::nonNull)
126 .findFirst()
127 .orElseGet(() -> modelLocator.locateExistingPom(projectDirectory));
128 if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
129 throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
130 }
131 return pom;
132 }
133
134 protected org.apache.maven.api.model.Model read(
135 Path pomFile, InputStream input, Reader reader, Map<String, ?> options) throws IOException {
136 Source source = (Source) options.get(ModelProcessor.SOURCE);
137 if (pomFile == null && source instanceof org.apache.maven.building.FileSource) {
138 pomFile = ((org.apache.maven.building.FileSource) source).getPath();
139 }
140 if (pomFile != null) {
141 Path projectDirectory = pomFile.getParent();
142 List<ModelParserException> exceptions = new ArrayList<>();
143 for (ModelParser parser : modelParsers) {
144 try {
145 Optional<Model> model = parser.locateAndParse(projectDirectory, options);
146 if (model.isPresent()) {
147 return model.get().withPomFile(pomFile);
148 }
149 } catch (ModelParserException e) {
150 exceptions.add(e);
151 }
152 }
153 try {
154 return readXmlModel(pomFile, null, null, options);
155 } catch (IOException e) {
156 exceptions.forEach(e::addSuppressed);
157 throw e;
158 }
159 } else {
160 return readXmlModel(pomFile, input, reader, options);
161 }
162 }
163
164 private org.apache.maven.api.model.Model readXmlModel(
165 Path pomFile, InputStream input, Reader reader, Map<String, ?> options) throws IOException {
166 if (pomFile != null) {
167 return modelReader.read(pomFile, options).getDelegate();
168 } else if (input != null) {
169 return modelReader.read(input, options).getDelegate();
170 } else {
171 return modelReader.read(reader, options).getDelegate();
172 }
173 }
174
175 @Deprecated
176 @Override
177 public org.apache.maven.model.Model read(File file, Map<String, ?> options) throws IOException {
178 Objects.requireNonNull(file, "file cannot be null");
179 return read(file.toPath(), options);
180 }
181
182 @Override
183 public org.apache.maven.model.Model read(Path path, Map<String, ?> options) throws IOException {
184 Objects.requireNonNull(path, "path cannot be null");
185 org.apache.maven.api.model.Model model = read(path, null, null, options);
186 return new org.apache.maven.model.Model(model);
187 }
188
189 @Override
190 public org.apache.maven.model.Model read(InputStream input, Map<String, ?> options) throws IOException {
191 Objects.requireNonNull(input, "input cannot be null");
192 try (InputStream in = input) {
193 org.apache.maven.api.model.Model model = read(null, in, null, options);
194 return new org.apache.maven.model.Model(model);
195 } catch (ModelParserException e) {
196 throw new ModelParseException("Unable to read model: " + e, e.getLineNumber(), e.getColumnNumber(), e);
197 }
198 }
199
200 @Override
201 public org.apache.maven.model.Model read(Reader reader, Map<String, ?> options) throws IOException {
202 Objects.requireNonNull(reader, "reader cannot be null");
203 try (Reader r = reader) {
204 org.apache.maven.api.model.Model model = read(null, null, r, options);
205 return new org.apache.maven.model.Model(model);
206 }
207 }
208 }