Example #1
0
 private void makePatternMap(List<String> patternPairs) throws IllegalArgumentException {
   if (patternPairs != null) {
     patternMap = new LinkedMap();
     for (String pair : patternPairs) {
       // Find the last occurrence of comma to avoid regexp quoting
       int pos = pair.lastIndexOf(',');
       if (pos < 0) {
         throw new IllegalArgumentException("Marformed pattern,int pair; no comma: " + pair);
       }
       String regexp = pair.substring(0, pos);
       String pristr = pair.substring(pos + 1);
       int pri;
       Pattern pat;
       try {
         pri = Integer.parseInt(pristr);
         int flags = Perl5Compiler.READ_ONLY_MASK;
         pat = RegexpUtil.getCompiler().compile(regexp, flags);
         patternMap.put(pat, pri);
       } catch (NumberFormatException e) {
         throw new IllegalArgumentException("Illegal priority: " + pristr);
       } catch (MalformedPatternException e) {
         throw new IllegalArgumentException("Illegal regexp: " + regexp);
       }
     }
   }
 }
Example #2
0
 /**
  * Return the value associated with the first pattern that the string matches, or the specified
  * default value if none, considering only patterns whose associated value is less than or equal
  * to maxPri.
  */
 public int getMatch(String str, int dfault, int maxPri) {
   Perl5Matcher matcher = RegexpUtil.getMatcher();
   for (Map.Entry<Pattern, Integer> ent : patternMap.entrySet()) {
     if (ent.getValue() <= maxPri) {
       Pattern pat = ent.getKey();
       if (matcher.contains(str, pat)) {
         log.debug2("getMatch(" + str + "): " + ent.getValue());
         return ent.getValue();
       }
     }
   }
   log.debug2("getMatch(" + str + "): default: " + dfault);
   return dfault;
 }