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