1 package org.eclipse.aether.util.artifact;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.Map;
24
25 import org.eclipse.aether.artifact.AbstractArtifact;
26 import org.eclipse.aether.artifact.Artifact;
27
28
29
30
31
32 public final class SubArtifact
33 extends AbstractArtifact
34 {
35
36 private final Artifact mainArtifact;
37
38 private final String classifier;
39
40 private final String extension;
41
42 private final File file;
43
44 private final Map<String, String> properties;
45
46
47
48
49
50
51
52
53
54
55
56 public SubArtifact( Artifact mainArtifact, String classifier, String extension )
57 {
58 this( mainArtifact, classifier, extension, (File) null );
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72 public SubArtifact( Artifact mainArtifact, String classifier, String extension, File file )
73 {
74 this( mainArtifact, classifier, extension, null, file );
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88 public SubArtifact( Artifact mainArtifact, String classifier, String extension, Map<String, String> properties )
89 {
90 this( mainArtifact, classifier, extension, properties, null );
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public SubArtifact( Artifact mainArtifact, String classifier, String extension, Map<String, String> properties,
106 File file )
107 {
108 if ( mainArtifact == null )
109 {
110 throw new IllegalArgumentException( "no artifact specified" );
111 }
112 this.mainArtifact = mainArtifact;
113 this.classifier = classifier;
114 this.extension = extension;
115 this.file = file;
116 this.properties = copyProperties( properties );
117 }
118
119 private SubArtifact( Artifact mainArtifact, String classifier, String extension, File file,
120 Map<String, String> properties )
121 {
122
123 this.mainArtifact = mainArtifact;
124 this.classifier = classifier;
125 this.extension = extension;
126 this.file = file;
127 this.properties = properties;
128 }
129
130 public String getGroupId()
131 {
132 return mainArtifact.getGroupId();
133 }
134
135 public String getArtifactId()
136 {
137 return mainArtifact.getArtifactId();
138 }
139
140 public String getVersion()
141 {
142 return mainArtifact.getVersion();
143 }
144
145 public String getBaseVersion()
146 {
147 return mainArtifact.getBaseVersion();
148 }
149
150 public boolean isSnapshot()
151 {
152 return mainArtifact.isSnapshot();
153 }
154
155 public String getClassifier()
156 {
157 return expand( classifier, mainArtifact.getClassifier() );
158 }
159
160 public String getExtension()
161 {
162 return expand( extension, mainArtifact.getExtension() );
163 }
164
165 public File getFile()
166 {
167 return file;
168 }
169
170 public Artifact setFile( File file )
171 {
172 if ( ( this.file == null ) ? file == null : this.file.equals( file ) )
173 {
174 return this;
175 }
176 return new SubArtifact( mainArtifact, classifier, extension, file, properties );
177 }
178
179 public Map<String, String> getProperties()
180 {
181 return properties;
182 }
183
184 public Artifact setProperties( Map<String, String> properties )
185 {
186 if ( this.properties.equals( properties ) || ( properties == null && this.properties.isEmpty() ) )
187 {
188 return this;
189 }
190 return new SubArtifact( mainArtifact, classifier, extension, properties, file );
191 }
192
193 private static String expand( String pattern, String replacement )
194 {
195 String result = "";
196 if ( pattern != null )
197 {
198 result = pattern.replace( "*", replacement );
199
200 if ( replacement.length() <= 0 )
201 {
202 if ( pattern.startsWith( "*" ) )
203 {
204 int i = 0;
205 for ( ; i < result.length(); i++ )
206 {
207 char c = result.charAt( i );
208 if ( c != '-' && c != '.' )
209 {
210 break;
211 }
212 }
213 result = result.substring( i );
214 }
215 if ( pattern.endsWith( "*" ) )
216 {
217 int i = result.length() - 1;
218 for ( ; i >= 0; i-- )
219 {
220 char c = result.charAt( i );
221 if ( c != '-' && c != '.' )
222 {
223 break;
224 }
225 }
226 result = result.substring( 0, i + 1 );
227 }
228 }
229 }
230 return result;
231 }
232
233 }