1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.aether.artifact;
12
13 import java.io.File;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20
21
22
23
24 public final class DefaultArtifact
25 extends AbstractArtifact
26 {
27
28 private final String groupId;
29
30 private final String artifactId;
31
32 private final String version;
33
34 private final String classifier;
35
36 private final String extension;
37
38 private final File file;
39
40 private final Map<String, String> properties;
41
42
43
44
45
46
47
48
49 public DefaultArtifact( String coords )
50 {
51 this( coords, Collections.<String, String> emptyMap() );
52 }
53
54
55
56
57
58
59
60
61
62 public DefaultArtifact( String coords, Map<String, String> properties )
63 {
64 Pattern p = Pattern.compile( "([^: ]+):([^: ]+)(:([^: ]*)(:([^: ]+))?)?:([^: ]+)" );
65 Matcher m = p.matcher( coords );
66 if ( !m.matches() )
67 {
68 throw new IllegalArgumentException( "Bad artifact coordinates " + coords
69 + ", expected format is <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>" );
70 }
71 groupId = m.group( 1 );
72 artifactId = m.group( 2 );
73 extension = get( m.group( 4 ), "jar" );
74 classifier = get( m.group( 6 ), "" );
75 version = m.group( 7 );
76 file = null;
77 this.properties = copyProperties( properties );
78 }
79
80 private static String get( String value, String defaultValue )
81 {
82 return ( value == null || value.length() <= 0 ) ? defaultValue : value;
83 }
84
85
86
87
88
89
90
91
92
93
94 public DefaultArtifact( String groupId, String artifactId, String extension, String version )
95 {
96 this( groupId, artifactId, "", extension, version );
97 }
98
99
100
101
102
103
104
105
106
107
108
109 public DefaultArtifact( String groupId, String artifactId, String classifier, String extension, String version )
110 {
111 this( groupId, artifactId, classifier, extension, version, null, (File) null );
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public DefaultArtifact( String groupId, String artifactId, String classifier, String extension, String version,
128 ArtifactType type )
129 {
130 this( groupId, artifactId, classifier, extension, version, null, type );
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public DefaultArtifact( String groupId, String artifactId, String classifier, String extension, String version,
149 Map<String, String> properties, ArtifactType type )
150 {
151 this.groupId = emptify( groupId );
152 this.artifactId = emptify( artifactId );
153 if ( classifier != null || type == null )
154 {
155 this.classifier = emptify( classifier );
156 }
157 else
158 {
159 this.classifier = emptify( type.getClassifier() );
160 }
161 if ( extension != null || type == null )
162 {
163 this.extension = emptify( extension );
164 }
165 else
166 {
167 this.extension = emptify( type.getExtension() );
168 }
169 this.version = emptify( version );
170 this.file = null;
171 this.properties = merge( properties, ( type != null ) ? type.getProperties() : null );
172 }
173
174 private static Map<String, String> merge( Map<String, String> dominant, Map<String, String> recessive )
175 {
176 Map<String, String> properties;
177
178 if ( ( dominant == null || dominant.isEmpty() ) && ( recessive == null || recessive.isEmpty() ) )
179 {
180 properties = Collections.emptyMap();
181 }
182 else
183 {
184 properties = new HashMap<String, String>();
185 if ( recessive != null )
186 {
187 properties.putAll( recessive );
188 }
189 if ( dominant != null )
190 {
191 properties.putAll( dominant );
192 }
193 properties = Collections.unmodifiableMap( properties );
194 }
195
196 return properties;
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public DefaultArtifact( String groupId, String artifactId, String classifier, String extension, String version,
212 Map<String, String> properties, File file )
213 {
214 this.groupId = emptify( groupId );
215 this.artifactId = emptify( artifactId );
216 this.classifier = emptify( classifier );
217 this.extension = emptify( extension );
218 this.version = emptify( version );
219 this.file = file;
220 this.properties = copyProperties( properties );
221 }
222
223 DefaultArtifact( String groupId, String artifactId, String classifier, String extension, String version, File file,
224 Map<String, String> properties )
225 {
226
227 this.groupId = emptify( groupId );
228 this.artifactId = emptify( artifactId );
229 this.classifier = emptify( classifier );
230 this.extension = emptify( extension );
231 this.version = emptify( version );
232 this.file = file;
233 this.properties = properties;
234 }
235
236 private static String emptify( String str )
237 {
238 return ( str == null ) ? "" : str;
239 }
240
241 public String getGroupId()
242 {
243 return groupId;
244 }
245
246 public String getArtifactId()
247 {
248 return artifactId;
249 }
250
251 public String getVersion()
252 {
253 return version;
254 }
255
256 public String getClassifier()
257 {
258 return classifier;
259 }
260
261 public String getExtension()
262 {
263 return extension;
264 }
265
266 public File getFile()
267 {
268 return file;
269 }
270
271 public Map<String, String> getProperties()
272 {
273 return properties;
274 }
275
276 }