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;
  }
Beispiel #2
0
 /**
  * Creates the variables:<br>
  * basename_gn, where n=0...# of groups<br>
  * basename_g = number of groups (apart from g0)
  */
 private void saveGroups(JMeterVariables vars, String basename, MatchResult match) {
   StringBuilder buf = new StringBuilder();
   buf.append(basename);
   buf.append("_g"); // $NON-NLS-1$
   int pfxlen = buf.length();
   String prevString = vars.get(buf.toString());
   int previous = 0;
   if (prevString != null) {
     try {
       previous = Integer.parseInt(prevString);
     } catch (NumberFormatException e) {
       log.warn("Could not parse " + prevString + " " + e);
     }
   }
   // Note: match.groups() includes group 0
   final int groups = match.groups();
   for (int x = 0; x < groups; x++) {
     buf.append(x);
     vars.put(buf.toString(), match.group(x));
     buf.setLength(pfxlen);
   }
   vars.put(buf.toString(), Integer.toString(groups - 1));
   for (int i = groups; i <= previous; i++) {
     buf.append(i);
     vars.remove(buf.toString()); // remove the remaining _gn vars
     buf.setLength(pfxlen);
   }
 }
Beispiel #3
0
  /**
   * This function returns the matched string in input when the pattern is found.
   *
   * @param pattern pattern string to match
   * @param input input string
   * @return returns the portion of the matched string in the input
   * @exception throws a FrameworkException when either pattern/input is null.
   */
  public static String getMatch(String pattern, String input) throws FrameworkException {
    if (input == null || pattern == null) {
      throw new FrameworkException(
          "RegexUtils: getMatch(): input or pattern is null."
              + "input ="
              + input
              + ", "
              + "pattern = "
              + pattern);
    }

    Perl5Util util = new Perl5Util();
    boolean isMatched = false;
    MatchResult matchResult = null;

    // default return value is the original input
    String result = input;

    pattern = makePerl5MatchPattern(pattern);

    if (util.match(pattern, input)) {
      matchResult = util.getMatch();
      result = matchResult.toString();
    }

    return result;
  }
Beispiel #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;
    }
Beispiel #5
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);
   }
 }
Beispiel #6
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;
    }
Beispiel #7
0
 private void saveGroups(MatchResult result) {
   if (result != null) {
     JMeterVariables vars = getVariables();
     for (int x = 0; x < result.groups(); x++) {
       vars.put(name + "_g" + x, result.group(x));
     }
   }
 }
Beispiel #8
0
  public RubyValue post_match() {
    String res = "";
    int nMatchEnd = result_.endOffset(result_.groups() - 1);
    if (nMatchEnd >= 0 && nMatchEnd + 1 < str_.length()) res = str_.substring(nMatchEnd);

    if (res == null) return RubyConstant.QNIL;

    return ObjectFactory.createString(res);
  }
Beispiel #9
0
 /** 查找 */
 public static void simpleContains() throws Exception {
   Pattern pattern = new Perl5Compiler().compile("\\d+");
   Perl5Matcher matcher = new Perl5Matcher();
   PatternMatcherInput matcherInput = new PatternMatcherInput("北京2008年8月08日20时");
   System.out.println("simpleContains:");
   while (matcher.contains(matcherInput, pattern)) {
     MatchResult result = matcher.getMatch();
     System.out.println(result.toString());
   }
 }
Beispiel #10
0
  public RubyValue to_a() {
    RubyArray ar = new RubyArray();
    for (int i = 0; i < result_.groups(); i++) {
      String str = result_.group(i);
      if (str != null) ar.add(ObjectFactory.createString(str));
      else ar.add(RubyConstant.QNIL);
    }

    return ar;
  }
Beispiel #11
0
  public RubyValue captures() {
    RubyArray ar = new RubyArray();
    for (int i = 1; i < result_.groups(); i++) {
      String res = result_.group(i);
      if (res == null) ar.add(RubyConstant.QNIL);
      else ar.add(ObjectFactory.createString(res));
    }

    return ar;
  }
Beispiel #12
0
  /**
   * This function replace only the first pattern matched with the replacement.
   *
   * @param pattern pattern string to match
   * @param input input string
   * @param replacement replacement string
   * @return returns the processed string
   * @exception throws a FrameworkException when the pattern/replacement is null.
   */
  public static String replaceLast(String pattern, String input, String replacement)
      throws FrameworkException {
    if ((pattern == null) || (input == null) || (replacement == null)) {
      throw new FrameworkException(
          "RegexUtil: replaceLast(): pattern or input cannot be null. "
              + "pattern = "
              + pattern
              + "input = "
              + input
              + "replacement = "
              + replacement);
    }

    Perl5Util util = new Perl5Util();
    MatchResult matchResult = null;
    String result = null;
    String pre = null;
    String post = null;
    StringBuffer resultBuffer = new StringBuffer();

    int length = input.length();

    String regex = makePerl5SubstitutionPattern(pattern, replacement);
    pattern = makePerl5MatchPattern(pattern);

    // counts the number of match and grab the last one
    while (util.match(pattern, input)) {
      // getting the string before match
      pre = util.preMatch();
      resultBuffer.append(pre);

      // get the matched string
      matchResult = util.getMatch();
      resultBuffer.append(matchResult.toString());

      // get the post string after the match
      post = util.postMatch();

      // the post becomes the new input
      input = post;
    }

    // get the last match found
    matchResult = util.getMatch();

    // do the string replacement on the pattern found
    result = util.substitute(regex, matchResult.toString());

    resultBuffer.append(result);
    resultBuffer.append(post);

    return resultBuffer.toString();
  }
Beispiel #13
0
	/**
	 * insert正则表达式解析insert语句 如果insert语句与正则表达式匹配,本方法根据表达式对语句的分组
	 * 将insert语句拆分为6部分,并且将这几部分存放到字符串数组返回 分别为: 1.insert关键字 2.into关键字 3.表名称 4.表字段组
	 * 5.values关键字 6.插入字段值组
	 * 
	 * 比如insert语句: Insert into oa_meetingpromptsound ( soundCode , soundName ,
	 * soundFileName ) values ( '。.尹标平','bb','d()d' ) 将被分解为以下部分: 1.Insert 2.into
	 * 3.oa_meetingpromptsound 4.( soundCode , soundName , soundFileName )
	 * 5.values 6.( '。.尹标平','bb','d()d' )
	 */
	public static String[] parserInsert(String insert)
	{
		/**
		 * 定义insert语句的正则表达式 该表达式将insert语句拆分为6部分,分别为: 1.insert关键字 2.into关键字 3.表名称
		 * 4.表字段组 5.values关键字 6.插入字段值组 比如insert语句: Insert into
		 * oa_meetingpromptsound ( soundCode , soundName , soundFileName )
		 * values ( '。.尹标平','bb','d()d' ) 将被分解为以下部分: 1.Insert 2.into
		 * 3.oa_meetingpromptsound 4.( soundCode , soundName , soundFileName )
		 * 5.values 6.( '。.尹标平','bb','d()d' )
		 */
		String patternStr = "\\s*(insert)\\s+" + // 解析insert关键词
				"(into)\\s+" + // 解析into关键词
				"([^\\(^\\s]+)\\s*" + // 解析表名称
				"(\\([^\\)]+\\))\\s*" + // 解析表字段
				"(values)\\s*" + // 解析value关键词
				"(\\(.*(.*\n*)*.*)"; // 解析字段值

		/**
		 * 编译正则表达式patternStr,并用该表达式与传入的sql语句进行模式匹配,
		 * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回 该数组
		 */

		PatternCompiler compiler = new Perl5Compiler();
		Pattern pattern = null;
		try
		{
			pattern = compiler.compile(patternStr,
					Perl5Compiler.CASE_INSENSITIVE_MASK);
		}
		catch (MalformedPatternException e)
		{
			e.printStackTrace();

			return null;
		}
		PatternMatcher matcher = new Perl5Matcher();
		MatchResult result = null;
		String[] tokens = null;
		boolean match = matcher.matches(insert, pattern);

		if (match)
		{
			result = matcher.getMatch();
			tokens = new String[6];
			for (int i = 0; i < 6; i++)
			{
				tokens[i] = result.group(i + 1).trim();
			}
		}

		return tokens;
	}
Beispiel #14
0
 /** 分组 */
 public static void simpleResults() throws Exception {
   Pattern pattern =
       new Perl5Compiler().compile("(\\d+\\.\\d+\\.\\d+\\.\\d+)@(\\d{2}/\\d{2}/\\d{4})");
   Perl5Matcher matcher = new Perl5Matcher();
   PatternMatcherInput matcherInput = new PatternMatcherInput("202.108.9.38@08/10/2008");
   System.out.println("simpleResults:");
   while (matcher.contains(matcherInput, pattern)) {
     MatchResult result = matcher.getMatch();
     for (int i = 0; i < result.groups(); i++) {
       System.out.printf("%s : %s\n", i, result.group(i));
     }
   }
 }
  public void process() {
    Sampler sampler = JMeterContextService.getContext().getCurrentSampler();
    SampleResult responseText = JMeterContextService.getContext().getPreviousResult();
    if (responseText == null) {
      return;
    }
    initRegex(getArgumentName());
    String text = new String(responseText.getResponseData());
    Perl5Matcher matcher = JMeterUtils.getMatcher();
    String value = "";
    if (matcher.contains(text, case1)) {
      MatchResult result = matcher.getMatch();
      value = result.group(1);
    } else if (matcher.contains(text, case2)) {
      MatchResult result = matcher.getMatch();
      value = result.group(1);
    } else if (matcher.contains(text, case3)) {
      MatchResult result = matcher.getMatch();
      value = result.group(1);
    } else if (matcher.contains(text, case4)) {
      MatchResult result = matcher.getMatch();
      value = result.group(1);
    }

    modify((HTTPSampler) sampler, value);
  }
Beispiel #16
0
  // @RubyLevelMethod(name="[]")
  public RubyValue aref(RubyValue arg) {
    if (arg instanceof RubyFixnum) {
      int index = arg.toInt();
      if (index < 0) index += result_.groups();

      String res = result_.group(index);
      if (res == null) return RubyConstant.QNIL;

      return ObjectFactory.createString(res);
    } else if (arg instanceof RubyString) {
      // TODO: implement m = /(?<foo>a+)b/.match("ccaaab"); m["foo"]   #=> "aaa"
      throw new RuntimeException("Not implemented");
    }

    RubyArray ar = (RubyArray) to_a();
    return ar.aref(arg);
  }
Beispiel #17
0
  public RubyValue pre_match() {
    String res = "";
    int nMatchEnd = result_.beginOffset(0);
    if (nMatchEnd >= 0 && nMatchEnd < str_.length()) res = str_.substring(0, nMatchEnd);

    if (res == null) return RubyConstant.QNIL;

    return ObjectFactory.createString(res);
  }
Beispiel #18
0
	public static String[] parserValues(String values)
	{
		
		String patternStr = "([^\\,]*)[\\,]?"; // 解析字段值
		
		String patternStr1 = "('?[^\\,]*'?)[\\,]?"; // 解析字段值

		/**
		 * 编译正则表达式patternStr,并用该表达式与传入的sql语句进行模式匹配,
		 * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回 该数组
		 */
		String[] ret = RegexUtil.containWithPatternMatcherInput(values,patternStr1);

		PatternCompiler compiler = new Perl5Compiler();
		Pattern pattern = null;
		try
		{
			pattern = compiler.compile(patternStr,
					Perl5Compiler.CASE_INSENSITIVE_MASK);
		}
		catch (MalformedPatternException e)
		{
			e.printStackTrace();

			return null;
		}
		PatternMatcher matcher = new Perl5Matcher();
		MatchResult result = null;
		String[] tokens = null;
		boolean match = matcher.matches(values.trim(), pattern);
		System.out.println(match);
		if (match)
		{
			result = matcher.getMatch();
			tokens = new String[6];
			for (int i = 0; i < 6; i++)
			{
				tokens[i] = result.group(i + 1).trim();
				System.out.println(tokens[i]);
			}
		}

		return tokens;
	}
    public TableEntryLocation[] parseTableEntryLocations(String expression) {
      resultList.clear();
      if (expression != null) {
        matcher.setMultiline(true);
        if (patternMatcherInput == null) {
          patternMatcherInput = new PatternMatcherInput(expression);
        } else {
          patternMatcherInput.setInput(expression);
        }

        recompilePatternIfNecessary(locationPattern);

        while (matcher.contains(patternMatcherInput, pattern)) {
          MatchResult matchResult = matcher.getMatch();
          resultList.add(new TableEntryLocation(matchResult.group(1), matchResult.group(2)));
        }
      }
      return resultList.toArray(new TableEntryLocation[0]);
    }
Beispiel #20
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;
  }
Beispiel #21
0
 public void appendSubstitution(
     StringBuffer stringBuffer,
     MatchResult matchResult,
     int i,
     PatternMatcherInput patternMatcherInput,
     PatternMatcher patternMatcher,
     Pattern pattern) {
   String hex = matchResult.group(1);
   char c = (char) Integer.parseInt(hex, 16);
   stringBuffer.append(c);
 }
Beispiel #22
0
  /**
   * The function checks and handles the situation where another begin token is found in between the
   * first begin token and the first end token. Such first beginToken is ignored. The current input
   * to be passed in to the replacement function will be the string between the LAST begin token and
   * the FIRST end token found.
   *
   * @param beginToken the pattern to look for
   * @param current input string
   * @param resultBuffer the buffer to hold the result
   * @return returns a Vector containing the substrings of the input that occur
   */
  private static final String skipOrphanedBeginToken(
      String beginToken, String current, StringBuffer resultBuffer) {
    Perl5Util util = new Perl5Util();
    boolean nextBeginTokenMatch = true;
    MatchResult matchResult = null;
    String remainderStr = null;
    String subCurrent = null;
    StringBuffer currentBuffer = new StringBuffer();
    String pre = null;

    // subCurrent is copy of the current to check if there is any next beginTokens
    subCurrent = current;

    while (nextBeginTokenMatch) {
      nextBeginTokenMatch = util.match(beginToken, subCurrent);

      if (nextBeginTokenMatch == true) {
        // pre is the string before the beginToken
        pre = util.preMatch();
        currentBuffer.append(pre);

        // get the  matched beginToken
        matchResult = util.getMatch();
        currentBuffer.append(matchResult.toString());

        // get the remaining string after the beginToken
        remainderStr = util.postMatch();
        subCurrent = remainderStr;
      } else // there is no match
      {
        // appending the string before the last begin token to the result
        resultBuffer.append(currentBuffer.toString());

        // finally get the string to perform the substitution on
        current = subCurrent;
        break;
      }
    } // while

    return current;
  }
 /** Parse a string of the form [x1,y1},[x2,y2},...,[xN,yN} into a list of Points ([int,int]) */
 public static List<Point> parseString(String str) throws NumberFormatException {
   if (StringUtil.isNullString(str)) {
     throw new IllegalArgumentException("Must supply non-empty string");
   }
   ArrayList<Point> res = new ArrayList<Point>();
   Perl5Matcher matcher = RegexpUtil.getMatcher();
   while (matcher.contains(str, ONE_POINT_PAT)) {
     MatchResult matchResult = matcher.getMatch();
     String xstr = matchResult.group(1);
     String ystr = matchResult.group(2);
     str = matchResult.group(3);
     try {
       int x = Integer.parseInt(xstr);
       int y = Integer.parseInt(ystr);
       res.add(new Point(x, y));
     } catch (NumberFormatException e) {
       throw new IllegalArgumentException("bad point [" + xstr + "," + ystr + "] in " + str);
     }
   }
   res.trimToSize();
   return res;
 }
Beispiel #24
0
 private String generateResult(MatchResult match) {
   saveGroups(match);
   StringBuffer result = new StringBuffer();
   for (int a = 0; a < template.length; a++) {
     if (template[a] instanceof String) {
       result.append(template[a]);
     } else {
       result.append(match.group(((Integer) template[a]).intValue()));
     }
   }
   JMeterVariables vars = getVariables();
   vars.put(name, result.toString());
   return result.toString();
 }
Beispiel #25
0
 private String generateResult(MatchResult match) {
   StringBuilder result = new StringBuilder();
   for (Object obj : template) {
     if (log.isDebugEnabled()) {
       log.debug(
           "RegexExtractor: Template piece " + obj + " (" + obj.getClass().getSimpleName() + ")");
     }
     if (obj instanceof Integer) {
       result.append(match.group(((Integer) obj).intValue()));
     } else {
       result.append(obj);
     }
   }
   if (log.isDebugEnabled()) {
     log.debug("Regex Extractor result = " + result.toString());
   }
   return result.toString();
 }
  // Overridden Methods
  @Override
  protected void inspectContents(File file, String contents) {
    StringBuffer buf = new StringBuffer();

    // Split it into lines
    StringSplitter splitter = new StringSplitter(contents, getLineEnding());

    // Scan each line
    int totalMatchesForThisFile = 0;
    int lineCount = 1;
    while (splitter.hasMoreElements()) {
      String line = (String) splitter.nextElement();

      input = new PatternMatcherInput(line);
      try {
        while (util.match(regex, input)) {
          result = util.getMatch();
          totalMatchesForThisFile++;
          totalNumberOfMatches++;
          buf.append(
              "  line: " + lineCount + " position: " + result.beginOffset(0) + getLineEnding());
        }
      } catch (MalformedPerl5PatternException e) {
        System.out.println("MalformedPerl5PatternException: " + e.getMessage());
        System.out.println("Valid expression: [m]/pattern/[i][m][s][x]");
        return;
      }

      lineCount++;
    }

    // Output the results
    if (totalMatchesForThisFile > 0) {
      if (totalMatchesForThisFile == 1) {
        System.out.println(
            "Found " + totalMatchesForThisFile + " match in file: " + file.getPath());
      } else {
        System.out.println(
            "Found " + totalMatchesForThisFile + " matches in file: " + file.getPath());
      }
      System.out.print(buf.toString());
      System.out.println("");
    }
  }
Beispiel #27
0
 public String toString() {
   return result_.group(0);
 }
  private void processComponentStart(
      String tagName,
      String jwcId,
      boolean emptyTag,
      int startLine,
      int cursorStart,
      ILocation startLocation)
      throws TemplateParseException {
    if (jwcId.equalsIgnoreCase(CONTENT_ID)) {
      processContentTag(tagName, startLine, cursorStart, emptyTag);

      return;
    }

    boolean isRemoveId = jwcId.equalsIgnoreCase(REMOVE_ID);

    if (_ignoring && !isRemoveId)
      templateParseProblem(
          Tapestry.format(
              "TemplateParser.component-may-not-be-ignored", tagName, Integer.toString(startLine)),
          startLocation,
          startLine,
          cursorStart);

    String type = null;
    boolean allowBody = false;

    if (_patternMatcher.matches(jwcId, _implicitIdPattern)) {
      MatchResult match = _patternMatcher.getMatch();

      jwcId = match.group(IMPLICIT_ID_PATTERN_ID_GROUP);
      type = match.group(IMPLICIT_ID_PATTERN_TYPE_GROUP);

      String libraryId = match.group(IMPLICIT_ID_PATTERN_LIBRARY_ID_GROUP);
      String simpleType = match.group(IMPLICIT_ID_PATTERN_SIMPLE_TYPE_GROUP);

      // If (and this is typical) no actual component id was specified,
      // then generate one on the fly.
      // The allocated id for anonymous components is
      // based on the simple (unprefixed) type, but starts
      // with a leading dollar sign to ensure no conflicts
      // with user defined component ids (which don't allow dollar signs
      // in the id).

      if (jwcId == null) jwcId = _idAllocator.allocateId("$" + simpleType);

      try {
        allowBody = _delegate.getAllowBody(libraryId, simpleType, startLocation);
      } catch (ApplicationRuntimeException e) {
        // give subclasses a chance to handle and rethrow
        templateParseProblem(e, startLine, cursorStart);
      }

    } else {
      if (!isRemoveId) {
        if (!_patternMatcher.matches(jwcId, _simpleIdPattern))
          templateParseProblem(
              Tapestry.format(
                  "TemplateParser.component-id-invalid",
                  tagName,
                  Integer.toString(startLine),
                  jwcId),
              startLocation,
              startLine,
              cursorStart);

        if (!_delegate.getKnownComponent(jwcId))
          templateParseProblem(
              Tapestry.format(
                  "TemplateParser.unknown-component-id",
                  tagName,
                  Integer.toString(startLine),
                  jwcId),
              startLocation,
              startLine,
              cursorStart);

        try {
          allowBody = _delegate.getAllowBody(jwcId, startLocation);
        } catch (ApplicationRuntimeException e) {
          // give subclasses a chance to handle and rethrow
          templateParseProblem(e, startLine, cursorStart);
        }
      }
    }

    // Ignore the body if we're removing the entire tag,
    // of if the corresponding component doesn't allow
    // a body.

    boolean ignoreBody = !emptyTag && (isRemoveId || !allowBody);

    if (_ignoring && ignoreBody)
      templateParseProblem(
          Tapestry.format("TemplateParser.nested-ignore", tagName, Integer.toString(startLine)),
          new Location(_resourceLocation, startLine),
          startLine,
          cursorStart);

    if (!emptyTag) pushNewTag(tagName, startLine, isRemoveId, ignoreBody);

    // End any open block.

    addTextToken(cursorStart - 1);

    if (!isRemoveId) {
      addOpenToken(tagName, jwcId, type, startLocation);

      if (emptyTag) _tokens.add(_factory.createCloseToken(tagName, getCurrentLocation()));
    }

    advance();
  }
Beispiel #29
0
 // @RubyLevelMethod(name="to_s")
 public RubyString to_s() {
   return ObjectFactory.createString(result_.toString());
 }
  /** {@inheritDoc} */
  @Override
  public Iterator<URL> getEmbeddedResourceURLs(
      String userAgent, byte[] html, URL baseUrl, URLCollection urls, String encoding)
      throws HTMLParseException {
    Pattern pattern = null;
    Perl5Matcher matcher = null;
    try {
      matcher = JMeterUtils.getMatcher();
      PatternMatcherInput input = localInput.get();
      // TODO: find a way to avoid the cost of creating a String here --
      // probably a new PatternMatcherInput working on a byte[] would do
      // better.
      input.setInput(new String(html, encoding));
      pattern =
          JMeterUtils.getPatternCache()
              .getPattern(
                  REGEXP,
                  Perl5Compiler.CASE_INSENSITIVE_MASK
                      | Perl5Compiler.SINGLELINE_MASK
                      | Perl5Compiler.READ_ONLY_MASK);

      while (matcher.contains(input, pattern)) {
        MatchResult match = matcher.getMatch();
        String s;
        if (log.isDebugEnabled()) {
          log.debug("match groups " + match.groups() + " " + match.toString());
        }
        // Check for a BASE HREF:
        for (int g = 1; g <= NUM_BASE_GROUPS && g <= match.groups(); g++) {
          s = match.group(g);
          if (s != null) {
            if (log.isDebugEnabled()) {
              log.debug("new baseUrl: " + s + " - " + baseUrl.toString());
            }
            try {
              baseUrl = ConversionUtils.makeRelativeURL(baseUrl, s);
            } catch (MalformedURLException e) {
              // Doesn't even look like a URL?
              // Maybe it isn't: Ignore the exception.
              if (log.isDebugEnabled()) {
                log.debug("Can't build base URL from RL " + s + " in page " + baseUrl, e);
              }
            }
          }
        }
        for (int g = NUM_BASE_GROUPS + 1; g <= match.groups(); g++) {
          s = match.group(g);
          if (s != null) {
            if (log.isDebugEnabled()) {
              log.debug("group " + g + " - " + match.group(g));
            }
            urls.addURL(s, baseUrl);
          }
        }
      }
      return urls.iterator();
    } catch (UnsupportedEncodingException e) {
      throw new HTMLParseException(e.getMessage(), e);
    } catch (MalformedCachePatternException e) {
      throw new HTMLParseException(e.getMessage(), e);
    } finally {
      JMeterUtils.clearMatcherMemory(matcher, pattern);
    }
  }