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