[Zope3-checkins] CVS: Packages3/workflow/stateful - xmlimportexport.py:1.3

Michael Howitz mh+zope@gocept.com
Thu, 27 Mar 2003 08:31:55 -0500


Update of /cvs-repository/Packages3/workflow/stateful
In directory cvs.zope.org:/tmp/cvs-serv12006/stateful

Modified Files:
	xmlimportexport.py 
Log Message:
now the import of an fix example stateful workflow works


=== Packages3/workflow/stateful/xmlimportexport.py 1.2 => 1.3 ===
--- Packages3/workflow/stateful/xmlimportexport.py:1.2	Thu Mar 27 05:55:58 2003
+++ Packages3/workflow/stateful/xmlimportexport.py	Thu Mar 27 08:31:54 2003
@@ -17,6 +17,7 @@
 """
 __metaclass__ = type
 
+
 from zope.app.pagetemplate.viewpagetemplatefile \
      import ViewPageTemplateFile
 from zope.app.interfaces.workflow import IProcessDefinitionImportHandler
@@ -26,6 +27,71 @@
 from types import StringTypes
 from zope.proxy.context import ContextMethod 
 
+from xml.sax import parse
+from xml.sax.handler import ContentHandler
+from StringIO import StringIO
+from zope.app.workflow.stateful.definition import State, Transition
+
+class XMLStatefulImporter(ContentHandler):
+    def __init__(self, context):
+        self.context = context
+    
+    def startElement(self, name, attrs):
+        handler = getattr(self, 'start' + name.title().replace('-', ''), None)
+        if not handler:
+            raise ValueError, 'Unknown element %s' % name
+
+        handler(attrs)
+
+    def endElement(self, name):
+        handler = getattr(self, 'end' + name.title().replace('-', ''), None)
+        if handler:
+            handler()
+
+    def noop(*args):
+        pass
+
+    startStates      = noop
+    startTransitions = noop
+
+    def startWorkflow(self, attrs):
+        title = attrs['title'].encode('latin-1')
+        import pdb
+        pdb.set_trace()
+        getAdapter(self.context, IZopeDublinCore).setQualifiedTitles([('',title)])
+
+    def startSchema(self, attrs):
+        name = attrs['name'].encode('latin-1')
+        self.context.setRelevantDataSchema(name)
+
+    def startState(self, attrs):
+        title = attrs.get('title', '').encode('latin-1')
+        name  = attrs['name'].encode('latin-1')
+        state = State()
+        getAdapter(state, IZopeDublinCore).setQualifiedTitles([('',title)])
+        if name != 'INITIAL':
+            self.context.addState(name, state)
+
+    def startTransition(self, attrs):
+        title  = attrs.get('title', '').encode('latin-1')
+        source = attrs['sourceState'].encode('latin-1')
+        dest   = attrs['destinationState'].encode('latin-1')
+        condi  = attrs.get('condition', '').encode('latin-1')
+        permi  = attrs.get('permission', '').encode('latin-1')
+        triggM = attrs['triggerMode'].encode('latin-1')
+        name   = attrs['name'].encode('latin-1')
+        trans = Transition(source      = source,
+                           destination = dest,
+                           condition   = condi,
+                           permission  = permi,
+                           triggerMode = triggM)
+        print getAdapter(trans, IZopeDublinCore).setQualifiedTitles([('',title)])
+
+        self.context.addTransition(name, trans)
+            
+
+
+        
 class XMLImportHandler:
 
     __implements__ = IProcessDefinitionImportHandler
@@ -33,8 +99,10 @@
     def canImport(self, data):
         return True
 
-    def doImport(self, data):
-        return 'The Process Definition'
+    def doImport(self, context, data):
+        if not hasattr(data, "read"):
+            data = StringIO(data)
+        parse(data, XMLStatefulImporter(context))
     
 
 class XMLExportHandler: