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.eclipse.aether.internal.impl;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import org.eclipse.aether.artifact.Artifact;
25  import org.eclipse.aether.metadata.Metadata;
26  
27  import static java.util.Objects.requireNonNull;
28  import static org.eclipse.aether.util.PathUtils.validateArtifactComponents;
29  import static org.eclipse.aether.util.PathUtils.validateMetadataComponents;
30  
31  /**
32   * Default implementation of {@link LocalPathComposer}.
33   *
34   * @since 1.8.1
35   */
36  @Singleton
37  @Named
38  public final class DefaultLocalPathComposer implements LocalPathComposer {
39      @Override
40      public String getPathForArtifact(Artifact artifact, boolean local) {
41          requireNonNull(artifact);
42          validateArtifactComponents(artifact);
43  
44          StringBuilder path = new StringBuilder(128);
45  
46          path.append(artifact.getGroupId().replace('.', '/')).append('/');
47  
48          path.append(artifact.getArtifactId()).append('/');
49  
50          path.append(artifact.getBaseVersion()).append('/');
51  
52          path.append(artifact.getArtifactId()).append('-');
53          if (local) {
54              path.append(artifact.getBaseVersion());
55          } else {
56              path.append(artifact.getVersion());
57          }
58  
59          if (!artifact.getClassifier().isEmpty()) {
60              path.append('-').append(artifact.getClassifier());
61          }
62  
63          if (!artifact.getExtension().isEmpty()) {
64              path.append('.').append(artifact.getExtension());
65          }
66  
67          return path.toString();
68      }
69  
70      @Override
71      public String getPathForMetadata(Metadata metadata, String repositoryKey) {
72          requireNonNull(metadata);
73          requireNonNull(repositoryKey);
74          validateMetadataComponents(metadata);
75  
76          StringBuilder path = new StringBuilder(128);
77  
78          if (!metadata.getGroupId().isEmpty()) {
79              path.append(metadata.getGroupId().replace('.', '/')).append('/');
80  
81              if (!metadata.getArtifactId().isEmpty()) {
82                  path.append(metadata.getArtifactId()).append('/');
83  
84                  if (!metadata.getVersion().isEmpty()) {
85                      path.append(metadata.getVersion()).append('/');
86                  }
87              }
88          }
89  
90          path.append(insertRepositoryKey(metadata.getType(), repositoryKey));
91  
92          return path.toString();
93      }
94  
95      private String insertRepositoryKey(String metadataType, String repositoryKey) {
96          if (metadataType.contains("/") && !metadataType.endsWith("/")) {
97              int lastSlash = metadataType.lastIndexOf('/');
98              return metadataType.substring(0, lastSlash + 1)
99                      + insertRepositoryKey(metadataType.substring(lastSlash + 1), repositoryKey);
100         } else {
101             String result;
102             int idx = metadataType.indexOf('.');
103             if (idx < 0) {
104                 result = metadataType + '-' + repositoryKey;
105             } else {
106                 result = metadataType.substring(0, idx) + '-' + repositoryKey + metadataType.substring(idx);
107             }
108             return result;
109         }
110     }
111 }