View Javadoc
1   /*
2    * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and contributors.
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the GNU Lesser General Public License
6    * (LGPL) version 2.1 which accompanies this distribution, and is available at
7    * http://www.gnu.org/licenses/lgpl-2.1.html
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * Contributors:
15   *     bstefanescu
16   */
17  package org.nuxeo.build.maven.filter;
18  
19  /**
20   * TODO NXBT-258
21   */
22  public abstract class SegmentMatch {
23  
24      public static final SegmentMatch ANY = new SegmentMatch() {
25          @Override
26          public boolean match(String arg0) {
27              return true;
28          }
29  
30          @Override
31          public String toString() {
32              return "AnySegmentMatch";
33          }
34      };
35  
36      abstract boolean match(String segment);
37  
38      public static SegmentMatch parse(String pattern) {
39          int p = pattern.indexOf("*");
40          if (p == -1) {
41              return new ExactMatch(pattern);
42          }
43          int len = pattern.length();
44          if (len == 0) {
45              return SegmentMatch.ANY;
46          }
47          if (p == len - 1) {
48              return new PrefixMatch(pattern.substring(0, pattern.length() - 1));
49          }
50          if (p == 0) {
51              return new SuffixMatch(pattern.substring(1));
52          }
53          return new MiddleMatch(pattern.substring(0, p),
54                  pattern.substring(p + 1));
55      }
56  
57  }