1 22 package org.jboss.util.state.xml; 23 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 30 import org.jboss.logging.Logger; 31 import org.jboss.util.state.State; 32 import org.jboss.util.state.StateMachine; 33 import org.jboss.util.state.Transition; 34 import org.jboss.util.xml.DOMUtils; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 39 74 public class StateMachineParser 75 { 76 private static Logger log = Logger.getLogger(StateMachineParser.class); 77 78 81 public StateMachine parse(URL source) throws Exception 82 { 83 InputStream in = source.openConnection().getInputStream(); 85 Element root = DOMUtils.parse(in); 86 87 String description = root.getAttribute("description"); 88 HashMap nameToStateMap = new HashMap (); 89 HashMap nameToTransitionsMap = new HashMap (); 90 HashSet states = new HashSet (); 91 State startState = null; 92 93 NodeList stateList = root.getChildNodes(); 95 for (int i = 0; i < stateList.getLength(); i++) 96 { 97 Node stateNode = stateList.item(i); 98 if (stateNode.getNodeName().equals("state")) 99 { 100 Element stateElement = (Element )stateNode; 101 String stateName = stateElement.getAttribute("name"); 102 State s = new State(stateName); 103 states.add(s); 104 nameToStateMap.put(stateName, s); 105 HashMap transitions = new HashMap (); 106 107 NodeList transitionList = stateElement.getChildNodes(); 109 for (int j = 0; j < transitionList.getLength(); j++) 110 { 111 Node transitionNode = transitionList.item(j); 112 if (transitionNode.getNodeName().equals("transition")) 113 { 114 Element transitionElement = (Element )transitionNode; 115 String name = transitionElement.getAttribute("name"); 116 String targetName = transitionElement.getAttribute("target"); 117 transitions.put(name, targetName); 118 } 119 } 120 nameToTransitionsMap.put(stateName, transitions); 121 if (Boolean.valueOf(stateElement.getAttribute("isStartState")) == Boolean.TRUE) 122 startState = s; 123 } 124 } 125 126 Iterator transitions = nameToTransitionsMap.keySet().iterator(); 128 StringBuffer resolveFailed = new StringBuffer (); 129 while (transitions.hasNext()) 130 { 131 String stateName = (String )transitions.next(); 132 State s = (State)nameToStateMap.get(stateName); 133 HashMap stateTransitions = (HashMap )nameToTransitionsMap.get(stateName); 134 Iterator it = stateTransitions.keySet().iterator(); 135 while (it.hasNext()) 136 { 137 String name = (String )it.next(); 138 String targetName = (String )stateTransitions.get(name); 139 State target = (State)nameToStateMap.get(targetName); 140 if (target == null) 141 { 142 String msg = "Failed to resolve target state: " + targetName + " for transition: " + name; 143 resolveFailed.append(msg); 144 log.debug(msg); 145 } 146 Transition t = new Transition(name, target); 147 s.addTransition(t); 148 } 149 } 150 151 if (resolveFailed.length() > 0) 152 throw new Exception ("Failed to resolve transition targets: " + resolveFailed); 153 154 StateMachine sm = new StateMachine(states, startState, description); 155 return sm; 156 } 157 } 158 | Popular Tags |