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