Example #1
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 replaceFirst(String pattern, String input, String replacement)
      throws FrameworkException {
    if ((pattern == null) || (input == null) || (replacement == null)) {
      throw new FrameworkException(
          "RegexUtil: replaceFirst(): pattern or input cannot be null. "
              + "pattern = "
              + pattern
              + "input = "
              + input
              + "replacement = "
              + replacement);
    }

    String result = null;
    String perl5SubstitutionPattern = null;
    Perl5Util util = new Perl5Util();

    // default return value
    result = input;

    perl5SubstitutionPattern = makePerl5SubstitutionPattern(pattern, replacement);
    pattern = makePerl5MatchPattern(pattern);

    if (util.match(pattern, input)) {
      result = util.substitute(perl5SubstitutionPattern, input);
    }

    return result;
  }
Example #2
0
  /**
   * This function takes a perl5 regular expression as the pattern to perform pattern matching and
   * string substitution.
   *
   * <p>"s/pattern/replacement/[g][i][m][o][s][x]"
   *
   * <p>g - substitute globally (all occurence) i - case insensitive m - treat the input as
   * consisting of multiple lines o - interpolate once s - treat the input as consisting of a single
   * line x - enable extended expression syntax incorporating whitespace and comments
   *
   * <p>to perform string substitution. Unless the [g] option is specified, the dafault is to
   * replace only the first occurrence.
   *
   * @param perl5Pattern - Perl5 regular expression
   * @param input - input string
   * @return result - processed string. The original input is returned when there is no match
   * @exception - FrameworkException is thrown when either pattern or input is null
   */
  public static String substitute(String perl5Pattern, String input) throws FrameworkException {
    if ((perl5Pattern == null) || (input == null)) {
      throw new FrameworkException(
          "RegexUtil: replaceAll(): Either input or pattern is null."
              + "input = "
              + input
              + ", "
              + "pattern = "
              + perl5Pattern);
    }

    if (Debug.isLevelEnabled(Debug.MSG_STATUS)) {
      Debug.log(
          Debug.MSG_STATUS,
          "RegexUtils: replaceAll: perl5Pattern = " + perl5Pattern + " input = " + input);
    }

    Perl5Util util = new Perl5Util();
    String result = input;

    try {
      result = util.substitute(perl5Pattern, input);
    } catch (RuntimeException e) {
      throw new FrameworkException("RegexUtils: substitute: " + e.getMessage());
    }

    return result;
  }
  public String filter(String in) {
    String pat = "/" + Filter.RELATIVE_TIME_PAT + "/";

    if (util.match(pat, in)) {
      // System.out.println("Removing relative time from line ["+in+"]");
      return util.substitute("s/" + Filter.RELATIVE_TIME_PAT + "//", in);
    } else {
      return in;
    }
  }
 public long getLastModified(HttpServletRequest request) {
   if (Calendar.getInstance().getTimeInMillis() > 0)
     return Calendar.getInstance()
         .getTimeInMillis(); // comment this line if you want allow browser to check when resource
                             // was last modified
   String userID = (String) request.getSession().getAttribute(SportletProperties.PORTLET_USER);
   if (userID == null || userID.equals("")) {
     if (DEBUG)
       log(
           "LastModifiedRequest blocked (userID="
               + userID
               + ") !!! Request: "
               + request.getRequestURI()
               + "\nIP: "
               + request.getRemoteAddr()
               + "\n");
     return Calendar.getInstance().getTimeInMillis();
   } else if (!inited) {
     return Calendar.getInstance().getTimeInMillis();
   } else {
     String userDirPath = secureDirPath + "/" + userID;
     if (!(new File(userDirPath).isDirectory())) {
       if (DEBUG)
         log(
             "LastModifiedRequest blocked (userDirPath="
                 + userDirPath
                 + " is not directory) !!! Request: "
                 + request.getRequestURI()
                 + "\nIP: "
                 + request.getRemoteAddr()
                 + "\n");
       return Calendar.getInstance().getTimeInMillis();
     } else {
       String resourcePath =
           util.substitute(
               "s!" + request.getContextPath() + request.getServletPath() + "!!",
               request.getRequestURI());
       File resource = new File(userDirPath + resourcePath);
       if (!resource.exists()) {
         log(
             "LastModifiedRequest blocked (Not found, resource="
                 + userDirPath
                 + resourcePath
                 + ") !!! Request: "
                 + request.getRequestURI()
                 + "\nIP: "
                 + request.getRemoteAddr()
                 + "\n");
         return new Date().getTime();
       } else {
         return resource.lastModified();
       }
     }
   }
 }
Example #5
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();
  }
Example #6
0
  /**
   * This function substitues ALL occurence of the pattern in the input with the replacement passed
   * in. This function can handle such situation as when some characters in the replacement is also
   * used in the pattern.
   *
   * <p>i.e.) pattern - & or &/[option] replacement - &amp or &amp/[option]
   *
   * @param pattern pattern string to match
   * @param input input string
   * @param replacement replacement string to be replaced with
   * @return returns processed string. Default return value is the original input string.
   * @exception throws FrameworkException when either pattern/input/replacement is null.
   */
  public static String replaceAll(String pattern, String input, String replacement)
      throws FrameworkException {
    if ((pattern == null) || (replacement == null) || (input == null)) {
      Debug.log(
          Debug.ALL_ERRORS,
          "RegexUtils.replaceAll(): pattern or replacement or input" + "is null.");

      throw new FrameworkException(
          "ERROR: RegexUtils: replaceAll(): pattern or replacement or input is null.");
    }

    Perl5Util util = new Perl5Util();

    String negativeLookAheadPattern = makePerl5MatchPatternNegativeLookAhead(pattern, replacement);
    String substitutionPattern =
        makePerl5SubstitutionPattern(negativeLookAheadPattern, replacement);

    String result = util.substitute(substitutionPattern, input);

    return result;
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String userID = (String) request.getSession().getAttribute(SportletProperties.PORTLET_USER);
    /*
            if (userID == null || userID.equals("")) {
                if (DEBUG)
                    log("Request blocked (userID=" + userID + ") !!! Request: " + request.getRequestURI() + "\nIP: " + request.getRemoteAddr() + "\n");
                response.setStatus(403);
            } else
    */
    if (!inited) {
      response.setStatus(503);
    } else {

      Enumeration params = request.getParameterNames();
      String saveAs = null;
      String contentType = null;
      boolean shared = false;
      while (params.hasMoreElements()) {
        String paramName = (String) params.nextElement();
        if (util.match("/(.+_)?saveAs/", paramName)) {
          saveAs = request.getParameter(paramName);
          if (contentType != null && shared) break;
        } else if (util.match("/(.+_)?contentType/", paramName)) {
          contentType = request.getParameter(paramName);
          if (saveAs != null && shared) break;
        } else if (util.match("/(.+_)?shared/", paramName)) {
          String sharedString = request.getParameter(paramName);
          if (sharedString.equals("true")) shared = true;
          if (saveAs != null && contentType != null) break;
        }
      }

      if (userID == null || userID.equals("") || shared) {
        if (DEBUG)
          log(
              "No userID - request redirected to GUEST. Request: "
                  + request.getRequestURI()
                  + "\nIP: "
                  + request.getRemoteAddr()
                  + "\n");
        userID = GUEST_SECUREDIR;
      }
      String userDirPath = secureDirPath + "/" + userID;
      if (!(new File(userDirPath).isDirectory())) {
        if (DEBUG)
          log(
              "Request blocked (userDirPath="
                  + userDirPath
                  + " is not directory) !!! Request: "
                  + request.getRequestURI()
                  + "\nIP: "
                  + request.getRemoteAddr()
                  + "\n");

        response.setStatus(403);
      } else {
        String resourcePath =
            util.substitute(
                "s!" + request.getContextPath() + request.getServletPath() + "!!",
                request.getRequestURI());
        File resource = new File(userDirPath + resourcePath);
        if (!resource.canRead() || resource.isDirectory()) {
          if (DEBUG)
            log(
                "Request blocked (Not found, resource="
                    + userDirPath
                    + resourcePath
                    + ") !!! Request: "
                    + request.getRequestURI()
                    + "\nIP: "
                    + request.getRemoteAddr()
                    + "\n");

          response.setStatus(404);
        } else {
          /*
                              Enumeration params = request.getParameterNames();
                              String saveAs = null;
                              String contentType = null;
                              while (params.hasMoreElements()) {
                                  String paramName = (String) params.nextElement();
                                  if (util.match("/(.+_)?saveAs/", paramName)) {
                                      saveAs = request.getParameter(paramName);
                                      if (contentType != null)
                                          break;
                                  } else if (util.match("/(.+_)?contentType/", paramName)) {
                                      contentType = request.getParameter(paramName);
                                      if (saveAs != null)
                                          break;
                                  }
                              }
          */

          if (contentType == null) contentType = getServletContext().getMimeType(resourcePath);
          setHeaders(request, response, saveAs, contentType, resource.length());

          ServletOutputStream output = response.getOutputStream();
          FileInputStream input = new FileInputStream(resource);
          rewrite(input, output);
          input.close();
        }
      }
    }
  }
 protected String trim(String content_with_htmltag) {
   Perl5Util util = new Perl5Util();
   String content_without_htmltag = util.substitute("s/<.*?>|\\s//g", content_with_htmltag);
   return content_without_htmltag;
 }
Example #9
0
  public RElement createRGGElement(Element element, RGG rggInstance) {
    if (element.getNodeType() != Element.ELEMENT_NODE)
      throw new IllegalArgumentException("elements node type must be ELEMENT_NODE");

    /**
     * **************** initialize and set attributes values *************************************
     */
    String text = element.getAttribute(RGG.getConfiguration().getString("TEXT"));
    String colspan = element.getAttribute(RGG.getConfiguration().getString("COLUMN-SPAN"));
    String alignment = element.getAttribute(RGG.getConfiguration().getString("ALIGNMENT"));
    String enabled = element.getAttribute(RGG.getConfiguration().getString("ENABLED"));
    /**
     * ********************************************************************************************
     */
    Perl5Util util = new Perl5Util();

    VLabel vlabel = new VLabel(text);
    if (StringUtils.isNotBlank(colspan)) {
      if (StringUtils.isNumeric(colspan)) {
        vlabel.setColumnSpan(Integer.parseInt(colspan));
      } else if (StringUtils.equals(colspan, RGG.getConfiguration().getString("FULL-SPAN")))
        vlabel.setColumnSpan(LayoutInfo.FULL_SPAN);
      else
        throw new NumberFormatException(
            RGG.getConfiguration().getString("COLUMN-SPAN")
                + " seems not to be a number: "
                + colspan);
    }

    if (StringUtils.isNotBlank(alignment)) {
      if (StringUtils.equalsIgnoreCase(RGG.getConfiguration().getString("CENTER"), alignment)) {
        vlabel.setHorizontalAlignment(SwingConstants.CENTER);
      } else if (StringUtils.equalsIgnoreCase(
          RGG.getConfiguration().getString("RIGHT"), alignment)) {
        vlabel.setHorizontalAlignment(SwingConstants.RIGHT);
      } else if (StringUtils.equalsIgnoreCase(
          RGG.getConfiguration().getString("LEFT"), alignment)) {
        vlabel.setHorizontalAlignment(SwingConstants.LEFT);
      } else if (StringUtils.equalsIgnoreCase(RGG.getConfiguration().getString("TOP"), alignment)) {
        vlabel.setHorizontalAlignment(SwingConstants.TOP);
      } else if (StringUtils.equalsIgnoreCase(
          RGG.getConfiguration().getString("BOTTOM"), alignment)) {
        vlabel.setHorizontalAlignment(SwingConstants.BOTTOM);
      }
    }

    if (StringUtils.isNotBlank(enabled)) {
      if (util.match("/(\\w+)\\./", enabled)) {
        String id = util.group(1);
        enabled = util.substitute("s/" + id + "\\.//g", enabled);
        AutoBinding<Object, Object, Object, Object> binding =
            Bindings.createAutoBinding(
                AutoBinding.UpdateStrategy.READ, // one-way binding
                rggInstance.getObject(id), // source of value
                ELProperty.create(enabled), // the property to get
                vlabel, // the "backing bean"
                BeanProperty.create("enabled") // property to set
                );
        binding.bind();
      }
    }

    RLabel rLabel = new RLabel(vlabel);
    if (element.hasChildNodes()) { // it can only be <iport>
      setInputPorts(rLabel, element);
    }
    return rLabel;
  }