KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > state > xml > StateMachineParser


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.util.state.xml;
23
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
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 JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38
39 /** Parse an xml representation of a state machine. A sample document is:
40  
41 <state-machine description="JACC PolicyConfiguration States">
42    <state name="open">
43       <transition name="inService" target="open" />
44       <transition name="getContextID" target="open" />
45       <transition name="getPolicyConfiguration" target="open" />
46       <transition name="addToRole" target="open" />
47       <transition name="removeRole" target="open" />
48       <transition name="addToExcludedPolicy" target="open" />
49       <transition name="removeExcludedPolicy" target="open" />
50       <transition name="addToUncheckedPolicy" target="open" />
51       <transition name="removeUncheckedPolicy" target="open" />
52       <transition name="linkConfiguration" target="open" />
53       <transition name="commit" target="inService" />
54       <transition name="delete" target="deleted" />
55    </state>
56    <state name="inService">
57       <transition name="getPolicyConfiguration" target="open" />
58       <transition name="getContextID" target="inService" />
59       <transition name="inService" target="inService" />
60       <transition name="delete" target="deleted" />
61    </state>
62    <state name="deleted" isStartState="true">
63       <transition name="getPolicyConfiguration" target="open" />
64       <transition name="delete" target="deleted" />
65       <transition name="inService" target="deleted" />
66       <transition name="getContextID" target="deleted" />
67    </state>
68 </state-machine>
69
70  @author Scott.Stark@jboss.org
71  @author Dimitris.Andreadis@jboss.org
72  @version $Revision: 2047 $
73  */

74 public class StateMachineParser
75 {
76    private static Logger log = Logger.getLogger(StateMachineParser.class);
77
78    /**
79     * Parse an xml description of the state machine
80     */

81    public StateMachine parse(URL JavaDoc source) throws Exception JavaDoc
82    {
83       // parse the XML document into a DOM structure
84
InputStream JavaDoc in = source.openConnection().getInputStream();
85       Element JavaDoc root = DOMUtils.parse(in);
86
87       String JavaDoc description = root.getAttribute("description");
88       HashMap JavaDoc nameToStateMap = new HashMap JavaDoc();
89       HashMap JavaDoc nameToTransitionsMap = new HashMap JavaDoc();
90       HashSet JavaDoc states = new HashSet JavaDoc();
91       State startState = null;
92
93       // parse states
94
NodeList JavaDoc stateList = root.getChildNodes();
95       for (int i = 0; i < stateList.getLength(); i++)
96       {
97          Node JavaDoc stateNode = stateList.item(i);
98          if (stateNode.getNodeName().equals("state"))
99          {
100             Element JavaDoc stateElement = (Element JavaDoc)stateNode;
101             String JavaDoc stateName = stateElement.getAttribute("name");
102             State s = new State(stateName);
103             states.add(s);
104             nameToStateMap.put(stateName, s);
105             HashMap JavaDoc transitions = new HashMap JavaDoc();
106             
107             // parse transitions
108
NodeList JavaDoc transitionList = stateElement.getChildNodes();
109             for (int j = 0; j < transitionList.getLength(); j++)
110             {
111                Node JavaDoc transitionNode = transitionList.item(j);
112                if (transitionNode.getNodeName().equals("transition"))
113                {
114                   Element JavaDoc transitionElement = (Element JavaDoc)transitionNode;
115                   String JavaDoc name = transitionElement.getAttribute("name");
116                   String JavaDoc 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       // Resolve all transition targets
127
Iterator JavaDoc transitions = nameToTransitionsMap.keySet().iterator();
128       StringBuffer JavaDoc resolveFailed = new StringBuffer JavaDoc();
129       while (transitions.hasNext())
130       {
131          String JavaDoc stateName = (String JavaDoc)transitions.next();
132          State s = (State)nameToStateMap.get(stateName);
133          HashMap JavaDoc stateTransitions = (HashMap JavaDoc)nameToTransitionsMap.get(stateName);
134          Iterator JavaDoc it = stateTransitions.keySet().iterator();
135          while (it.hasNext())
136          {
137             String JavaDoc name = (String JavaDoc)it.next();
138             String JavaDoc targetName = (String JavaDoc)stateTransitions.get(name);
139             State target = (State)nameToStateMap.get(targetName);
140             if (target == null)
141             {
142                String JavaDoc 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 JavaDoc("Failed to resolve transition targets: " + resolveFailed);
153
154       StateMachine sm = new StateMachine(states, startState, description);
155       return sm;
156    }
157 }
158
Popular Tags