package gov.nih.nci.cbiit.cmts.transform;

import java.util.Stack;
import java.util.StringTokenizer;

public class QueryBuilderUtil {

	/**
	 * Retrieve the relative path of an xpath based on current home path
	 * @param currentHome
	 * @param path
	 * @return
	 */
	public static String  retrieveRelativePath(String currentHome, String path)
	{
		
		if (path.startsWith(currentHome))
		{
			String pathEnd=path.substring(currentHome.length());
			if (pathEnd.startsWith("/")||pathEnd.equals(""))
				return pathEnd;
//			return path.substring(currentHome.length());
		}
		
		StringBuilder ancestorPath = new StringBuilder();
		StringTokenizer stCur = new StringTokenizer(currentHome, "/");
		StringTokenizer stNew = new StringTokenizer(path, "/");
 
		//find the common shared ancestor
		while(stCur.hasMoreTokens() && stNew.hasMoreTokens()) 
		{
			String t = stCur.nextToken(); 
			if (t.equals(stNew.nextToken()))
 				ancestorPath.append("/").append(t);
			else 
				break;
		}
		if (ancestorPath.length()==0)//there not any shared ancestor
			return null;
		
		//There are shared ancestor
		String ret = "";
		ret += "/..";
		while(stCur.hasMoreTokens()) {
			ret += "/..";
			stCur.nextToken();
		}
		
		ret += path.substring(ancestorPath.toString().length());
		System.out.println("QueryBuilderUtil.retrieveRelativePath()...return...home="+currentHome+"...path="+path+"..relative="+ret);
		return ret;	
	}
	
	public static String buildXPath(Stack<String>  pathStack) 
	{
		StringBuilder sb = new StringBuilder();
		for (String s:pathStack) {
			sb.append("/").append(s);
		}
		
		return sb.toString();
	}
}

@@ -214,27 +214,29 @@
  @param 
-currentHome
+referencePath
 %0A%09 * @pa
@@ -239,17 +239,25 @@
  @param 
-p
+absoluteP
 ath%0A%09 * 
@@ -324,47 +324,132 @@
 ing 
-currentHome, String path)%0A%09%7B%0A%09%09%0A%09%09if (p
+referencePath, String absolutePath)%0A%09%7B%0A%09%09if (referencePath==null)%0A%09%09%09return absolutePath; //the root element%0A%09%09if (absoluteP
 ath.
@@ -459,27 +459,29 @@
 rtsWith(
-currentHome
+referencePath
 ))%0A%09%09%7B%0A%09
@@ -497,17 +497,25 @@
 pathEnd=
-p
+absoluteP
 ath.subs
@@ -516,35 +516,37 @@
 h.substring(
-currentHome
+referencePath
 .length());%0A
@@ -621,58 +621,8 @@
 nd;%0A
-//%09%09%09return path.substring(currentHome.length());%0A
 %09%09%7D%0A
@@ -722,27 +722,29 @@
 kenizer(
-currentHome
+referencePath
 , %22/%22);%0A
@@ -789,17 +789,25 @@
 kenizer(
-p
+absoluteP
 ath, %22/%22
@@ -1043,16 +1043,35 @@
 ak;%0A%09%09%7D%0A
+%09%09String ret = %22%22;%0A
 %09%09if (an
@@ -1135,20 +1135,17 @@
 %09ret
-urn 
+=
 null;%0A%09%09
 %0A%09%09/
@@ -1140,17 +1140,30 @@
 null;%0A%09%09
-%0A
+else%0A%09%09%7B%0A%09%09%09%0A%09
 %09%09//Ther
@@ -1185,34 +1185,16 @@
 cestor%0A%09
-%09String ret = %22%22;%0A
 %09%09ret +=
@@ -1201,16 +1201,17 @@
  %22/..%22;%0A
+%09
 %09%09while(
@@ -1235,16 +1235,17 @@
 ns()) %7B%0A
+%09
 %09%09%09ret +
@@ -1256,16 +1256,17 @@
 ..%22;%0A%09%09%09
+%09
 stCur.ne
@@ -1274,29 +1274,32 @@
 tToken();%0A%09%09
+%09
 %7D%0A%09%09
-%0A
+%09%0A%09
 %09%09ret += pat
@@ -1295,17 +1295,25 @@
 %09ret += 
-p
+absoluteP
 ath.subs
@@ -1353,16 +1353,20 @@
 gth());%0A
+%09%09%7D%0A
 %09%09System
@@ -1441,19 +1441,21 @@
 e=%22+
-currentHome
+referencePath
 +%22..
@@ -1462,17 +1462,25 @@
 .path=%22+
-p
+absoluteP
 ath+%22..r
@@ -1682,19 +1682,16 @@
 s);%0A%09%09%7D%0A
-%09%09%0A
 %09%09return

check Null for reference path