Migrated the templates handler to templates reader.

Sat, 11 Sep 2010 18:09:03 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 11 Sep 2010 18:09:03 +0200
changeset 587
3078a9781573
parent 586
9dbffeed66a9
child 588
573767cf6bde

Migrated the templates handler to templates reader.

Documentation/Source/eric5.E5XML.TemplatesHandler.html file | annotate | diff | comparison | revisions
E5XML/TasksReader.py file | annotate | diff | comparison | revisions
E5XML/TemplatesHandler.py file | annotate | diff | comparison | revisions
E5XML/TemplatesReader.py file | annotate | diff | comparison | revisions
Templates/TemplateViewer.py file | annotate | diff | comparison | revisions
--- a/Documentation/Source/eric5.E5XML.TemplatesHandler.html	Sat Sep 11 17:36:08 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,158 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
-'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
-<html><head>
-<title>eric5.E5XML.TemplatesHandler</title>
-<style>
-body {
-    background: #EDECE6;
-    margin: 0em 1em 10em 1em;
-    color: black;
-}
-
-h1 { color: white; background: #85774A; }
-h2 { color: white; background: #85774A; }
-h3 { color: white; background: #9D936E; }
-h4 { color: white; background: #9D936E; }
-    
-a { color: #BA6D36; }
-
-</style>
-</head>
-<body><a NAME="top" ID="top"></a>
-<h1>eric5.E5XML.TemplatesHandler</h1>
-<p>
-Module implementing the handler class for reading an XML templates file.
-</p>
-<h3>Global Attributes</h3>
-<table>
-<tr><td>None</td></tr>
-</table>
-<h3>Classes</h3>
-<table>
-<tr>
-<td><a href="#TemplatesHandler">TemplatesHandler</a></td>
-<td>Class implementing a sax handler to read an XML templates file.</td>
-</tr>
-</table>
-<h3>Functions</h3>
-<table>
-<tr><td>None</td></tr>
-</table>
-<hr /><hr />
-<a NAME="TemplatesHandler" ID="TemplatesHandler"></a>
-<h2>TemplatesHandler</h2>
-<p>
-    Class implementing a sax handler to read an XML templates file.
-</p>
-<h3>Derived from</h3>
-XMLHandlerBase
-<h3>Class Attributes</h3>
-<table>
-<tr><td>None</td></tr>
-</table>
-<h3>Methods</h3>
-<table>
-<tr>
-<td><a href="#TemplatesHandler.__init__">TemplatesHandler</a></td>
-<td>Constructor</td>
-</tr><tr>
-<td><a href="#TemplatesHandler.endTemplate">endTemplate</a></td>
-<td>Handler method for the "Template" end tag.</td>
-</tr><tr>
-<td><a href="#TemplatesHandler.endTemplateDescription">endTemplateDescription</a></td>
-<td>Handler method for the "TemplateDescription" end tag.</td>
-</tr><tr>
-<td><a href="#TemplatesHandler.endTemplateText">endTemplateText</a></td>
-<td>Handler method for the "TemplateText" end tag.</td>
-</tr><tr>
-<td><a href="#TemplatesHandler.getVersion">getVersion</a></td>
-<td>Public method to retrieve the version of the templates.</td>
-</tr><tr>
-<td><a href="#TemplatesHandler.startDocumentTemplates">startDocumentTemplates</a></td>
-<td>Handler called, when the document parsing is started.</td>
-</tr><tr>
-<td><a href="#TemplatesHandler.startTemplate">startTemplate</a></td>
-<td>Handler method for the "Template" start tag.</td>
-</tr><tr>
-<td><a href="#TemplatesHandler.startTemplateGroup">startTemplateGroup</a></td>
-<td>Handler method for the "TemplateGroup" start tag.</td>
-</tr><tr>
-<td><a href="#TemplatesHandler.startTemplates">startTemplates</a></td>
-<td>Handler method for the "Templates" start tag.</td>
-</tr>
-</table>
-<a NAME="TemplatesHandler.__init__" ID="TemplatesHandler.__init__"></a>
-<h4>TemplatesHandler (Constructor)</h4>
-<b>TemplatesHandler</b>(<i>templateViewer=None</i>)
-<p>
-        Constructor
-</p><dl>
-<dt><i>templateViewer</i></dt>
-<dd>
-reference to the template viewer object
-</dd>
-</dl><a NAME="TemplatesHandler.endTemplate" ID="TemplatesHandler.endTemplate"></a>
-<h4>TemplatesHandler.endTemplate</h4>
-<b>endTemplate</b>(<i></i>)
-<p>
-        Handler method for the "Template" end tag.
-</p><a NAME="TemplatesHandler.endTemplateDescription" ID="TemplatesHandler.endTemplateDescription"></a>
-<h4>TemplatesHandler.endTemplateDescription</h4>
-<b>endTemplateDescription</b>(<i></i>)
-<p>
-        Handler method for the "TemplateDescription" end tag.
-</p><a NAME="TemplatesHandler.endTemplateText" ID="TemplatesHandler.endTemplateText"></a>
-<h4>TemplatesHandler.endTemplateText</h4>
-<b>endTemplateText</b>(<i></i>)
-<p>
-        Handler method for the "TemplateText" end tag.
-</p><a NAME="TemplatesHandler.getVersion" ID="TemplatesHandler.getVersion"></a>
-<h4>TemplatesHandler.getVersion</h4>
-<b>getVersion</b>(<i></i>)
-<p>
-        Public method to retrieve the version of the templates.
-</p><dl>
-<dt>Returns:</dt>
-<dd>
-String containing the version number.
-</dd>
-</dl><a NAME="TemplatesHandler.startDocumentTemplates" ID="TemplatesHandler.startDocumentTemplates"></a>
-<h4>TemplatesHandler.startDocumentTemplates</h4>
-<b>startDocumentTemplates</b>(<i></i>)
-<p>
-        Handler called, when the document parsing is started.
-</p><a NAME="TemplatesHandler.startTemplate" ID="TemplatesHandler.startTemplate"></a>
-<h4>TemplatesHandler.startTemplate</h4>
-<b>startTemplate</b>(<i>attrs</i>)
-<p>
-        Handler method for the "Template" start tag.
-</p><dl>
-<dt><i>attrs</i></dt>
-<dd>
-list of tag attributes
-</dd>
-</dl><a NAME="TemplatesHandler.startTemplateGroup" ID="TemplatesHandler.startTemplateGroup"></a>
-<h4>TemplatesHandler.startTemplateGroup</h4>
-<b>startTemplateGroup</b>(<i>attrs</i>)
-<p>
-        Handler method for the "TemplateGroup" start tag.
-</p><dl>
-<dt><i>attrs</i></dt>
-<dd>
-list of tag attributes
-</dd>
-</dl><a NAME="TemplatesHandler.startTemplates" ID="TemplatesHandler.startTemplates"></a>
-<h4>TemplatesHandler.startTemplates</h4>
-<b>startTemplates</b>(<i>attrs</i>)
-<p>
-        Handler method for the "Templates" start tag.
-</p><dl>
-<dt><i>attrs</i></dt>
-<dd>
-list of tag attributes
-</dd>
-</dl>
-<div align="right"><a href="#top">Up</a></div>
-<hr />
-</body></html>
\ No newline at end of file
--- a/E5XML/TasksReader.py	Sat Sep 11 17:36:08 2010 +0200
+++ b/E5XML/TasksReader.py	Sat Sep 11 18:09:03 2010 +0200
@@ -48,8 +48,7 @@
             self.readNext()
             if self.isStartElement():
                 if self.name() == "Tasks":
-                    self.version = self.attribute("version", 
-                        tasksFileFormatVersion)
+                    self.version = self.attribute("version", tasksFileFormatVersion)
                 elif self.name() == "Task":
                     self.__readTask()
         
--- a/E5XML/TemplatesHandler.py	Sat Sep 11 17:36:08 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,108 +0,0 @@
-# -*- coding: utf-8 -*-
-
-# Copyright (c) 2005 - 2010 Detlev Offenbach <detlev@die-offenbachs.de>
-#
-
-"""
-Module implementing the handler class for reading an XML templates file.
-"""
-
-from E5Gui.E5Application import e5App
-
-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 = e5App().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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/E5XML/TemplatesReader.py	Sat Sep 11 18:09:03 2010 +0200
@@ -0,0 +1,87 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a class for reading an XML templates file.
+"""
+
+from E5Gui.E5Application import e5App
+
+from .Config import templatesFileFormatVersion
+from .XMLStreamReaderBase import XMLStreamReaderBase
+
+class TemplatesReader(XMLStreamReaderBase):
+    """
+    Class for reading an XML tasks file.
+    """
+    def __init__(self, device, viewer = None):
+        """
+        Constructor
+        
+        @param device reference to the I/O device to read from (QIODevice)
+        @param viewer reference to the template viewer object (TemplateViewer)
+        """
+        XMLStreamReaderBase.__init__(self, device)
+        
+        if viewer:
+            self.viewer = viewer
+        else:
+            self.viewer = e5App().getObject("TemplateViewer")
+        
+        self.version = ""
+        self.groupName = "DEFAULT"
+    
+    def readXML(self):
+        """
+        Public method to read and parse the XML document.
+        """
+        while not self.atEnd():
+            self.readNext()
+            if self.isStartElement():
+                if self.name() == "Templates":
+                    self.version = self.attribute("version", templatesFileFormatVersion)
+                elif self.name() == "TemplateGroup":
+                    self.__readTemplateGroup()
+        
+        self.showErrorMessage()
+    
+    def __readTemplateGroup(self):
+        """
+        Private method to read a template group.
+        """
+        self.groupName = self.attribute('name', "DEFAULT")
+        language = self.attribute('language', "All")
+        self.viewer.addGroup(self.groupName, language)
+        
+        while not self.atEnd():
+            self.readNext()
+            if self.isEndElement() and self.name() == "TemplateGroup":
+                break
+            
+            if self.isStartElement():
+                if self.name() == "Template":
+                    self.__readTemplate()
+    
+    def __readTemplate(self):
+        """
+        Private method to read the template definition.
+        """
+        templateName = self.attribute('name', '')
+        templateDescription = ""
+        templateText = ""
+        
+        while not self.atEnd():
+            self.readNext()
+            if self.isEndElement() and self.name() == "Template" and templateName:
+                self.viewer.addEntry(self.groupName, templateName, 
+                                     templateDescription, templateText,
+                                     quiet = True)
+                break
+            
+            if self.isStartElement():
+                if self.name() == "TemplateDescription":
+                    templateDescription = self.readElementText()
+                elif self.name() == "TemplateText":
+                    templateText = self.readElementText()
--- a/Templates/TemplateViewer.py	Sat Sep 11 17:36:08 2010 +0200
+++ b/Templates/TemplateViewer.py	Sat Sep 11 18:09:03 2010 +0200
@@ -10,7 +10,6 @@
 import datetime
 import os
 import re
-import io
 
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
@@ -24,10 +23,7 @@
 
 import Preferences
 
-from E5XML.XMLUtilities import make_parser
-from E5XML.XMLErrorHandler import XMLErrorHandler, XMLFatalParseError
-from E5XML.XMLEntityResolver import XMLEntityResolver
-from E5XML.TemplatesHandler import TemplatesHandler
+from E5XML.TemplatesReader import TemplatesReader
 from E5XML.TemplatesWriter import TemplatesWriter
 
 import UI.PixmapCache
@@ -878,60 +874,20 @@
         
         @param filename name of a templates file to read (string)
         """
-        try:
-            if filename is None:
-                filename = os.path.join(Utilities.getConfigDir(), "eric5templates.e4c")
-                if not os.path.exists(filename):
-                    return
-            f = open(filename, "r", encoding = "utf-8")
-            line = f.readline()
-            dtdLine = f.readline()
-            f.close()
-        except IOError:
+        if filename is None:
+            filename = os.path.join(Utilities.getConfigDir(), "eric5templates.e4c")
+            if not os.path.exists(filename):
+                return
+        
+        f = QFile(filename)
+        if f.open(QIODevice.ReadOnly):
+            reader = TemplatesReader(f, viewer = self)
+            reader.readXML()
+        else:
             E5MessageBox.critical(self,
                 self.trUtf8("Read templates"),
                 self.trUtf8("<p>The templates file <b>{0}</b> could not be read.</p>")
                     .format(filename))
-            return
-            
-        # now read the file
-        if line.startswith('<?xml'):
-            parser = make_parser(dtdLine.startswith("<!DOCTYPE"))
-            handler = TemplatesHandler(templateViewer = self)
-            er = XMLEntityResolver()
-            eh = XMLErrorHandler()
-            
-            parser.setContentHandler(handler)
-            parser.setEntityResolver(er)
-            parser.setErrorHandler(eh)
-            
-            try:
-                f = open(filename, "r", encoding = "utf-8")
-                try:
-                    try:
-                        parser.parse(f)
-                    except UnicodeEncodeError:
-                        f.seek(0)
-                        buf = io.StringIO(f.read())
-                        parser.parse(buf)
-                finally:
-                    f.close()
-            except IOError:
-                E5MessageBox.critical(self,
-                    self.trUtf8("Read templates"),
-                    self.trUtf8("<p>The templates file <b>{0}</b> could not be read.</p>")
-                        .format(filename))
-                return
-            except XMLFatalParseError:
-                pass
-                
-            eh.showParseMessages()
-        else:
-            E5MessageBox.critical(self,
-                self.trUtf8("Read templates"),
-                self.trUtf8("<p>The templates file <b>{0}</b> has an"
-                            " unsupported format.</p>")
-                    .format(filename))
     
     def __configure(self):
         """

eric ide

mercurial