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

📄 converterutils.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      weka.core.converters.ArffSaver.class.getName() + ","    + weka.core.converters.C45Saver.class.getName() + ","    + weka.core.converters.CSVSaver.class.getName() + ","    + weka.core.converters.DatabaseConverter.class.getName() + ","    + weka.core.converters.LibSVMSaver.class.getName() + ","    + weka.core.converters.SerializedInstancesSaver.class.getName() + ","    + weka.core.converters.XRFFSaver.class.getName();    /** all available loaders (extension &lt;-&gt; classname) */  protected static Hashtable<String,String> m_FileLoaders;    /** all available URL loaders (extension &lt;-&gt; classname) */  protected static Hashtable<String,String> m_URLFileLoaders;  /** all available savers (extension &lt;-&gt; classname) */  protected static Hashtable<String,String> m_FileSavers;    // determine all loaders/savers  static {    Vector classnames;        try {      // generate properties       // Note: does NOT work with RMI, hence m_FileLoadersCore/m_FileSaversCore      GenericPropertiesCreator creator = new GenericPropertiesCreator();      creator.execute(false);      Properties props = creator.getOutputProperties();      // init      m_FileLoaders    = new Hashtable<String,String>();      m_URLFileLoaders = new Hashtable<String,String>();      m_FileSavers     = new Hashtable<String,String>();            // loaders      m_FileLoaders = getFileConverters(	  		props.getProperty(Loader.class.getName(), CORE_FILE_LOADERS),	  		new String[]{FileSourcedConverter.class.getName()});            // URL loaders      m_URLFileLoaders = getFileConverters(	  		   props.getProperty(Loader.class.getName(), CORE_FILE_LOADERS),	  		   new String[]{	  		     FileSourcedConverter.class.getName(), 	  		     URLSourcedLoader.class.getName()});      // savers      m_FileSavers = getFileConverters(	  		props.getProperty(Saver.class.getName(), CORE_FILE_SAVERS),	  		new String[]{FileSourcedConverter.class.getName()});    }    catch (Exception e) {      // ignore    }    finally {      // loaders      if (m_FileLoaders.size() == 0) {	classnames = ClassDiscovery.find(	               AbstractFileLoader.class, 	               AbstractFileLoader.class.getPackage().getName());	if (classnames.size() > 0)	  m_FileLoaders = getFileConverters(	                    classnames,	                    new String[]{FileSourcedConverter.class.getName()});	else	  m_FileLoaders = getFileConverters(	                    CORE_FILE_LOADERS,	                    new String[]{FileSourcedConverter.class.getName()});      }      // URL loaders      if (m_URLFileLoaders.size() == 0) {        classnames = ClassDiscovery.find(		       AbstractFileLoader.class, 		       AbstractFileLoader.class.getPackage().getName());        if (classnames.size() > 0)	  m_URLFileLoaders = getFileConverters(	  		       classnames,	  		       new String[]{	  			   FileSourcedConverter.class.getName(), 	  			   URLSourcedLoader.class.getName()});        else          m_URLFileLoaders = getFileConverters(	                       CORE_FILE_LOADERS,	                       new String[]{	                	   FileSourcedConverter.class.getName(), 	                	   URLSourcedLoader.class.getName()});      }      // savers      if (m_FileSavers.size() == 0) {	classnames = ClassDiscovery.find(		       AbstractFileSaver.class, 		       AbstractFileSaver.class.getPackage().getName());	if (classnames.size() > 0)	  m_FileSavers = getFileConverters(	  		   classnames,	  		   new String[]{FileSourcedConverter.class.getName()});	else	  m_FileSavers = getFileConverters(	                   CORE_FILE_SAVERS,	                   new String[]{FileSourcedConverter.class.getName()});      }    }  }    /**   * returns a hashtable with the association    * "file extension &lt;-&gt; converter classname" for the comma-separated list   * of converter classnames.   *    * @param classnames	comma-separated list of converter classnames   * @param intf	interfaces the converters have to implement   * @return		hashtable with ExtensionFileFilters   */  protected static Hashtable<String,String> getFileConverters(String classnames, String[] intf) {    Vector	list;    String[]	names;    int		i;        list  = new Vector();    names = classnames.split(",");    for (i = 0; i < names.length; i++)      list.add(names[i]);        return getFileConverters(list, intf);  }    /**   * returns a hashtable with the association    * "file extension &lt;-&gt; converter classname" for the list of converter    * classnames.   *    * @param classnames	list of converter classnames   * @param intf	interfaces the converters have to implement   * @return		hashtable with ExtensionFileFilters   */  protected static Hashtable<String,String> getFileConverters(Vector classnames, String[] intf) {    Hashtable<String,String>	result;    String 			classname;    Class 			cls;    String[] 			ext;    FileSourcedConverter 	converter;    int 			i;    int				n;        result = new Hashtable<String,String>();        for (i = 0; i < classnames.size(); i++) {      classname = (String) classnames.get(i);      // all necessary interfaces implemented?      for (n = 0; n < intf.length; n++) {	if (!ClassDiscovery.hasInterface(intf[n], classname))	  continue;      }            // get data from converter      try {	cls       = Class.forName(classname);	converter = (FileSourcedConverter) cls.newInstance();	ext       = converter.getFileExtensions();      }      catch (Exception e) {	cls       = null;	converter = null;	ext       = new String[0];      }            if (converter == null)	continue;      for (n = 0; n < ext.length; n++)	result.put(ext[n], classname);    }        return result;  }  /**   * Gets token, skipping empty lines.   *   * @param tokenizer 		the stream tokenizer   * @throws IOException 	if reading the next token fails   */  public static void getFirstToken(StreamTokenizer tokenizer)     throws IOException {        while (tokenizer.nextToken() == StreamTokenizer.TT_EOL){};    if ((tokenizer.ttype == '\'') ||	(tokenizer.ttype == '"')) {      tokenizer.ttype = StreamTokenizer.TT_WORD;    } else if ((tokenizer.ttype == StreamTokenizer.TT_WORD) &&	       (tokenizer.sval.equals("?"))) {      tokenizer.ttype = '?';    }  }  /**   * Gets token.   *   * @param tokenizer 		the stream tokenizer   * @throws IOException 	if reading the next token fails   */  public static void getToken(StreamTokenizer tokenizer) throws IOException {        tokenizer.nextToken();    if (tokenizer.ttype== StreamTokenizer.TT_EOL) {      return;    }    if ((tokenizer.ttype == '\'') ||	(tokenizer.ttype == '"')) {      tokenizer.ttype = StreamTokenizer.TT_WORD;    } else if ((tokenizer.ttype == StreamTokenizer.TT_WORD) &&	       (tokenizer.sval.equals("?"))) {      tokenizer.ttype = '?';    }  }  /**   * Throws error message with line number and last token read.   *   * @param theMsg 		the error message to be thrown   * @param tokenizer 		the stream tokenizer   * @throws IOException 	containing the error message   */  public static void errms(StreamTokenizer tokenizer, String theMsg)     throws IOException {        throw new IOException(theMsg + ", read " + tokenizer.toString());  }  /**   * returns a vector with the classnames of all the loaders from the    * given hashtable   *    * @param ht		the hashtable with the extension/converter relation   * @return		the classnames of the loaders   */  protected static Vector<String> getConverters(Hashtable<String,String> ht) {    Vector<String>	result;    Enumeration<String>	enm;    String		converter;        result = new Vector<String>();        // get all classnames    enm = ht.elements();    while (enm.hasMoreElements()) {      converter = enm.nextElement();      if (!result.contains(converter))	result.add(converter);    }        // sort names    Collections.sort(result);        return result;  }    /**   * tries to determine the converter to use for this kind of file, returns   * null if none can be found in the given hashtable   *    * @param filename	the file to return a converter for   * @param ht		the hashtable with the relation extension/converter   * @return		the converter if one was found, null otherwise   */  protected static Object getConverterForFile(String filename, Hashtable<String,String> ht) {    Object	result;    String	extension;    int		index;        result = null;        index = filename.lastIndexOf('.');    if (index > -1) {      extension = filename.substring(index).toLowerCase();      result    = getConverterForExtension(extension, ht);      // is it a compressed format?      if (extension.equals(".gz") && result == null) {	index     = filename.lastIndexOf('.', index - 1);	extension = filename.substring(index).toLowerCase();	result    = getConverterForExtension(extension, ht);      }    }        return result;  }  /**   * tries to determine the loader to use for this kind of extension, returns   * null if none can be found   *    * @param extension	the file extension to return a converter for   * @param ht		the hashtable with the relation extension/converter   * @return		the converter if one was found, null otherwise   */  protected static Object getConverterForExtension(String extension, Hashtable<String,String> ht) {    Object	result;    String	classname;        result    = null;    classname = (String) ht.get(extension);    if (classname != null) {      try {	result = Class.forName(classname).newInstance();      }      catch (Exception e) {	result = null;	e.printStackTrace();      }    }        return result;  }    /**   * checks whether the given class is one of the hardcoded core file loaders   *    * @param classname	the class to check   * @return		true if the class is one of the core loaders   * @see		#CORE_FILE_LOADERS   */  public static boolean isCoreFileLoader(String classname) {    boolean	result;    String[]	classnames;        classnames = CORE_FILE_LOADERS.split(",");    result     = (Arrays.binarySearch(classnames, classname) >= 0);        return result;  }    /**   * returns a vector with the classnames of all the file loaders   *    * @return		the classnames of the loaders   */  public static Vector<String> getFileLoaders() {    return getConverters(m_FileLoaders);  }    /**   * tries to determine the loader to use for this kind of file, returns   * null if none can be found   *    * @param filename	the file to return a converter for   * @return		the converter if one was found, null otherwise   */  public static AbstractFileLoader getLoaderForFile(String filename) {    return (AbstractFileLoader) getConverterForFile(filename, m_FileLoaders);  }  /**   * tries to determine the loader to use for this kind of file, returns   * null if none can be found   *    * @param file	the file to return a converter for   * @return		the converter if one was found, null otherwise   */  public static AbstractFileLoader getLoaderForFile(File file) {    return getLoaderForFile(file.getAbsolutePath());  }  /**   * tries to determine the loader to use for this kind of extension, returns   * null if none can be found   *    * @param extension	the file extension to return a converter for   * @return		the converter if one was found, null otherwise   */  public static AbstractFileLoader getLoaderForExtension(String extension) {    return (AbstractFileLoader) getConverterForExtension(extension, m_FileLoaders);  }  /**   * returns a vector with the classnames of all the URL file loaders   *    * @return		the classnames of the loaders   */  public static Vector<String> getURLFileLoaders() {    return getConverters(m_URLFileLoaders);  }    /**   * tries to determine the URL loader to use for this kind of file, returns   * null if none can be found   *    * @param filename	the file to return a URL converter for   * @return		the converter if one was found, null otherwise   */  public static AbstractFileLoader getURLLoaderForFile(String filename) {    return (AbstractFileLoader) getConverterForFile(filename, m_URLFileLoaders);  }  /**   * tries to determine the URL loader to use for this kind of file, returns   * null if none can be found   *    * @param file	the file to return a URL converter for   * @return		the converter if one was found, null otherwise   */  public static AbstractFileLoader getURLLoaderForFile(File file) {    return getURLLoaderForFile(file.getAbsolutePath());  }  /**   * tries to determine the URL loader to use for this kind of extension, returns   * null if none can be found   *    * @param extension	the file extension to return a URL converter for   * @return		the converter if one was found, null otherwise   */  public static AbstractFileLoader getURLLoaderForExtension(String extension) {    return (AbstractFileLoader) getConverterForExtension(extension, m_URLFileLoaders);  }    /**   * checks whether the given class is one of the hardcoded core file savers   *    * @param classname	the class to check   * @return		true if the class is one of the core savers   * @see		#CORE_FILE_SAVERS   */  public static boolean isCoreFileSaver(String classname) {    boolean	result;    String[]	classnames;        classnames = CORE_FILE_SAVERS.split(",");    result     = (Arrays.binarySearch(classnames, classname) >= 0);        return result;  }  /**   * returns a vector with the classnames of all the file savers   *    * @return		the classnames of the savers   */  public static Vector<String> getFileSavers() {    return getConverters(m_FileSavers);  }  /**   * tries to determine the saver to use for this kind of file, returns   * null if none can be found   *    * @param filename	the file to return a converter for   * @return		the converter if one was found, null otherwise   */  public static AbstractFileSaver getSaverForFile(String filename) {    return (AbstractFileSaver) getConverterForFile(filename, m_FileSavers);  }  /**   * tries to determine the saver to use for this kind of file, returns   * null if none can be found   *    * @param file	the file to return a converter for   * @return		the converter if one was found, null otherwise   */  public static AbstractFileSaver getSaverForFile(File file) {    return getSaverForFile(file.getAbsolutePath());  }  /**   * tries to determine the saver to use for this kind of extension, returns   * null if none can be found   *    * @param extension	the file extension to return a converter for   * @return		the converter if one was found, null otherwise   */  public static AbstractFileSaver getSaverForExtension(String extension) {    return (AbstractFileSaver) getConverterForExtension(extension, m_FileSavers);  }}

⌨️ 快捷键说明

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