IRQL 언어 해석

자세히 보기
package com.rayeen.spider.vertical.util;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.rayeen.spider.vertical.ParalleIRVirtualMachine;
import com.rayeen.spider.vertical.algorithm.AddFunction;
import com.rayeen.spider.vertical.algorithm.ClearTagFunction;
import com.rayeen.spider.vertical.algorithm.DoubleParameterFunction;
import com.rayeen.spider.vertical.algorithm.EqNullOperator;
import com.rayeen.spider.vertical.algorithm.EqOperator;
import com.rayeen.spider.vertical.algorithm.EqStringOperator;
import com.rayeen.spider.vertical.algorithm.FullUrlFunction;
import com.rayeen.spider.vertical.algorithm.Function;
import com.rayeen.spider.vertical.algorithm.MaxlengthFunction;
import com.rayeen.spider.vertical.algorithm.Operator;
import com.rayeen.spider.vertical.algorithm.RecursiveFunction;
import com.rayeen.spider.vertical.algorithm.ReplaceFunction;
import com.rayeen.spider.vertical.algorithm.SprintfFunction;
import com.rayeen.spider.vertical.algorithm.UneqNullOperator;
import com.rayeen.spider.vertical.algorithm.UneqOperator;
import com.rayeen.spider.vertical.algorithm.UneqStringOperator;
import com.rayeen.spider.vertical.algorithm.UniParameterFunction;
import com.rayeen.spider.vertical.auxiliary.CrawlResultSetCollection;
import com.rayeen.spider.vertical.auxiliary.SemanticException;
import com.rayeen.spider.vertical.auxiliary.TableMerge;
import com.rayeen.spider.vertical.constant.ArgumentType;
import com.rayeen.spider.vertical.constant.ConfConstant;
import com.rayeen.spider.vertical.constant.ErrorType;
import com.rayeen.spider.vertical.constant.FunctionConstant;

public class ResutTree {

	static final Logger LOG = Logger.getLogger(ResutTree.class);

	static Map FunctionNameMap = new ConcurrentHashMap();

	//       
	static Set uniParameterFunction = new HashSet();

	//
	static Set doubleParameterFunction = new HashSet();

	static {
		uniParameterFunction.add(FunctionConstant.FULL_URL);
		uniParameterFunction.add(FunctionConstant.CLEAR_TAG);

		doubleParameterFunction.add(FunctionConstant.MAX_LENGTH);
		doubleParameterFunction.add(FunctionConstant.ADD);

	}

	static Function getFunctionInstance(String func) {

		Class cls = FunctionNameMap.get(func).getClass();
		Function f = null;
		try {
			f = (Function) cls.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return f;
	}

	//   f(..) showF:toByte  
	static Pattern FUNC_PATTERN = Pattern
			.compile("(\\w+)\\s*\\((.+?)\\)\\s+(\\w+(:\\w+)?)");

	Map fieldFunctionMap = new ConcurrentHashMap();

	Map showFieldFunctionMap = new ConcurrentHashMap();

	static Map operators = new LinkedHashMap();
	static {
		operators.put("!=", new UneqOperator());
		operators.put("=", new EqOperator());
		operators.put("is", new EqNullOperator());
		operators.put("not", new UneqNullOperator());

		FunctionNameMap.put(FunctionConstant.FULL_URL, new FullUrlFunction());
		FunctionNameMap.put(FunctionConstant.SPRINGTF, new SprintfFunction());
		FunctionNameMap
				.put(FunctionConstant.RECURSIVE, new RecursiveFunction());
		FunctionNameMap.put(FunctionConstant.ADD, new AddFunction());
		FunctionNameMap.put(FunctionConstant.REPLACE, new ReplaceFunction());
		FunctionNameMap.put(FunctionConstant.CLEAR_TAG, new ClearTagFunction());
		FunctionNameMap.put(FunctionConstant.MAX_LENGTH,
				new MaxlengthFunction());

	}

	public ResutTree(URL rootUrl,
			Map> hierarchyResult,
			CrawlResultSetCollection crawlRSC) {

		this.rootUrl = rootUrl;
		this.hierarchyResult = hierarchyResult;
		this.crawlRSC = crawlRSC;
		/**
		 * curUniResultTableMap     key "tableName->extractName"  
		 */
	}

	static void error(String str) throws SemanticException {
		LOG.error(str);
		throw new SemanticException(str);
	}

	static void warn(String str) {
		LOG.error(str);
	}

	URL rootUrl;

	Map> hierarchyResult = new HashMap>();

	CrawlResultSetCollection crawlRSC;

	Map mergeMap = new ConcurrentHashMap();

	class Pfk {
		String p;//   

		String f;//     

		String k;//     

		public Pfk(String p, String f, String k) {
			super();
			this.p = p;
			this.f = f;
			this.k = k;

			if (StringUtils.isEmpty(k)) {
				this.k = f;
			}
		}

		public String toString() {
			return p + ":" + f + ":" + k;
		}
	}

	class Pkpk {

		Operator operator;

		ArgumentType argumentType;

		List argList;

		/**
		 *   argmentType , operator  argList      
		 * 
		 */
		public Pkpk(ArgumentType argmentType, Operator operator,
				List argList) {
			this.argumentType = argmentType;
			this.operator = operator;
			this.argList = argList;
		}

		public String toString() {
			return argumentType + ":" + operator + ":" + argList.toString();
		}

		public List getArgList() {
			return argList;
		}

		public void setArgList(List argList) {
			this.argList = argList;
		}

		public ArgumentType getArgmentType() {
			return argumentType;
		}

		public void setArgmentType(ArgumentType argmentType) {
			this.argumentType = argmentType;
		}

		public Operator getOperator() {
			return operator;
		}

		public void setOperator(Operator operator) {
			this.operator = operator;
		}

	}

	private List filterFinalResult(List> mainRows,
			List pfkList, ArrayList hierarchyPfkList,
			List pkpkList) throws SemanticException {

		Map fieldMap = new HashMap();
		for (Pfk tmp : pfkList) {
			if (fieldMap.containsKey(tmp.k)) {
				error("duplicate show key :" + tmp.k);
			} else {
				fieldMap.put(tmp.p + "." + tmp.f, tmp.k);
			}
		}

		for (Pfk tmp : hierarchyPfkList) {
			if (fieldMap.containsKey(tmp.k)) {
				error("duplicate show key :" + tmp.k);
			} else {
				fieldMap.put(tmp.p + "." + tmp.f, tmp.k);
			}
		}

		List resultList = new ArrayList();
		for (Map res : mainRows) {

			boolean fit = true;
			for (Pkpk p : pkpkList) {

				if (p.getArgmentType() == ArgumentType.ONE) {
					String p1 = p.getArgList().get(0);
					String f1 = p.getArgList().get(1);
					if (!p.getOperator().operator(res.get(p1 + "." + f1))
							.equals(ConfConstant.TRUE)) {
						fit = false;
						break;
					}
				}

				if (p.getArgmentType() == ArgumentType.TWO) {
					String p1 = p.getArgList().get(0);
					String f1 = p.getArgList().get(1);

					String p2 = p.getArgList().get(2);
					String f2 = p.getArgList().get(3);

					if (!p.getOperator().operator(res.get(p1 + "." + f1),
							res.get(p2 + "." + f2)).equals(ConfConstant.TRUE)) {
						fit = false;
						break;
					}
				}

			}


			if (fit) {

				Map tmpMap = new HashMap();
				for (String key : res.keySet()) {
					if (fieldMap.containsKey(key) &&  !fieldMap.get(key).endsWith(":FUNCTION")) {

						String value = res.get(key);

						tmpMap.put(fieldMap.get(key), value);
					}
				}

				//    function     
				for (String key : showFieldFunctionMap.keySet()) {
					Function func = showFieldFunctionMap.get(key);
					String fieldValue = "";

					if (func instanceof FullUrlFunction) {
						String field = ((FullUrlFunction) func).getField();
						fieldValue = res.get(field);
						// tmpMap.remove(ParseUtils.parseFieldShowValue(field));
						fieldValue = func.operator(new Object[] { rootUrl,
								fieldValue });

					} else if (func instanceof SprintfFunction) {
						SprintfFunction sf = (SprintfFunction) func;
						List fields = sf.getFields();
						List args = new ArrayList();
						args.add(sf.getFormat());
						for (String f : fields) {
							args.add(res.get(f));
							// tmpMap.remove(ParseUtils.parseFieldShowValue(f));
						}
						fieldValue = sf.operator(args.toArray());

					} else if (func instanceof ReplaceFunction) {
						ReplaceFunction rpf = (ReplaceFunction) func;
						String field = res.get(rpf.getField());
						fieldValue = rpf.operator(new Object[] { field,
								rpf.getPatternStr(), rpf.getReplaceStr() });

						// tmpMap.remove(ParseUtils.parseFieldShowValue(addf.getField()));
					} else if (func instanceof ClearTagFunction) {
						ClearTagFunction ctf = (ClearTagFunction) func;
						String value = res.get(ctf.getField());
						fieldValue = ctf.operator(new Object[] { value });

						// tmpMap.remove(ParseUtils.parseFieldShowValue(addf.getField()));
					} else if (func instanceof DoubleParameterFunction) {
						//             
						//     ,       ,  Add,Maxlength
						String field = ((DoubleParameterFunction) func)
								.getField();
						String parameter = ((DoubleParameterFunction) func)
								.getParameter();
						fieldValue = res.get(field);
						fieldValue = func.operator(new Object[] { fieldValue });

					} else if (func instanceof RecursiveFunction) {

						RecursiveFunction rf = (RecursiveFunction) func;
						List args = new ArrayList();

						String functions = rf.getFunctions();
						args.add(functions);

						String[] params = rf.getParams();

						for (String f : params) {
							//       ,       
							if (!f.startsWith("\"")) {
								args.add(res.get(f));
							} else { //   ,        
								args.add(ParseUtils.parseStrContent(f));
							}
							// tmpMap.remove(ParseUtils.parseFieldShowValue(f));
						}
						fieldValue = rf.operator(args.toArray());

					}

					// fieldValue=showFieldFunctionMap.get(key).operator(new
					// Object[]{value});
					tmpMap.put(key, fieldValue);
				}

				resultList.add(tmpMap);
			}

		}
		return resultList;

	}

	/**
	 * get main row and call "filterFinalResult"
	 * 
	 * @param pageNameMap
	 * @param pfkList
	 * @param pkpkList
	 * @return
	 * @throws SemanticException
	 */
	@SuppressWarnings("unchecked")
	private List getFinalResult(Map pageNameMap,
			List pfkList, ArrayList hierarchyPfkList,
			List pkpkList) throws SemanticException {

		long threadId = Thread.currentThread().getId();

		//      
		Collections.sort(pfkList, new Comparator() {

			public int compare(Object o1, Object o2) {
				Pfk p1 = (Pfk) o1;
				Pfk p2 = (Pfk) o2;
				return p1.p.compareToIgnoreCase(p2.p);
			}

		});
		//

		//             
		Map> pkfMap = new HashMap();
		for (Pfk tmp : pfkList) {
			if (!pkfMap.containsKey(tmp.p)) {
				Set set = new HashSet();
				set.add(tmp.f);
				pkfMap.put(tmp.p, set);
			} else {
				pkfMap.get(tmp.p).add(tmp.f);
			}

		}

		Map> hierarchyPkfMap = new HashMap();
		for (Pfk tmp : hierarchyPfkList) {
			if (!hierarchyPkfMap.containsKey(tmp.p)) {
				Set set = new HashSet();
				set.add(tmp.f);
				hierarchyPkfMap.put(tmp.p, set);
			} else {
				hierarchyPkfMap.get(tmp.p).add(tmp.f);
			}

		}

		//   Map         IRQL             。
		//          。
		Map> pkpkMap = new HashMap();

		for (Pkpk tmp : pkpkList) {

			if (tmp.getArgmentType() == ArgumentType.ONE) {
				if (tmp.getArgList().size() >= 2) {
					String page = tmp.getArgList().get(0);
					String field = tmp.getArgList().get(1);

					if (!pkpkMap.containsKey(page)) {
						Set set = new HashSet();
						pkpkMap.put(page, set);
					}
					//    set
					//   set    
					//               
					if (pkfMap.containsKey(page)) {
						pkpkMap.get(page).add(field);
					}
				}

			}

			if (tmp.getArgmentType() == ArgumentType.TWO) {
				if (tmp.getArgList().size() == 2) {
					String page = tmp.getArgList().get(2);
					String field = tmp.getArgList().get(3);

					if (!pkpkMap.containsKey(page)) {
						Set set = new HashSet();
						pkpkMap.put(page, set);
					}

					if (pkfMap.containsKey(page)) {
						pkpkMap.get(page).add(field);
					}
				}
			}

		}

		String curTableName = "";
		String exTableName = "";
		//  select P1.bcname bcname,P2.scame scame,P3.songname
		// songname,P3.downlink downlink"
		//    ,       

		List> mainRows = new ArrayList();

		for (Pfk pfk : pfkList) {

			//       
			//     

			if (!pageNameMap.containsKey(pfk.p)) {
				ParalleIRVirtualMachine.error("invalid pagename:" + pfk.p,
						ErrorType.SEMANTIC);
			}
			String tableName = pageNameMap.get(pfk.p);

			if (mergeMap.containsKey(tableName)) {
				continue;
			}

			if (null == tableName) {
				ParalleIRVirtualMachine.error("invalid page alias" + pfk.p,
						ErrorType.SEMANTIC);
			}

			curTableName = tableName;

			List> rows = new ArrayList();

			//           ,       
			// "   ->down"       shareTable    
			//          if(tableName.contains("->"))
			if (crawlRSC.getGlobalShareResultTableMap(tableName).size() == 0) {

				//   ->   
				String[] prPair = tableName.split("->");
				if (prPair.length == 2) {

					String pageName = prPair[0];
					String ruleName = prPair[1];
					//                      
					if (crawlRSC.getGlobalUniResultTableMap(pageName, ruleName)
							.size() == 0) {
						warn("Thread-" + threadId
								+ ":invalid uniTable pagename:" + pageName
								+ "->" + ruleName);
						continue;
					} else {

						rows = crawlRSC.getGlobalUniResultTableMap(pageName,
								ruleName);
					}
				} else {
					warn("invalid pagename:" + tableName + " or match failed");
				}
			} else {
				//        
				rows = crawlRSC.getGlobalShareResultTableMap(tableName);
			}

			if (rows.size() == 0) {
				break;
			}

			//          
			if (StringUtils.isNotEmpty(exTableName)
					&& !StringUtils.equalsIgnoreCase(curTableName, exTableName)) {

				//  mainRows rows  
				List> tmpRows = new ArrayList();
				for (Map result : mainRows) {
					//       ,      +           

					for (Map map : rows) {
						Map tpMap = new HashMap();
						tpMap.putAll(result);
						
						//    pfk.k     :toByte,         :toByte,     
						String key=pfk.k;
						if(pfk.k.endsWith(ConfConstant.TO_BYTE)){
							int ix = pfk.k.lastIndexOf(ConfConstant.TO_BYTE);
							key=	key.substring(0, ix);
						}
						
						tpMap.put(pfk.p + "." + pfk.f, map.get(key));

						//          
						if (pkpkMap.containsKey(pfk.p)) {
							Set ext = pkpkMap.get(pfk.p);
							for (String f : ext) {
								tpMap.put(pfk.p + "." + f, map.get(f));
							}
						}
						//         
						tmpRows.add(tpMap);
					}

				}

				mainRows = tmpRows;//       

			} else {//           

				if (StringUtils.isEmpty(exTableName)) {

					for (Map map : rows) {
						Map tpMap = new HashMap();
						
						String fld=pfk.f;
						if(fld.endsWith(ConfConstant.FUNCTOIN_POSTFIX)){
							fld=fld.substring(0,fld.length()-ConfConstant.FUNCTOIN_POSTFIX.length()  );
						}
						
						tpMap.put(pfk.p + "." + pfk.f, map.get(fld));

						//          
						if (pkpkMap.containsKey(pfk.p)) {
							Set ext = pkpkMap.get(pfk.p);
							for (String f : ext) {
								tpMap.put(pfk.p + "." + f, map.get(f));
							}
						}

						mainRows.add(tpMap);
					}
				} else {

					for (int i = 0; i < mainRows.size(); i += rows.size()) {
						
						
						//   ":FUNCTION"    ,      
						String fld=pfk.f;
						if(fld.endsWith(ConfConstant.FUNCTOIN_POSTFIX)){
							fld=fld.substring(0,fld.length()-ConfConstant.FUNCTOIN_POSTFIX.length()  );
						}
						

						String pf = pfk.p + "." + pfk.f;
						for (int j = 0; j < rows.size(); j++) {
							mainRows.get(i + j).put(pf, rows.get(j).get(fld));

							if (pkpkMap.containsKey(pfk.p)) {
								Set ext = pkpkMap.get(pfk.p);
								for (String f : ext) {
									mainRows.get(i + j).put(pfk.p + "." + f,
											rows.get(j).get(f));
								}
							}
						}
					}

				}

			}

			exTableName = tableName;

		}

		//            ,       
		for (String targetKey : mergeMap.keySet()) {

			String p = "";
			for (String pkey : pageNameMap.keySet()) {
				if (pageNameMap.get(pkey).equals(targetKey)) {
					p = pkey;
					break;
				}
			}
			if (StringUtils.isEmpty(p)) {
				ParalleIRVirtualMachine.error(
						"invalid page alias:" + targetKey, ErrorType.SEMANTIC);
			}

			if (StringUtils.isEmpty(targetKey))
				continue;

			TableMerge merge = mergeMap.get(targetKey);
			Set srcTbls = merge.getMergedTable();

			List> mainMergeRows = new ArrayList();

			//         
			// mainRow.add(new HashMap());

			for (String tableName : srcTbls) {

				List> rows = new ArrayList();

				String[] prPair = tableName.split("->");

				if (prPair.length == 2) { //     
					String pageName = prPair[0].trim();
					String ruleName = prPair[1].trim();

					if (crawlRSC.getGlobalUniResultTableMap(pageName, ruleName)
							.size() == 0) {
						warn("Thread-" + threadId
								+ ":invalid uniTable pagename:" + pageName
								+ "->" + ruleName);
						continue;
					} else {
						rows = crawlRSC.getGlobalUniResultTableMap(pageName,
								ruleName);

						if (mainRows.size() == 0) {

							List> tmpRows = new ArrayList();

							for (Map row : rows) {
								Map tmpMap = new HashMap();
								for (String columName : row.keySet()) {
									tmpMap.put(p + "." + columName, row
											.get(columName));
								}
								tmpRows.add(tmpMap);
							}
							mainRows = tmpRows;
						} else {

							// 2x2=>4
							List> tmpRows = new ArrayList();

							for (Map mainColumn : mainRows) {

								for (Map row : rows) {
									Map tmpMap = new HashMap();
									for (String columName : row.keySet()) {
										tmpMap.put(p + "." + columName, row
												.get(columName));
									}
									tmpMap.putAll(mainColumn);
									tmpRows.add(tmpMap);
								}
							}
							mainRows = tmpRows;
						}

						// }
					}
				} else {//     

					String pageName = tableName.trim();
					if (crawlRSC.getGlobalShareResultTableMap(pageName).size() == 0) {
						warn("Thread-" + threadId + ":invalid  pagename:"
								+ pageName);
						continue;
					} else {
						rows = crawlRSC.getGlobalShareResultTableMap(pageName);

						if (mainRows.size() == 0) {
							mainRows = rows;
						} else {
							for (Map mainColumn : mainRows) {
								for (Map column : rows) {
									mainColumn.putAll(column);
								}
							}
						}

					}
				}
			}

		}

		//    hierarchyPfkList    
		for (Pfk pfk : hierarchyPfkList) {

			//     
			String tableName = pageNameMap.get(pfk.p);

			curTableName = tableName;

			if (!hierarchyResult.containsKey(tableName)) {
				String err = "invalid hierarchy tableName:" + tableName;
				err += ",
if u want to specify a hierarchy tableName, u needn't to write the matchName"; err += ",
because only one matchName can be used as a hierarchyTable"; error(err); continue; } Map row = hierarchyResult.get(tableName); // , “.” , // , , for (int i = 0; i < mainRows.size(); i++) { String pf = pfk.p + "." + pfk.f; mainRows.get(i).put(pf, row.get(pfk.f)); } } return filterFinalResult(mainRows, pfkList, hierarchyPfkList, pkpkList); } public List getMapResult(String irql) throws SemanticException { int pm = irql.indexOf(";"); if (-1 == pm) { String err = "invalid IRQL format:" + irql; err += "
have u forget to put ';' after Page Define?"; ParalleIRVirtualMachine.error(err, ErrorType.SEMANTIC); } String pageStr = irql.substring(0, pm); Map pageNameMap = new HashMap(); String[] fields = pageStr.split(","); for (String field : fields) { String[] kv = field.split(":"); if (kv.length == 2) { pageNameMap.put(kv[1].trim(), kv[0].trim()); } else { error("error pageMap description:" + field); } } String prefix = ""; String postfix = ""; int w = irql.indexOf("where"); if (w != -1) { prefix = irql.substring(pm + 1, w).trim(); postfix = irql.substring(w); } else { prefix = irql; } int s = prefix.indexOf("select"); if (s == -1) { ParalleIRVirtualMachine.error("miss 'select'", ErrorType.GRAMMER); } s = s + "select".length(); String fieldStr = prefix.substring(s).trim(); Set functionalFields = new HashSet(); Matcher m = FUNC_PATTERN.matcher(fieldStr); boolean found = false; int last = 0; while (m.find(last)) { found = true; last = m.end(); String field = m.group(0); String functionName = m.group(1); Function fun = FunctionNameMap.get(functionName); String showField = m.group(3); if (null == fun) { error("unexisting function anme :" + functionName); } // if (uniParameterFunction.contains(functionName)) { UniParameterFunction func = (UniParameterFunction) getFunctionInstance(functionName); field = m.group(2); // if (field.contains(",") || field.contains(")")) { error("invalid Uniunction format"); } String tableFieldKey = field.replace("->", "."); // ":FUNCTION" tableFieldKey=tableFieldKey+ConfConstant.FUNCTOIN_POSTFIX; func.setField(tableFieldKey); functionalFields.add(field); showFieldFunctionMap.put(showField, func); } if (doubleParameterFunction.contains(functionName)) { DoubleParameterFunction func = (DoubleParameterFunction) getFunctionInstance(functionName); String[] args = m.group(2).split(","); func.setParameter(ParseUtils.parseStrContent(args[1].trim())); if (args.length != 2) { error("invalid DoubleParameterFunction format"); } String tableFieldKey = args[0].replace("->", "."); // ":FUNCTION" tableFieldKey=tableFieldKey+ConfConstant.FUNCTOIN_POSTFIX; func.setField(tableFieldKey); functionalFields.add(args[0]);// “ ”, showFieldFunctionMap.put(showField, func); } // 3 if (functionName.equals(FunctionConstant.REPLACE)) { ReplaceFunction rpf = new ReplaceFunction(); String[] args = m.group(2).split(","); if (args.length != 3) { error("invalid parameter for replace function"); } String tableFieldKey = args[0].replace("->", "."); // ":FUNCTION" tableFieldKey=tableFieldKey+ConfConstant.FUNCTOIN_POSTFIX; rpf.setField(tableFieldKey); String patternStr = args[1].replace("\\\"", ""); String replaceStr = args[2].replace("\\\"", ""); rpf.setPatternStr(patternStr); rpf.setReplaceStr(replaceStr); functionalFields.add(tableFieldKey); showFieldFunctionMap.put(showField, rpf); } // N if (functionName.equals(FunctionConstant.SPRINGTF)) { SprintfFunction spf = new SprintfFunction(); String[] args = m.group(2).split(","); spf.setFormat(ParseUtils.parseStrContent(args[0].trim())); ArrayList spFields = new ArrayList(); for (int i = 1; i < args.length; i++) { // , , String tableFieldKey = args[i].replace("->", "."); // ":FUNCTION" tableFieldKey=tableFieldKey+ConfConstant.FUNCTOIN_POSTFIX; spFields.add(tableFieldKey); functionalFields.add(args[i]); } spf.setFields(spFields.toArray(new String[0])); showFieldFunctionMap.put(showField, spf); } // if (functionName.equals(FunctionConstant.RECURSIVE)) { RecursiveFunction rf = new RecursiveFunction(); String[] args = m.group(2).split(","); rf.setFunctions(ParseUtils.parseStrContent(args[0].trim())); List params = new ArrayList(); for (int i = 1; i < args.length; i++) { // , , if (!args[i].trim().startsWith("\"")) { String tableFieldKey = args[i].replace("->", "."); // ":FUNCTION" tableFieldKey=tableFieldKey+ConfConstant.FUNCTOIN_POSTFIX; functionalFields.add(tableFieldKey); params.add(args[i]); } else { params.add(args[i]); } } rf.setParams(params.toArray(new String[0])); showFieldFunctionMap.put(showField, rf); } } if (found) { fieldStr = m.replaceAll(""); } // fieldStr , ArrayList pfk = new ArrayList(); ArrayList hierarchyPfk = new ArrayList(); fields = fieldStr.split(","); Set metNames = new HashSet(); // 。。。 // k “:FUNCTION” , // cleartag(P.f) f, P.f f2, f f2 //f “:FUNCTION” , filterFinalResult if (fieldMap.containsKey(key) && !fieldMap.get(key).endsWith(":FUNCTION")) { // “:FUNCTION” // k “:FUNCTION” , P.f showField //fieldMap={"P.f:FUNCTION"=>"f1","P.f"=>"f2"} // "P.f"=>"f2 , function for (String field : functionalFields) { field = field.trim(); // if (field.indexOf("->") > 0) { String[] pf = field.trim().split("->"); if (pf.length == 2) { String f = ParseUtils.getRealFieldName(pf[1]); //hierarchyPfk.add(new Pfk(pf[0].trim(), f, f)); hierarchyPfk.add(new Pfk(pf[0].trim(), f+ConfConstant.FUNCTOIN_POSTFIX, f)); } else { error("error prefix pf:" + field); } } else { String[] pf = field.trim().split("\\."); if (pf.length == 2) { String f = ParseUtils.getRealFieldName(pf[1]); //pfk.add(new Pfk(pf[0].trim(), f, f)); pfk.add(new Pfk(pf[0].trim(), f+ConfConstant.FUNCTOIN_POSTFIX, f)); } else { error("error prefix pf:" + field); } } } for (String field : fields) { if (StringUtils.isBlank(field)) continue; field = field.trim(); String[] kv = field.trim().split("\\s+"); if (kv.length == 2) { // if (!metNames.contains(kv[1].trim())) { metNames.add(kv[1].trim()); } else { error("duplicate show field:" + kv[1]); } // select P.songName songName,P->downlink downlink; if (kv[0].indexOf("->") > 0) { String[] pf = kv[0].split("->"); if (pf.length == 2) { hierarchyPfk.add(new Pfk(pf[0].trim(), pf[1].trim(), kv[1].trim())); } else { error("error prefix pf:" + kv[0]); } } else { String[] pf = kv[0].trim().split("\\."); if (pf.length == 2) { pfk.add(new Pfk(pf[0].trim(), pf[1].trim(), kv[1] .trim())); } else { error("error prefix pf:" + kv[0]); } } } else if (kv.length == 1) { // select P.songName,P->downlink; // if (kv[0].indexOf("->") > 0) { String[] pf = kv[0].trim().split("->"); if (pf.length == 2) { String f = ParseUtils.getRealFieldName(pf[1]); // if (!metNames.contains(f)) { metNames.add(f); } else { error("duplicate show field:" + f); } metNames.add(pf[1]); hierarchyPfk.add(new Pfk(pf[0].trim(), f, f)); } else { error("error prefix pf:" + kv[0]); } } else { String[] pf = kv[0].trim().split("\\."); if (pf.length == 2) { String f = ParseUtils.getRealFieldName(pf[1]); if (!metNames.contains(f)) { metNames.add(f); } else { error("duplicate show field:" + f); } pfk.add(new Pfk(pf[0].trim(), f, pf[1].trim())); } else { error("error prefix pf:" + kv[0]); } } } else { StringBuffer fieldsError = new StringBuffer(); for (String f : kv) { fieldsError.append(f); } error("error fields description:" + fieldsError); } }// end foreach fields w = postfix.indexOf("where"); if (w != -1) { fieldStr = postfix.substring(w + "where".length()).trim(); } else { fieldStr = ""; } ArrayList pkpk = new ArrayList(); fields = new String[0]; // if (StringUtils.isNotEmpty(fieldStr)) { fields = fieldStr.split("and"); } for (String field : fields) { field = field.trim(); String optag = "="; // Operator operator = null; // for (String op : operators.keySet()) { if (field.contains(op)) { optag = op; operator = operators.get(op); break; } } // P1.F1=P2.F2, P1.F1=NULL String[] kv = field.split(optag); if (kv.length == 2) {// , String tableKey = kv[0].trim().replace("->", "."); String[] pf = tableKey.split("\\."); String p1 = null, f1 = null, p2 = null, f2 = null; if (pf.length == 2) { p1 = pf[0].trim(); f1 = pf[1].trim(); } else { error("error post pf"); } List args = new CopyOnWriteArrayList(); String targetTableKey = kv[1].trim(); // , if (targetTableKey.equals(ConfConstant.NULL)) { args.addAll(Arrays.asList(new String[] { p1, f1 })); Pkpk cmpNullOP = new Pkpk(ArgumentType.ONE, operator, args); pkpk.add(cmpNullOP); } else if (targetTableKey.startsWith("\"") && targetTableKey.endsWith("\"")) { String str = targetTableKey.substring(1, targetTableKey .length() - 1); // if (operator instanceof UneqOperator) { operator = new UneqStringOperator(str); } else { operator = new EqStringOperator(str); } args.addAll(Arrays.asList(new String[] { p1, f1, str })); Pkpk cmpStrOP = new Pkpk(ArgumentType.ONE, operator, args); pkpk.add(cmpStrOP); } else { pf = kv[1].trim().split("\\."); if (pf.length == 2) { p2 = pf[0].trim(); f2 = pf[1].trim(); args.addAll(Arrays .asList(new String[] { p1, f1, p2, f2 })); } else { error("error post pf"); } Pkpk cmpOP = new Pkpk(ArgumentType.TWO, operator, args); pkpk.add(cmpOP); } } else { error("error fields description"); } } return getFinalResult(pageNameMap, pfk, hierarchyPfk, pkpk); } public static void main(String[] argv) { // Map>> pageMap = new HashMap(); // P1 List> page = new ArrayList(); HashMap map = new HashMap(); map.put("bcid", "v_bcid"); map.put("bcname", "v_bcname"); page.add(map); map = new HashMap(); map.put("bcid", "v_bcid1"); map.put("bcname", "v_bcname1"); page.add(map); pageMap.put("pagename1", page); // P2 page = new ArrayList(); map = new HashMap(); map.put("bcid", "v_bcid"); map.put("scid", "v_sci"); map.put("scname", "v_scname"); page.add(map); map = new HashMap(); map.put("bcid", "v_bcid1"); map.put("scid", "v_sci1"); map.put("scname", "v_scname1"); page.add(map); map = new HashMap(); map.put("bcid", "v_bcid1"); map.put("scid", "v_sci2"); map.put("scname", "v_scname2"); page.add(map); pageMap.put("pagename2", page); // P3 page = new ArrayList(); map = new HashMap(); map.put("scid", "v_sci"); map.put("scname", "v_scname"); map.put("songname", "v_songname1"); map.put("downlink", "v_downlink1"); page.add(map); map = new HashMap(); map.put("scid", "v_sci"); map.put("scname", "v_scname"); map.put("songname", "v_songname2"); map.put("downlink", "v_downlink2"); page.add(map); map = new HashMap(); map.put("scid", "v_sci1"); map.put("scname", "v_scname"); map.put("songname", "v_songname3"); map.put("downlink", "v_downlink3"); page.add(map); map = new HashMap(); map.put("scid", "v_sci1"); map.put("scname", "v_scname"); map.put("songname", "v_songname4"); map.put("downlink", "v_downlink4"); page.add(map); pageMap.put("pagename3", page); Map> hierarchy = new HashMap(); // Map>> curUniResultTableMap =new // ConcurrentHashMap(); // ResutTree resutTree = new ResutTree(pageMap, // hierarchy,curUniResultTableMap); // resutTree.setPageMap(pageMap); // // String str = "pagename1:P1,pagename2:P2,pagename3:P3;select P1.bcname // bcname,P2.scname sncame,P3.songname songname,P3.downlink downlink" // + " downlink where P1.bcid=P2.bcid and P2.scid=P3.scid; dao->insert // "; // // int semi = str.lastIndexOf(";"); // // String dao = str.substring(semi + 1); // String irql = str.substring(0, semi); // // ArrayList am = resutTree.getMapResult(irql); } public void setMergeMap(Map mergeMap) { this.mergeMap = mergeMap; } }

좋은 웹페이지 즐겨찾기