Repository /Debellor/debellor-1.0.jar:org.debellor.rseslib.ParamConverter


Back

No file description

Source code

/*
 *  Debellor
 *
 *  Copyright (C) 2008-2009 by Marcin Wojnarski
 *
 *  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 3 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, see http://www.gnu.org/licenses/.
 */
package org.debellor.rseslib;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import org.debellor.core.Parameters;
import org.debellor.core.Parameters.Entry;
import org.debellor.core.exception.parameters.UnknownParameterException;
import org.debellor.core.parameters.ParametersInfo;

import rseslib.processing.classification.Classifier;
import rseslib.system.Configuration;

/**
 * Conversions between Debellor {@code Parameters} and 
 * Rseslib properties.
 * 
 * @author Marcin Wojnarski
 *
 */
public class ParamConverter {

	public static Properties defaultPropertiesOf(Class<? extends Classifier> classifier) 
		throws RseslibConversionException 
	{
        Class superclass = classifier;
        while(superclass != null && superclass != Configuration.class)
        	superclass = superclass.getSuperclass();
        if(superclass == null) 
        	return new Properties();
        
        Properties prop = new Properties();
		Class cls = classifier;

		try {
			while(cls != null) {
				String path = '/' + cls.getName().replace('.', '/') + ".properties";
				//System.err.println(ParamConverter.class.getResource(path));
				InputStream in = ParamConverter.class.getResourceAsStream(path);
				if(in != null)
					prop.load(new BufferedInputStream(in));
				cls = cls.getSuperclass();
			}
		} catch(Exception e) {
			throw new RseslibConversionException(
					"Error when loading default parameters (properties) " + 
					"of Rseslib classifier " + classifier.getSimpleName(), e);
		}
        
        return prop;
	}

	public static Properties propertiesFrom(Parameters params, 
											Class<? extends Classifier> classifier) 
		throws RseslibConversionException, UnknownParameterException 
	{
		Properties prop = defaultPropertiesOf(classifier);

		for(int i = 0; i < params.size(); i++) {
			Entry p = params.getEntry(i);
			if(prop.setProperty(p.name, p.value) == null)
				throw new UnknownParameterException(p.name);
		}

		return prop;
	}

	static ParametersInfo listParameters(Class<? extends Classifier> classifier)
			throws RseslibConversionException 
	{
		ParametersInfo paramInfo = new ParametersInfo();
		Properties prop = defaultPropertiesOf(classifier);
		for(Map.Entry<Object, Object> entry : prop.entrySet())
			paramInfo.set((String) entry.getKey(), (String) entry.getValue());
		return paramInfo;
	}

}

Copyright © 2008-2011 by TunedIT
Design by luksite