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.services;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.file.Path;
24  
25  import org.apache.maven.api.Session;
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  
30  import static org.apache.maven.api.services.BaseRequest.nonNull;
31  
32  /**
33   * Provides access to the contents of a source independently of the
34   * backing store (e.g. file system, database, memory).
35   * <p>
36   * This is mainly used to parse files into objects such as
37   * {@link org.apache.maven.api.Project},
38   * {@link org.apache.maven.api.model.Model},
39   * {@link org.apache.maven.api.settings.Settings}, or
40   * {@link org.apache.maven.api.toolchain.PersistedToolchains}.
41   *
42   * @since 4.0.0
43   * @see org.apache.maven.api.services.ProjectBuilder#build(Session, Source)
44   * @see org.apache.maven.api.services.SettingsBuilder#build(Session, Source, Source, Source)
45   * @see org.apache.maven.api.services.ToolchainsBuilder#build(Session, Source, Source)
46   */
47  @Experimental
48  public interface Source {
49  
50      /**
51       * Provides access the file to be parsed, if this source is backed by a file.
52       *
53       * @return the underlying {@code Path}, or {@code null} if this source is not backed by a file
54       */
55      @Nullable
56      Path getPath();
57  
58      /**
59       * Creates a new byte stream to the source contents.
60       * Closing the returned stream is the responsibility of the caller.
61       *
62       * @return a byte stream to the source contents, never {@code null}
63       * @throws IOException in case of IO issue
64       */
65      @Nonnull
66      InputStream openStream() throws IOException;
67  
68      /**
69       * Provides a user-friendly hint about the location of the source.
70       * This could be a local file path, a URI or just an empty string.
71       * The intention is to assist users during error reporting.
72       *
73       * @return a user-friendly hint about the location of the source, never {@code null}
74       */
75      @Nonnull
76      String getLocation();
77  
78      /**
79       * Returns a new source identified by a relative path. Implementation <strong>MUST</strong>
80       * be able to accept <code>relative</code> parameter values that
81       * <ul>
82       * <li>use either / or \ file path separator,</li>
83       * <li>have .. parent directory references,</li>
84       * <li>point either at file or directory.</li>
85       * </ul>
86       *
87       * @param relative is the path of the requested source relative to this source
88       * @return related source or <code>null</code> if no such source
89       */
90      @Nullable
91      Source resolve(@Nonnull String relative);
92  
93      /**
94       * Creates a Source for the following Path
95       */
96      @Nonnull
97      static Source fromPath(@Nonnull Path path) {
98          return new PathSource(nonNull(path, "path cannot be null"));
99      }
100 }