View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import org.eclipse.aether.artifact.Artifact;
26  import org.eclipse.aether.metadata.Metadata;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * Default implementation of {@link LocalPathComposer}.
32   *
33   * @since 1.8.1
34   */
35  @Singleton
36  @Named
37  public final class DefaultLocalPathComposer implements LocalPathComposer
38  {
39      @Override
40      public String getPathForArtifact( Artifact artifact, boolean local )
41      {
42          requireNonNull( 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          {
55              path.append( artifact.getBaseVersion() );
56          }
57          else
58          {
59              path.append( artifact.getVersion() );
60          }
61  
62          if ( artifact.getClassifier().length() > 0 )
63          {
64              path.append( '-' ).append( artifact.getClassifier() );
65          }
66  
67          if ( artifact.getExtension().length() > 0 )
68          {
69              path.append( '.' ).append( artifact.getExtension() );
70          }
71  
72          return path.toString();
73      }
74  
75      @Override
76      public String getPathForMetadata( Metadata metadata, String repositoryKey )
77      {
78          requireNonNull( metadata );
79          requireNonNull( repositoryKey );
80  
81          StringBuilder path = new StringBuilder( 128 );
82  
83          if ( metadata.getGroupId().length() > 0 )
84          {
85              path.append( metadata.getGroupId().replace( '.', '/' ) ).append( '/' );
86  
87              if ( metadata.getArtifactId().length() > 0 )
88              {
89                  path.append( metadata.getArtifactId() ).append( '/' );
90  
91                  if ( metadata.getVersion().length() > 0 )
92                  {
93                      path.append( metadata.getVersion() ).append( '/' );
94                  }
95              }
96          }
97  
98          path.append( insertRepositoryKey( metadata.getType(), repositoryKey ) );
99  
100         return path.toString();
101     }
102 
103     private String insertRepositoryKey( String metadataType, String repositoryKey )
104     {
105         String result;
106         int idx = metadataType.indexOf( '.' );
107         if ( idx < 0 )
108         {
109             result = metadataType + '-' + repositoryKey;
110         }
111         else
112         {
113             result = metadataType.substring( 0, idx ) + '-' + repositoryKey + metadataType.substring( idx );
114         }
115         return result;
116     }
117 }