1 package com.openinventions.webappfilter.processor; 2 3 import com.openinventions.metaframework.*; 4 import org.apache.commons.logging.*; 5 6 public class CommandTraverser implements Processor { 7 private static final Log log = LogFactory.getLog(CommandTraverser.class); 8 private static final int MAX_ACTIONS = 50; 9 10 public void process(State state, Element context) throws Exception { 11 ElementFactory factory = new JXPathElementFileFactory(); 12 String filterDirection = (String ) state.get("filterDirection"); 13 String singleCommand = ""; 14 15 if (state.isExists("singleCommand")) { 16 singleCommand = (String ) state.get("singleCommand"); 17 } 18 int i = 0; 19 while (filterDirection.equals("")) { 20 String path = (String ) state.get("nextCommand"); 21 Element config = (Element) state.get("com.openinventions.metaframework.Config"); 22 23 if (log.isDebugEnabled()) { 24 log.debug("path = " + path); 25 log.debug("config.isExists = " + config.isExists(path)); 26 } 27 28 path = normalizePath(path); 29 30 if (log.isDebugEnabled()) { 31 log.debug("path = " + path); 32 log.debug("config.isExists = " + config.isExists(path)); 33 } 34 35 if (!config.isExists(path)) { 36 if (state.isExists("lastCommand")) { 37 path = (String ) state.get("lastCommand"); 38 if ((path == null) || path.equals("")) { 39 log.error("bad lastCommand. path not found = " + path); 40 } 41 } else { 42 log.error("nothing to traverse. path not found = " + path); 43 } 44 } 45 Element commandContext = (Element) config.getElement(path); 46 path = commandContext.getPath(); 47 state.set("nextCommand", path); 48 49 Processor command = null; 50 String commandName = config.getValue("name(" + path + ")"); 51 52 if (log.isDebugEnabled()) { 53 log.debug("path = " + path); 54 log.debug("commandName = " + commandName); 55 } 56 57 String relative = ""; 58 if (state.isExists("relativeCommand")) { 59 relative = (String ) state.get("relativeCommand"); 60 } 61 62 if (config.isExists("commands/command[@name='" + commandName + "']")) { 64 String refid = config.getValue("commands/command[@name='" + commandName + "']/@refid"); 65 Element commandElement = null; 66 if (!refid.equals("")) { 67 commandElement = config.getElement("commands/command[@id='" + refid + "']"); 68 } else { 69 commandElement = config.getElement("commands/command[@name='" + commandName + "']"); 70 } 71 commandName = config.getValue("name(" + commandElement.getPath() + "/child::*[1])"); 72 if (log.isDebugEnabled()) { 73 log.debug("refid = " + refid); 74 log.debug("commandElement.getPath() = " + commandElement.getPath()); 75 log.debug("commandName = " + commandName); 76 } 77 command = (Processor) state.get(commandName); 78 command.process(state, commandElement.getElement("/child::*[1]")); 79 } else { 80 command = (Processor) state.get(commandName); 81 command.process(state, commandContext); 82 } 83 84 if (!relative.equals("")) { 85 String nextCommand = (String ) state.get("nextCommand"); 86 if (nextCommand.equals(path)) { 88 state.set("nextCommand", path + relative); 89 } 90 } 91 92 filterDirection = (String ) state.get("filterDirection"); 93 i++; 94 if (i > MAX_ACTIONS) { 96 log.error(MAX_ACTIONS + " actions is maximum supported. possible infinite loop encountered"); 97 break; 98 } 99 100 if (singleCommand.equals("true")) { 101 break; 102 } 103 } 104 } 105 106 private String normalizePath(String path) { 107 while (path.indexOf("/following-sibling::*[1]/preceding-sibling::*[1]") >= 0) { 109 path = path.replaceAll("/following-sibling::\\*\\[1\\]/preceding-sibling::\\*\\[1\\]", ""); 110 } 111 112 while (path.indexOf("/preceding-sibling::*[1]/following-sibling::*[1]") >= 0) { 113 path = path.replaceAll("/preceding-sibling::\\*\\[1\\]/following-sibling::\\*\\[1\\]", ""); 114 } 115 return path; 116 } 117 } 118 119 170 | Popular Tags |