Пример #1
0
  @Override
  public Object visit(StringBinaryComparison n, Void arg) {
    String first = (String) n.getLeftOperand().accept(this, null);
    String second = (String) n.getRightOperand().accept(this, null);

    Operator op = n.getOperator();
    switch (op) {
      case EQUALSIGNORECASE:
        return first.equalsIgnoreCase(second) ? TRUE_VALUE : FALSE_VALUE;
      case EQUALS:
        return first.equals(second) ? TRUE_VALUE : FALSE_VALUE;
      case ENDSWITH:
        return first.endsWith(second) ? TRUE_VALUE : FALSE_VALUE;
      case CONTAINS:
        return first.contains(second) ? TRUE_VALUE : FALSE_VALUE;
      case PATTERNMATCHES:
        return second.matches(first) ? TRUE_VALUE : FALSE_VALUE;
      case APACHE_ORO_PATTERN_MATCHES:
        {
          Perl5Matcher matcher = new Perl5Matcher();
          Perl5Compiler compiler = new Perl5Compiler();
          Pattern pattern;
          try {
            pattern = compiler.compile(first);
          } catch (MalformedPatternException e) {
            throw new RuntimeException(e);
          }
          return matcher.matches(second, pattern) ? TRUE_VALUE : FALSE_VALUE;
        }
      default:
        log.warn("StringComparison: unimplemented operator!" + op);
        return null;
    }
  }
Пример #2
0
 /** Convert specific starting hand to HoldemAtomicGroup object.
     @param groupSpec starting hand (e.g., AhKd, 8h3s)
 */
 public HoldemAtomicGroup(String groupSpec) {
   myspec = groupSpec;
   myhands = new HashSet();
   Perl5Compiler compiler = new Perl5Compiler();
   Perl5Matcher matcher = new Perl5Matcher();
   Pattern atomicPattern;
   try {
     atomicPattern = compiler.compile
       ("^([AKQJT98765432])([shdc])([AKQJT98765432])([shdc])$");
   } catch (MalformedPatternException e) {
     throw new RuntimeException("BUG: " + e.toString());
   }
   MatchResult result;
   if (matcher.matches(groupSpec, atomicPattern)) {
     result = matcher.getMatch();
     int rank1 = Deck.parseRank(result.group(1));
     int suit1 = Deck.parseSuit(result.group(2));
     int rank2 = Deck.parseRank(result.group(3));
     int suit2 = Deck.parseSuit(result.group(4));
     addAtomic(rank1, suit1, rank2, suit2);
   } else {
     throw new IllegalArgumentException("unable to parse groupSpec: " +
                                        groupSpec);
   }
 }
 /** Initialize ORO fields from patterns String[]. */
 protected void initPatternRepresentation(String[] patterns) throws IllegalArgumentException {
   this.compiledPatterns = new Pattern[patterns.length];
   Perl5Compiler compiler = new Perl5Compiler();
   for (int i = 0; i < patterns.length; i++) {
     // compile the pattern to be thread-safe
     try {
       this.compiledPatterns[i] = compiler.compile(patterns[i], Perl5Compiler.READ_ONLY_MASK);
     } catch (MalformedPatternException ex) {
       throw new IllegalArgumentException(ex.getMessage());
     }
   }
   this.matcher = new Perl5Matcher();
 }
Пример #4
0
  public TemplateParser() {
    Perl5Compiler compiler = new Perl5Compiler();

    try {
      _simpleIdPattern = compiler.compile(SIMPLE_ID_PATTERN);
      _implicitIdPattern = compiler.compile(IMPLICIT_ID_PATTERN);
    } catch (MalformedPatternException ex) {
      throw new ApplicationRuntimeException(ex);
    }

    _patternMatcher = new Perl5Matcher();

    _factory = new TemplateTokenFactory();
  }
Пример #5
0
  /**
   * Compiles and caches a regexp pattern for the given string pattern.
   *
   * @param stringPattern a pattern string
   * @param caseSensitive case sensitive true/false
   * @return compiles and caches a regexp pattern for the given string pattern
   * @throws MalformedPatternException
   */
  private Pattern getTestPattern(String stringPattern, boolean caseSensitive)
      throws MalformedPatternException {
    Pattern pattern = compiledPatterns.get(stringPattern);
    if (pattern == null) {
      if (caseSensitive) {
        pattern = compiler.compile(stringPattern);
      } else {
        pattern = compiler.compile(stringPattern, Perl5Compiler.CASE_INSENSITIVE_MASK);
      }

      compiledPatterns.put(stringPattern, pattern);
      Debug.logVerbose(
          "Compiled and cached a pattern: '" + stringPattern + "' - " + Thread.currentThread(),
          module);
    }
    return pattern;
  }
 private void recompilePatternIfNecessary(String regexpPattern) {
   if (pattern == null || !regexpPattern.equals(pattern.getPattern())) {
     try {
       pattern = compiler.compile(regexpPattern);
     } catch (MalformedPatternException e) {
       throw new RuntimeException(e);
     }
   }
 }
Пример #7
0
 /**
  * Return true if the regexp pattern is found to occur in the input string.
  *
  * @param input the input string
  * @param p the pattern
  */
 public static boolean regexp(String input, String p) {
   try {
     Pattern pattern = compiler.compile(p);
     return sMatcher.contains(input, pattern);
   } catch (MalformedPatternException e) {
     // We should probably print something to a debug log or something to mention that the pattern
     // we tested
     // threw an error and probably has some bogus regexp syntax.
     return false;
   }
 }
Пример #8
0
  /**
   * Perform string substitution using pattern matching.
   *
   * @param str the source string
   * @param p pattern to look for
   * @param s the string to replace <i>pattern</i> with. Perl5 references to matches are allowed.
   *     See <a
   *     href="http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/Perl5Substitution.html">http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/Perl5Substitution.html</a>
   * @param numSubs number of substitutions to perform, Util.SUBSTITUTE_ALL will cause all
   *     occurences to be replaced
   * @return the string with the substitution made for numSubs occurences of the pattern
   */
  public static String regsub(String str, String p, String s, int numSubs) {
    try {
      Pattern pattern = compiler.compile(p);
      Perl5Substitution subst = new Perl5Substitution(s);

      String result = Util.substitute(sMatcher, pattern, subst, str, numSubs);
      return result;
    } catch (MalformedPatternException e) {
      throw new CompilationError(e);
    }
  }
Пример #9
0
 public final void setExpr(String expr) throws MalformedPatternException {
   Perl5Compiler patternCompiler = new Perl5Compiler();
   pattern = patternCompiler.compile(expr);
 }