View Javadoc

1   package org.apache.maven.toolchain;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
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      private RequirementMatcherFactory()
33      {
34      }
35  
36      public static RequirementMatcher createExactMatcher( String provideValue )
37      {
38          return new ExactMatcher( provideValue );
39      }
40  
41      public static RequirementMatcher createVersionMatcher( String provideValue )
42      {
43          return new VersionMatcher( provideValue );
44      }
45  
46      private static final class ExactMatcher
47          implements RequirementMatcher
48      {
49  
50          private String provides;
51  
52          private ExactMatcher( String provides )
53          {
54              this.provides = provides;
55          }
56  
57          public boolean matches( String requirement )
58          {
59              return provides.equalsIgnoreCase( requirement );
60          }
61      }
62  
63      private static final class VersionMatcher
64          implements RequirementMatcher
65      {
66  
67          DefaultArtifactVersion version;
68  
69          private VersionMatcher( String version )
70          {
71              this.version = new DefaultArtifactVersion( version );
72          }
73  
74          public boolean matches( String requirement )
75          {
76              try
77              {
78                  VersionRange range = VersionRange.createFromVersionSpec( requirement );
79                  if ( range.hasRestrictions() )
80                  {
81                      return range.containsVersion( version );
82                  }
83                  else
84                  {
85                      return range.getRecommendedVersion().compareTo( version ) == 0;
86                  }
87              }
88              catch ( InvalidVersionSpecificationException ex )
89              {
90                  //TODO error reporting
91                  ex.printStackTrace();
92                  return false;
93              }
94          }
95      }
96  }