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.index.artifact;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  /**
28   * An M1 <code>GavCalculator</code>. Heavily under-maintained.
29   *
30   * @author Jason van Zyl
31   * @author Tamas Cservenak
32   * @deprecated Maven1 support to be dropped.
33   */
34  @Singleton
35  @Named("maven1")
36  @Deprecated
37  public class M1GavCalculator implements GavCalculator {
38  
39      private static final Pattern PAT1 = Pattern.compile("^([^0-9]+)-([0-9].+)\\.([^0-9]+)(\\.md5|\\.sha1){0,1}$");
40  
41      private static final Pattern PAT2 = Pattern.compile("^([a-z0-9-_]+)-([0-9-].+)\\.([^0-9]+)(\\.md5|\\.sha1){0,1}$");
42  
43      public Gav pathToGav(String str) {
44          try {
45              String s = str.startsWith("/") ? str.substring(1) : str;
46  
47              int n1 = s.lastIndexOf('/');
48  
49              if (n1 == -1) {
50                  return null;
51              }
52  
53              int n2 = s.lastIndexOf('/', n1 - 1);
54  
55              if (n2 == -1) {
56                  return null;
57              }
58  
59              String g = s.substring(0, n2).replace('/', '.');
60              String middle = s.substring(n2 + 1, n1);
61              String n = s.substring(n1 + 1);
62  
63              String classifier = null;
64              if ("java-sources".equals(middle)) {
65                  classifier = "sources";
66              } else if ("javadocs".equals(middle)) {
67                  classifier = "javadoc";
68              } else if ("ejbs".equals(middle)
69                      && (n.endsWith("client.jar") || n.endsWith("client.jar.sha1") || n.endsWith("client.jar.md5"))) {
70                  classifier = "client";
71              }
72  
73              boolean checksum = false;
74              Gav.HashType checksumType = null;
75              if (s.endsWith(".md5")) {
76                  checksum = true;
77                  checksumType = Gav.HashType.md5;
78                  s = s.substring(0, s.length() - 4);
79              } else if (s.endsWith(".sha1")) {
80                  checksum = true;
81                  checksumType = Gav.HashType.sha1;
82                  s = s.substring(0, s.length() - 5);
83              }
84  
85              if (s.endsWith("maven-metadata.xml")) {
86                  return null;
87              }
88  
89              String ext = s.substring(s.lastIndexOf('.') + 1);
90  
91              Matcher m = PAT1.matcher(n);
92              if (m.matches()) {
93                  String a = m.group(1);
94                  String version = m.group(2);
95                  if (classifier != null) {
96                      version = version.substring(0, version.length() - (classifier.length() + 1));
97                  }
98  
99                  return new Gav(g, a, version, classifier, ext, null, null, n, checksum, checksumType, false, null);
100             } else {
101                 m = PAT2.matcher(n);
102                 if (m.matches()) {
103                     String a = m.group(1);
104                     String version = m.group(2);
105                     if (classifier != null) {
106                         version = version.substring(0, version.length() - (classifier.length() + 1));
107                     }
108 
109                     return new Gav(g, a, version, classifier, ext, null, null, n, checksum, checksumType, false, null);
110                 } else {
111                     return null;
112                 }
113             }
114         } catch (IndexOutOfBoundsException e) {
115             return null;
116         }
117     }
118 
119     /**
120      * // XXX this is not accurate, m1 is using packaging as an artifact folder name.
121      */
122     public String gavToPath(Gav gav) {
123         StringBuilder path = new StringBuilder("/");
124 
125         path.append(gav.getGroupId());
126 
127         path.append("/");
128 
129         if (gav.getClassifier() == null) {
130             path.append(gav.getExtension());
131         } else {
132             if (gav.getClassifier().startsWith("source")) {
133                 path.append("java-source");
134             } else if (gav.getClassifier().startsWith("client")) {
135                 path.append("ejb");
136             } else {
137                 path.append(gav.getClassifier());
138             }
139         }
140         path.append("s");
141 
142         path.append("/");
143 
144         path.append(gav.getArtifactId());
145 
146         path.append("-");
147 
148         path.append(gav.getVersion());
149 
150         if (gav.getClassifier() != null) {
151             path.append("-");
152 
153             path.append(gav.getClassifier());
154         }
155 
156         path.append(".");
157 
158         path.append(gav.getExtension());
159 
160         if (gav.isHash()) {
161             path.append(".");
162 
163             path.append(gav.getHashType().toString());
164         }
165 
166         return path.toString();
167     }
168 }