private String getDomain(String url) {
    PatternMatcher matcher = new Perl5Matcher();
    PatternCompiler compiler = new Perl5Compiler();
    PatternMatcherInput input = new PatternMatcherInput(url);
    Pattern pattern = null;

    String patternString = HTTP_PATTERN;
    try {
      pattern = compiler.compile(patternString);
    } catch (MalformedPatternException e) {
      logger.error("Incorrect pattern: " + patternString, e);
    }
    while (matcher.contains(input, pattern)) {
      MatchResult result = matcher.getMatch();
      String sResult = result.group(1);
      logger.info("Match: " + sResult);
      return sResult;
    }
    patternString = WWW_PATTERN;
    try {
      pattern = compiler.compile(patternString);
    } catch (MalformedPatternException e) {
      logger.error("Incorrect pattern: " + patternString, e);
    }
    while (matcher.contains(input, pattern)) {
      MatchResult result = matcher.getMatch();
      String sResult = result.group(1);
      logger.info("Match: " + sResult);
      return sResult;
    }
    return null;
  }
Пример #2
0
 private void generateTemplate(String rawTemplate) {
   List pieces = new ArrayList();
   List combined = new LinkedList();
   PatternMatcher matcher = new Perl5Matcher();
   Util.split(pieces, new Perl5Matcher(), templatePattern, rawTemplate);
   PatternMatcherInput input = new PatternMatcherInput(rawTemplate);
   int count = 0;
   Iterator iter = pieces.iterator();
   boolean startsWith = isFirstElementGroup(rawTemplate);
   while (iter.hasNext()) {
     boolean matchExists = matcher.contains(input, templatePattern);
     if (startsWith) {
       if (matchExists) {
         combined.add(new Integer(matcher.getMatch().group(1)));
       }
       combined.add(iter.next());
     } else {
       combined.add(iter.next());
       if (matchExists) {
         combined.add(new Integer(matcher.getMatch().group(1)));
       }
     }
   }
   if (matcher.contains(input, templatePattern)) {
     combined.add(new Integer(matcher.getMatch().group(1)));
   }
   template = combined.toArray();
 }
Пример #3
0
    public Value matchPositions(Value str) {
      PatternMatcher matcher = getMatcher();

      // Do the matching
      PatternMatcherInput jStr = new PatternMatcherInput(string(str));
      Pair result = null;
      Pair prev = null;
      boolean found = false;

      while (matcher.contains(jStr, pattern)) {
        found = true;

        MatchResult matchResult = matcher.getMatch();

        for (int i = 0, length = matchResult.groups(); i < length; i++) {
          Pair m =
              new Pair(
                  Quantity.valueOf(matchResult.beginOffset(i)),
                  Quantity.valueOf(matchResult.endOffset(i)));
          Pair elem = new Pair(m, EMPTYLIST);
          if (result == null) result = prev = elem;
          else {
            prev.setCdr(elem);
            prev = elem;
          }
        }
      }

      if (!found) return FALSE;
      else return result;
    }
Пример #4
0
    public Value match(Value str) {
      PatternMatcher matcher = getMatcher();

      // Do the matching
      PatternMatcherInput jStr = new PatternMatcherInput(string(str));
      Pair result = null;
      Pair prev = null;
      boolean found = false;

      while (matcher.contains(jStr, pattern)) {
        found = true;

        MatchResult matchResult = matcher.getMatch();

        for (int i = 0, length = matchResult.groups(); i < length; i++) {
          Pair m = new Pair(new SchemeString(matchResult.group(i)), EMPTYLIST);
          if (result == null) result = prev = m;
          else {
            prev.setCdr(m);
            prev = m;
          }
        }
      }

      if (!found) return FALSE;
      else return result;
    }
 private boolean matchExpression(String regex, String expression) {
   PatternCompiler compiler = new Perl5Compiler();
   try {
     Pattern pattern =
         compiler.compile(
             "\\b("
                 + UpdateContextVariablesHelper.replaceSpecialChar(regex)
                 + ")(\\b|\\_)"); //$NON-NLS-1$ //$NON-NLS-2$
     PatternMatcher matcher = new Perl5Matcher();
     ((Perl5Matcher) matcher).setMultiline(true);
     if (matcher.contains(expression, pattern)) {
       return true;
     }
   } catch (MalformedPatternException e) {
     //
   }
   return false;
 }
Пример #6
0
  private void initTemplate() {
    if (template != null) {
      return;
    }
    // Contains Strings and Integers
    List<Object> combined = new ArrayList<Object>();
    String rawTemplate = getTemplate();
    PatternMatcher matcher = JMeterUtils.getMatcher();
    Pattern templatePattern =
        JMeterUtils.getPatternCache()
            .getPattern(
                "\\$(\\d+)\\$" // $NON-NLS-1$
                ,
                Perl5Compiler.READ_ONLY_MASK & Perl5Compiler.SINGLELINE_MASK);
    if (log.isDebugEnabled()) {
      log.debug("Pattern = " + templatePattern.getPattern());
      log.debug("template = " + rawTemplate);
    }
    int beginOffset = 0;
    MatchResult currentResult;
    PatternMatcherInput pinput = new PatternMatcherInput(rawTemplate);
    while (matcher.contains(pinput, templatePattern)) {
      currentResult = matcher.getMatch();
      final int beginMatch = currentResult.beginOffset(0);
      if (beginMatch > beginOffset) { // string is not empty
        combined.add(rawTemplate.substring(beginOffset, beginMatch));
      }
      combined.add(Integer.valueOf(currentResult.group(1))); // add match as Integer
      beginOffset = currentResult.endOffset(0);
    }

    if (beginOffset < rawTemplate.length()) { // trailing string is not empty
      combined.add(rawTemplate.substring(beginOffset, rawTemplate.length()));
    }
    if (log.isDebugEnabled()) {
      log.debug("Template item count: " + combined.size());
      for (Object o : combined) {
        log.debug(o.getClass().getSimpleName() + " '" + o.toString() + "'");
      }
    }
    template = combined;
  }
Пример #7
0
  public String execute(SampleResult previousResult, Sampler currentSampler)
      throws InvalidVariableException {
    try {
      searchPattern = compiler.compile(((CompoundVariable) values[0]).execute());
      generateTemplate(((CompoundVariable) values[1]).execute());

      if (values.length > 2) {
        valueIndex = ((CompoundVariable) values[2]).execute();
      }
      if (valueIndex.equals("")) {
        valueIndex = "1";
      }

      if (values.length > 3) {
        between = ((CompoundVariable) values[3]).execute();
      }

      if (values.length > 4) {
        String dv = ((CompoundVariable) values[4]).execute();
        if (!dv.equals("")) {
          defaultValue = dv;
        }
      }

      if (values.length > 5) {
        name = ((CompoundVariable) values[values.length - 1]).execute();
      }
    } catch (MalformedPatternException e) {
      log.error("", e);
      throw new InvalidVariableException("Bad regex pattern");
    } catch (Exception e) {
      throw new InvalidVariableException(e.getMessage());
    }

    getVariables().put(name, defaultValue);
    if (previousResult == null || previousResult.getResponseData() == null) {
      return defaultValue;
    }

    List collectAllMatches = new ArrayList();
    try {
      PatternMatcher matcher = (PatternMatcher) localMatcher.get();
      String responseText = new String(previousResult.getResponseData());
      PatternMatcherInput input = new PatternMatcherInput(responseText);
      while (matcher.contains(input, searchPattern)) {
        MatchResult match = matcher.getMatch();
        collectAllMatches.add(match);
      }
    } catch (NumberFormatException e) {
      log.error("", e);
      return defaultValue;
    } catch (Exception e) {
      return defaultValue;
    }

    if (collectAllMatches.size() == 0) {
      return defaultValue;
    }

    if (valueIndex.equals(ALL)) {
      StringBuffer value = new StringBuffer();
      Iterator it = collectAllMatches.iterator();
      boolean first = true;
      while (it.hasNext()) {
        if (!first) {
          value.append(between);
        } else {
          first = false;
        }
        value.append(generateResult((MatchResult) it.next()));
      }
      return value.toString();
    } else if (valueIndex.equals(RAND)) {
      MatchResult result =
          (MatchResult) collectAllMatches.get(rand.nextInt(collectAllMatches.size()));
      return generateResult(result);
    } else {
      try {
        int index = Integer.parseInt(valueIndex) - 1;
        MatchResult result = (MatchResult) collectAllMatches.get(index);
        return generateResult(result);
      } catch (NumberFormatException e) {
        float ratio = Float.parseFloat(valueIndex);
        MatchResult result =
            (MatchResult) collectAllMatches.get((int) (collectAllMatches.size() * ratio + .5) - 1);
        return generateResult(result);
      } catch (IndexOutOfBoundsException e) {
        return defaultValue;
      }
    }
  }