View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.api.spi;
20  
21  import java.nio.file.Path;
22  import java.util.Map;
23  import java.util.Optional;
24  
25  import org.apache.maven.api.annotations.Consumer;
26  import org.apache.maven.api.annotations.Experimental;
27  import org.apache.maven.api.annotations.Nonnull;
28  import org.apache.maven.api.annotations.Nullable;
29  import org.apache.maven.api.model.Model;
30  import org.apache.maven.api.services.Source;
31  
32  /**
33   * The {@code ModelParser} interface is used to locate and read {@link Model}s from the file system.
34   * This allows plugging in additional syntaxes for the main model read by Maven when building a project.
35   *
36   * @since 4.0.0
37   */
38  @Experimental
39  @Consumer
40  public interface ModelParser extends SpiService {
41  
42      /**
43       * Locates the pom in the given directory.
44       *
45       * @param dir the directory to locate the pom for, never {@code null}
46       * @return a {@code Source} pointing to the located pom or an empty {@code Optional} if none was found by this parser
47       */
48      @Nonnull
49      Optional<Source> locate(@Nonnull Path dir);
50  
51      /**
52       * Parse the model obtained previously by a previous call to {@link #locate(Path)}.
53       *
54       * @param source the source to parse, never {@code null}
55       * @param options possible parsing options, may be {@code null}
56       * @return the parsed {@link Model}, never {@code null}
57       * @throws ModelParserException if the model cannot be parsed
58       */
59      @Nonnull
60      Model parse(@Nonnull Source source, @Nullable Map<String, ?> options) throws ModelParserException;
61  
62      /**
63       * Locate and parse the model in the specified directory.
64       * This is equivalent to {@code locate(dir).map(s -> parse(s, options))}.
65       *
66       * @param dir the directory to locate the pom for, never {@code null}
67       * @param options possible parsing options, may be {@code null}
68       * @return an optional parsed {@link Model} or {@code null} if none could be found
69       * @throws ModelParserException if the located model cannot be parsed
70       */
71      @Nonnull
72      default Optional<Model> locateAndParse(@Nonnull Path dir, @Nullable Map<String, ?> options)
73              throws ModelParserException {
74          return locate(dir).map(s -> parse(s, options));
75      }
76  }