package routines.system; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.dom4j.XPath; import org.dom4j.tree.AbstractNode; public class DocumentToFlat { private org.dom4j.Document doc; private String currentLoop; private String originalLoop; private String[] currentRelativePathMappings; private String[] absolutePathMappings; private Map xmlNameSpaceMap; private boolean top = false; boolean isOptional = false; // use to judge if the loop node is mandotary in the source file. //check whether namespace define exist in UI private boolean defineNS = true; private NameSpaceTool namespaceTool; private List nodes; //result show private List> resultSet = new ArrayList>(); public DocumentToFlat() { } /** * Document to Flat */ public void flat() { XPath loopXpath = null; if(!defineNS) { loopXpath = doc.createXPath(namespaceTool.addDefaultNSPrefix(currentLoop, currentLoop)); } else { loopXpath = doc.createXPath(currentLoop); } loopXpath.setNamespaceURIs(xmlNameSpaceMap); nodes = loopXpath.selectNodes(doc); if(this.isOptional && nodes.size() == 0 && !top) { setParentAsLoop(); flat(); } else if (nodes !=null ) { //reset relative paths if(currentLoop != originalLoop) {//not point to the same string for(int i=0;i row = new HashMap(); resultSet.add(row); //init columns for one row for(int i=0;i 0) { end = idx; } else if(idx == 0) {//currentLoop is root top = true; } currentLoop = currentLoop.substring(0, end); } public List> getResultSet() { return resultSet; } public void setDoc(org.dom4j.Document doc) { this.doc = doc; } public void setOriginalLoop(String originalLoop) { this.originalLoop = originalLoop; this.currentLoop = originalLoop; } public void setCurrentRelativePathMappings(String[] currentRelativePathMappings) { this.currentRelativePathMappings = currentRelativePathMappings; } public void setAbsolutePathMappings(String[] absolutePathMappings) { this.absolutePathMappings = absolutePathMappings; } public void setXmlNameSpaceMap(Map xmlNameSpaceMap) { this.xmlNameSpaceMap = xmlNameSpaceMap; } public void setDefineNS(boolean defineNS) { this.defineNS = defineNS; } public void setNamespaceTool(NameSpaceTool namespaceTool) { this.namespaceTool = namespaceTool; } private Map lookupInfo; private Map xpathOfResults; private Map xpathToTypeMap; private Map xpathToPatternMap; private boolean loopChanged = false; public DocumentToFlat(Map lookupInfo, Map xpathOfResults, Map xpathToTypeMap, Map xpathToPatternMap) { this.lookupInfo = lookupInfo; this.xpathOfResults = xpathOfResults; this.xpathToTypeMap = xpathToTypeMap; this.xpathToPatternMap = xpathToPatternMap; } public void flatForLookup(boolean isOptionalLoop) { XPath loopXpath = doc.createXPath(currentLoop); loopXpath.setNamespaceURIs(xmlNameSpaceMap); nodes = loopXpath.selectNodes(doc); if(isOptionalLoop && nodes.size() == 0 && !top) { setParentAsLoop(); flatForLookup(isOptionalLoop); } else if(currentLoop != originalLoop) {//not point to the same string loopChanged = true; reset(); } } private void reset() { lookupInfo = resetMapRelativeXpathKey(lookupInfo); xpathToTypeMap = resetMapRelativeXpathKey(xpathToTypeMap); xpathToPatternMap = resetMapRelativeXpathKey(xpathToPatternMap); xpathOfResults = resetMapRelativeXpathValue(xpathOfResults); } private Map resetMapRelativeXpathKey(Map source) { Map content = new HashMap(); for(String key : source.keySet()) { String newKey = resetRelativeXPath(key); content.put(newKey, source.get(key)); } return content; } private Map resetMapRelativeXpathValue(Map source) { Map content = new HashMap(); for(String key : source.keySet()) { String value = source.get(key); String newValue = resetRelativeXPath(value); content.put(key, newValue); } return content; } public List getNodes() { return nodes; } public Map getLookupInfo() { return lookupInfo; } public Map getXpathOfResults() { return xpathOfResults; } public Map getXpathToTypeMap() { return xpathToTypeMap; } public Map getXpathToPatternMap() { return xpathToPatternMap; } public boolean isLoopChanged() { return loopChanged; } public void setIsOptional(boolean isLoopOptional) { this.isOptional = isLoopOptional; } }