Example #1
0
  public String preTranslate(WikiContext context, String content) {
    for (int i = 0; i < c_profanities.length; i++) {
      String word = c_profanities[i];
      String replacement = word.charAt(0) + "*" + word.charAt(word.length() - 1);

      content = TextUtil.replaceString(content, word, replacement);
    }

    return content;
  }
Example #2
0
  private String replaceReferrerString(
      WikiContext context, String sourceText, String from, String to) {
    StringBuilder sb = new StringBuilder(sourceText.length() + 32);

    //
    //  This monstrosity just looks for a JSPWiki link pattern.  But it is pretty
    //  cool for a regexp, isn't it?  If you can understand this in a single reading,
    //  you have way too much time in your hands.
    //
    Pattern linkPattern =
        Pattern.compile("([\\[\\~]?)\\[([^\\|\\]]*)(\\|)?([^\\|\\]]*)(\\|)?([^\\|\\]]*)\\]");

    Matcher matcher = linkPattern.matcher(sourceText);

    int start = 0;

    // System.out.println("====");
    // System.out.println("SRC="+sourceText.trim());
    while (matcher.find(start)) {
      char charBefore = (char) -1;

      if (matcher.start() > 0) charBefore = sourceText.charAt(matcher.start() - 1);

      if (matcher.group(1).length() > 0 || charBefore == '~' || charBefore == '[') {
        //
        //  Found an escape character, so I am escaping.
        //
        sb.append(sourceText.substring(start, matcher.end()));
        start = matcher.end();
        continue;
      }

      String text = matcher.group(2);
      String link = matcher.group(4);
      String attr = matcher.group(6);

      /*
      System.out.println("MATCH="+matcher.group(0));
      System.out.println("   text="+text);
      System.out.println("   link="+link);
      System.out.println("   attr="+attr);
      */
      if (link.length() == 0) {
        text = replaceSingleLink(context, text, from, to);
      } else {
        link = replaceSingleLink(context, link, from, to);

        //
        //  A very simple substitution, but should work for quite a few cases.
        //
        text = TextUtil.replaceString(text, from, to);
      }

      //
      //  Construct the new string
      //
      sb.append(sourceText.substring(start, matcher.start()));
      sb.append("[" + text);
      if (link.length() > 0) sb.append("|" + link);
      if (attr.length() > 0) sb.append("|" + attr);
      sb.append("]");

      start = matcher.end();
    }

    sb.append(sourceText.substring(start));

    return sb.toString();
  }