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 getCheckedRef(Exclusion.class);
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      @Override
59      public void setRefid(Reference ref) {
60          if (groupId != null || artifactId != null || extension != null || classifier != null) {
61              throw tooManyAttributes();
62          }
63          super.setRefid(ref);
64      }
65  
66      public String getGroupId() {
67          if (isReference()) {
68              return getRef().getGroupId();
69          }
70          return (groupId != null) ? groupId : WILDCARD;
71      }
72  
73      public void setGroupId(String groupId) {
74          checkAttributesAllowed();
75          if (this.groupId != null) {
76              throw ambiguousCoords();
77          }
78          this.groupId = groupId;
79      }
80  
81      public String getArtifactId() {
82          if (isReference()) {
83              return getRef().getArtifactId();
84          }
85          return (artifactId != null) ? artifactId : WILDCARD;
86      }
87  
88      public void setArtifactId(String artifactId) {
89          checkAttributesAllowed();
90          if (this.artifactId != null) {
91              throw ambiguousCoords();
92          }
93          this.artifactId = artifactId;
94      }
95  
96      public String getClassifier() {
97          if (isReference()) {
98              return getRef().getClassifier();
99          }
100         return (classifier != null) ? classifier : WILDCARD;
101     }
102 
103     public void setClassifier(String classifier) {
104         checkAttributesAllowed();
105         if (this.classifier != null) {
106             throw ambiguousCoords();
107         }
108         this.classifier = classifier;
109     }
110 
111     public String getExtension() {
112         if (isReference()) {
113             return getRef().getExtension();
114         }
115         return (extension != null) ? extension : WILDCARD;
116     }
117 
118     public void setExtension(String extension) {
119         checkAttributesAllowed();
120         if (this.extension != null) {
121             throw ambiguousCoords();
122         }
123         this.extension = extension;
124     }
125 
126     public void setCoords(String coords) {
127         checkAttributesAllowed();
128         if (groupId != null || artifactId != null || extension != null || classifier != null) {
129             throw ambiguousCoords();
130         }
131         Pattern p = Pattern.compile("([^: ]+)(:([^: ]+)(:([^: ]+)(:([^: ]*))?)?)?");
132         Matcher m = p.matcher(coords);
133         if (!m.matches()) {
134             throw new BuildException("Bad exclusion coordinates '" + coords
135                     + "', expected format is <groupId>[:<artifactId>[:<extension>[:<classifier>]]]");
136         }
137         groupId = m.group(1);
138         artifactId = m.group(3);
139         if (artifactId == null) {
140             artifactId = "*";
141         }
142         extension = m.group(5);
143         if (extension == null) {
144             extension = "*";
145         }
146         classifier = m.group(7);
147         if (classifier == null) {
148             classifier = "*";
149         }
150     }
151 
152     private BuildException ambiguousCoords() {
153         return new BuildException(
154                 "You must not specify both 'coords' and " + "('groupId', 'artifactId', 'extension', 'classifier')");
155     }
156 }