View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.plugin.jboss.modules.descriptor;
17  
18  import java.util.regex.Matcher;
19  import java.util.regex.Pattern;
20  import java.util.regex.PatternSyntaxException;
21  
22  import org.apache.commons.lang.ObjectUtils;
23  import org.apache.commons.lang.StringUtils;
24  import org.sonatype.aether.artifact.Artifact;
25  
26  import de.smartics.maven.plugin.jboss.modules.domain.MatchContext;
27  import de.smartics.maven.plugin.jboss.modules.domain.matching.DoubleMatchContext;
28  import de.smartics.maven.plugin.jboss.modules.domain.matching.SingleMatchContext;
29  
30  /**
31   * Models an inclusion or exclusion. An include/exclude matches if all given
32   * information matches (that is: <code>and</code>ed).
33   */
34  public class ArtifactClusion
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    // --- members --------------------------------------------------------------
41  
42    /**
43     * The groupId to match. May be a regular expression.
44     */
45    private String groupId;
46  
47    /**
48     * The groupId pattern to match. May be <code>null</code>, if groupId does not
49     * specify a pattern.
50     */
51    private Pattern groupIdPattern;
52  
53    /**
54     * The artifactId to match. May be a regular expression.
55     */
56    private String artifactId;
57  
58    /**
59     * The artifactId pattern to match. May be <code>null</code>, if artifactId
60     * does not specify a pattern.
61     */
62    private Pattern artifactIdPattern;
63  
64    // ****************************** Initializer *******************************
65  
66    // ****************************** Constructors ******************************
67  
68    /**
69     * Default constructor.
70     */
71    public ArtifactClusion()
72    {
73    }
74  
75    // ****************************** Inner Classes *****************************
76  
77    // ********************************* Methods ********************************
78  
79    // --- init -----------------------------------------------------------------
80  
81    // --- factory --------------------------------------------------------------
82  
83    /**
84     * Helper to create an instance.
85     *
86     * @param groupId the groupId to match. May be a regular expression.
87     * @param artifactId the artifactId to match. May be a regular expression.
88     * @return the new instance.
89     */
90    public static ArtifactClusion create(final String groupId, final String artifactId)
91    {
92      final ArtifactClusion clusion = new ArtifactClusion();
93      clusion.setGroupId(groupId);
94      clusion.setArtifactId(artifactId);
95      return clusion;
96    }
97  
98    // --- get&set --------------------------------------------------------------
99  
100   /**
101    * Returns the groupId to match. May be a regular expression.
102    *
103    * @return the groupId to match.
104    */
105   public String getGroupId()
106   {
107     return groupId;
108   }
109 
110   /**
111    * Sets the groupId to match.May be a regular expression.
112    *
113    * @param groupId the groupId to match.
114    */
115   public void setGroupId(final String groupId)
116   {
117     if (StringUtils.isNotBlank(groupId))
118     {
119       this.groupId = groupId;
120       groupIdPattern = compilePattern(groupId);
121     }
122   }
123 
124   private static Pattern compilePattern(final String pattern)
125   {
126     if (StringUtils.isNotBlank(pattern))
127     {
128       try
129       {
130         return Pattern.compile(pattern);
131       }
132       catch (final PatternSyntaxException e)
133       {
134         // ignore
135       }
136     }
137     return null;
138   }
139 
140   /**
141    * Returns the artifactId to match. May be a regular expression.
142    *
143    * @return the artifactId to match.
144    */
145   public String getArtifactId()
146   {
147     return artifactId;
148   }
149 
150   /**
151    * Sets the artifactId to match. May be a regular expression.
152    *
153    * @param artifactId the artifactId to match.
154    */
155   public void setArtifactId(final String artifactId)
156   {
157     if (StringUtils.isNotBlank(artifactId))
158     {
159       this.artifactId = artifactId;
160       artifactIdPattern = compilePattern(artifactId);
161     }
162   }
163 
164   // --- business -------------------------------------------------------------
165 
166   /**
167    * Checks if the clusion matches the artifact.
168    *
169    * @param artifact the artifact to match.
170    * @return a context to access the match result, with <code>true</code> if the
171    *         artifact matches groupId and artifactId, <code>false</code>
172    *         otherwise.
173    */
174   public MatchContext matches(final Artifact artifact)
175   {
176     final SingleMatchContext matchesGroupId =
177         matches(groupIdPattern, groupId, artifact.getGroupId());
178     if (matchesGroupId != null && !matchesGroupId.isMatched())
179     {
180       return new SingleMatchContext(false);
181     }
182     final SingleMatchContext matchesArtifactId =
183         matches(artifactIdPattern, artifactId, artifact.getArtifactId());
184 
185     final boolean result =
186         (matchesGroupId != null && matchesGroupId.isMatched() && (matchesArtifactId == null || matchesArtifactId
187             .isMatched()))
188             || (matchesArtifactId != null && matchesArtifactId.isMatched());
189 
190     final MatchContext context =
191         new DoubleMatchContext(result, matchesGroupId, matchesArtifactId);
192     return context;
193   }
194 
195   private static SingleMatchContext matches(final Pattern pattern,
196       final String id, final String inputId)
197   {
198     if (pattern != null)
199     {
200       final Matcher matcher = pattern.matcher(inputId);
201       return new SingleMatchContext(matcher);
202     }
203 
204     if (StringUtils.isNotBlank(id))
205     {
206       return new SingleMatchContext(id.equals(inputId));
207     }
208 
209     return null;
210   }
211 
212   // --- object basics --------------------------------------------------------
213 
214   @Override
215   public String toString()
216   {
217     return ObjectUtils.toString(groupId) + ':'
218            + ObjectUtils.toString(artifactId);
219   }
220 }