E5XML/TemplatesHandler.py

changeset 50
a36eecf45b2e
parent 45
9a18f4dbb493
child 53
c3eb7cc1ff8b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/E5XML/TemplatesHandler.py	Tue Jan 12 17:55:24 2010 +0000
@@ -0,0 +1,111 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2005 - 2010 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing the handler class for reading an XML templates file.
+"""
+
+import os
+import time
+
+from E4Gui.E4Application import e4App
+
+from .Config import templatesFileFormatVersion
+from .XMLHandlerBase import XMLHandlerBase
+
+class TemplatesHandler(XMLHandlerBase):
+    """
+    Class implementing a sax handler to read an XML templates file.
+    """
+    def __init__(self, templateViewer=None):
+        """
+        Constructor
+        
+        @param templateViewer reference to the template viewer object
+        """
+        XMLHandlerBase.__init__(self)
+        
+        self.startDocumentSpecific = self.startDocumentTemplates
+        
+        self.elements.update({
+            'Templates' : (self.startTemplates, self.defaultEndElement),
+            'TemplateGroup' : (self.startTemplateGroup, self.defaultEndElement),
+            'Template' : (self.startTemplate, self.endTemplate),
+            'TemplateDescription' : (self.defaultStartElement, 
+                                     self.endTemplateDescription), 
+            'TemplateText' : (self.defaultStartElement, self.endTemplateText),
+        })
+    
+        if templateViewer:
+            self.viewer = templateViewer
+        else:
+            self.viewer = e4App().getObject("TemplateViewer")
+        
+    def startDocumentTemplates(self):
+        """
+        Handler called, when the document parsing is started.
+        """
+        self.version = ''
+        
+    ###################################################
+    ## below follow the individual handler functions
+    ###################################################
+    
+    def startTemplateGroup(self, attrs):
+        """
+        Handler method for the "TemplateGroup" start tag.
+        
+        @param attrs list of tag attributes
+        """
+        self.groupName = attrs.get('name', "DEFAULT")
+        language = attrs.get('language', "All")
+        self.viewer.addGroup(self.groupName, language)
+
+    def startTemplate(self, attrs):
+        """
+        Handler method for the "Template" start tag.
+        
+        @param attrs list of tag attributes
+        """
+        self.templateName = attrs.get('name', '')
+        self.templateDescription = ""
+        self.templateText = ""
+
+    def endTemplate(self):
+        """
+        Handler method for the "Template" end tag.
+        """
+        if self.templateName and self.templateText:
+            self.viewer.addEntry(self.groupName, self.templateName, 
+                                 self.templateDescription, self.templateText,
+                                 quiet=True)
+
+    def endTemplateText(self):
+        """
+        Handler method for the "TemplateText" end tag.
+        """
+        self.templateText = self.unescape(self.buffer)
+
+    def endTemplateDescription(self):
+        """
+        Handler method for the "TemplateDescription" end tag.
+        """
+        self.templateDescription = self.unescape(self.buffer)
+    
+    def startTemplates(self, attrs):
+        """
+        Handler method for the "Templates" start tag.
+        
+        @param attrs list of tag attributes
+        """
+        self.version = attrs.get('version', templatesFileFormatVersion)
+        
+    def getVersion(self):
+        """
+        Public method to retrieve the version of the templates.
+        
+        @return String containing the version number.
+        """
+        return self.version

eric ide

mercurial