fix Bug TDI-19179: a better solution for ignore the namespace prefix in xpath

git-svn-id: http://talendforge.org/svn/tos/trunk@77297 f6f1c999-d317-4740-80b0-e6d1abc6f99e
This commit is contained in:
wwang
2012-01-29 08:43:37 +00:00
parent 4322619001
commit e95b73a0cc

View File

@@ -309,106 +309,4 @@ public class StringUtils {
return String.valueOf(d);
}
/**
* delete namespace prefix in the xpath
* @param xpath the source xpath
* @return
*
* example
* ./ns:aa/ns:b ==> ./aa/b
* ../ns:aa/ns:b ==> ../aa/b
* ../aa/ns:b ==> ../aa/b
* ../aa/ns:b/../ns:a/a ==> ../aa/b/../a/a
* /aa/b/c ==> /aa/b/c
* /aa/b/c/@e ==> /aa/b/c/@e
* /aa/b/c/@ns:e ==> /aa/b/c/@e
* aa ==> aa
* ns:aa ==> aa
* child::book ==> child::book
* attribute::attr ==> attribute::attr
* to[@a:attri="xxx:yyy"]/ns:a ==> to[@attri="xxx:yyy"]/a
* to[.="to:yyy"]/ns:a ==> to[.="to:yyy"]/a
* substring('12:3"4:5',2,3) ==> substring('12:3"4:5',2,3)
* substring("12:3'4:5",2,3) ==> substring("12:3'4:5",2,3)
* //name[@ns:attri="xxx:yyyy"] ==> //name[@attri="xxx:yyyy"]
* //name[@ns:attri="xxx:yyyy"]/ns:b/@ns:a ==> //name[@attri="xxx:yyyy"]/b/@a
* body[ns:age/ns:name/@ns:b="12:3"]/age ==> body[age/name/@b="12:3"]/age
*
*/
public static String deletePrefixForXpath(String xpath) {
if(xpath==null) {
return null;
}
char[] block = null;
StringBuffer sb = new StringBuffer();
int size = xpath.length();
int start = -1;
int offset = 0;
char next = ' ';
boolean literal = false;
char textEnclosure = ' ';
for(int i=0;i<size;i++) {
if(':' == next) {
next = ' ';
continue;
}
int end = -1;
char c = xpath.charAt(i);
if(c == '\'' || c == '"') {
if(!literal) {//literal start
literal = true;
textEnclosure = c;
} else if(textEnclosure == c){//literal end
literal = false;
textEnclosure = ' ';
}
}
if(literal) {
continue;
}
if('/' == c || '@' == c || '[' == c) {//element or attribute start
start = i;
}
if(':' == c) {//local element name or attribute name start
if(i<size-1) {
next = xpath.charAt(i+1);
}
if(':'!=next) {
end = i;
}
}
if(end!=-1) {
if(block==null) {
block = xpath.toCharArray();
}
sb.append(block,offset,start+1-offset);
offset = end + 1;
}
}
if(offset == 0) {
return xpath;
}
if(offset < size) {//append the trail
if(block==null) {
block = xpath.toCharArray();
}
sb.append(block,offset,size - offset);
}
return sb.toString();
}
}