1 package org.apache.maven.resolver.internal.ant.types;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.types.DataType;
28 import org.apache.tools.ant.types.Reference;
29
30
31
32 public class Exclusion
33 extends DataType
34 {
35
36 private static final String WILDCARD = "*";
37
38 private String groupId;
39
40 private String artifactId;
41
42 private String classifier;
43
44 private String extension;
45
46 protected Exclusion getRef()
47 {
48 return (Exclusion) getCheckedRef();
49 }
50
51 public void validate( Task task )
52 {
53 if ( isReference() )
54 {
55 getRef().validate( task );
56 }
57 else
58 {
59 if ( groupId == null && artifactId == null && classifier == null && extension == null )
60 {
61 throw new BuildException( "You must specify at least one of "
62 + "'groupId', 'artifactId', 'classifier' or 'extension'" );
63 }
64 }
65 }
66
67 public void setRefid( Reference ref )
68 {
69 if ( groupId != null || artifactId != null || extension != null || classifier != null )
70 {
71 throw tooManyAttributes();
72 }
73 super.setRefid( ref );
74 }
75
76 public String getGroupId()
77 {
78 if ( isReference() )
79 {
80 return getRef().getGroupId();
81 }
82 return ( groupId != null ) ? groupId : WILDCARD;
83 }
84
85 public void setGroupId( String groupId )
86 {
87 checkAttributesAllowed();
88 if ( this.groupId != null )
89 {
90 throw ambiguousCoords();
91 }
92 this.groupId = groupId;
93 }
94
95 public String getArtifactId()
96 {
97 if ( isReference() )
98 {
99 return getRef().getArtifactId();
100 }
101 return ( artifactId != null ) ? artifactId : WILDCARD;
102 }
103
104 public void setArtifactId( String artifactId )
105 {
106 checkAttributesAllowed();
107 if ( this.artifactId != null )
108 {
109 throw ambiguousCoords();
110 }
111 this.artifactId = artifactId;
112 }
113
114 public String getClassifier()
115 {
116 if ( isReference() )
117 {
118 return getRef().getClassifier();
119 }
120 return ( classifier != null ) ? classifier : WILDCARD;
121 }
122
123 public void setClassifier( String classifier )
124 {
125 checkAttributesAllowed();
126 if ( this.classifier != null )
127 {
128 throw ambiguousCoords();
129 }
130 this.classifier = classifier;
131 }
132
133 public String getExtension()
134 {
135 if ( isReference() )
136 {
137 return getRef().getExtension();
138 }
139 return ( extension != null ) ? extension : WILDCARD;
140 }
141
142 public void setExtension( String extension )
143 {
144 checkAttributesAllowed();
145 if ( this.extension != null )
146 {
147 throw ambiguousCoords();
148 }
149 this.extension = extension;
150 }
151
152 public void setCoords( String coords )
153 {
154 checkAttributesAllowed();
155 if ( groupId != null || artifactId != null || extension != null || classifier != null )
156 {
157 throw ambiguousCoords();
158 }
159 Pattern p = Pattern.compile( "([^: ]+)(:([^: ]+)(:([^: ]+)(:([^: ]*))?)?)?" );
160 Matcher m = p.matcher( coords );
161 if ( !m.matches() )
162 {
163 throw new BuildException( "Bad exclusion coordinates '" + coords
164 + "', expected format is <groupId>[:<artifactId>[:<extension>[:<classifier>]]]" );
165 }
166 groupId = m.group( 1 );
167 artifactId = m.group( 3 );
168 if ( artifactId == null )
169 {
170 artifactId = "*";
171 }
172 extension = m.group( 5 );
173 if ( extension == null )
174 {
175 extension = "*";
176 }
177 classifier = m.group( 7 );
178 if ( classifier == null )
179 {
180 classifier = "*";
181 }
182 }
183
184 private BuildException ambiguousCoords()
185 {
186 return new BuildException( "You must not specify both 'coords' and "
187 + "('groupId', 'artifactId', 'extension', 'classifier')" );
188 }
189
190 }