|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2005 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the handler class for reading an XML templates file. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import time |
|
12 |
|
13 from E4Gui.E4Application import e4App |
|
14 |
|
15 from Config import templatesFileFormatVersion |
|
16 from XMLHandlerBase import XMLHandlerBase |
|
17 |
|
18 class TemplatesHandler(XMLHandlerBase): |
|
19 """ |
|
20 Class implementing a sax handler to read an XML templates file. |
|
21 """ |
|
22 def __init__(self, templateViewer=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param templateViewer reference to the template viewer object |
|
27 """ |
|
28 XMLHandlerBase.__init__(self) |
|
29 |
|
30 self.startDocumentSpecific = self.startDocumentTemplates |
|
31 |
|
32 self.elements.update({ |
|
33 'Templates' : (self.startTemplates, self.defaultEndElement), |
|
34 'TemplateGroup' : (self.startTemplateGroup, self.defaultEndElement), |
|
35 'Template' : (self.startTemplate, self.endTemplate), |
|
36 'TemplateDescription' : (self.defaultStartElement, |
|
37 self.endTemplateDescription), |
|
38 'TemplateText' : (self.defaultStartElement, self.endTemplateText), |
|
39 }) |
|
40 |
|
41 if templateViewer: |
|
42 self.viewer = templateViewer |
|
43 else: |
|
44 self.viewer = e4App().getObject("TemplateViewer") |
|
45 |
|
46 def startDocumentTemplates(self): |
|
47 """ |
|
48 Handler called, when the document parsing is started. |
|
49 """ |
|
50 self.version = '' |
|
51 |
|
52 ################################################### |
|
53 ## below follow the individual handler functions |
|
54 ################################################### |
|
55 |
|
56 def startTemplateGroup(self, attrs): |
|
57 """ |
|
58 Handler method for the "TemplateGroup" start tag. |
|
59 |
|
60 @param attrs list of tag attributes |
|
61 """ |
|
62 self.groupName = attrs.get('name', "DEFAULT") |
|
63 language = attrs.get('language', "All") |
|
64 self.viewer.addGroup(self.groupName, language) |
|
65 |
|
66 def startTemplate(self, attrs): |
|
67 """ |
|
68 Handler method for the "Template" start tag. |
|
69 |
|
70 @param attrs list of tag attributes |
|
71 """ |
|
72 self.templateName = attrs.get('name', '') |
|
73 self.templateDescription = "" |
|
74 self.templateText = "" |
|
75 |
|
76 def endTemplate(self): |
|
77 """ |
|
78 Handler method for the "Template" end tag. |
|
79 """ |
|
80 if self.templateName and self.templateText: |
|
81 self.viewer.addEntry(self.groupName, self.templateName, |
|
82 self.templateDescription, self.templateText, |
|
83 quiet=True) |
|
84 |
|
85 def endTemplateText(self): |
|
86 """ |
|
87 Handler method for the "TemplateText" end tag. |
|
88 """ |
|
89 self.templateText = self.unescape(self.utf8_to_code(self.buffer)) |
|
90 |
|
91 def endTemplateDescription(self): |
|
92 """ |
|
93 Handler method for the "TemplateDescription" end tag. |
|
94 """ |
|
95 self.templateDescription = self.unescape(self.utf8_to_code(self.buffer)) |
|
96 |
|
97 def startTemplates(self, attrs): |
|
98 """ |
|
99 Handler method for the "Templates" start tag. |
|
100 |
|
101 @param attrs list of tag attributes |
|
102 """ |
|
103 self.version = attrs.get('version', templatesFileFormatVersion) |
|
104 |
|
105 def getVersion(self): |
|
106 """ |
|
107 Public method to retrieve the version of the templates. |
|
108 |
|
109 @return String containing the version number. |
|
110 """ |
|
111 return self.version |