1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver.internal.ant.tasks;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.TreeSet;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 import org.apache.tools.ant.BuildException;
31 import org.eclipse.aether.artifact.Artifact;
32
33
34
35 class Layout {
36
37 public static final String GID = "{groupId}";
38
39 public static final String GID_DIRS = "{groupIdDirs}";
40
41 public static final String AID = "{artifactId}";
42
43 public static final String VER = "{version}";
44
45 public static final String BVER = "{baseVersion}";
46
47 public static final String EXT = "{extension}";
48
49 public static final String CLS = "{classifier}";
50
51 private final String[] tokens;
52
53 Layout(String layout) throws BuildException {
54 Collection<String> valid = new HashSet<>(Arrays.asList(GID, GID_DIRS, AID, VER, BVER, EXT, CLS));
55 List<String> tokens = new ArrayList<>();
56 Matcher m = Pattern.compile("(\\{[^}]*\\})|([^{]+)").matcher(layout);
57 while (m.find()) {
58 if (m.group(1) != null && !valid.contains(m.group(1))) {
59 throw new BuildException("Invalid variable '" + m.group() + "' in layout, supported variables are "
60 + new TreeSet<String>(valid));
61 }
62 tokens.add(m.group());
63 }
64 this.tokens = tokens.toArray(new String[0]);
65 }
66
67 public String getPath(Artifact artifact) {
68 StringBuilder buffer = new StringBuilder(128);
69
70 for (int i = 0; i < tokens.length; i++) {
71 String token = tokens[i];
72 if (GID.equals(token)) {
73 buffer.append(artifact.getGroupId());
74 } else if (GID_DIRS.equals(token)) {
75 buffer.append(artifact.getGroupId().replace('.', '/'));
76 } else if (AID.equals(token)) {
77 buffer.append(artifact.getArtifactId());
78 } else if (VER.equals(token)) {
79 buffer.append(artifact.getVersion());
80 } else if (BVER.equals(token)) {
81 buffer.append(artifact.getBaseVersion());
82 } else if (CLS.equals(token)) {
83 if (artifact.getClassifier().length() <= 0) {
84 if (i > 0) {
85 String lt = tokens[i - 1];
86 if (!lt.isEmpty() && "-_".indexOf(lt.charAt(lt.length() - 1)) >= 0) {
87 buffer.setLength(buffer.length() - 1);
88 }
89 }
90 } else {
91 buffer.append(artifact.getClassifier());
92 }
93 } else if (EXT.equals(token)) {
94 buffer.append(artifact.getExtension());
95 } else {
96 buffer.append(token);
97 }
98 }
99
100 return buffer.toString();
101 }
102 }