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.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.util.Objects;
27  
28  class PathSource implements ModelSource {
29  
30      private final Path path;
31      private final String location;
32  
33      PathSource(Path path) {
34          this(path, null);
35      }
36  
37      PathSource(Path path, String location) {
38          this.path = path;
39          this.location = location != null ? location : path.toString();
40      }
41  
42      @Override
43      public Path getPath() {
44          return path;
45      }
46  
47      @Override
48      public InputStream openStream() throws IOException {
49          return Files.newInputStream(path);
50      }
51  
52      @Override
53      public String getLocation() {
54          return location;
55      }
56  
57      @Override
58      public Source resolve(String relative) {
59          return new PathSource(path.resolve(relative));
60      }
61  
62      @Override
63      public ModelSource resolve(ModelLocator locator, String relative) {
64          String norm = relative.replace('\\', File.separatorChar).replace('/', File.separatorChar);
65          Path path = getPath().getParent().resolve(norm);
66          Path relatedPom = locator.locateExistingPom(path);
67          if (relatedPom != null) {
68              return new PathSource(relatedPom.normalize(), null);
69          }
70          return null;
71      }
72  
73      @Override
74      public boolean equals(Object o) {
75          return this == o || o instanceof PathSource ps && Objects.equals(path, ps.path);
76      }
77  
78      @Override
79      public int hashCode() {
80          return Objects.hash(path);
81      }
82  
83      @Override
84      public String toString() {
85          return "PathSource[" + "location='" + location + '\'' + ", " + "path=" + path + ']';
86      }
87  }