View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package net.sf.zel.vm;
6   
7   /**
8    *
9    * @author zoly
10   */
11  public class WildCardMatcher
12  {
13      public WildCardMatcher(String wildcardExp)
14      {
15          this.wildcardExp = wildcardExp;
16      }
17  
18      private String wildcardExp;
19  
20  
21      /**
22       * Returns true if a matches b and false otherwise
23       *
24       * @param o Object
25       * @return boolean
26       */
27      public final boolean match(final String str)
28      {
29          return match(this.wildcardExp, str);
30      }
31  
32  
33      /**
34       * static wildcard matching
35       * @param wildcard
36       * @param v
37       * @return return true ifr string v matches the wildcard
38       */
39      @SuppressWarnings("empty-statement")
40      public static final boolean match(final String wildcard, final String v)
41      {
42          int i = 0;
43          int j = 0;
44          final int length = wildcard.length();
45          for (; i < length; i++, j++)
46          {
47              final char some2 = wildcard.charAt(i);
48              if (some2 != v.charAt(j))
49              {
50                  if (some2 == '*')
51                  {
52                      i++;
53                      if (i == length)
54                      {
55                          return true;
56                      }
57                      final char some = wildcard.charAt(i);
58                      while (some != v.charAt(j))
59                          ++j;
60                      j--;
61                  } else if (some2 != '?')
62                  {
63                      return false;
64                  }
65              }
66          }
67          if (j != v.length())
68          {
69              return false;
70          }
71          return true;
72      }
73  
74      /**
75       * return the java regexp version of this class
76       * @return
77       */
78      public String getJavaRegexp()
79      {
80          final StringBuilder buff = new StringBuilder();
81          final int length = wildcardExp.length();
82          for (int i = 0; i < length; i++)
83          {
84              final char c = wildcardExp.charAt(i);
85              switch (c)
86              {
87                  case '*':
88                      buff.append("[^\\[\\]\\.]+");
89                      break;
90                  case '?':
91                      buff.append(".");
92                      break;
93                  case '[':
94                  case ']':
95                  case '.':
96                      buff.append('\\').append(c);
97                      break;
98                  default:
99                      buff.append(c);
100             }
101         }
102         return buff.toString();
103     }
104 
105     /**
106      * return if this variable object is a exact mach or a set of variables
107      * I use a for cycle to iterate through strings
108      * using a CharacterIterator migh tbe better ?
109      *
110      * @param str - string to test
111      * @return true if str is a wildcard matcher
112      */
113 
114     public static final boolean isWildcardMatcher(final String str)
115     {
116         final int length = str.length();
117         for (int i = 0; i < length; i++)
118         {
119             final char c = str.charAt(i);
120             if (c == '*' || c == '?')
121             {
122                 return true;
123             }
124         }
125         return false;
126     }
127 
128     @Override
129     public String toString()
130     {
131         return "`" + wildcardExp + "`";
132     }
133 
134     @Override
135     public boolean equals(Object o)
136     {
137       if (o == null)
138           return false;
139       else if (o instanceof String)
140           return ((String)o).equals(this.wildcardExp);
141       else if (o instanceof WildCardMatcher)
142           return ((WildCardMatcher)o).wildcardExp.equals(this.wildcardExp);
143       return false;
144     }
145 
146     @Override
147     public int hashCode()
148     {
149         int hash = 5;
150         hash = 43 * hash + (this.wildcardExp != null ? this.wildcardExp.hashCode() : 0);
151         return hash;
152     }
153 }