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.di.Named;
30  import org.apache.maven.api.model.Model;
31  import org.apache.maven.api.services.Source;
32  
33  /**
34   * The {@code ModelParser} interface is used to locate and read {@link Model}s from the file system.
35   * This allows plugging in additional syntaxes for the main model read by Maven when building a project.
36   *
37   * @since 4.0.0
38   */
39  @Experimental
40  @Consumer
41  @Named
42  public interface ModelParser extends SpiService {
43  
44      /**
45       * Option that can be specified in the options map.  The value should be a Boolean.
46       */
47      String STRICT = "strict";
48  
49      /**
50       * Locates the pom in the given directory.
51       *
52       * @param dir the directory to locate the pom for, never {@code null}
53       * @return a {@code Source} pointing to the located pom or an empty {@code Optional} if none was found by this parser
54       */
55      @Nonnull
56      Optional<Source> locate(@Nonnull Path dir);
57  
58      /**
59       * Parse the model obtained previously by a previous call to {@link #locate(Path)}.
60       *
61       * @param source the source to parse, never {@code null}
62       * @param options possible parsing options, may be {@code null}
63       * @return the parsed {@link Model}, never {@code null}
64       * @throws ModelParserException if the model cannot be parsed
65       */
66      @Nonnull
67      Model parse(@Nonnull Source source, @Nullable Map<String, ?> options) throws ModelParserException;
68  
69      /**
70       * Locate and parse the model in the specified directory.
71       * This is equivalent to {@code locate(dir).map(s -> parse(s, options))}.
72       *
73       * @param dir the directory to locate the pom for, never {@code null}
74       * @param options possible parsing options, may be {@code null}
75       * @return an optional parsed {@link Model} or {@code null} if none could be found
76       * @throws ModelParserException if the located model cannot be parsed
77       */
78      @Nonnull
79      default Optional<Model> locateAndParse(@Nonnull Path dir, @Nullable Map<String, ?> options)
80              throws ModelParserException {
81          return locate(dir).map(s -> parse(s, options));
82      }
83  }