fix Bug TDI-19179: If one XSD contains some prefixes, later it's impossible to read a xml without prefix.

git-svn-id: http://talendforge.org/svn/tos/trunk@76636 f6f1c999-d317-4740-80b0-e6d1abc6f99e
This commit is contained in:
wwang
2012-01-12 09:32:16 +00:00
parent 96ab934eb9
commit e591e4d779

View File

@@ -309,4 +309,68 @@ public class StringUtils {
return String.valueOf(d);
}
/**
* delete namespace prefix in the xpath
* @param xpath the source xpath
* @return
*/
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 = ' ';
for(int i=0;i<size;i++) {
if(':' == next) {
next = ' ';
continue;
}
int end = -1;
char c = xpath.charAt(i);
if('/' == 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();
}
}