📄 db2arff.java
字号:
package com.prcomps.cahitarf;
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Driver;
import java.text.MessageFormat;
/**
*/
public class Db2Arff
{
public static final String PROP_JDBC_DRIVER = "jdbc.driver";
public static final String PROP_JDBC_USER = "jdbc.user";
public static final String PROP_JDBC_PASSWORD = "jdbc.password";
public static final String PROP_JDBC_URL = "jdbc.url";
public static final String PROP_JDBC_SELECT = "jdbc.select";
public static final String PROP_FIELD_ = "attr.";
public static final String PROP_RELATION = "relation";
private static ResourceBundle bundle = ResourceBundle.getBundle("messages");
private Properties properties;
private Configuration conf = new Configuration();
private Connection conn = null;
private int bufferSize = 2 * 1024 * 1024;
private int maxRows = 0;
public Db2Arff()
{
}
/**
* Scans ./lib directory and loads JDBC drivers to context classpath
* @throws FileNotFoundException
*/
public void loadDrivers()
throws FileNotFoundException
{
// absolute file system path of ./lib directory
String libPath = getAppPath();
if ( libPath == null )
libPath = System.getProperty( "user.dir" );
libPath += "/lib";
// check if './lib' exists
File libDir = new File( libPath );
if ( ! libDir.exists() || ! libDir.isDirectory() )
throw new FileNotFoundException(
MessageFormat.format( bundle.getString( "db2arff.libdir.notfound" ), new Object[] { libPath } ) );
// Get file list of .jar and .zip archives located in
File libFiles[] = libDir.listFiles( new FilenameFilter() {
public boolean accept( File f, String name )
{
// TODO: themes.jar, skinlf.jar, easylayout.jar ?
return name.endsWith(".jar") || name.endsWith( ".zip" );
}
});
// convert file list to URL list
URL libURLs[] = new URL[ libFiles.length ];
for ( int i=0; i<libFiles.length; i++ )
try
{
libURLs[i] = libFiles[i].toURL();
}
catch ( MalformedURLException e )
{
throw new FileNotFoundException( e.getMessage() );
}
// Load JDBC classes
URLClassLoader loader = new URLClassLoader( libURLs, getClass().getClassLoader() );
Thread.currentThread().setContextClassLoader( loader );
}
/**
*
* @param path
* @throws IOException
*/
public void loadProperties( String path )
throws IOException
{
File confFile = new File( path );
properties = new Properties();
FileInputStream in = new FileInputStream( confFile );
properties.load( in );
in.close();
conf = new Configuration();
}
/**
*
* @param out
* @param fields
*/
private void createArffHeader( PrintStream out, Configuration.Field[] fields )
{
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat( "dd MMM yyyy HH:mm:ss" );
out.print( "% Created by Cahit Arf at " );
out.print( df.format( new java.util.Date() ) );
out.print( "\n\n" );
out.print( "@RELATION " );
String relation = properties.getProperty( "relation", "CahitArf" ).trim();
if ( relation.indexOf( ' ' ) >= 0
&& (
( !relation.startsWith( "\"" ) && !relation.endsWith( "\"" ) ) ||
( !relation.endsWith( "'") && !relation.startsWith( "'" ) )
)
)
relation = "'" + relation + "'";
out.print( relation );
out.print( "\n\n" );
for ( int i=0; i<fields.length; i++ )
out.print( fields[i].toDefinitionString() );
out.print( "\n\n@DATA\n" );
}
/**
* A wrapper to toArff( PrintSream out ). Creates a new file pointed by the path parameter, creates
* a PrintStream to the file, and then calls the main toArff() method.
*
* @param path Output file path.
*/
public void toArff( String path )
{
File arff = new File( path );
try
{
arff.createNewFile();
PrintStream out = new PrintStream( new FileOutputStream( arff ) );
toArff( out );
out.close();
}
catch ( IOException e )
{
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
}
/**
*
* @param out
*/
public void toArff( PrintStream out )
{
PrintStream tmp = null;
ByteArrayOutputStream bout = null;
Configuration.Field[] fields = null;
try
{
fields = conf.createFields( properties );
ResultSet rs = getQueryResult();
if ( fields.length == 0 )
fields = conf.createFields( rs );
if ( fields.length != rs.getMetaData().getColumnCount() )
{
// TODO
}
if ( conf.isIncludesClass() )
{
tmp = out;
bout = new ByteArrayOutputStream( bufferSize );
out = new PrintStream( bout );
}
else
createArffHeader( out, fields );
conf.setOut( out );
int len = fields.length - 1;
while ( rs.next() )
{
for ( int i=0; i<len; i++ )
{
fields[i].toData( rs.getString( i+1 ) );
out.print( ',' );
}
fields[len].toData( rs.getString( len+1 ) );
out.print( '\n' );
}
rs.close();
release();
}
catch ( SQLException e )
{
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
if ( conf.isIncludesClass() )
{
try
{
createArffHeader( tmp, fields );
bout.writeTo( tmp );
out = null;
}
catch ( IOException e )
{
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
}
}
/**
*
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public Connection connect()
throws SQLException, ClassNotFoundException
{
if ( conn == null )
try
{
Driver driver = (Driver)Class.forName(
properties.getProperty( PROP_JDBC_DRIVER ),
false,
Thread.currentThread().getContextClassLoader() ).newInstance();
//DriverManager.registerDriver( driver );
Properties connInfo = new Properties();
if ( properties.getProperty( PROP_JDBC_USER ) != null )
connInfo.setProperty( "user", properties.getProperty( PROP_JDBC_USER ) );
if ( properties.getProperty( PROP_JDBC_PASSWORD ) != null )
connInfo.setProperty( "password", properties.getProperty( PROP_JDBC_PASSWORD ) );
conn = driver.connect( properties.getProperty( PROP_JDBC_URL ), connInfo );
}
catch ( InstantiationException e )
{
throw new ClassNotFoundException( e.getMessage() );
}
catch ( IllegalAccessException e )
{
throw new ClassNotFoundException( e.getMessage() );
}
return conn;
}
/**
*
* @throws SQLException
*/
public void release()
throws SQLException
{
if ( conn == null )
return;
conn.close();
}
/**
*
* @return
* @throws SQLException
*/
public ResultSet getQueryResult()
throws SQLException
{
if ( conn == null )
try
{
connect();
}
catch ( ClassNotFoundException e )
{
throw new SQLException( e.getMessage() );
}
String select = properties.getProperty( PROP_JDBC_SELECT, "" );
// TODO: put variables / V1.1
Statement stmt = conn.createStatement();
stmt.setMaxRows( maxRows );
return stmt.executeQuery( select );
}
/**
*
* @return
*/
public String getAppPath()
{
URL url = getClass().getClassLoader().getResource( "com/prcomps/cahitarf" );
if ( url == null )
return null;
String surl = url.toString();
if ( surl.startsWith( "jar:" ) )
{
surl = surl.substring( "jar:file:".length(), surl.indexOf('!') );
surl = surl.substring( 0, surl.lastIndexOf( '/' ) );
}
else if ( surl.startsWith( "file:" ) )
{
surl = surl.substring( "file:".length(), surl.lastIndexOf( "/com/prcomps/cahitarf") );
}
else
return null;
int spc = surl.indexOf( "%20" );
if ( spc > -1 )
{
StringBuffer str = new StringBuffer( surl.length() );
int pos = 0;
while ( spc > -1 )
{
str.append( surl.substring( pos, spc ) ).append( ' ' );
pos = spc + 3;
spc = surl.indexOf( "%20", pos );
}
str.append( surl.substring( pos ) );
surl = str.toString();
}
return surl;
}
/**
*
* @return
*/
public Properties getProperties() { return properties; }
/**
*
* @param properties
*/
public void setProperties( Properties properties )
{
this.properties = properties;
}
/**
*
* @return
*/
public int getMaxRows() { return maxRows; }
/**
*
* @param maxRows
*/
public void setMaxRows( int maxRows ) { this.maxRows = maxRows; }
/**
*
* @param args
*/
public static void main( String args[] )
{
if ( args.length < 2 )
{
System.err.println( bundle.getString( "db2arff.usage" ) );
System.exit( 1 );
}
Db2Arff app = new Db2Arff();
try
{
app.loadProperties( args[0] );
}
catch ( IOException e )
{
System.err.println( MessageFormat.format( bundle.getString("db2arff.conf.notfound"), args ) );
System.exit( 1 );
}
try
{
app.loadDrivers();
}
catch ( FileNotFoundException e )
{
System.err.println( e.getMessage() );
System.exit( 1 );
}
app.toArff( args[1] );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -