1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.normalize();
39 this.location = location != null ? location : this.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);
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 }