Exemple #1
0
 public static void addAll(Vector to, Vector all) {
   synchronized (to) {
     for (int i = 0; i < all.size(); ++i) {
       to.addElement(all.elementAt(i));
     }
   }
 }
Exemple #2
0
 public static void addNew(Vector to, Vector all) {
   synchronized (to) {
     for (int i = 0; i < all.size(); ++i) {
       if (0 <= Util.getIndex(to, all.elementAt(i))) continue;
       to.addElement(all.elementAt(i));
     }
   }
 }
Exemple #3
0
 public static boolean hasURL(String msg) {
   if (null == msg) return false;
   Vector<String> result = new Vector<String>();
   parseForUrl(result, msg, '.', URL_CHAR_PREV, URL_CHAR_OTHER, 1);
   parseForUrl(result, msg, ':', URL_CHAR_PROTOCOL, URL_CHAR_OTHER, 1);
   parseForUrl(result, msg, '+', URL_CHAR_NONE, URL_CHAR_DIGIT, 1);
   parseForUrl(result, msg, '@', URL_CHAR_PREV, URL_CHAR_OTHER, 1);
   return !result.isEmpty();
 }
Exemple #4
0
 public static int getIndex(Vector v, Object o) {
   synchronized (v) {
     int size = v.size();
     for (int i = 0; i < size; ++i) {
       if (v.elementAt(i) == o) {
         return i;
       }
     }
   }
   return -1;
 }
Exemple #5
0
 public static Vector parseMessageForURL(String msg) {
   if (null == msg) return null;
   // we are parsing 100 links only
   final int MAX_LINK_COUNT = 100;
   Vector<String> result = new Vector<String>();
   parseForUrl(result, msg, '.', URL_CHAR_PREV, URL_CHAR_OTHER, MAX_LINK_COUNT);
   parseForUrl(result, msg, ':', URL_CHAR_PROTOCOL, URL_CHAR_OTHER, MAX_LINK_COUNT);
   parseForUrl(result, msg, '+', URL_CHAR_NONE, URL_CHAR_DIGIT, MAX_LINK_COUNT);
   parseForUrl(result, msg, '@', URL_CHAR_PREV, URL_CHAR_OTHER, MAX_LINK_COUNT);
   return result.isEmpty() ? null : result;
 }
Exemple #6
0
 public static void sort(Vector subnodes) {
   for (int i = 1; i < subnodes.size(); ++i) {
     Sortable currNode = (Sortable) subnodes.elementAt(i);
     int j = i - 1;
     for (; j >= 0; --j) {
       Sortable itemJ = (Sortable) subnodes.elementAt(j);
       if (compareNodes(itemJ, currNode) <= 0) {
         break;
       }
       subnodes.setElementAt(itemJ, j + 1);
     }
     if (j + 1 != i) {
       subnodes.setElementAt(currNode, j + 1);
     }
   }
 }
Exemple #7
0
  private static void parseForUrl(
      Vector<String> result, String msg, char ch, int before, int after, int limit) {
    if (limit <= result.size()) {
      return;
    }
    int size = msg.length();
    int findIndex = 0;
    int beginIdx;
    int endIdx;
    for (; ; ) {
      if (findIndex >= size) break;
      int ptIndex = msg.indexOf(ch, findIndex);
      if (ptIndex == -1) break;

      for (endIdx = ptIndex + 1; endIdx < size; ++endIdx) {
        if (!isURLChar(msg.charAt(endIdx), after)) {
          break;
        }
      }

      findIndex = endIdx;
      if (endIdx - ptIndex < 2) continue;

      if (URL_CHAR_NONE != before) {
        for (beginIdx = ptIndex - 1; beginIdx >= 0; --beginIdx) {
          if (!isURLChar(msg.charAt(beginIdx), before)) {
            break;
          }
        }
        if ((beginIdx == -1) || !isURLChar(msg.charAt(beginIdx), before)) {
          beginIdx++;
        }
        if (ptIndex == beginIdx) continue;

      } else {
        beginIdx = ptIndex;
        if ((0 < beginIdx) && !isURLChar(msg.charAt(beginIdx - 1), before)) {
          continue;
        }
      }
      if (endIdx - beginIdx < 5) continue;
      putUrl(result, msg.substring(beginIdx, endIdx));
      if (limit < result.size()) {
        return;
      }
    }
  }
Exemple #8
0
 /* Divide text to array of parts using serparator charaster */
 public static String[] explode(String text, char separator) {
   if (StringUtils.isEmpty(text)) {
     return new String[0];
   }
   Vector<String> tmp = new Vector<String>();
   int start = 0;
   int end = text.indexOf(separator, start);
   while (end >= start) {
     tmp.addElement(text.substring(start, end));
     start = end + 1;
     end = text.indexOf(separator, start);
   }
   tmp.addElement(text.substring(start));
   String[] result = new String[tmp.size()];
   tmp.copyInto(result);
   return result;
 }
Exemple #9
0
 public static void removeAll(Vector to, Vector all) {
   synchronized (to) {
     int current = 0;
     for (int index = 0; index < to.size(); ++index) {
       if (0 <= Util.getIndex(all, to.elementAt(index))) continue;
       if (current < index) {
         to.setElementAt(to.elementAt(index), current);
         current++;
       }
     }
     if (current < to.size()) to.setSize(current);
   }
 }
Exemple #10
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);
    }
  }