ignore all lines till ";" found in last line,if condition found in 1st line is true.How?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Vijendra_Singh
Posted On:   Monday, April 10, 2006 07:37 AM

I am ignoring views/procedures from db.sql file by appending "-- " if condition found true.But views/procedures usually take many lines of code. After this condition true i have to ignore all respective lines till i found ";" in end of the view/procedure. SO i have written some code please modify this if someone can to achive the target. try { // open the file for reading FileReader inputstream = new FileReader (sqlFileName); BufferedReader rdr = new BufferedReader( inputstream ); // the StringBuilder which stores the processed lines StringBuffer dataLines = new StringBuffer(); // re   More>>

I am ignoring views/procedures from db.sql file by appending "-- " if condition found true.But views/procedures usually take many lines of code.
After this condition true i have to ignore all respective lines till i found ";" in end of the view/procedure.

SO i have written some code please modify this if someone can to achive the target.






try {

// open the file for reading

FileReader inputstream = new FileReader (sqlFileName);

BufferedReader rdr = new BufferedReader( inputstream );



// the StringBuilder which stores the processed lines

StringBuffer dataLines = new StringBuffer();



// read the file line by line

String dataLine;

while( ( dataLine = rdr.readLine() ) != null ) {



// split the input data into words upto first 3 words only

String[] tokens = dataLine.split( " ", 3 );



// if the line starts with 'create' and a 'view' follows it,

// then add a "--" to the beginning of the line

if( tokens[ 0 ].equalsIgnoreCase( "create" ) &&

tokens[ 1 ].equalsIgnoreCase( "view" ) ||

tokens[ 0 ].equalsIgnoreCase( "create" ) &&

tokens[ 1 ].equalsIgnoreCase( "procedure" )

)

dataLines.append( "-- " );

dataLines.append( dataLine ).append( '
' );

}

rdr.close(); // close the file

inputstream.close();



// open the file for writing new data to it

FileWriter outputstream=new FileWriter( sqlFileName );

BufferedWriter writer = new BufferedWriter(outputstream);



// write the new StringBuilder's data back to the file.

writer.write( dataLines.toString(), 0, dataLines.length() );

writer.close(); // close the file

outputstream.close();



_stream = new FileReader (sqlFileName);



} catch( IOException e ) {

e.printStackTrace();

}

 







Any idea highly appreciated.

Vijendra    <<Less

Re: ignore all lines till ";" found in last line,if condition found in 1st line is true.How?

Posted By:   Robert_Lybarger  
Posted On:   Monday, April 10, 2006 07:57 AM

With your current approach, you'll probably need a boolean and some additional logic. You have four possible states to check for: (1) you haven't started a view/proc yet, (2) you found the start of a view/proc, (3) you're still in a view/proc but haven't found the end, (4) you found the end of the view/proc. Consider:

Consider:



boolean pending=false;
while (read line is not EOF) {
if (!pending) {
if (line contains view or proc.) {
//state #2
pending = true;
add the "--" or whatever
}
else {
//state #1
you're not in a[nother] view/proc yet
}
}
if (pending) { // don't use 'else'
if (line contains ';') {
//state #4
pending = false;
do whatever... you're finished
}
else {
state #3
you're in continuation of view/proc
but haven't found end yet
}
}
}


Note the 'if' I marked "don't use else" which allows the logic to fall through and catch both states #2 and #4 (start and end) on the same line.

About | Sitemap | Contact