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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 class Layout {
62
63 public static final String GID = "{groupId}";
64
65 public static final String GID_DIRS = "{groupIdDirs}";
66
67 public static final String AID = "{artifactId}";
68
69 public static final String VER = "{version}";
70
71 public static final String BVER = "{baseVersion}";
72
73 public static final String EXT = "{extension}";
74
75 public static final String CLS = "{classifier}";
76
77 private final String[] tokens;
78
79 Layout(String layout) throws BuildException {
80 Collection<String> valid = new HashSet<>(Arrays.asList(GID, GID_DIRS, AID, VER, BVER, EXT, CLS));
81 List<String> tokens = new ArrayList<>();
82 Matcher m = Pattern.compile("(\\{[^}]*\\})|([^{]+)").matcher(layout);
83 while (m.find()) {
84 if (m.group(1) != null && !valid.contains(m.group(1))) {
85 throw new BuildException("Invalid variable '" + m.group() + "' in layout, supported variables are "
86 + new TreeSet<String>(valid));
87 }
88 tokens.add(m.group());
89 }
90 this.tokens = tokens.toArray(new String[0]);
91 }
92
93 public String getPath(Artifact artifact) {
94 StringBuilder buffer = new StringBuilder(128);
95
96 for (int i = 0; i < tokens.length; i++) {
97 String token = tokens[i];
98 if (GID.equals(token)) {
99 buffer.append(artifact.getGroupId());
100 } else if (GID_DIRS.equals(token)) {
101 buffer.append(artifact.getGroupId().replace('.', '/'));
102 } else if (AID.equals(token)) {
103 buffer.append(artifact.getArtifactId());
104 } else if (VER.equals(token)) {
105 buffer.append(artifact.getVersion());
106 } else if (BVER.equals(token)) {
107 buffer.append(artifact.getBaseVersion());
108 } else if (CLS.equals(token)) {
109 if (artifact.getClassifier().length() <= 0) {
110 if (i > 0) {
111 String lt = tokens[i - 1];
112 if (!lt.isEmpty() && "-_".indexOf(lt.charAt(lt.length() - 1)) >= 0) {
113 buffer.setLength(buffer.length() - 1);
114 }
115 }
116 } else {
117 buffer.append(artifact.getClassifier());
118 }
119 } else if (EXT.equals(token)) {
120 buffer.append(artifact.getExtension());
121 } else {
122 buffer.append(token);
123 }
124 }
125
126 return buffer.toString();
127 }
128 }