1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cling.invoker;
20
21 import java.io.IOException;
22 import java.nio.file.Path;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.Properties;
28 import java.util.ServiceLoader;
29 import java.util.function.Function;
30
31 import org.apache.maven.api.annotations.Nonnull;
32 import org.apache.maven.api.annotations.Nullable;
33 import org.apache.maven.api.services.model.RootLocator;
34 import org.apache.maven.cling.logging.Slf4jConfiguration;
35 import org.apache.maven.execution.MavenExecutionRequest;
36 import org.codehaus.plexus.interpolation.AbstractValueSource;
37 import org.codehaus.plexus.interpolation.BasicInterpolator;
38 import org.codehaus.plexus.interpolation.StringSearchInterpolator;
39 import org.codehaus.plexus.logging.Logger;
40
41 import static java.util.Objects.requireNonNull;
42
43
44
45
46 public final class Utils {
47 private Utils() {}
48
49 @Nonnull
50 public static String stripLeadingAndTrailingQuotes(String str) {
51 requireNonNull(str, "str");
52 final int length = str.length();
53 if (length > 1
54 && str.startsWith("\"")
55 && str.endsWith("\"")
56 && str.substring(1, length - 1).indexOf('"') == -1) {
57 str = str.substring(1, length - 1);
58 }
59 return str;
60 }
61
62 @Nonnull
63 public static Path getCanonicalPath(Path path) {
64 requireNonNull(path, "path");
65 try {
66 return path.toRealPath();
67 } catch (IOException e) {
68 return getCanonicalPath(path.getParent()).resolve(path.getFileName());
69 }
70 }
71
72 @Nonnull
73 public static Map<String, String> toMap(Properties properties) {
74 requireNonNull(properties, "properties");
75 HashMap<String, String> map = new HashMap<>();
76 for (String key : properties.stringPropertyNames()) {
77 map.put(key, properties.getProperty(key));
78 }
79 return map;
80 }
81
82 @Nonnull
83 public static Properties toProperties(Map<String, String> properties) {
84 requireNonNull(properties, "properties");
85 Properties map = new Properties();
86 for (String key : properties.keySet()) {
87 map.put(key, properties.get(key));
88 }
89 return map;
90 }
91
92 @Nonnull
93 public static BasicInterpolator createInterpolator(Collection<Map<String, String>> properties) {
94 StringSearchInterpolator interpolator = new StringSearchInterpolator();
95 interpolator.addValueSource(new AbstractValueSource(false) {
96 @Override
97 public Object getValue(String expression) {
98 for (Map<String, String> props : properties) {
99 String val = props.get(expression);
100 if (val != null) {
101 return val;
102 }
103 }
104 return null;
105 }
106 });
107 return interpolator;
108 }
109
110 @Nonnull
111 public static Function<String, String> prefix(String prefix, Function<String, String> cb) {
112 return s -> {
113 String v = null;
114 if (s.startsWith(prefix)) {
115 v = cb.apply(s.substring(prefix.length()));
116 }
117 return v;
118 };
119 }
120
121 @SafeVarargs
122 @Nonnull
123 public static Function<String, String> or(Function<String, String>... callbacks) {
124 return s -> {
125 for (Function<String, String> cb : callbacks) {
126 String r = cb.apply(s);
127 if (r != null) {
128 return r;
129 }
130 }
131 return null;
132 };
133 }
134
135 public static int toMavenExecutionRequestLoggingLevel(Slf4jConfiguration.Level level) {
136 requireNonNull(level, "level");
137 return switch (level) {
138 case DEBUG -> MavenExecutionRequest.LOGGING_LEVEL_DEBUG;
139 case INFO -> MavenExecutionRequest.LOGGING_LEVEL_INFO;
140 case ERROR -> MavenExecutionRequest.LOGGING_LEVEL_ERROR;
141 };
142 }
143
144 public static int toPlexusLoggingLevel(Slf4jConfiguration.Level level) {
145 requireNonNull(level, "level");
146 return switch (level) {
147 case DEBUG -> Logger.LEVEL_DEBUG;
148 case INFO -> Logger.LEVEL_INFO;
149 case ERROR -> Logger.LEVEL_ERROR;
150 };
151 }
152
153 @Nullable
154 public static Path findRoot(Path topDirectory) {
155 requireNonNull(topDirectory, "topDirectory");
156 Path rootDirectory =
157 ServiceLoader.load(RootLocator.class).iterator().next().findRoot(topDirectory);
158 if (rootDirectory != null) {
159 return getCanonicalPath(rootDirectory);
160 }
161 return null;
162 }
163
164 @Nonnull
165 public static Path findMandatoryRoot(Path topDirectory) {
166 requireNonNull(topDirectory, "topDirectory");
167 return getCanonicalPath(Optional.ofNullable(
168 ServiceLoader.load(RootLocator.class).iterator().next().findMandatoryRoot(topDirectory))
169 .orElseThrow());
170 }
171 }