[jboss-svn-commits] JBoss Common SVN: r2047 - in common-core/trunk: . src/main/java/org/jboss/util/state src/main/java/org/jboss/util/state/xml

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Sep 20 17:34:59 EDT 2006


Author: dimitris at jboss.org
Date: 2006-09-20 17:34:53 -0400 (Wed, 20 Sep 2006)
New Revision: 2047

Modified:
   common-core/trunk/pom.xml
   common-core/trunk/src/main/java/org/jboss/util/state/State.java
   common-core/trunk/src/main/java/org/jboss/util/state/StateMachine.java
   common-core/trunk/src/main/java/org/jboss/util/state/xml/StateMachineParser.java
Log:
JBCOMMON-9, remove dom4j dependency

Modified: common-core/trunk/pom.xml
===================================================================
--- common-core/trunk/pom.xml	2006-09-20 21:11:05 UTC (rev 2046)
+++ common-core/trunk/pom.xml	2006-09-20 21:34:53 UTC (rev 2047)
@@ -92,11 +92,6 @@
   </build>
   <dependencies>
     <dependency>
-      <groupId>dom4j</groupId>
-      <artifactId>dom4j</artifactId>
-      <version>1.5</version>
-    </dependency>
-    <dependency>
       <groupId>apache-xerces</groupId>
       <artifactId>xml-apis</artifactId>
       <version>2.7.1</version>

Modified: common-core/trunk/src/main/java/org/jboss/util/state/State.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/state/State.java	2006-09-20 21:11:05 UTC (rev 2046)
+++ common-core/trunk/src/main/java/org/jboss/util/state/State.java	2006-09-20 21:34:53 UTC (rev 2047)
@@ -1,24 +1,24 @@
 /*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
 package org.jboss.util.state;
 
 import java.util.HashMap;
@@ -111,17 +111,15 @@
    {
       StringBuffer tmp = new StringBuffer("State(name=");
       tmp.append(name);
-      tmp.append("\n");
       Iterator i = allowedTransitions.entrySet().iterator();
       while( i.hasNext() )
       {
          Map.Entry e = (Map.Entry) i.next();
-         tmp.append("\t on: ");
+         tmp.append("\n\t on: ");
          tmp.append(e.getKey());
          Transition t = (Transition) e.getValue();
          tmp.append(" go to: ");
          tmp.append(t.getTarget().getName());
-         tmp.append('\n');
       }
       tmp.append(')');
       return tmp.toString();

Modified: common-core/trunk/src/main/java/org/jboss/util/state/StateMachine.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/state/StateMachine.java	2006-09-20 21:11:05 UTC (rev 2046)
+++ common-core/trunk/src/main/java/org/jboss/util/state/StateMachine.java	2006-09-20 21:34:53 UTC (rev 2047)
@@ -1,24 +1,24 @@
 /*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
 package org.jboss.util.state;
 
 import java.util.HashSet;
@@ -145,11 +145,10 @@
    {
       StringBuffer tmp = new StringBuffer("StateMachine[:\n");
       tmp.append("\tCurrentState: "+currentState.getName());
-      tmp.append('\n');
       Iterator i = states.iterator();
       while( i.hasNext() )
       {
-         tmp.append(i.next());
+         tmp.append('\n').append(i.next());
       }
       tmp.append(']');
       return tmp.toString();

Modified: common-core/trunk/src/main/java/org/jboss/util/state/xml/StateMachineParser.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/state/xml/StateMachineParser.java	2006-09-20 21:11:05 UTC (rev 2046)
+++ common-core/trunk/src/main/java/org/jboss/util/state/xml/StateMachineParser.java	2006-09-20 21:34:53 UTC (rev 2047)
@@ -21,20 +21,20 @@
  */
 package org.jboss.util.state.xml;
 
+import java.io.InputStream;
 import java.net.URL;
-import java.util.Iterator;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 
-import org.dom4j.io.SAXReader;
-import org.dom4j.Document;
-import org.dom4j.DocumentException;
-import org.dom4j.Element;
-
 import org.jboss.logging.Logger;
+import org.jboss.util.state.State;
 import org.jboss.util.state.StateMachine;
-import org.jboss.util.state.State;
 import org.jboss.util.state.Transition;
+import org.jboss.util.xml.DOMUtils;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 /** Parse an xml representation of a state machine. A sample document is:
  
@@ -68,6 +68,7 @@
 </state-machine>
 
  @author Scott.Stark at jboss.org
+ @author Dimitris.Andreadis at jboss.org
  @version $Revision$
  */
 public class StateMachineParser
@@ -75,60 +76,70 @@
    private static Logger log = Logger.getLogger(StateMachineParser.class);
 
    /**
-    * 
-    * @param source
-    * @return
-    * @throws DocumentException
-    */ 
-   public StateMachine parse(URL source) throws DocumentException
+    * Parse an xml description of the state machine
+    */
+   public StateMachine parse(URL source) throws Exception
    {
-      SAXReader reader = new SAXReader();
-      Document document = reader.read(source);
-      Element root = document.getRootElement();
-      String description = root.attributeValue("description");
-      Iterator i = root.elementIterator();
+      // parse the XML document into a DOM structure
+      InputStream in = source.openConnection().getInputStream();
+      Element root = DOMUtils.parse(in);
+
+      String description = root.getAttribute("description");
       HashMap nameToStateMap = new HashMap();
       HashMap nameToTransitionsMap = new HashMap();
       HashSet states = new HashSet();
       State startState = null;
-      while( i.hasNext() )
+
+      // parse states
+      NodeList stateList = root.getChildNodes();
+      for (int i = 0; i < stateList.getLength(); i++)
       {
-         Element stateElement = (Element) i.next();
-         String stateName = stateElement.attributeValue("name");
-         State s = new State(stateName);
-         states.add(s);
-         nameToStateMap.put(stateName, s);
-         HashMap transitions = new HashMap();
-         Iterator it = stateElement.elementIterator();
-         while( it.hasNext() )
+         Node stateNode = stateList.item(i);
+         if (stateNode.getNodeName().equals("state"))
          {
-            Element transElement = (Element) it.next();
-            String name = transElement.attributeValue("name");
-            String targetName = transElement.attributeValue("target");
-            transitions.put(name, targetName);
+            Element stateElement = (Element)stateNode;
+            String stateName = stateElement.getAttribute("name");
+            State s = new State(stateName);
+            states.add(s);
+            nameToStateMap.put(stateName, s);
+            HashMap transitions = new HashMap();
+            
+            // parse transitions
+            NodeList transitionList = stateElement.getChildNodes();
+            for (int j = 0; j < transitionList.getLength(); j++)
+            {
+               Node transitionNode = transitionList.item(j);
+               if (transitionNode.getNodeName().equals("transition"))
+               {
+                  Element transitionElement = (Element)transitionNode;
+                  String name = transitionElement.getAttribute("name");
+                  String targetName = transitionElement.getAttribute("target");
+                  transitions.put(name, targetName);
+               }
+            }
+            nameToTransitionsMap.put(stateName, transitions);
+            if (Boolean.valueOf(stateElement.getAttribute("isStartState")) == Boolean.TRUE)
+               startState = s;
          }
-         nameToTransitionsMap.put(stateName, transitions);
-         if( Boolean.valueOf(stateElement.attributeValue("isStartState")) == Boolean.TRUE )
-            startState = s;
       }
-
+      
       // Resolve all transition targets
       Iterator transitions = nameToTransitionsMap.keySet().iterator();
       StringBuffer resolveFailed = new StringBuffer();
-      while( transitions.hasNext() )
+      while (transitions.hasNext())
       {
-         String stateName = (String) transitions.next();
-         State s = (State) nameToStateMap.get(stateName);
-         HashMap stateTransitions = (HashMap) nameToTransitionsMap.get(stateName);
+         String stateName = (String)transitions.next();
+         State s = (State)nameToStateMap.get(stateName);
+         HashMap stateTransitions = (HashMap)nameToTransitionsMap.get(stateName);
          Iterator it = stateTransitions.keySet().iterator();
-         while( it.hasNext() )
+         while (it.hasNext())
          {
-            String name = (String) it.next();
-            String targetName = (String) stateTransitions.get(name);
-            State target = (State) nameToStateMap.get(targetName);
-            if( target == null )
+            String name = (String)it.next();
+            String targetName = (String)stateTransitions.get(name);
+            State target = (State)nameToStateMap.get(targetName);
+            if (target == null)
             {
-               String msg = "Failed to resolve target state: "+targetName+" for transition: "+name;
+               String msg = "Failed to resolve target state: " + targetName + " for transition: " + name;
                resolveFailed.append(msg);
                log.debug(msg);
             }
@@ -137,8 +148,8 @@
          }
       }
 
-      if( resolveFailed.length() > 0 )
-         throw new DocumentException("Failed to resolve transition targets: "+resolveFailed);
+      if (resolveFailed.length() > 0)
+         throw new Exception("Failed to resolve transition targets: " + resolveFailed);
 
       StateMachine sm = new StateMachine(states, startState, description);
       return sm;




More information about the jboss-svn-commits mailing list