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(
82 Collection<ModelParser> modelParsers, ModelLocator modelLocator, ModelReader modelReader) {
83 this.modelParsers = modelParsers;
84 this.modelLocator = modelLocator;
85 this.modelReader = modelReader;
86 }
87
88 @Deprecated
89 @Override
90 public File locatePom(File projectDirectory) {
91 return locatePom(projectDirectory.toPath()).toFile();
92 }
93
94 @Override
95 public Path locatePom(Path projectDirectory) {
96
97
98 Path pom = modelParsers.stream()
99 .map(m -> m.locate(projectDirectory)
100 .map(org.apache.maven.api.services.Source::getPath)
101 .orElse(null))
102 .filter(Objects::nonNull)
103 .findFirst()
104 .orElseGet(() -> modelLocator.locatePom(projectDirectory));
105 if (!pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
106 throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
107 }
108 return pom;
109 }
110
111 @Deprecated
112 @Override
113 public File locateExistingPom(File projectDirectory) {
114 Path path = locateExistingPom(projectDirectory.toPath());
115 return path != null ? path.toFile() : null;
116 }
117
118 @Override
119 public Path locateExistingPom(Path projectDirectory) {
120
121
122 Path pom = modelParsers.stream()
123 .map(m -> m.locate(projectDirectory)
124 .map(org.apache.maven.api.services.Source::getPath)
125 .orElse(null))
126 .filter(Objects::nonNull)
127 .findFirst()
128 .orElseGet(() -> modelLocator.locateExistingPom(projectDirectory));
129 if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
130 throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
131 }
132 return pom;
133 }
134
135 protected org.apache.maven.api.model.Model read(
136 Path pomFile, InputStream input, Reader reader, Map<String, ?> options) throws IOException {
137 Source source = (Source) options.get(ModelProcessor.SOURCE);
138 if (pomFile == null && source instanceof org.apache.maven.building.FileSource) {
139 pomFile = ((org.apache.maven.building.FileSource) source).getPath();
140 }
141 if (pomFile != null) {
142 Path projectDirectory = pomFile.getParent();
143 List<ModelParserException> exceptions = new ArrayList<>();
144 for (ModelParser parser : modelParsers) {
145 try {
146 Optional<Model> model = parser.locateAndParse(projectDirectory, options);
147 if (model.isPresent()) {
148 return model.get().withPomFile(pomFile);
149 }
150 } catch (ModelParserException e) {
151 exceptions.add(e);
152 }
153 }
154 try {
155 return readXmlModel(pomFile, null, null, options);
156 } catch (IOException e) {
157 exceptions.forEach(e::addSuppressed);
158 throw e;
159 }
160 } else {
161 return readXmlModel(pomFile, input, reader, options);
162 }
163 }
164
165 private org.apache.maven.api.model.Model readXmlModel(
166 Path pomFile, InputStream input, Reader reader, Map<String, ?> options) throws IOException {
167 if (pomFile != null) {
168 return modelReader.read(pomFile, options).getDelegate();
169 } else if (input != null) {
170 return modelReader.read(input, options).getDelegate();
171 } else {
172 return modelReader.read(reader, options).getDelegate();
173 }
174 }
175
176 @Deprecated
177 @Override
178 public org.apache.maven.model.Model read(File file, Map<String, ?> options) throws IOException {
179 Objects.requireNonNull(file, "file cannot be null");
180 return read(file.toPath(), options);
181 }
182
183 @Override
184 public org.apache.maven.model.Model read(Path path, Map<String, ?> options) throws IOException {
185 Objects.requireNonNull(path, "path cannot be null");
186 org.apache.maven.api.model.Model model = read(path, null, null, options);
187 return new org.apache.maven.model.Model(model);
188 }
189
190 @Override
191 public org.apache.maven.model.Model read(InputStream input, Map<String, ?> options) throws IOException {
192 Objects.requireNonNull(input, "input cannot be null");
193 try (InputStream in = input) {
194 org.apache.maven.api.model.Model model = read(null, in, null, options);
195 return new org.apache.maven.model.Model(model);
196 } catch (ModelParserException e) {
197 throw new ModelParseException("Unable to read model: " + e, e.getLineNumber(), e.getColumnNumber(), e);
198 }
199 }
200
201 @Override
202 public org.apache.maven.model.Model read(Reader reader, Map<String, ?> options) throws IOException {
203 Objects.requireNonNull(reader, "reader cannot be null");
204 try (Reader r = reader) {
205 org.apache.maven.api.model.Model model = read(null, null, r, options);
206 return new org.apache.maven.model.Model(model);
207 }
208 }
209 }