fix Bug TDI-18966: Support optional elements as loop element

git-svn-id: http://talendforge.org/svn/tos/trunk@78652 f6f1c999-d317-4740-80b0-e6d1abc6f99e
This commit is contained in:
wwang
2012-02-22 05:28:18 +00:00
parent 12962428f6
commit e518035714

View File

@@ -63,6 +63,13 @@ public class Document {
org.dom4j.XPath xpathObjectForDoc = document.createXPath(loopXPath);
xpathObjectForDoc.setNamespaceURIs(nsMapping);
java.util.List<org.dom4j.tree.AbstractNode> nodes = xpathObjectForDoc.selectNodes(document);
if(nodes.size()==0) {
//set root as loop when no loop nodes
loopXPath = resetLoop(loopXPath,lookupInfo,xpathOfResults,xpathToTypeMap,xpathToPatternMap);
xpathObjectForDoc = document.createXPath(loopXPath);
xpathObjectForDoc.setNamespaceURIs(nsMapping);
nodes = xpathObjectForDoc.selectNodes(document);
}
for (org.dom4j.tree.AbstractNode node : nodes) {
boolean reject = false;
// lookup action
@@ -120,5 +127,59 @@ public class Document {
}
return result;
}
private String resetLoop(String loop, Map<String, Object> lookupInfo,
Map<String, String> xpathOfResults, Map<String, String> xpathToTypeMap, Map<String, String> xpathToPatternMap) {
resetMapRelativeXpathKey(lookupInfo,loop);
resetMapRelativeXpathKey(xpathToTypeMap,loop);
resetMapRelativeXpathKey(xpathToPatternMap,loop);
resetMapRelativeXpathValue(xpathOfResults,loop);
int index = loop.indexOf("/",1);
return loop.substring(0, index>0 ? index : loop.length());
}
private void resetMapRelativeXpathKey(Map<String, ? extends Object> source,String loop) {
Map content = new HashMap();
for(String key : source.keySet()) {
String newKey = resetRelativeXPath(loop,key);
content.put(newKey, source.get(key));
}
source.clear();
source.putAll(content);
}
private void resetMapRelativeXpathValue(Map<String,String> source,String loop) {
Map content = new HashMap();
for(String key : source.keySet()) {
String value = source.get(key);
String newValue = resetRelativeXPath(loop,value);
content.put(key, newValue);
}
source.clear();
source.putAll(content);
}
private String resetRelativeXPath(String loop,String relativePath) {
String absolutePath = loop;
for(String step : relativePath.split("/")) {
if("..".equals(step)) {
absolutePath = absolutePath.substring(0,absolutePath.lastIndexOf("/"));
} else if(".".equals(step)){
//do nothing
} else if(!"".equals(step)){
absolutePath += "/" + step;
}
}
String result = null;
int index = absolutePath.indexOf("/", 1);
if(index<0) {
result = ".";
} else {
result = absolutePath.substring(index+1);
}
return result;
}
}