Exemple #1
0
  public static String urlEscape(String param) {
    String urlOK = "";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < param.length(); ++i) {
      char ch = param.charAt(i);
      char lowerCh = Character.toLowerCase(ch);
      if (Character.isDigit(ch) || (-1 != "[email protected]".indexOf(lowerCh))) {
        sb.append(ch);

      } else if (' ' == ch) {
        sb.append('+');

      } else if ((0x7F & ch) == ch) {
        putCh(sb, ch);

      } else if ((0xFFF & ch) == ch) {
        putCh(sb, 0xD0 | (ch >> 6));
        putCh(sb, 0x80 | (0x3F & ch));

      } else {
        putCh(sb, 0xE8 | (ch >> 12));
        putCh(sb, 0x80 | (0x3F & (ch >> 6)));
        putCh(sb, 0x80 | (0x3F & ch));
      }
    }
    return sb.toString();
  }
Exemple #2
0
  public static long createGmtDate(String sdate) {
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    try {
      sdate = sdate.trim();
      int[] ofs = sdate.endsWith("Z") ? ofsFieldsB : ofsFieldsA;
      long result;
      if (Character.isDigit(sdate.charAt(0))) {
        int fieldLength = 4; // yearlen
        for (int i = 0; i < calFields.length; ++i) {
          int begIndex = ofs[i];
          int field = strToIntDef(sdate.substring(begIndex, begIndex + fieldLength), 0);
          if (1 == i) {
            field += Calendar.JANUARY - 1;
          }
          fieldLength = 2;
          c.set(calFields[i], field);
        }
        result = Math.max(0, c.getTime().getTime() / 1000);

      } else {
        String[] rfcDate = Util.explode(sdate, ' ');
        c.set(Calendar.YEAR, strToIntDef(rfcDate[3], 0));

        for (int i = 0; i < months.length; ++i) {
          if (months[i].equals(rfcDate[2])) {
            c.set(Calendar.MONTH, i);
            break;
          }
        }
        c.set(Calendar.DAY_OF_MONTH, strToIntDef(rfcDate[1], 0));
        c.set(Calendar.HOUR_OF_DAY, strToIntDef(rfcDate[4].substring(0, 2), 0));
        c.set(Calendar.MINUTE, strToIntDef(rfcDate[4].substring(3, 5), 0));
        c.set(Calendar.SECOND, strToIntDef(rfcDate[4].substring(6), 0));

        long delta =
            strToIntDef(rfcDate[5].substring(1, 3), 0) * 60 * 60
                + strToIntDef(rfcDate[5].substring(3, 5), 0) * 60;
        if ('+' == rfcDate[5].charAt(0)) {
          delta = -delta;
        }
        result = Math.max(0, c.getTime().getTime() / 1000 + delta);
      }
      return result;
    } catch (Exception ignored) {
    }
    return 0;
  }
Exemple #3
0
  private static boolean isURLChar(char chr, int mode) {
    if (mode == URL_CHAR_PROTOCOL) {
      return ((chr >= 'A') && (chr <= 'Z')) || ((chr >= 'a') && (chr <= 'z'));
    }

    if (mode == URL_CHAR_PREV) {
      return ((chr >= 'A') && (chr <= 'Z'))
          || ((chr >= 'a') && (chr <= 'z'))
          || ((chr >= '0') && (chr <= '9'))
          || ('@' == chr)
          || ('-' == chr)
          || ('_' == chr)
          || ('%' == chr);
    }
    if (URL_CHAR_DIGIT == mode) return Character.isDigit(chr);
    if (URL_CHAR_NONE == mode) return (' ' == chr) || ('\n' == chr);

    return !((chr <= ' ') || (chr == '\n'));
  }
Exemple #4
0
  private static void putUrl(Vector<String> urls, String url) {
    final String skip = "?!;:,.";
    final String openDelemiters = "{[(«";
    final String delemiters = "}])»";
    int cutIndex = url.length() - 1;
    for (; cutIndex >= 0; --cutIndex) {
      char lastChar = url.charAt(cutIndex);
      if (-1 != skip.indexOf(lastChar)) {
        continue;
      }
      int delemiterIndex = delemiters.indexOf(lastChar);
      if (-1 != delemiterIndex) {
        if (-1 == url.indexOf(openDelemiters.charAt(delemiterIndex))) {
          continue;
        }
      }
      break;
    }

    if (cutIndex <= 0) {
      return;

    } else if (cutIndex != url.length() - 1) {
      url = url.substring(0, cutIndex + 1);
    }

    if (-1 == url.indexOf(':')) {
      boolean isPhone = ('+' == url.charAt(0));
      boolean hasDot = false;
      boolean nonDigit = false;
      for (int i = isPhone ? 1 : 0; i < url.length(); ++i) {
        char ch = url.charAt(i);
        if ('.' == ch) {
          hasDot = true;
        } else if (!Character.isDigit(ch)) {
          nonDigit = true;
          break;
        }
      }
      if (isPhone) {
        if (!nonDigit && !hasDot && (7 <= url.length())) {
          url = "tel:" + url;
        } else {
          return;
        }
      } else {
        if (nonDigit) {
          if (-1 == url.indexOf('/')) {
            if (-1 == url.indexOf('@')) return;
            // jid or email
          } else {
            url = "http:\57\57" + url;
          }
        } else {
          return;
        }
      }
    }
    int protoEnd = url.indexOf(':');
    if (-1 != protoEnd) {
      if (url.length() <= protoEnd + 5) {
        return;
      }
      for (int i = 0; i < protoEnd; ++i) {
        if (!isURLChar(url.charAt(i), URL_CHAR_PROTOCOL)) {
          return;
        }
      }
    }
    if (!urls.contains(url)) {
      urls.addElement(url);
    }
  }