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