You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gu...@apache.org on 2010/06/29 15:01:55 UTC

svn commit: r958957 - in /commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram: src/org/apache/commons/scxml/modeling/diagram/part/ModelingImportWizard.java xslt/import.xsl

Author: guixl
Date: Tue Jun 29 13:01:55 2010
New Revision: 958957

URL: http://svn.apache.org/viewvc?rev=958957&view=rev
Log:
Finish transition element import convert function

Modified:
    commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram/src/org/apache/commons/scxml/modeling/diagram/part/ModelingImportWizard.java
    commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram/xslt/import.xsl

Modified: commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram/src/org/apache/commons/scxml/modeling/diagram/part/ModelingImportWizard.java
URL: http://svn.apache.org/viewvc/commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram/src/org/apache/commons/scxml/modeling/diagram/part/ModelingImportWizard.java?rev=958957&r1=958956&r2=958957&view=diff
==============================================================================
--- commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram/src/org/apache/commons/scxml/modeling/diagram/part/ModelingImportWizard.java (original)
+++ commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram/src/org/apache/commons/scxml/modeling/diagram/part/ModelingImportWizard.java Tue Jun 29 13:01:55 2010
@@ -17,14 +17,28 @@
 package org.apache.commons.scxml.modeling.diagram.part;
 
 import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.StringReader;
+import java.io.StringWriter;
 import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
 import org.apache.commons.scxml.modeling.export.ScxmlExportStreamHandlerAction;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.runtime.CoreException;
@@ -49,9 +63,12 @@ import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.INewWizard;
 import org.eclipse.ui.IWorkbench;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.InputSource;
 
 /**
- * @generated
+ * @SCXML document import to workspace
  */
 public class ModelingImportWizard extends Wizard implements INewWizard {
 
@@ -184,6 +201,7 @@ public class ModelingImportWizard extend
 
 		private boolean indeterminate;
 		private IFile xmlFile;
+		private HashMap<String,XpathMapClass> targetList = new HashMap<String,XpathMapClass>();
 
 		/**
 		 * LongRunningOperation constructor
@@ -212,7 +230,7 @@ public class ModelingImportWizard extend
 		 */
 		public void run(IProgressMonitor monitor)
 				throws InvocationTargetException, InterruptedException {
-			monitor.beginTask("SCXML document export progress", 13);
+			monitor.beginTask("SCXML document export progress", 14);
 
 			try {
 				ScxmlExportStreamHandlerAction shc = new ScxmlExportStreamHandlerAction();
@@ -220,14 +238,16 @@ public class ModelingImportWizard extend
 				monitor.worked(1);
 				documentContent=documentContent.replace("xmlns=\"http://www.w3.org/2005/07/scxml\"", "");
 				monitor.worked(1);
-				monitor.worked(1);
-				monitor.worked(1);
-				monitor.worked(1);
+				documentContent=preHandlerContent(documentContent);
 				System.out.println(documentContent);
+				monitor.worked(1);
 				String content = shc.ExecuteXSL(documentContent, this
 						.getClass().getClassLoader().getResourceAsStream(
 								"xslt/import.xsl"));
+				System.out.println(content);
 				monitor.worked(1);
+				content=getTargetList(content);
+				monitor.worked(3);
 				content=content.replace("CONSTANT_org.apache.commons.scxml_CONSTANT", "org.apache.commons.scxml:");
 				monitor.worked(1);
 				content=content.replace("CONSTANT_xmi_version_CONSTANT", "xml:version");
@@ -248,6 +268,211 @@ public class ModelingImportWizard extend
 				e.printStackTrace();
 			}
 		}
+		//pre-handler document content, get subState elements
+		public String preHandlerContent(String documentContent){
+			try {
+			      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+			      DocumentBuilder builder = factory.newDocumentBuilder();
+			      InputSource is = new InputSource( new StringReader( documentContent ) );
+			      Document doc = builder.parse( is );
+			      Element root=doc.getDocumentElement();
+			      getSubStateElement(root);
+			      TransformerFactory tFactory = TransformerFactory.newInstance();
+				  Transformer transformer = null;
+					try {
+						transformer = tFactory.newTransformer();
+						transformer.setOutputProperty("encoding","UTF-8");
+					} catch (TransformerConfigurationException e1) {
+						e1.printStackTrace();
+					}
+					DOMSource source = new DOMSource(doc);
+					ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
+					StreamResult result = new StreamResult(byteRep);
+					try {
+						transformer.transform(source, result);
+					} catch (TransformerException e) {
+						e.printStackTrace();
+					}
+					String retContent=byteRep.toString();
+					return retContent;
+			}
+		    catch( Exception ex ) {
+		    	ex.printStackTrace();
+		      	return documentContent;
+		    }
+		}
+		
+		public String getTargetList(String documentContent) {
+			try {
+			      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+			      DocumentBuilder builder = factory.newDocumentBuilder();
+			      InputSource is = new InputSource( new StringReader( documentContent ) );
+			      Document doc = builder.parse( is );
+			      Element root=doc.getDocumentElement();
+			      root.setAttribute("tempId", "/");
+			      convertParseTargetElement(root);
+			      replaceTargetElement(root);
+			      root.removeAttribute("tempId");
+			      removeTempIdElement(root);
+			      TransformerFactory tFactory = TransformerFactory.newInstance();
+				  Transformer transformer = null;
+					try {
+						transformer = tFactory.newTransformer();
+						transformer.setOutputProperty("encoding","UTF-8");
+					} catch (TransformerConfigurationException e1) {
+						e1.printStackTrace();
+					}
+					DOMSource source = new DOMSource(doc);
+					ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
+					StreamResult result = new StreamResult(byteRep);
+					try {
+						transformer.transform(source, result);
+					} catch (TransformerException e) {
+						e.printStackTrace();
+					}
+					String retContent=byteRep.toString();
+					Iterator ite=targetList.keySet().iterator();
+					while(ite.hasNext()){
+						XpathMapClass map = targetList.get(ite.next());
+						String target = map.getTarget();
+						if(target!=null&&map.getXpath()!=null){
+							retContent = retContent.replace("targetStatus=\"" + target
+									+ "\"", "targetStatus=\"" + map.getXpath() + "\"");
+						}
+					}
+					return retContent;
+			}
+		    catch( Exception ex ) {
+		    	ex.printStackTrace();
+		      	return documentContent;
+		    }
+
+		}
+		
+		//change state to subState
+		private void getSubStateElement(Element element){
+			org.w3c.dom.NodeList children = element.getChildNodes();
+			String elementName = element.getNodeName();
+			Element childElement = null;
+	        for(int j = 0; j < children.getLength(); j++)
+	        {
+	        	if(children.item(j) instanceof org.w3c.dom.Element){
+	        		childElement = ((Element)children.item(j));
+        			if("state".equalsIgnoreCase(elementName)){
+        				childElement.setAttribute("state_type", "subState");
+        			}
+        			getSubStateElement(childElement);
+	        	}
+	        }
+		}
+		
+		//add tempId for each element and get all transition target value list
+		private void convertParseTargetElement(Element element){
+			org.w3c.dom.NodeList children = element.getChildNodes();
+			String elementName = null;
+			Element childElement = null;
+			int stateListNumber=0;
+			int subStateNumber=0;
+	        for(int j = 0; j < children.getLength(); j++)
+	        {
+	        	if(children.item(j) instanceof org.w3c.dom.Element){
+	        		childElement = ((Element)children.item(j));
+	        		elementName = childElement.getNodeName();
+	        		if("targetConnection".equalsIgnoreCase(elementName)){
+	        			String target=childElement.getAttribute("targetStatus");
+	        			if(target!=null){
+	        				XpathMapClass map = new XpathMapClass();
+		    				map.setTarget(target);
+		    				targetList.put(target,map);
+	        			}
+	        		}else{
+	        			if("stateList".equalsIgnoreCase(elementName)||"subState".equalsIgnoreCase(elementName)){
+	        				String fatherTempId=element.getAttribute("tempId");
+	        				if(fatherTempId!=null){
+	        					if("stateList".equalsIgnoreCase(elementName)) childElement.setAttribute("tempId", fatherTempId+"/@stateList."+String.valueOf(stateListNumber++));
+	        					else if("subState".equalsIgnoreCase(elementName)) childElement.setAttribute("tempId", fatherTempId+"/@subState."+String.valueOf(subStateNumber++));
+	        				}
+	        			}
+	        			convertParseTargetElement(childElement);
+	        		}
+	        	}
+	        }
+		}
+		
+		//get xpath value for each stateList element, subState element
+		private void replaceTargetElement(Element element){
+			org.w3c.dom.NodeList children = element.getChildNodes();
+			String elementName = null;
+			Element childElement = null;
+	        for(int j = 0; j < children.getLength(); j++)
+	        {
+	        	if(children.item(j) instanceof org.w3c.dom.Element){
+	        		childElement = ((Element)children.item(j));
+	        		elementName = childElement.getNodeName();
+        			if("stateList".equalsIgnoreCase(elementName)||"subState".equalsIgnoreCase(elementName)){
+        				String tempId=childElement.getAttribute("tempId");
+        				String id=childElement.getAttribute("id");
+        				if(tempId!=null&&id!=null){
+        					XpathMapClass targetMap=targetList.get(id);
+        					if(targetMap!=null){
+        						targetMap.setXpath(tempId);
+        						targetList.put(id, targetMap);
+        					}
+        				}
+        			}
+        			replaceTargetElement(childElement);
+	        	}
+	        }
+		}
+		
+		//remove tempId attribute from dom tree
+		private void removeTempIdElement(Element element){
+			org.w3c.dom.NodeList children = element.getChildNodes();
+			Element childElement = null;
+	        for(int j = 0; j < children.getLength(); j++)
+	        {
+	        	if(children.item(j) instanceof org.w3c.dom.Element){
+	        		childElement = ((Element)children.item(j));
+	        		if(childElement.getAttribute("tempId")!=null){
+	        			childElement.removeAttribute("tempId");
+	        		}
+	        		if(childElement.getAttribute("state_type")!=null){
+	        			childElement.removeAttribute("state_type");
+	        		}
+	        		removeTempIdElement(childElement);
+	        	}
+	        }
+		}
+	}
+	
+	class XpathMapClass {
+		private String target;
+		private String xpath;
+		private String id;
+
+		public String getTarget() {
+			return target;
+		}
+
+		public void setTarget(String target) {
+			this.target = target;
+		}
+
+		public String getXpath() {
+			return xpath;
+		}
+
+		public void setXpath(String xpath) {
+			this.xpath = xpath;
+		}
+
+		public String getId() {
+			return id;
+		}
+
+		public void setId(String id) {
+			this.id = id;
+		}
 	}
 
 	/**

Modified: commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram/xslt/import.xsl
URL: http://svn.apache.org/viewvc/commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram/xslt/import.xsl?rev=958957&r1=958956&r2=958957&view=diff
==============================================================================
--- commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram/xslt/import.xsl (original)
+++ commons/sandbox/gsoc/2010/scxml-eclipse/trunk/src/main/java/org.apache.commons.scxml.modeling.diagram/xslt/import.xsl Tue Jun 29 13:01:55 2010
@@ -17,26 +17,34 @@
 </xsl:template>
 
 <xsl:template match="state">
-	<xsl:element name="stateList">
-		<xsl:choose>
-		<xsl:when test="@id">
-			<xsl:attribute name="id"><xsl:value-of  select="@id"/></xsl:attribute>
-		</xsl:when>
-		<xsl:otherwise>
-			<xsl:attribute name="id"><xsl:value-of  select="generate-id()"/></xsl:attribute>
-		</xsl:otherwise>
-		</xsl:choose>
-		<xsl:for-each select="*">
+	<xsl:choose>
+	<xsl:when test="@state_type">
+		<xsl:element name="subState">
 			<xsl:choose>
-			<xsl:when test="name()='state'">
-				<xsl:call-template name="subState" />
+			<xsl:when test="@id">
+				<xsl:attribute name="id"><xsl:value-of  select="@id"/></xsl:attribute>
 			</xsl:when>
 			<xsl:otherwise>
-				<xsl:apply-templates/>
+				<xsl:attribute name="id"><xsl:value-of  select="generate-id()"/></xsl:attribute>
 			</xsl:otherwise>
 			</xsl:choose>
-		</xsl:for-each>
-	</xsl:element>
+			<xsl:apply-templates/>
+		</xsl:element>
+	</xsl:when>
+	<xsl:otherwise>
+		<xsl:element name="stateList">
+			<xsl:choose>
+			<xsl:when test="@id">
+				<xsl:attribute name="id"><xsl:value-of  select="@id"/></xsl:attribute>
+			</xsl:when>
+			<xsl:otherwise>
+				<xsl:attribute name="id"><xsl:value-of  select="generate-id()"/></xsl:attribute>
+			</xsl:otherwise>
+			</xsl:choose>
+			<xsl:apply-templates/>
+		</xsl:element>
+	</xsl:otherwise>
+	</xsl:choose>
 </xsl:template>
 
 <xsl:template match="parallel">
@@ -48,20 +56,6 @@
 	</xsl:element>
 </xsl:template>
 
-<xsl:template name="subState">
-	<xsl:element name="subState">
-		<xsl:choose>
-		<xsl:when test="@id">
-			<xsl:attribute name="id"><xsl:value-of  select="@id"/></xsl:attribute>
-		</xsl:when>
-		<xsl:otherwise>
-			<xsl:attribute name="id"><xsl:value-of  select="generate-id()"/></xsl:attribute>
-		</xsl:otherwise>
-		</xsl:choose>
-		<xsl:apply-templates/>
-	</xsl:element>
-</xsl:template>
-
 <xsl:template match="onEntry">
 	<xsl:element name="onEntry">
 		<xsl:apply-templates/>
@@ -82,7 +76,7 @@
 		<xsl:if test="@cond">
 			<xsl:attribute name="cond"><xsl:value-of  select="@cond"/></xsl:attribute>
 		</xsl:if>
-		<xsl:attribute name="target"><xsl:value-of  select="@target"/></xsl:attribute>
+		<xsl:attribute name="targetStatus"><xsl:value-of  select="@target"/></xsl:attribute>
 	</xsl:element>
 </xsl:template>