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

📄 mlpropertyresourcebundle.java

📁 为了下东西 随便发了个 datamining 的源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*
 * Created on 2006/8/8
 *
 * @Author: Xiaojun Chen
 * $Revision$ 1.0
 *
 */
package eti.bi.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import eti.bi.exception.SysException;

/**
 * use "UTF-8" format
 */
public class MLPropertyResourceBundle extends ResourceBundle {
	private HashMap lookup = new HashMap();
	private Properties properties = new Properties();
	/**
	 * add resource
	 * 
	 * @param propertyfile
	 *            property file added
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public void addResource(String propertyfile) throws IOException {
		File file = new File(propertyfile);
		if (!file.exists()) {
			throw new FileNotFoundException("File propertyfile not found!");
		}
		if (!file.isFile()) {
			throw new FileNotFoundException(propertyfile + " should be a file!");
		}
		InputStream in = new FileInputStream(file);
		addResource(in);
		in.close();
	}

	/**
	 * add resource
	 * 
	 * @param in
	 *            input stream of property file added
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public void addResource(InputStream in) throws IOException{
    	if(in ==null){
    		throw new FileNotFoundException("No input file!");
    	}

    	properties.clear();
    	properties.load(in);
    	lookup.putAll(properties);
    	properties.clear();
    }
	
	/**
	 * add resource
	 * 
	 * @param in
	 *            input stream of property file added
	 * @param charsetName
	 * 						the name of a supported
     *                 {@link java.nio.charset.Charset </code>charset<code>}
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public void addResource(InputStream in, String charsetName) throws IOException, UnsupportedEncodingException{
    	if(in ==null){
    		throw new FileNotFoundException("No input file!");
    	}
    	if(charsetName==null) {
    		addResource(in);
    		return;
    	}
    	
    	properties.clear();
    	properties.load(in);
    	Enumeration enu = properties.keys();
    	String key;
    	String value;
    	while(enu.hasMoreElements()) {
    		key = (String) enu.nextElement();
    		value = properties.getProperty(key);
    		value = new String(value.getBytes("ISO-8859-1"),charsetName);
    		lookup.put(key, value);
    	}
    	properties.clear();
    }
	
	/**
	 * add resource
	 * 
	 * @param properties
	 *            properties added
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public void addResource(Properties properties) throws Exception {
		lookup.putAll(properties);
	}

	/**
	 * add resource
	 * 
	 * @param url
	 *            URL to add
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public void addResource(URL url) throws Exception {
		if (url != null) {
			InputStream in = url.openStream();
			addResource(in);
			in.close();
		}
	}

	@SuppressWarnings("unchecked")
	public void addProperty(String key, String value) throws Exception {
		if (lookup.get(key) != null) {
			throw new SysException("The key has already registered!");
		}
		lookup.put(key, value);
	}
	
	public Properties getProperties() {
		return new Properties() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -4146703037651491396L;
			
			public String getProperty(String key) {
				Object oval = lookup.get(key);
				String sval = (oval instanceof String) ? (String)oval : null;
				return sval;
			}

			/**
			 * Searches for the property with the specified key in this
			 * property list. If the key is not found in this property
			 * list, the default property list, and its defaults,
			 * recursively, are then checked. The method returns the
			 * default value argument if the property is not found.
			 * 
			 * @param key
			 *            the hashtable key.
			 * @param defaultValue
			 *            a default value.
			 * 
			 * @return the value in this property list with the
			 *         specified key value.
			 * @see #setProperty
			 * @see #defaults
			 */
			public String getProperty(String key, String defaultValue) {
				String val = getProperty(key);
				return (val == null) ? defaultValue : val;
			}
		};
	}
	
	// Implements java.util.ResourceBundle.handleGetObject; inherits javadoc
	// specification.
	public Object handleGetObject(String key) {
		if (key == null) {
			throw new NullPointerException();
		}
		return lookup.get(key);
	}
	
	/**
	 * Implementation of ResourceBundle.getKeys.
	 */
	@SuppressWarnings("unchecked")
	public Enumeration<String> getKeys() {
		ResourceBundle parent = this.parent;
		return new ResourceBundleEnumeration(lookup.keySet(), (parent != null) ? parent.getKeys() : null);
	}

	public void clear() {
		lookup.clear();
	}
}
/**
 * Implements an Enumeration that combines elements from a Set and an
 * Enumeration. Used by ListResourceBundle and PropertyResourceBundle.
 */
class ResourceBundleEnumeration implements Enumeration<String> {
	Set<String> set;
	Iterator<String> iterator;
	Enumeration<String> enumeration; // may remain null

	/**
	 * Constructs a resource bundle enumeration.
	 * 
	 * @param set
	 *            an set providing some elements of the enumeration
	 * @param enumeration
	 *            an enumeration providing more elements of the enumeration.
	 *            enumeration may be null.
	 */
	ResourceBundleEnumeration(Set<String> set, Enumeration<String> enumeration) {
		this.set = set;
		this.iterator = set.iterator();
		this.enumeration = enumeration;
	}
	String next = null;

	public boolean hasMoreElements() {
		if (next == null) {
			if (iterator.hasNext()) {
				next = iterator.next();
			} else if (enumeration != null) {
				while (next == null && enumeration.hasMoreElements()) {
					next = enumeration.nextElement();
					if (set.contains(next)) {
						next = null;
					}
				}
			}
		}
		return next != null;
	}

	public String nextElement() {
		if (hasMoreElements()) {
			String result = next;
			next = null;
			return result;
		} else {
			throw new NoSuchElementException();
		}
	}
}

⌨️ 快捷键说明

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