gnodet commented on code in PR #1197:
URL: https://github.com/apache/maven/pull/1197#discussion_r1253112641
##########
maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProcessor.java:
##########
@@ -63,32 +75,110 @@
@Typed(ModelProcessor.class)
public class DefaultModelProcessor implements ModelProcessor {
- private final ModelLocator locator;
- private final ModelReader reader;
+ private final Collection<ModelParser> modelParsers;
+ private final ModelLocator modelLocator;
+ private final ModelReader modelReader;
@Inject
- public DefaultModelProcessor(ModelLocator locator, ModelReader reader) {
- this.locator = locator;
- this.reader = reader;
+ public DefaultModelProcessor(
+ Collection<ModelParser> modelParsers, ModelLocator modelLocator,
ModelReader modelReader) {
+ this.modelParsers = modelParsers;
+ this.modelLocator = modelLocator;
+ this.modelReader = modelReader;
}
@Override
public File locatePom(File projectDirectory) {
- return locator.locatePom(projectDirectory);
+ return locatePom(projectDirectory.toPath()).toFile();
+ }
+
+ public Path locatePom(Path projectDirectory) {
+ // Note that the ModelProcessor#locatePom never returns null
+ // while the ModelParser#locatePom needs to return an existing path !
+ Path pom = modelParsers.stream()
+ .map(m -> m.locatePom(projectDirectory))
+ .filter(Objects::nonNull)
+ .filter(Files::exists)
+ .findFirst()
+ .orElseGet(
+ () ->
modelLocator.locatePom(projectDirectory.toFile()).toPath());
+ if (!pom.getParent().equals(projectDirectory)) {
+ throw new IllegalArgumentException("The POM found does not belong
to the given directory: " + pom);
+ }
+ return pom;
+ }
+
+ protected org.apache.maven.api.model.Model read(
+ Path pomFile, InputStream input, Reader reader, Map<String, ?>
options) throws IOException {
+ Source source = (Source) options.get(ModelProcessor.SOURCE);
+ if (pomFile == null && source != null) {
+ Path p = Paths.get(source.getLocation());
+ if (Files.exists(p)) {
+ pomFile = p;
+ }
+ }
+ if (pomFile != null) {
+ Path projectDirectory = pomFile.getParent();
+ Path path = pomFile;
+ List<ModelParser> parsers = modelParsers.stream()
+ .filter(p -> path.equals(p.locatePom(projectDirectory)))
+ .collect(Collectors.toList());
+ List<ModelParserException> exceptions = new ArrayList<>();
+ for (ModelParser parser : parsers) {
+ try {
+ Model model = parser.parse(new FileSource(pomFile),
options);
+ return model.withPomFile(pomFile);
+ } catch (ModelParserException e) {
+ exceptions.add(e);
+ }
+ }
+ try {
+ return readXmlModel(pomFile, null, null, options);
+ } catch (IOException e) {
+ exceptions.forEach(e::addSuppressed);
+ throw e;
+ }
+ } else {
+ return readXmlModel(pomFile, input, reader, options);
+ }
+ }
+
+ private org.apache.maven.api.model.Model readXmlModel(
+ Path pomFile, InputStream input, Reader reader, Map<String, ?>
options) throws IOException {
+ if (pomFile != null) {
+ return modelReader.read(pomFile.toFile(), options).getDelegate();
+ } else if (input != null) {
+ return modelReader.read(input, options).getDelegate();
+ } else {
+ return modelReader.read(reader, options).getDelegate();
+ }
}
@Override
- public Model read(File input, Map<String, ?> options) throws IOException {
- return reader.read(input, options);
+ public org.apache.maven.model.Model read(File file, Map<String, ?>
options) throws IOException {
+ Objects.requireNonNull(file, "file cannot be null");
+ Path path = file.toPath();
+ org.apache.maven.api.model.Model model = read(path, null, null,
options);
+ return new org.apache.maven.model.Model(model);
}
@Override
- public Model read(Reader input, Map<String, ?> options) throws IOException
{
- return reader.read(input, options);
+ public org.apache.maven.model.Model read(InputStream input, Map<String, ?>
options) throws IOException {
+ Objects.requireNonNull(input, "input cannot be null");
+ try (InputStream in = input) {
+ org.apache.maven.api.model.Model model = read(null, in, null,
options);
+ return new org.apache.maven.model.Model(model);
+ } catch (ModelParserException e) {
Review Comment:
I agree, but that's always the problem to bridge between the v3 and v4 apis.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]