Package gnu.regexp

Examples of gnu.regexp.RE


  private boolean extractRegExp(String line) {

    try {
      String delim;

      RE findDelim = new RE("[$%@].+[\\s]*=~[\\s]*[m]?(.)", 0,
          RESyntax.RE_SYNTAX_PERL5);

      REMatch match = findDelim.getMatch(line);
      if (match == null)
        return false;
      delim = match.toString(1);
      if (delim == null)
        return false;
      String temp = line;
      temp.replaceAll("\\" + delim, "xx");
      RE findRegExp = new RE("([$%@][^\\s]+)[\\s]*=~[\\s]*[m]?" + delim
          + "(.*)" + delim + "(.*)", 0, RESyntax.RE_SYNTAX_PERL5);
      match = findRegExp.getMatch(temp);
      if (match == null)
        return false;

      String var = line.substring(match.getStartIndex(1), match
          .getEndIndex(1));
View Full Code Here


      this.name = name;
      label = jEdit.getProperty("macro-handler."
        + name + ".label", name);
      try
      {
        filter = new RE(MiscUtilities.globToRE(
          jEdit.getProperty(
          "macro-handler." + name + ".glob")));
      }
      catch (Exception e)
      {
View Full Code Here

          invalidPatterns.add(pattern);
        }
        final String strContainKey = new String(tempBuffer.delete(0,
            SPConstants.CONTAINS.length()));
        try {
          new RE(strContainKey); // with case
        } catch (final Exception e) {
          invalidPatterns.add(pattern);
        }
        continue;
      }

      if (pattern.startsWith(SPConstants.REGEXP)) {
        final StringBuffer tempBuffer = new StringBuffer(pattern);
        if (tempBuffer == null) {
          invalidPatterns.add(pattern);
        }
        final String strRegexPattrn = new String(tempBuffer.delete(0,
            SPConstants.REGEXP.length()));
        try {
          new RE(strRegexPattrn);
        } catch (final Exception e) {
          invalidPatterns.add(pattern);
        }
        continue;
      }

      if (pattern.startsWith(SPConstants.REGEXP_CASE)) {
        final StringBuffer tempBuffer = new StringBuffer(pattern);
        if (tempBuffer == null) {
          invalidPatterns.add(pattern);
        }
        final String strRegexCasePattrn = new String(tempBuffer.delete(0,
            SPConstants.REGEXP_CASE.length()));
        try {
          new RE(strRegexCasePattrn);
        } catch (final Exception e) {
          invalidPatterns.add(pattern);
        }
        continue;
      }

      if (pattern.startsWith(SPConstants.REGEXP_IGNORE_CASE)) {
        final StringBuffer tempBuffer = new StringBuffer(pattern);
        if (tempBuffer == null) {
          invalidPatterns.add(pattern);
        }
        final String strRegexIgnoreCasePattrn = new String(tempBuffer.delete(0,
            SPConstants.REGEXP_IGNORE_CASE.length()));
        try {
          new RE(strRegexIgnoreCasePattrn, RE.REG_ICASE); // ignore
          // case
        } catch (final Exception e) {
          invalidPatterns.add(pattern);
        }
        continue;
      }

      if (pattern.startsWith(SPConstants.CARET)
          || pattern.endsWith(SPConstants.DOLLAR)) {
        StringBuffer tempBuffer = new StringBuffer(pattern);
        boolean bDollar = false;
        if (pattern.startsWith(SPConstants.CARET)) {
          tempBuffer = new StringBuffer(pattern);
          final int indexOfStar = tempBuffer.indexOf("*");
          if (indexOfStar != -1) {
            tempBuffer.replace(indexOfStar, indexOfStar + "*".length(),
                "[0-9].*");
          } else {
            tempBuffer.delete(0, "^".length());
            if (pattern.endsWith(SPConstants.DOLLAR)) {
              bDollar = true;
              tempBuffer.delete(
                  tempBuffer.length() - SPConstants.DOLLAR.length(),
                  tempBuffer.length());
            }
            try {
              final URL urlPatt = new URL(tempBuffer.toString());
              final int port = urlPatt.getPort();
              final String strHost = urlPatt.getHost().toString();
              if ((port == -1) && (strHost != null) && (strHost.length() != 0)) {
                tempBuffer = new StringBuffer("^" + urlPatt.getProtocol()
                    + SPConstants.URL_SEP + urlPatt.getHost() + ":[0-9].*"
                    + urlPatt.getPath());
              }
              if (bDollar) {
                tempBuffer.append(SPConstants.DOLLAR);
              }
            } catch (final Exception e) {
              tempBuffer = new StringBuffer(pattern);
            }
          }
        }

        try {
          new RE(tempBuffer);
        } catch (final Exception e) {
          invalidPatterns.add(pattern);
        }
        continue;
      }

      String patternDecoded = pattern;
      try {
        patternDecoded = URLDecoder.decode(pattern, "UTF-8");
      } catch (final Exception e) {
        // eatup exception. use the original value
        patternDecoded = pattern;
      }

      if (patternDecoded == null) {
        invalidPatterns.add(pattern);
      }

      boolean containProtocol = false;
      try {
        final RE re = new RE(SPConstants.URL_SEP);
        final REMatch reMatch = re.getMatch(patternDecoded);
        if (reMatch != null) {
          containProtocol = true; // protocol is present
        }

      } catch (final Exception e) {
        containProtocol = false;
      }

      if (containProtocol) {
        String urlPatt1stPart = null;
        String urlPatt2ndPart = null;
        boolean bPortStar = false;
        try {
          final URL urlPatt = new URL(patternDecoded);
          final int port = urlPatt.getPort();
          String strPort = "";
          if (port == -1) {
            strPort = "[0-9].*";
          } else {
            strPort = port + "";
          }
          urlPatt1stPart = "^" + urlPatt.getProtocol() + SPConstants.URL_SEP
              + urlPatt.getHost() + SPConstants.COLON + strPort;
          if (!(urlPatt.getFile()).startsWith(SPConstants.SLASH)) { // The pattern must have "/"
                                                                    // after the port
            invalidPatterns.add(pattern);
          }
          urlPatt2ndPart = "^" + urlPatt.getFile();
        } catch (final Exception e) {
          bPortStar = true;
        }

        if (bPortStar) {
          final int indexOfStar = patternDecoded.indexOf("*");
          if (indexOfStar != -1) {
            urlPatt1stPart = "^" + patternDecoded.substring(0, indexOfStar)
                + "[0-9].*";
            if (!(patternDecoded.substring(indexOfStar + 1))
                .startsWith(SPConstants.SLASH)) {
              invalidPatterns.add(pattern);
            }
            urlPatt2ndPart = "^" + patternDecoded.substring(indexOfStar + 1);
          }
        }

        try {
          new RE(urlPatt1stPart);
          new RE(urlPatt2ndPart);
        } catch (final Exception e) {
          invalidPatterns.add(pattern);
        }
      } else {
        String urlPatt1stPart = null;
        String urlPatt2ndPart = null;
        if (patternDecoded.indexOf(SPConstants.SLASH) != -1) {
          if (patternDecoded.indexOf(SPConstants.COLON) == -1) {
            urlPatt1stPart = patternDecoded.substring(0,
                patternDecoded.indexOf(SPConstants.SLASH))
                + ":[0-9].*";
          } else {
            urlPatt1stPart = patternDecoded.substring(0,
                patternDecoded.indexOf(SPConstants.SLASH));
          }
          urlPatt2ndPart = patternDecoded.substring(patternDecoded
              .indexOf(SPConstants.SLASH));
        } else {
          invalidPatterns.add(pattern);
        }

        urlPatt1stPart = "^.*://.*" + urlPatt1stPart;
        urlPatt2ndPart = "^" + urlPatt2ndPart;
        try {
          new RE(urlPatt1stPart);
          new RE(urlPatt2ndPart);
        } catch (final Exception e) {
          invalidPatterns.add(pattern);
        }
      }
    }
View Full Code Here

    // string in pattern
    if (pattern.startsWith(SPConstants.CONTAINS)) {
      final StringBuffer tempBuffer = new StringBuffer(pattern);
      final String strContainKey = new String(
          tempBuffer.delete(0, SPConstants.CONTAINS.length()));
      RE re;
      try {
        re = new RE(strContainKey); // with case
        final REMatch reMatch = re.getMatch(strValue);
        if (reMatch != null) {
          return true;
        }
        return false;
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    }

    // handle regexp
    // if pattern starts with "regexp:", then check for regex match with
    // case
    if (pattern.startsWith(SPConstants.REGEXP)) {
      final StringBuffer tempBuffer = new StringBuffer(pattern);
      final String strRegexPattrn = new String(
          tempBuffer.delete(0, SPConstants.REGEXP.length()));
      RE re;
      try {
        re = new RE(strRegexPattrn);
        final REMatch reMatch = re.getMatch(strValue);
        if (reMatch != null) {
          return true;
        }
        return false;
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    }

    // handle regexpCase
    // if pattern starts with "regexpCase:", then check for regex match with
    // case
    if (pattern.startsWith(SPConstants.REGEXP_CASE)) {
      final StringBuffer tempBuffer = new StringBuffer(pattern);
      final String strRegexCasePattrn = new String(
          tempBuffer.delete(0, SPConstants.REGEXP_CASE.length()));
      RE re;
      try {
        re = new RE(strRegexCasePattrn);
        final REMatch reMatch = re.getMatch(strValue);
        if (reMatch != null) {
          return true;
        }
        return false;
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    }

    // handle regexpIgnoreCase
    // if pattern starts with "regexpIgnoreCase:", then check for regex
    // match without case
    if (pattern.startsWith(SPConstants.REGEXP_IGNORE_CASE)) {
      final StringBuffer tempBuffer = new StringBuffer(pattern);
      final String strRegexIgnoreCasePattrn = new String(
          tempBuffer.delete(0, SPConstants.REGEXP_IGNORE_CASE.length()));
      RE re;
      try {
        re = new RE(strRegexIgnoreCasePattrn, RE.REG_ICASE); // ignore
        // case
        final REMatch reMatch = re.getMatch(strValue);
        if (reMatch != null) {
          return true;
        }
        return false;
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    }

    // handle "^" and "$"
    if (pattern.startsWith(SPConstants.CARET)
        || pattern.endsWith(SPConstants.DOLLAR)) {
      StringBuffer tempBuffer = new StringBuffer(pattern);
      boolean bDollar = false;
      String strValueModified = strValue;
      if (pattern.startsWith(SPConstants.CARET)) {
        URL urlValue;
        try {
          urlValue = new URL(strValue);
          int port = urlValue.getPort();
          if (port == -1) {
            port = urlValue.getDefaultPort();
            strValueModified = urlValue.getProtocol() + SPConstants.URL_SEP
                + urlValue.getHost() + SPConstants.COLON + port
                + urlValue.getFile();
          }
        } catch (final MalformedURLException e1) {
          LOGGER.log(Level.FINE, e1.getMessage());
          return false;
        }
        tempBuffer = new StringBuffer(pattern);
        final int indexOfStar = tempBuffer.indexOf("*");
        if (indexOfStar != -1) {
          tempBuffer.replace(indexOfStar, indexOfStar + "*".length(), "[0-9].*");
        } else {
          tempBuffer.delete(0, "^".length());
          if (pattern.endsWith(SPConstants.DOLLAR)) {
            bDollar = true;
            tempBuffer.delete(tempBuffer.length() - SPConstants.DOLLAR.length(), tempBuffer.length());
          }
          try {
            final URL urlPatt = new URL(tempBuffer.toString());
            final int port = urlPatt.getPort();

            final String strHost = urlPatt.getHost().toString();

            if ((port == -1) && (strHost != null) && (strHost.length() != 0)) {
              tempBuffer = new StringBuffer("^" + urlPatt.getProtocol()
                  + SPConstants.URL_SEP + urlPatt.getHost() + ":[0-9].*"
                  + urlPatt.getPath());
            }
            if (bDollar) {
              tempBuffer.append(SPConstants.DOLLAR);
            }
          } catch (final MalformedURLException e) {
            LOGGER.log(Level.FINE, e.getMessage());
            tempBuffer = new StringBuffer(pattern);
          }
        }
      }

      RE re;
      try {
        re = new RE(tempBuffer);
        final REMatch reMatch = re.getMatch(strValueModified);
        if (reMatch != null) {
          return true;
        }
        return false;
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    }

    // url decode the pattern
    String patternDecoded = pattern;
    try {
      patternDecoded = URLDecoder.decode(pattern, "UTF-8");
    } catch (final Exception e) {
      LOGGER.log(Level.FINE, e.getMessage());
      patternDecoded = pattern;
    }

    if (patternDecoded == null) {
      return false;
    }

    boolean containProtocol = false;
    try {
      final RE re = new RE(SPConstants.URL_SEP);
      final REMatch reMatch = re.getMatch(patternDecoded);
      if (reMatch != null) {
        containProtocol = true; // protocol is present
      }
    } catch (final REException e) {
      containProtocol = false;
    }

    if (containProtocol) {
      // split the test URL into two parts
      String urlValue1stPart = null;
      String urlValue2ndPart = null;

      URL urlValue;
      try {
        urlValue = new URL(strValue);
        int port = urlValue.getPort();
        if (port == -1) {
          port = urlValue.getDefaultPort();
        }
        urlValue1stPart = urlValue.getProtocol() + SPConstants.URL_SEP
            + urlValue.getHost() + SPConstants.COLON + port;
        urlValue2ndPart = urlValue.getFile();

        if (urlValue2ndPart != null) {
          if (!urlValue2ndPart.startsWith(SPConstants.SLASH)) {
            urlValue2ndPart = SPConstants.SLASH + urlValue2ndPart;
          }
        }
      } catch (final MalformedURLException e1) {
        LOGGER.log(Level.FINE, e1.getMessage());
        return false;
      }

      // split the pattern into two parts
      String urlPatt1stPart = null;
      String urlPatt2ndPart = null;
      boolean bPortStar = false;
      try {
        final URL urlPatt = new URL(patternDecoded);
        final int port = urlPatt.getPort();
        String strPort = "";
        if (port == -1) {
          strPort = "[0-9].*";
        } else {
          strPort = port + "";
        }
        urlPatt1stPart = "^" + urlPatt.getProtocol() + SPConstants.URL_SEP
            + urlPatt.getHost() + SPConstants.COLON + strPort;
        if (!(urlPatt.getFile()).startsWith(SPConstants.SLASH)) { // The
          // pattern
          // must
          // have
          // "/"
          // at
          // after
          // the
          // port
          return false;
        }
        urlPatt2ndPart = "^" + urlPatt.getFile();
      } catch (final MalformedURLException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        bPortStar = true;
      }

      if (bPortStar) {
        final int indexOfStar = patternDecoded.indexOf("*");
        if (indexOfStar != -1) {
          urlPatt1stPart = "^" + patternDecoded.substring(0, indexOfStar)
              + "[0-9].*";
          if (!(patternDecoded.substring(indexOfStar + 1)).startsWith(SPConstants.SLASH)) {
            return false;
          }
          urlPatt2ndPart = "^" + patternDecoded.substring(indexOfStar + 1);
        }
      }

      // check 1st part of both with ignorecase
      RE re;
      try {
        re = new RE(urlPatt1stPart, RE.REG_ICASE); // ignore case for
        // 1st part
        REMatch reMatch = re.getMatch(urlValue1stPart);
        if (reMatch != null) {
          // check 2nd part of both with case
          re = new RE(urlPatt2ndPart);
          reMatch = re.getMatch(urlValue2ndPart);
          if (reMatch != null) {
            return true;
          }
        }
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      } catch (final Exception e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    } else {
      String pat1 = null;
      String pat2 = null;
      // split the pattern into two parts
      if (patternDecoded.indexOf(SPConstants.SLASH) != -1) {
        if (patternDecoded.indexOf(SPConstants.COLON) == -1) {
          pat1 = patternDecoded.substring(0, patternDecoded.indexOf(SPConstants.SLASH))
              + ":[0-9].*";
        } else {
          pat1 = patternDecoded.substring(0, patternDecoded.indexOf(SPConstants.SLASH));
        }
        pat2 = patternDecoded.substring(patternDecoded.indexOf(SPConstants.SLASH));
      } else {
        // The pattern must have "/" at after the port
        return false;
      }

      pat1 = "^.*://.*" + pat1;
      pat2 = "^" + pat2;
      URL urlValue;
      try {
        urlValue = new URL(strValue);
        int port = urlValue.getPort();
        if (port == -1) {
          port = urlValue.getDefaultPort();
        }
        final String urlValue1stPart = urlValue.getProtocol()
            + SPConstants.URL_SEP + urlValue.getHost() + SPConstants.COLON
            + port;
        String urlValue2ndPart = urlValue.getFile();

        if (urlValue2ndPart != null) {
          if (!urlValue2ndPart.startsWith(SPConstants.SLASH)) {
            urlValue2ndPart = SPConstants.SLASH + urlValue2ndPart;
          }
        }

        RE re;
        try {
          re = new RE(pat1, RE.REG_ICASE); // ignore case for 1st part
          REMatch reMatch = re.getMatch(urlValue1stPart);
          if (reMatch != null) {
            re = new RE(pat2); // with case for 2nd part
            reMatch = re.getMatch(urlValue2ndPart);
            if (reMatch != null) {
              return true;
            }
          }
        } catch (final REException e) {
View Full Code Here

//SIMPLE MATCHING OF LIST OF STRINGS AND PATTERN
  public Vector applyPattern(String pattern, List fileInfos) {
    Vector v = new Vector();
    try {
//log.debug("applyPat: pattern: " + pattern);
      RE re = new RE(pattern);
      String temp = null;

      Iterator it = fileInfos.iterator();
      while (it.hasNext()) {
        FileInfo finf = (FileInfo) it.next();
//log.debug("applyPat: candidate: " + finf.getFileName().getName());
        temp = finf.getFileName().getName().toString().replace('.', '\t');
        temp = temp.replace('-', '\n');

        if (re.isMatch(temp)) {
          v.add(finf);
//log.debug("Match! " + finf.getFileName().getName());
        }
      }
    } catch (REException e) {
View Full Code Here

TOP

Related Classes of gnu.regexp.RE

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.