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

📄 configuration.java

📁 weka机器学习系统(本站可下载)的拓展
💻 JAVA
字号:
package com.prcomps.cahitarf;

import com.prcomps.cahitarf.gui.WizardFrame;

import java.util.Properties;
import java.util.StringTokenizer;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.io.PrintStream;
import java.text.MessageFormat;
import java.sql.ResultSet;
import java.sql.Types;
import java.sql.SQLException;
import java.sql.ResultSetMetaData;

/**
 *
 * @author <a href="mailto:ayhan@primecomponents.com">Ayhan Alkan</a>
 */
public class Configuration
{
    private ResourceBundle bundle = WizardFrame.getBundle();

    private PrintStream out;

    private boolean includesClass = false;

    private static final int FLD_NUMERIC = 0;
    private static final int FLD_CLASS   = 1;
    private static final int FLD_STRING  = 2;

    private static final int QUOTE_NONE   = 0;
    private static final int QUOTE_SINGLE = 1;
    private static final int QUOTE_DOUBLE = 2;

    public Configuration()
    {
    }

    public Field[] createFields( Properties properties )
        throws IllegalArgumentException
    {
        List list = new LinkedList();

        includesClass = false;

        for( int i = 0; ; i++ )
        {
            String tmp = properties.getProperty( Db2Arff.PROP_FIELD_ + i );
            if ( tmp == null )
                break;
            Field fld = createField( tmp, i );
            list.add( fld );
        }

        return (Field[]) list.toArray( new Field[0] );
    }

    public Field[] createFields( ResultSet rs )
        throws IllegalArgumentException
    {
        try
        {
            ResultSetMetaData metaData = rs.getMetaData();

            int numberOfColumns =  metaData.getColumnCount();
            Field[] fields = new Field[ numberOfColumns ];

            for(int column = 0; column < numberOfColumns; column++)
                switch ( metaData.getColumnType( column + 1 ) )
                {
                    case Types.BIGINT:
                    case Types.DECIMAL:
                    case Types.DOUBLE:
                    case Types.FLOAT:
                    case Types.INTEGER:
                    case Types.NUMERIC:
                    case Types.REAL:
                    case Types.SMALLINT:
                    case Types.TINYINT:
                        fields[ column ] = new NumericField( metaData.getColumnLabel(column + 1) );
                        break;
                    default:
                        fields[ column ] = new ClassField( metaData.getColumnLabel(column + 1), QUOTE_DOUBLE );
                }
            return fields;
        }
        catch ( SQLException e )
        {
            throw new IllegalArgumentException( e.getMessage() );
            //e.printStackTrace();  //To change body of catch statement use Options | File Templates.
        }

    }

    private Field createField( String def, int n )
        throws IllegalArgumentException
    {
        Field field;

        StringTokenizer st = new StringTokenizer( def, ":" );
        String name;
        int    type;
        int    quote = QUOTE_NONE;

        if ( st.hasMoreTokens() )
            name = st.nextToken();
        else
            throw new IllegalArgumentException( MessageFormat.format(
                    bundle.getString( "conf.err.attr.name" ), new Object[]{ new Integer(n), def } ) );

        if ( st.hasMoreTokens() )
        {
            String types = st.nextToken().toLowerCase();
            if ( types.equals( "numeric") )
                type = FLD_NUMERIC;
            else if ( types.equals( "class") )
                type = FLD_CLASS;
            else if ( types.equals( "string" ) )
                type = FLD_STRING;
            else
                throw new IllegalArgumentException( MessageFormat.format(
                        bundle.getString( "conf.err.attr.type.unknown" ), new Object[]{ new Integer(n), def } ) );
        }
        else
            throw new IllegalArgumentException( MessageFormat.format(
                    bundle.getString( "conf.err.attr.type" ), new Object[]{ new Integer(n), def } ) );

        if ( st.hasMoreTokens() )
        {
            String qs = st.nextToken().toLowerCase();
            if ( qs.equals("q") )
                quote = QUOTE_DOUBLE;
            else if ( qs.equals("qs") )
                quote = QUOTE_SINGLE;
            else
                quote = QUOTE_NONE;
        }

        if ( type == FLD_NUMERIC )
            field = new NumericField( name );
        else if ( type == FLD_STRING )
            field = new StringField( name, quote );
        else
        {
            field = new ClassField( name, quote );
            includesClass = true;
        }

        return field;
    }

    public boolean isIncludesClass()        { return includesClass; }

    public void setOut( PrintStream out )    { this.out = out; }

    public interface Field
    {
        public String toDefinitionString();
        public void toData( String val );
    }

    private class StringField implements Field
    {
        private String name;
        private char quote = 0;

        public StringField( String name, int isquote )
        {
            this.name = name;
            if ( isquote == QUOTE_SINGLE )
                quote = '\'';
            else if ( isquote == QUOTE_DOUBLE )
                quote = '\"';
        }

        public String toDefinitionString()
        {
            return "@ATTRIBUTE " + name + " string\n";
        }

        public void toData( String val )
        {
            if ( quote == 0 )
                out.print( val );
            else
            {
                out.print( quote );
                out.print( val );
                out.print( quote );
            }
        }

    }

    private class NumericField implements Field
    {
        String name;

        public NumericField( String name )
        {
            this.name = name;
        }

        public String toDefinitionString()
        {
            return "@ATTRIBUTE " + name + " real\n";
        }

        public void toData( String val )
        {
            out.print( val );
        }
    }

    private class ClassField implements Field
    {
        Set valueset = new HashSet();
        private String name;
        private char quote = 0;

        public ClassField( String name, int isquote )
        {
            includesClass = true;
            this.name = name;
            if ( isquote == QUOTE_SINGLE )
                quote = '\'';
            else if ( isquote == QUOTE_DOUBLE )
                quote = '\"';
        }

        public String toDefinitionString()
        {
            StringBuffer str = new StringBuffer();
            str.append( "@ATTRIBUTE " ) .append( name ) .append( " {" );
            for ( Iterator i = valueset.iterator(); i.hasNext(); )
            {
                Object o = i.next();
                if ( quote == 0 )
                    str.append( o );
                else
                    str.append( quote ) .append( o ) .append( quote );
                str.append( ',' );
            }
            int offset = str.length() - 1;
            if ( str.charAt( offset ) == ',' )
                str.setCharAt( offset, '}' );
            else
                str.append( '}' );
            str.append( '\n' );
            return str.toString();
        }

        public void toData( String val )
        {
            valueset.add( val );
            if ( quote == 0 )
                out.print( val );
            else
            {
                out.print( quote );
                out.print( val );
                out.print( quote );
            }
        }
    }

}

⌨️ 快捷键说明

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