All pastes #515813 Raw Edit

vlk

public java v1 · immutable
#515813 ·published 2007-05-28 11:29 UTC
rendered paste body
package sk.bielyvlk.gpsdb;import java.lang.*;import java.io.*;import javax.microedition.io.*;import javax.microedition.io.file.*;import javax.microedition.lcdui.*;import sk.bielyvlk.vlkui.*;import sk.bielyvlk.gps.*;public class Import implements Runnable {	private Thread t = null;	protected String url;	private WpDb wpdb;	public Import( String url, WpDb wpdb ) {		this.url = url;		this.wpdb = wpdb;		t = new Thread( this );		t.start();		t.setPriority( Thread.MIN_PRIORITY );	}	public void run() {		Debug.debug( "IMPORT" + url );		if( url.startsWith( "file:///" ) ) {			Debug.debug( "IMPORT: is file" );			importFile();			return;		}		Debug.debug( "IMPORT: unknown protocol!" );	}	private void importFile() {		try {			FileConnection fc = (FileConnection)Connector.open( url, Connector.READ );			if( ! fc.exists() ) {				return;			}			InputStream fis = fc.openInputStream();			if( url.endsWith( ".csv" ) ) {				Debug.debug( "IMPORT: is csv" );				// TODO make it trhrow interfaces..				parseCsv( fis );			}			fc.close();			Debug.debug( "IMPORT: done." );		} catch( IOException e ) {			Debug.debug( "IMPORT: from file ERROR " + e );		}//		VlkGps.navData.refresh();	}	private void parseCsv( InputStream fis ) throws IOException {		StringBuffer sb = new StringBuffer( 400 );		String[] data = new String[ 20 ];		boolean csvOldType = false;		boolean header = true;		int bi;		while( true ) {			bi = fis.read();			if( bi == -1 ) break;			if( bi == '\r' ) continue;			if( bi == '\n' ) {				String s = sb.toString();				if( s.length() == 0 ) break;				int i = 0;				int pos = 0;				try {					// select data					while( true ) {						int nextPos = s.indexOf( ( csvOldType ) ? "," : ";", pos );						if( nextPos < 0 ) nextPos = s.length();						data[ i++ ] = s.startsWith( "\"" ) ? s.substring( pos + 1, nextPos - 1 ) : s.substring( pos, nextPos );						if( nextPos == s.length() ) break;						pos = nextPos + 1;						if( i > 20 ) { Debug.debug( "GPS too many columns" ); break; }					}					if( header ) {						// header						header = false;						// detect old csv file format						csvOldType = data[ 0 ].compareTo( "IDNT,LATITUDE,LONGITUDE,ALTITUDE,PROXIMITY,NAME,DESCRIPTION,CITY,COUNTRY,DATE,SYMBOL" ) == 0;						/*						os.write( ( "\"IDENT\";" ).getBytes() );	//  0						os.write( ( "\"LATITUDE\";" ).getBytes() );	//  1						os.write( ( "\"LONGITUDE\";" ).getBytes() );	//  2						os.write( ( "\"ELEVATION\";" ).getBytes() );	//  3						os.write( ( "\"PROXIMITY\";" ).getBytes() );	//  4						os.write( ( "\"SYMBOL\";" ).getBytes() );	//  5						os.write( ( "\"NAME\";" ).getBytes() );		//  6						os.write( ( "\"CITY\";" ).getBytes() );		//  7						os.write( ( "\"COUNTRY\";" ).getBytes() );	//  8						os.write( ( "\"DESCRIPTION\";" ).getBytes() );	//  9						os.write( ( "\"COLOR\";" ).getBytes() );	// 10						os.write( ( "\"BOLD\";" ).getBytes() );		// 11						os.write( ( "\"LARGE\";" ).getBytes() );	// 12						os.write( ( "\"HIDE IDENT\";" ).getBytes() );	// 13						os.write( ( "\"DATE TIME\"\r\n" ).getBytes() );	// 14						*/					} else {						Debug.debug( data[ 0 ] );						// data						if( csvOldType ) {							wpdb.add( new Waypoint( new Coords( Nmea.parseInt( data[ 2 ] ) / 2, Nmea.parseInt( data[ 1 ] ) / 2, Nmea.parseInt( data[ 3 ] ) / 100 ), new WaypointInfo( 0, false, false, false, data[ 5 ], data[ 0 ], data[ 7 ], data[ 8 ], data[ 6 ] ), Nmea.parseInt( data[ 4 ] ) * Coords.M, Nmea.parseInt( data[ 10 ] ) ) );						} else {							wpdb.add( new Waypoint( new Coords( data[ 2 ], data[ 1 ], data[ 3 ] ), new WaypointInfo( WaypointInfo.getColorId( data[ 10 ] ), parseBoolean( data[ 11 ] ), parseBoolean( data[ 12 ] ), parseBoolean( data[ 13 ] ), data[ 6 ], data[ 0 ], data[ 7 ], data[ 8 ], data[ 9 ] ), VlkText.parseFix( data[ 4 ], Coords.M ), WpSymbols.getId( data[ 5 ] ) ) );						}					}				} catch( Exception e ) {					Debug.debug( "IMPORT: parse ERROR i=" + i + " pos=" + pos );					Debug.debug( "" + e );				}				sb = new StringBuffer( 400 );			} else {				sb.append( (char)bi );			}		}		return;	}	private boolean parseBoolean( String s ) {		return s.startsWith( "true" ) ? true : false;	}}