diff --git a/org.talend.librariesmanager/resources/java/routines/system/Document.java b/org.talend.librariesmanager/resources/java/routines/system/Document.java index e99d30871c..b5017f408f 100644 --- a/org.talend.librariesmanager/resources/java/routines/system/Document.java +++ b/org.talend.librariesmanager/resources/java/routines/system/Document.java @@ -63,6 +63,13 @@ public class Document { org.dom4j.XPath xpathObjectForDoc = document.createXPath(loopXPath); xpathObjectForDoc.setNamespaceURIs(nsMapping); java.util.List 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 lookupInfo, + Map xpathOfResults, Map xpathToTypeMap, Map 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 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 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; + } + }