⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtsi.java

📁 MacroWeka扩展了著名数据挖掘工具weka
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      catch (Exception e) {
        System.err.println("Trying to create URL from '" + urlStr + "' generates this exception:\n" + e);
        result = null;
      }
    }
    
    if (VERBOSE)
      System.out.println("Classpath " + classpathPart + ", package " + pckgname + " -> " + result);
  
    return result;
  }
  
  /**
   * Return all the classes inheriting or implementing a given
   * class in a given package.
   * 
   * @param pckgname    the fully qualified name of the package
   * @param tosubclass  the Class object to inherit from
   * @return            a Vector with all the classnames
   */
  public static Vector find(String pckgname, Class tosubclass) {
    Vector          result;
    
    result = new Vector();
    
    // Code from JWhich
    // ======
    // Translate the package name into an absolute path
    String name = new String(pckgname);
    if (!name.startsWith("/")) {
      name = "/" + name;
    }	
    name = name.replace('.','/');

    // traverse complete classpath, since we might have additional classes
    // in a parallel path...
    StringTokenizer tok = new StringTokenizer(System.getProperty("java.class.path"), System.getProperty("path.separator"));
    while (tok.hasMoreTokens()) {
      String part = tok.nextToken();
      URL url     = getURL(part, name);

      // did we find the package in this classpath-part?
      if (url == null)
      	continue;

      // file in filesystem or jar? 
      File directory = new File(url.getFile());
      if (directory.exists()) {
        // Get the list of the files contained in the package
        String[] files = directory.list();
        for (int i = 0; i < files.length; i++) {
          // we are only interested in .class files
          if (files[i].endsWith(".class")) {
            // removes the .class extension
            String classname = files[i].substring(0, files[i].length() - 6);
            try {
              Class cls = Class.forName(pckgname + "." + classname);
              if (VERBOSE)
                System.out.println("- Checking: " + classname);
              if (    !Modifier.isAbstract(cls.getModifiers()) 
                   && !cls.isPrimitive()) {
                if (    (!tosubclass.isInterface() && isSubclass(tosubclass, cls)) 
                     || (tosubclass.isInterface() && hasInterface(tosubclass, cls))) {
                  if (!result.contains(cls.getName())) {
                    if (VERBOSE)
                      System.out.println("- Added: " + classname);
                    result.add(cls.getName());
                  }
                }
              }
            } 
            catch (ClassNotFoundException cnfex) {
              System.err.println(cnfex);
            } 
          }
        }
      } 
      else {
        try {
          // It does not work with the filesystem: we must
          // be in the case of a package contained in a jar file.
          JarURLConnection conn = (JarURLConnection)url.openConnection();
          String starts = conn.getEntryName();
          JarFile jfile = conn.getJarFile();
          Enumeration e = jfile.entries();
          while (e.hasMoreElements()) {
            ZipEntry entry = (ZipEntry)e.nextElement();
            String entryname = entry.getName();
            if (entryname.startsWith(starts)
                && (entryname.lastIndexOf('/') <= starts.length())
                && entryname.endsWith(".class")) {
              String classname = entryname.substring(0, entryname.length() - 6);
              if (classname.startsWith("/")) 
                classname = classname.substring(1);
              classname = classname.replace('/', '.');
              try {
                // package name is already included!
                //Class cls = Class.forName(pckgname + "." + classname);
                Class cls = Class.forName(classname);
                if (VERBOSE)
                  System.out.println("- Checking: " + classname);
                if (    !Modifier.isAbstract(cls.getModifiers()) 
                     && !cls.isPrimitive()) {
                  if (    (!tosubclass.isInterface() && isSubclass(tosubclass, cls)) 
                       || (tosubclass.isInterface() && hasInterface(tosubclass, cls))) {
                    if (!result.contains(cls.getName())) {
                      if (VERBOSE)
                        System.out.println("- Added: " + classname);
                      result.add(cls.getName());
                    }
                  }
                }
              } 
              catch (ClassNotFoundException cnfex) {
                System.err.println(cnfex);
              } 
            }
          }
        } 
        catch (IOException ioex) {
          System.err.println(ioex);
        } 
      }
    }
    
    // sort the result
    RTSI r = new RTSI();
    Collections.sort(result, r.new StringCompare());
    
    return result;
  }
  
  /**
   * for testing only
   */
  public static void main(String []args) {
    if (args.length == 2) {
      System.out.println(find(args[0], args[1]));
    } 
    else {
      if (args.length == 1) {
        System.out.println(find(args[0]));
      } 
      else {
        System.out.println("Usage: java " + RTSI.class.getName() + " [<package>] <subclass>");
      }
    }
  }
  
  /**
   * compares two strings with the following order:<br>
   * <ul>
   *    <li>case insensitive</li>
   *    <li>german umlauts (&auml; , &ouml; etc.) or other non-ASCII letters are treated as special chars</li>
   *    <li>special chars &lt; numbers &lt; letters</li>
   * </ul>
   */
  public class StringCompare implements Comparator {
    /**
     * appends blanks to the string if its shorter than <code>len</code>
     */
    private String fillUp(String s, int len) {
      while (s.length() < len)
        s += " ";
      return s;
    }
    
    /**
     * returns the group of the character: 0=special char, 1=number, 2=letter 
     */
    private int charGroup(char c) {
      int         result;
      
      result = 0;
      
      if ( (c >= 'a') && (c <= 'z') )
        result = 2;
      else if ( (c >= '0') && (c <= '9') )
        result = 1;
      
      return result;
    }
    
    /**
     * Compares its two arguments for order.
     */    
    public int compare(Object o1, Object o2) {
      String        s1;
      String        s2;
      int           i;
      int           result;
      int           v1;
      int           v2;
      
      result = 0;   // they're equal
      
      // get lower case string
      s1 = o1.toString().toLowerCase();
      s2 = o2.toString().toLowerCase();
      
      // same length
      s1 = fillUp(s1, s2.length());
      s2 = fillUp(s2, s1.length());
      
      for (i = 0; i < s1.length(); i++) {
        // same char?
        if (s1.charAt(i) == s2.charAt(i)) {
          result = 0;
        }
        else {
          v1 = charGroup(s1.charAt(i));
          v2 = charGroup(s2.charAt(i));
          
          // different type (special, number, letter)?
          if (v1 != v2) {
            if (v1 < v2)
              result = -1;
            else
              result = 1;
          }
          else {
            if (s1.charAt(i) < s2.charAt(i))
              result = -1;
            else
              result = 1;
          }
          
          break;
        }
      }
      
      return result;
    }
    
    /**
     * Indicates whether some other object is "equal to" this Comparator. 
     */
    public boolean equals(Object obj) {
      return (obj instanceof StringCompare);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -