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  
20  package org.apache.maven.toolchain;
21  
22  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
23  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
24  import org.apache.maven.artifact.versioning.VersionRange;
25  
26  /**
27   *
28   * @author mkleint
29   */
30  public final class RequirementMatcherFactory
31  {
32  
33      private RequirementMatcherFactory( )
34      {
35      }
36  
37      public static RequirementMatcher createExactMatcher( String provideValue )
38      {
39          return new ExactMatcher( provideValue );
40      }
41  
42      public static RequirementMatcher createVersionMatcher( String provideValue )
43      {
44          return new VersionMatcher( provideValue );
45      }
46  
47      private static final class ExactMatcher
48          implements RequirementMatcher
49      {
50  
51          private String provides;
52  
53          private ExactMatcher( String provides )
54          {
55              this.provides = provides;
56          }
57  
58          public boolean matches( String requirement )
59          {
60              return provides.equalsIgnoreCase( requirement );
61          }
62      }
63  
64      private static final class VersionMatcher
65          implements RequirementMatcher
66      {
67  
68          DefaultArtifactVersion version;
69  
70          private VersionMatcher( String version )
71          {
72              this.version = new DefaultArtifactVersion(version);
73          }
74  
75          public boolean matches( String requirement )
76          {
77              try 
78              {
79                  VersionRange range = VersionRange.createFromVersionSpec(requirement);
80                  if (range.hasRestrictions()) {
81                      return range.containsVersion(version);
82                  } else {
83                      return range.getRecommendedVersion().compareTo(version) == 0;
84                  }
85              } 
86              catch (InvalidVersionSpecificationException ex) 
87              {
88                  //TODO error reporting
89                  ex.printStackTrace();
90                  return false;
91              }
92          }
93      }
94  }