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  
29  /**
30   * Default implementation of {@link LocalPathComposer}.
31   *
32   * @since 1.8.1
33   */
34  @Singleton
35  @Named
36  public final class DefaultLocalPathComposer implements LocalPathComposer {
37      @Override
38      public String getPathForArtifact(Artifact artifact, boolean local) {
39          requireNonNull(artifact);
40  
41          StringBuilder path = new StringBuilder(128);
42  
43          path.append(artifact.getGroupId().replace('.', '/')).append('/');
44  
45          path.append(artifact.getArtifactId()).append('/');
46  
47          path.append(artifact.getBaseVersion()).append('/');
48  
49          path.append(artifact.getArtifactId()).append('-');
50          if (local) {
51              path.append(artifact.getBaseVersion());
52          } else {
53              path.append(artifact.getVersion());
54          }
55  
56          if (artifact.getClassifier().length() > 0) {
57              path.append('-').append(artifact.getClassifier());
58          }
59  
60          if (artifact.getExtension().length() > 0) {
61              path.append('.').append(artifact.getExtension());
62          }
63  
64          return path.toString();
65      }
66  
67      @Override
68      public String getPathForMetadata(Metadata metadata, String repositoryKey) {
69          requireNonNull(metadata);
70          requireNonNull(repositoryKey);
71  
72          StringBuilder path = new StringBuilder(128);
73  
74          if (metadata.getGroupId().length() > 0) {
75              path.append(metadata.getGroupId().replace('.', '/')).append('/');
76  
77              if (metadata.getArtifactId().length() > 0) {
78                  path.append(metadata.getArtifactId()).append('/');
79  
80                  if (metadata.getVersion().length() > 0) {
81                      path.append(metadata.getVersion()).append('/');
82                  }
83              }
84          }
85  
86          path.append(insertRepositoryKey(metadata.getType(), repositoryKey));
87  
88          return path.toString();
89      }
90  
91      private String insertRepositoryKey(String metadataType, String repositoryKey) {
92          String result;
93          int idx = metadataType.indexOf('.');
94          if (idx < 0) {
95              result = metadataType + '-' + repositoryKey;
96          } else {
97              result = metadataType.substring(0, idx) + '-' + repositoryKey + metadataType.substring(idx);
98          }
99          return result;
100     }
101 }