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 * Option that can be specified in the options map. The value should be a Boolean.
44 */
45 String STRICT = "strict";
46
47 /**
48 * Locates the pom in the given directory.
49 *
50 * @param dir the directory to locate the pom for, never {@code null}
51 * @return a {@code Source} pointing to the located pom or an empty {@code Optional} if none was found by this parser
52 */
53 @Nonnull
54 Optional<Source> locate(@Nonnull Path dir);
55
56 /**
57 * Parse the model obtained previously by a previous call to {@link #locate(Path)}.
58 *
59 * @param source the source to parse, never {@code null}
60 * @param options possible parsing options, may be {@code null}
61 * @return the parsed {@link Model}, never {@code null}
62 * @throws ModelParserException if the model cannot be parsed
63 */
64 @Nonnull
65 Model parse(@Nonnull Source source, @Nullable Map<String, ?> options) throws ModelParserException;
66
67 /**
68 * Locate and parse the model in the specified directory.
69 * This is equivalent to {@code locate(dir).map(s -> parse(s, options))}.
70 *
71 * @param dir the directory to locate the pom for, never {@code null}
72 * @param options possible parsing options, may be {@code null}
73 * @return an optional parsed {@link Model} or {@code null} if none could be found
74 * @throws ModelParserException if the located model cannot be parsed
75 */
76 @Nonnull
77 default Optional<Model> locateAndParse(@Nonnull Path dir, @Nullable Map<String, ?> options)
78 throws ModelParserException {
79 return locate(dir).map(s -> parse(s, options));
80 }
81 }