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.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 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[tokens.size()]);
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.length() > 0 && "-_".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 }