File: _builder.py

package info (click to toggle)
zope-zms 1%3A2.1.2.7-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,444 kB
  • ctags: 1,460
  • sloc: python: 13,956; xml: 10,281; makefile: 81; sh: 53
file content (420 lines) | stat: -rw-r--r-- 18,883 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
###################################################################################################
# _builder.py
#
# $Id: _builder.py,v 1.3 2003/11/21 07:29:45 dnordmann Exp $
# $Name:  $
# $Author: dnordmann $
# $Revision: 1.3 $
#
# Implementation of class Builder (see below).
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
###################################################################################################

# Imports
from Shared.DC.xml import pyexpat
import time
import Globals
# Product Imports.
import _globals

###################################################################################################
# class ParseError(Exception):
#
# General exception class to indicate parsing errors.
###################################################################################################
class ParseError(Exception): pass


###################################################################################################
# class Builder
#
# Implements a builder class (cp. design pattern "BUILDER") to build a tree of ZOPE objects
# out of an XML formatted document. Uses the class "pyexpat" (cp. module "Shared.DC.xml") for
# parsing the XML document. The general approach of the XML parser "pyexpat" is event driven, 
# where handler methods are called on occurence of XML tags. Builder redirects these events to
# a set of own handler methods (see below). To build up the object tree, Builder provides the
# following functionality:
#
# 1. Usually, the occurence of a XML tag induces the instanciation of a new node object. Therefore,
#    Builder contains a mapping table ("dGlobalAttrs"), that maps XML tags to python classes. The
#    handler method "Builder.OnStartElement()" creates a node object of the corresponding class.
#    This node object is then made current.
#
# 2. In General, events are directed to the current node object. Therefore, they have to contain 
#    a set of interface methods (see below). The node objects are responsible for handling these 
#    events. This includes the insertion into the object tree as well as the interpretation of 
#    XML tag parameters.
#
# 3. A dedicated root object is managed by Builder. The root object may be predefined or created
#    during the parsing process.
#
# Builder is usually used as a mix-in base class for other classes. For usage, the following
# issues must be taken into consideration:
#
# 1. Overwrite "dGlobalAttrs" with a dictionary, that maps XML-Tags to python classes.
# 2. Call "Builder.parse()" to initiate the parsing and building process.
# 3. Equip all python classes with the following interface methods:
#
#    - xmlOnStartElement(self, dTagName, dTagAttrs, oParentNode, oRoot)
#    - xmlOnCharacterData(self, sData, bInCData)
#    - xmlOnEndElement(self)
#    - xmlOnUnknownStartTag(self, sTagName, dTagAttrs)
#    - xmlOnUnknownEndTag(self, sTagName)
#    - xmlGetTagName(self)
#    - xmlGetParent(self)
#
###################################################################################################
class Builder:
    """ Builder """
  
    ######## class variables ########
    iBufferSize=1028 * 32   # buffer size for XML file parsing
  
      
    ###############################################################################################
    # Builder.__init__(self):
    #
    # Constructor.
    ###############################################################################################
    def __init__(self):
        """ Builder.__init__ """
        self.oRoot      = None   # root node of object tree
        self.oCurrNode  = None   # current node
        self.bInRootTag = 0  # inside root tag?
        self.bInCData   = 0  # inside CDATA section?


    ###############################################################################################
    # Builder.parse(self, root, input):
    #
    # Parse a given XML document and build a recursive object tree via event handler.
    #
    # IN:  input = XML document as string
    #            = XML document as file object   
    #      root  = pre-set root node for object tree (prevents the creation of a root object, when
    #              the first root tag appears in XML-document)
    #            = None, if no root object is given (will be instanciated)
    #
    # OUT: root object 
    #      None, if nothing was parsed
    ###############################################################################################
    def parse(self, input, root=None, bInRootTag=0):
        """ Builder.parse """
        
        # prepare builder
        self._unknownTagName  = None
        self.oRoot            = root
        self.oRootNode        = None
        self.oCurrNode        = None
        self.bInRootTag       = bInRootTag
        self.bInCData         = 0
        if bInRootTag:
          self.oCurrNode = root
        
        # create parser object
        p = pyexpat.ParserCreate()
        
        # connect parser object with handler methods
        p.StartElementHandler = self.OnStartElement
        p.EndElementHandler = self.OnEndElement
        p.CharacterDataHandler = self.OnCharacterData
        p.StartCdataSectionHandler = self.OnStartCData
        p.EndCdataSectionHandler = self.OnEndCData
        p.ProcessingInstructionHandler = self.OnProcessingInstruction
        p.CommentHandler = self.OnComment
        p.StartNamespaceDeclHandler = self.OnStartNamespaceDecl
        p.EndNamespaceDeclHandler = self.OnEndNamespaceDecl
        
        #### parsing ####
        #++ print "#### parsing ####"
        if type(input)=='string':
          # input is a string!
          rv = p.Parse(input, 1)
        else:
          # input is a file object!
          while 1:
            if Globals.DatabaseVersion == '3':
              get_transaction().commit(1)
            
            v=input.read(self.iBufferSize)
            if v=="":
              rv = 1
              break
            
            rv = p.Parse(v, 0)
            if not rv:
              break 
        
        # raise parser exception           
        if not rv:
            raise ParseError('%s at line %s' % (pyexpat.ErrorString(p.ErrorCode), p.ErrorLineNumber))
        ####
        
        return self.oRootNode


    ###############################################################################################
    # Builder.OnStartElement(self, name, attrs):
    #
    # Handler of XML-Parser: 
    # Called at the start of a XML element (resp. on occurence of a XML start tag).
    # Usually, the occurence of a XML tag induces the instanciation of a new node object. Therefore,
    # Builder contains a mapping table ("dGlobalAttrs"), that maps XML tags to python classes. The
    # newly created node object is then made current. If no matching class is found for a XML tag,
    # the event handler "xmlOnUnknownStart()" is called on the current object.
    #
    # IN: name  = element name (=tag name)
    #     attrs = dictionary of element attributes
    ###############################################################################################
    def OnStartElement(self, name, attrs):
        """ Builder.OnStartElement """
        #++ if _globals.debug: print "OnStartElement(" + str(name) + "," + str(attrs) + ") "
        
        if self.bInRootTag or \
           self.oRoot == None or \
           (self.oRoot.id == self.getDocumentElement().id and \
            self.dGlobalAttrs.has_key(name) and \
            self.dGlobalAttrs[name]['obj_class'] is not None):
            
            #++ print "We are inside the XML root tag OR no root object is set"
            #++ print "-> instanciate node object in any case"
            
            if self.dGlobalAttrs.has_key(name) and \
               self.dGlobalAttrs[name]['obj_class'] is not None:
                # class defined for tag!
                
                if self.oCurrNode==None and self.oRoot!=None and self.oRoot.id==self.getDocumentElement().id:
                  self.oCurrNode = self.oRoot
                    
                # create node instance  
                _globals.writeLog("  create new object <" + name + "> in " + str(self.oCurrNode))
                if 'id_fix' in attrs:
                  id = attrs[attrs.index('id_fix')+1]
                elif 'id_prefix' in attrs:
                  prefix = attrs[attrs.index('id_prefix')+1]
                  id = self.oCurrNode.getNewId(prefix)
                elif 'id' in attrs:
                  id = attrs[attrs.index('id')+1]
                  prefix = _globals.id_prefix(id)
                  id = self.oCurrNode.getNewId(prefix)
                else:
                  id = self.oCurrNode.getNewId()
                sort_id = self.oCurrNode.getNewSortId()
                
                ##### Create ####
                newNode = self.dGlobalAttrs[name]['obj_class'](id,sort_id)
                self.oCurrNode._setObject(newNode.id, newNode)
                newNode = getattr(self.oCurrNode,newNode.id)

                ##### Meta Objects ####
                if newNode.meta_type == 'ZMSCustom':
                  meta_id = attrs[attrs.index('meta_id')+1]
                  if meta_id not in self.getMetaobjIds():
                    raise ParseError("Unknown meta_id (" + meta_id + "): no special object available!")  # no special object available!
                  newNode.meta_id = meta_id
                ##### Object State ####
                newNode.resetObjVersion()
                obj_attrs = newNode.getObjAttrs()
                for lang in self.getLangIds():
                  req = {'lang':lang,'preview':'preview'}
                  ##### Object State ####
                  newNode.setObjStateNew(req)
                  ##### Init Properties ####
                  if 'active' in obj_attrs.keys():
                    newNode.setObjProperty('active',1,lang)
                  newNode.setObjProperty('change_uid','xml',lang)
                  newNode.setObjProperty('change_dt',time.time(),lang)
                
                _globals.writeLog("  object with id " + str(newNode.id) + " of class " + str(newNode.__class__) + " created in " + str(self.oCurrNode.__class__))
                
                if self.oRoot is None:   # root object set?
                    self.oRoot = newNode # -> set root node
                
                # notify new node                
                newNode.xmlOnStartElement(name, attrs, self.oCurrNode, self.oRoot)
                
                # set new node as current node
                self.oCurrNode = newNode
            
            else:
                # no class defined for tag 
                # -> offer to current object
                if self.oCurrNode==None:
                    raise ParseError("Unknown tag (" + name + "): no current object available!")  # no current object available!
                
                if not self.oCurrNode.xmlOnUnknownStartTag(name, attrs):
                    if self._unknownTagName == None:
                        self._unknownTagName = name
                    #++ print "Unknown start-tag (" + name + "): current object did not accept tag!"  # current object did not accept tag!
                    # raise ParseError("Unknown start-tag (" + name + "): current object did not accept tag!")  # current object did not accept tag!
          
        else:
            #++ print "we have encountered the XML root tag and a root object is predefined"
            #++ print "-> simply notify root object"
            self.oRoot.xmlOnStartElement(name, attrs, None, self.oRoot)
            
            # set root node as current node
            self.oCurrNode = self.oRoot

        # we are inside the XML root now!
        self.bInRootTag=1
        if self.oRootNode is None:
          self.oRootNode = self.oCurrNode


    ###############################################################################################
    # Builder.OnEndElement(self, name):
    #
    # Handler of XML-Parser: 
    # Called at the end of a XML element (resp. on occurence of a XML end tag).
    #
    # IN: name  = element name (=tag name)
    ###############################################################################################
    def OnEndElement(self, name):
        """ Builder.OnEndElement """
        #++ if _globals.debug: print "OnEndElement(" + str(name) + ")"
      
        # do we have a current node?
        if self.oCurrNode==None:
            raise ParseError("Unmatching end tag (" + name + ")")

        # is this the right tag name?
        if name==self.oCurrNode.xmlGetTagName():
        
            # notify current node
            self.oCurrNode.xmlOnEndElement()
      
            parent = self.oCurrNode.xmlGetParent()

            # set parent node as current node
            self.oCurrNode = parent
            

        else:
            if self.dGlobalAttrs.has_key(name) and \
               self.dGlobalAttrs[name]['obj_class'] is not None:
                if self.dGlobalAttrs.has_key(self.oCurrNode.xmlGetTagName()) and \
                   self.dGlobalAttrs[self.oCurrNode.xmlGetTagName()]['obj_class'] is not None:
                    # tag name is known, but not valid at this place!
                    raise ParseError("Unmatching end tag (/" + name + "), expected(/" + self.oCurrNode.xmlGetTagName() + ")")

            else:
                # tag name is unknown -> offer it to current object       
                if not self.oCurrNode.xmlOnUnknownEndTag(name):
                    if name == self._unknownTagName:
                        self._unknownTagName = None
                    #++ print "Unknown end-tag (/" + name + ")"  # current object did not accept tag!         
                    # raise ParseError("Unknown end-tag (" + name + ")")  # current object did not accept tag!         


    ###############################################################################################
    # Builder.OnCharacterData(self, data):
    #
    # Handler of XML-Parser:
    # Called after plain character data was parsed. Forwards the character data to the current 
    # node. The class attribute "bInCData" determines, wether the character data is nested in a 
    # CDATA block.
    #
    # IN: data = character data string
    ###############################################################################################
    def OnCharacterData(self, data):
        """ Builder.OnCharacterData """
        #++ print "OnCharacterData(" + str(data) + ")"
        
        # do we have a current node?
        if self.oCurrNode==None:
           raise ParseError("Unexpected character data found")
           
        # notify current node
        self.oCurrNode.xmlOnCharacterData(data, self.bInCData)


    ###############################################################################################
    # Builder.OnStartCData(self):
    #
    # Handler of XML-Parser:
    # Called at the start of a CDATA block (resp. on occurence of the "CDATA[" tag).
    ###############################################################################################
    def OnStartCData(self):
        """ Builder.OnStartCData """
        self.bInCData=1


    ###############################################################################################
    # Builder.OnEndCData(self):
    #
    # Handler of XML-Parser:
    # Called at the end of a CDATA block (resp. on occurence of the "]" tag).
    ###############################################################################################
    def OnEndCData(self):
        """ Builder.OnEndCData """
        self.bInCData=0


    ###############################################################################################
    # Builder.OnProcessingInstruction(self, target, data):
    #
    # Handler of XML-Parser:
    # Called on occurence of a processing instruction.
    #
    # IN: target = target (processing instruction)
    #     data   = dictionary of data
    ###############################################################################################
    def OnProcessingInstruction(self, target, data):
        """ Builder.OnProcessingInstruction """
        pass  # ignored


    ###############################################################################################
    # Builder.OnComment(self, data):
    #
    # Handler of XML-Parser:
    # Called on occurence of a comment.
    #
    # IN: data = comment string
    ###############################################################################################
    def OnComment(self, data):
        """ Builder.OnComment """
        pass  # ignored


    ###############################################################################################
    # Builder.OnStartNamespaceDecl(self, prefix, uri):
    #
    # Handler of XML-Parser:
    # Called at the start of a namespace declaration.
    #
    # IN: prefix = prefix of namespace
    #     uri    = namespace identifier
    ###############################################################################################
    def OnStartNamespaceDecl(self, prefix, uri):
        """ Builder.OnStartNamespaceDecl """
        pass  # ignored


    ###############################################################################################
    # Builder.OnEndNamespaceDecl(self, prefix):
    #
    # Handler of XML-Parser:
    # Called at the end of a namespace declaration.
    #
    # IN: prefix = prefix of namespace
    ###############################################################################################
    def OnEndNamespaceDecl(self, prefix):
        """ Builder.OnEndNamespaceDecl """
        pass  # ignored

###################################################################################################