?? scriptscanner.java
字號:
/**
* @(#) ScriptScanner.java
*/
package edu.hust.tool;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.Iterator;
/**
* ScriptScanner is a tool for escaping all the commentary in the sql script.
* Three commentaries are supported:
* <p>
* Block comment: codes in /* and */
/* Line comment: -- the remain of the linde
* // the remain of the line
* </p>
*
* <p>
* Usage:
* <code>
* ScriptScanner scanner = new ScriptScanner( script );
* // new ScriptScanner(
* // new File( scriptfile );
* for( Iterator iter = scanner.hasNext(); iter.hasNext(); ) {
* System.out.println( iter.next());
* }
* </code>
* </p>
*/
/*
* @author quickpoint At HUST
* @version 1.0 2007-03-26
*/
public class ScriptScanner {
private BufferedReader reader = null; // the reader( input stream )
private StringBuffer buf; // script buffer
private Collection<String> statements // sttements
= new ArrayList<String>();
private int totalStatements = 0; // total statements count
private boolean skipRemark = true; // skip remark flag
private boolean replaceQuote = true; // replace quote flag
// Constants used in the scanning
private static final String DELIMSTATEMENT = ";";
private static final char ENDLINE = '\n';
private static final char ENDSTATEMENT = ';';
private static final char RSPLASH = '/';
private static final char ONE_QUOTE = '\'';
private static final char DOUBLE_ONE_QUOTE = '"';
private static final char STAR = '*';
private static final char LINESIGN = '-';
/**
* Iterator on the statements
* @return iterator on the result of statements
*/
public Iterator iterator() {
return Collections.unmodifiableCollection(
statements ).iterator();
}
/**
* Total statements count
* @return total statements count
*/
public int totalStatements() {
return totalStatements;
}
//===============================================================
/**
* Construct a scanner by the script string
*/
public ScriptScanner( String script ) {
this( new StringReader( script ));
}
/**
* Construct a scanner by the file
*/
public ScriptScanner( File file ) {
try {
reader = new BufferedReader(
new FileReader( file ));
parse();
} catch ( FileNotFoundException ex ) {
ex.printStackTrace();
} catch ( IOException ex ) {
ex.printStackTrace();
}
}
/**
* Construct a scanner by the reader
*/
public ScriptScanner( Reader reader ) {
try {
this.reader = (reader instanceof BufferedReader )?
(BufferedReader)reader :
new BufferedReader( reader );
parse();
} catch ( IOException ex ) {
ex.printStackTrace();
}
}
/**
* Skip remark during scanning?
* @param skipRemark skip remark flag,
* if set as true, the mark will be skipped,
* else the mark will not be skipped.
* The default is true
*/
public void skipRemark( boolean skipRemark) {
this.skipRemark = skipRemark;
}
/**
* Replace quote druing scanning?
* @param replaceQuote replace quote flag.
* if set as true, the quote " will be replaced as ',
* else the quote will not be replaed.
* The default is true
*/
public void replaceQuote( boolean replaceQuote ) {
this.replaceQuote = replaceQuote;
}
// read one char
private int read() throws IOException {
return reader.read();
}
// do the parse
private void parse() throws IOException {
int ch = read();
buf = new StringBuffer();
while ( true ) {
if ( ch < 0 ) {
break; // end of the file
}
switch ( ch ) {
case ONE_QUOTE: // fall thru
case DOUBLE_ONE_QUOTE: {
int last = ch;
if ( replaceQuote ) {
buf.append( ( char ) ONE_QUOTE );
} else {
buf.append( ( char ) ch );
}
while ( true ) { // read till see the last
ch = read();
if ( ch < 0 ) {
break;
}
if ( replaceQuote && ch == last ) {
buf.append( ONE_QUOTE);
} else {
buf.append( (char) ch );
}
if ( ch == last ) {
break;
}
}
ch = read();
break;
}
case RSPLASH: {
int last = ch;
ch = read();
if ( ch < 0 ) {
break;
}
if ( ch == STAR ) {
// block remaark
if ( !skipRemark ) {
buf.append( ( char ) last );
buf.append( ( char ) ch );
}
while ( true ) { // read till see the */
ch = read();
if ( ch < 0 ) {
break;
}
if ( !skipRemark ) {
buf.append( ( char ) ch );
}
if ( ch == STAR ) {
ch = read();
if ( ch < 0 ) {
break;
}
if ( !skipRemark ) {
buf.append( ( char ) ch );
}
if ( ch == RSPLASH ) {
break;
}
}
}
ch = read();
} else if ( ch == RSPLASH ){
// single line remark
if ( !skipRemark ) {
buf.append( ( char ) last );
buf.append( ( char ) ch );
}
while ( true ) { // read till see the \r\n
ch = read();
if ( ch < 0 ) {
break;
}
if ( !skipRemark ) {
buf.append( ( char ) ch );
}
if ( ch == ENDLINE ) {
break;
}
}
ch = read();
} else {
buf.append( ( char ) last );
}
break;
}
case LINESIGN : {
int last = ch;
ch = read();
if ( ch < 0 ) {
break;
}
if ( ch == LINESIGN ) {
// single line remark
if ( !skipRemark ) {
buf.append( ( char ) last );
buf.append( ( char ) ch );
}
while ( true ) { // read till see the \r\n
ch = read();
if ( ch < 0 ) {
break;
}
if ( !skipRemark ) {
buf.append( ( char ) ch );
}
if ( ch == ENDLINE ) {
break;
}
}
ch = read();
} else {
buf.append( (char) ch );
}
break;
}
default: {
buf.append( ( char ) ch );
ch = read();
break;
}
}
}
// orange the results
StringTokenizer st = new StringTokenizer( buf.toString(), DELIMSTATEMENT );
while( st.hasMoreTokens()) {
String statement = st.nextToken().trim();
if ( statement.length() == 0) { // trip out the empty string
continue;
}
totalStatements++;
statements.add( statement + ENDSTATEMENT);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -