写了一个StringParser类,比较烂,不过可以用。。。输入是Toad--Formatter Plus格式化过的SQL,输出是排序后的表名.列名 列表
import java.io.*;
import java.util.*;
public class StringParser {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream("c:/1.sql"
));
String data = null;
HashMap colHolder;
HashMap tabHolder;
Set keyCol;
Set keyTab;
String curKey;
HashMap fullColHolder = new HashMap();
HashMap fullTabHolder = new HashMap();
Set keyFinal;
HashMap finalRes = new HashMap();
while ((data = br.readLine()) != null) {
if (!data.toString().startsWith("/*"
) { //ignore the first line generated by Toad formmater plus!
colHolder = getADotBHashMap(data);
keyCol = colHolder.keySet();
Iterator itCol = keyCol.iterator();
tabHolder = getTableHashMap(data);
keyTab = tabHolder.keySet();
Iterator itTab = keyTab.iterator();
while (itCol.hasNext()) {
curKey = (String) (itCol.next());
String curColumn = (String) colHolder.get(curKey);
fullColHolder.put(curKey, curColumn);
}
while (itTab.hasNext()) {
curKey = (String) (itTab.next());
String curTab = (String) tabHolder.get(curKey);
fullTabHolder.put(curKey, curTab);
}
}
// get the current line for loop processing
}
System.out.println("111111:"+fullTabHolder.size());
System.out.println("222222:"+fullColHolder.size());
keyCol = fullColHolder.keySet();
Iterator itCol = keyCol.iterator();
while (itCol.hasNext()) {
curKey = (String) (itCol.next());
String curColumn = ((String) fullColHolder.get(curKey)).trim();
keyTab = fullTabHolder.keySet();
Iterator itTab = keyTab.iterator();
while (itTab.hasNext()) {
String curTabKey = (String) (itTab.next());
String curTable = ((String) fullTabHolder.get(curTabKey)).trim();
if (curTable.substring(0, curTable.indexOf("."
)
.equalsIgnoreCase(
curColumn.substring(0, curColumn
.indexOf("."
))) {
String curVal = curTable.substring(curTable.indexOf("."
+ 1,
curTable.length())
+ "."
+ curColumn.substring(curColumn.indexOf("."
+1,
curColumn.length());
finalRes.put(curVal, curVal);
}
}
}
TreeMap finalResSorted = new TreeMap(finalRes);
keyFinal = finalResSorted.keySet();
Iterator itFinal = keyFinal.iterator();
while(itFinal.hasNext()){
curKey = (String)(itFinal.next());
System.out.println(finalResSorted.get(curKey));
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found"
;
} catch (IOException e1) {
e1.printStackTrace();
}
}
// read current line, from start to end, find A.B, put A.B into output array
// resume the rest of the current line, until finishes
public static String getADotB(String curLine) {
int posStart = 0;
int posEnd = 0;
for (int i = 0; i <= curLine.length(); i++) {
// first find the Dot
if (curLine.indexOf("."
>= 0) {
posStart = getPortion(curLine, curLine.indexOf("."
, 1);
posEnd = getPortion(curLine, curLine.indexOf("."
, 0);
}
return curLine.substring(posStart, posEnd);
}
return "null";
}
public static HashMap getADotBHashMap(String curLine) {
int posStart = 0;
int posEnd = 0;
HashMap hashADB = new HashMap();
for (int i = 0; i <= curLine.length(); i++) {
// first find the Dot
if (curLine.indexOf(".") >= 0) {
posStart = getPortion(curLine, curLine.indexOf("."), 1);
posEnd = getPortion(curLine, curLine.indexOf("."), 0);
String curColumn = curLine.substring(posStart, posEnd);
hashADB.put(curLine + posEnd, curColumn);
}
String subLine = curLine.substring(posEnd, curLine.length());
if (subLine.indexOf(".") >= 0) {
posStart = getPortion(subLine, subLine.indexOf("."), 1);
posEnd = getPortion(subLine, subLine.indexOf("."), 0);
hashADB.put(subLine + posEnd, subLine.substring(posStart,
posEnd));
}
}
return hashADB;
}
public static HashMap getTableHashMap(String curLine) {
int posStart = 0;
int posEnd = 0;
String tableAlias;
String tableName;
HashMap hashTableName = new HashMap();
for (int i = 0; i <= curLine.length(); i++) {
if (curLine.indexOf("FROM") >= 0) {
posStart = StringParser.getPortion(curLine, curLine
.indexOf("FROM") + 3, 0);
posEnd = StringParser.getPortion(curLine, posStart, 0);
tableName = curLine.substring(posStart + 1, posEnd);
posStart = StringParser.getPortion(curLine, curLine
.indexOf("FROM") + 5, 0);
posEnd = StringParser.getPortion(curLine, posStart, 0);
tableAlias = curLine.substring(posStart, posEnd);
hashTableName.put(curLine + posEnd, tableAlias + "."+ tableName);
String subLine = curLine.substring(posEnd, curLine.length());
if (subLine.indexOf(",") >= 0) {
posStart = StringParser.getPortion(subLine, 0, 0);
posEnd = StringParser.getPortion(subLine, posStart, 0);
tableName = subLine.substring(posStart + 1, posEnd);
posStart = StringParser.getPortion(subLine, subLine
.indexOf(" "), 0);
posEnd = StringParser.getPortion(subLine, posStart, 0);
tableAlias = subLine.substring(posStart, posEnd);
hashTableName.put(curLine + posEnd, tableAlias + "."
+ tableName);
}
}
}
return hashTableName;
}
// Find the adjacent space, direction = 0 -- right, 1 --left
public static int getPortion(String curLine, int indOfDot, int direction) {
int posResult = 0;
if (direction == 0) {
for (int j = indOfDot + 1; j < curLine.length(); j++) {
if (foundToken(curLine.substring(j, j + 1))) {
// find the delimiter
posResult = j;
return posResult;
}
if (j == curLine.length() - 1) {
posResult = j;
return posResult + 1;
}
}
} else {
for (int i = indOfDot; i > 0; i--) {
if (foundToken(curLine.substring(i - 1, i))) {
// find the delimiter
return i;
}
}
}
return 991214;
}
// function for determine that char is part of A-Z a-Z 1-9
public static boolean foundToken(String curChar) {
String testCond = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789_";
if (testCond.indexOf(curChar) >= 0)
return false;
return true;
}
}