import java.util.Stack;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StrCTSql {
// TEST
public static void main(String[] args){
String in="1a09eb1a-1da2-43ec-ad0f-55e73e4a55e3并1a09eb1a-1da2-43ec-ad0f-55e73e4a55e3 交 1a09eb1a-1da2-43ec-ad0f-55e73e4a55e3 减 1a09eb1a-1da2-43ec-ad0f-55e73e4a55e3",out="";
if(checkBracket(in)&&checkB(in)){
System.out.println(getSql(in));
}else{
System.out.println("ERROR!");
}
}
public static String getSql(String in){
Vector<String> v_temp=getExpression(in,"ADHOC.AD_CV_IMPORT_USER");
String out="";
for(int i=0;i<v_temp.size();i++){
out=out+v_temp.get(i);
}
return out;
}
//顺序提取所有个体
public static Vector<String> getExpression(String str,String tablename)
{
Vector<String> v_temp = new Vector<String>();
char[] temp = new char[str.length()];
str.getChars(0,str.length(),temp,0);
String fi = "";
int x=0,i=0;
String regex_operator = "[//并//减//交//(//)]"; //运算符和括号
Pattern p_operator = Pattern.compile(regex_operator);
Matcher m = null;
boolean b;
while (i<str.length())
{
Character c = new Character(temp[i]);
String s = c.toString();
if(s.endsWith(" ")){
i++;
continue;
}
m = p_operator.matcher(s);
b = m.matches();
if (b)
{
//System.out.println("matches operator");
if(fi!=null&&!fi.equals("")){
fi=" Select MSISDN from "+tablename+" where user_group_id = '"+fi+"' ";
v_temp.add(fi);
}
fi="";
if(s.equals("并")){
s="union";
}else if(s.equals("减")){
s="EXCEPT";
}else if(s.equals("交")){
s="INTERSECT";
}
v_temp.add(s);
}else
{
fi=fi+s;
}
i++;
}
fi=" Select MSISDN from "+tablename+" where user_group_id = '"+fi+"' ";
v_temp.add(fi);
return v_temp;
}
/*
* 正确性检查
*/
public static boolean checkB(String str)
{
boolean b_flag = true;
/*
* 非运算后面跟左括号 eg: 1111(
*/
String false_r1 = "[^ //并//减//交]//(";
if(!pabl(str,false_r1)){
return false;
}
/*
* 左括号后面跟运算符 eg: (并
*/
String false_r2 = "//([//并//减//交]";
if(!pabl(str,false_r2)){
return false;
}
/*
* 右括号前面放运算符 eg: 并)
*/
String false_r3 = "[//并//减//交//(]//)";
if(!pabl(str,false_r3)){
return false;
}
/*
* 右括号后面放非运算符 eg: )11111
*/
String false_r4 = "//)[^ //并//减//交]";
if(!pabl(str,false_r4)){
return false;
}
/*
* 两个运算符一起 并交
*/
String false_r5 = "[//并//减//交]{2}";
if(!pabl(str,false_r5)){
return false;
}
/*
* 运算符前后没有东西
*/
String false_r6 = "[//并//减//交]";
Pattern p_operator = Pattern.compile(false_r6,Pattern.MULTILINE);
Matcher m = null;
m = p_operator.matcher(str);
if(m.find()){
return false;
}
return b_flag;
}
public static boolean pabl(String strall,String pr){
Pattern p_operator = Pattern.compile(pr);
Matcher m = null;
m = p_operator.matcher(strall);
if(m.find()){
return false;
}else{
return true;
}
}
/*
*括号匹配检测
*/
public static boolean checkBracket(String str)
{
Stack<Character> s_check = new Stack<Character>();
boolean b_flag = true;
for (int i=0;i<str.length();i++)
{
char ch = str.charAt(i);
switch(ch)
{
case '(':
s_check.push(ch);
break;
case ')':
if (!s_check.isEmpty())
{
char chx = s_check.pop();
if (ch==')' && chx!='(')
{
b_flag = false;
}
} else {
b_flag = false;
}
break;
default:
break;
}
}
if (!s_check.isEmpty())
{
b_flag = false;
}
return b_flag;
}
}