View Javadoc

1   /*
2    * REPOWEB, repository manager.
3    *
4    * Terms of license - http://opensource.org/licenses/apachepl.php
5    */
6   package org.repoweb.model.common;
7   import java.io.File;
8   import org.repoweb.model.Artifact;
9   /***
10   * Abstract class to ease implementation of the Artifact interface.
11   */
12  public abstract class AbstractArtifact implements Artifact {
13      private static final String UNDEFINED = "";
14      private String _groupId;
15      private String _artifactId;
16      private String _type;
17      private String _version;
18      private final File _file;
19  
20      protected AbstractArtifact(File file) throws BadArtifactFileException {
21          _file = file;
22          try {
23              decodeType(file);
24              decodeGroupId(file);
25  
26              String fileName = decodeFileName(file);
27              decodeVersion(fileName);
28              decodeArtifactId(fileName);
29          }
30          catch (BadArtifactFileException error) {
31              throw error;
32          }
33          catch (Throwable e) {
34              throw new BadArtifactFileException("Bad file " + _file, e);
35          }
36      }
37  
38      public String getGroupId() {
39          return _groupId;
40      }
41  
42  
43      public String getArtifactId() {
44          return _artifactId;
45      }
46  
47  
48      public String getId() {
49          if (getGroupId().equals(getArtifactId())) {
50              return getArtifactId();
51          }
52          else {
53              return getGroupId() + ":" + getArtifactId();
54          }
55      }
56  
57  
58      public String getType() {
59          return _type;
60      }
61  
62  
63      public String getVersion() {
64          return _version;
65      }
66  
67  
68      public boolean isOverridden() {
69          final String standardName =
70              getArtifactId() + "-" + getVersion() + "." + getType();
71          return !getFileName().equals(standardName);
72      }
73  
74  
75      public String getFileName() {
76          return _file.getName();
77      }
78  
79  
80      protected File getFile() {
81          return _file;
82      }
83  
84  
85      public String toString() {
86          return "Artifact(" + getId() + "-" + getVersion() + "." + getType() + ")";
87      }
88  
89  
90      private void decodeVersion(String fileName) {
91          final int versionSeparator = findVersionSeparator(fileName);
92          if (versionSeparator != -1) {
93              _version = fileName.substring(versionSeparator + 1);
94          }
95          else {
96              _version = UNDEFINED;
97          }
98      }
99  
100 
101     private int findVersionSeparator(String txt) {
102         final int separator = txt.indexOf('-');
103 
104         if (separator == -1) {
105             return -1;
106         }
107 
108         if (separator == (txt.length() - 1)
109                 || Character.isDigit(txt.charAt(separator + 1))) {
110             return separator;
111         }
112 
113         final int nextSeparator = findVersionSeparator(txt.substring(separator + 1));
114 
115         return nextSeparator + separator + 1;
116     }
117 
118 
119     private void decodeArtifactId(String fileName) {
120         if (!UNDEFINED.equals(_version)) {
121             _artifactId =
122                 fileName.substring(0, fileName.length() - _version.length() - 1);
123         }
124         else if (fileName.endsWith("-")) {
125             _artifactId = fileName.substring(0, fileName.length() - 1);
126         }
127         else {
128             _artifactId = fileName;
129         }
130     }
131 
132 
133     private String decodeFileName(File file) {
134         final String fullFileName = file.getName();
135 
136         final int dotIndex = fullFileName.lastIndexOf('.');
137         if (dotIndex != -1) {
138             return fullFileName.substring(0, dotIndex);
139         }
140         else {
141             return fullFileName;
142         }
143     }
144 
145 
146     private void decodeGroupId(File file) {
147         _groupId = file.getParentFile().getParentFile().getName();
148     }
149 
150 
151     private void decodeType(File file) throws BadArtifactFileException {
152         _type = file.getParentFile().getName();
153 
154         if (!_type.endsWith("s")) {
155             throw new BadArtifactFileException(
156                 "Type folder should finish with 's' ('jars')");
157         }
158 
159         _type = _type.substring(0, _type.length() - 1);
160     }
161 
162     /***
163      * Signal a bad artifact file name.
164      */
165     public class BadArtifactFileException extends Exception {
166         public BadArtifactFileException(String msg) {
167             super(msg);
168         }
169 
170 
171         public BadArtifactFileException(String msg, Throwable rootCause) {
172             super(msg, rootCause);
173         }
174     }
175 }