1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 The sax module contains a collection of classes that provide a
19 (D)ocument (O)bject (M)odel representation of an XML document.
20 The goal is to provide an easy, intuative interface for managing XML
21 documents. Although, the term, DOM, is used above, this model is
22 B{far} better.
23
24 XML namespaces in suds are represented using a (2) element tuple
25 containing the prefix and the URI. Eg: I{('tns', 'http://myns')}
26
27 """
28
29 from logging import getLogger
30 import suds.metrics
31 from suds import *
32 from suds.sax import *
33 from suds.sax.document import Document
34 from suds.sax.element import Element
35 from suds.sax.text import Text
36 from suds.sax.attribute import Attribute
37 from xml.sax import make_parser, InputSource, ContentHandler
38 from xml.sax.handler import feature_external_ges
39 from cStringIO import StringIO
40
41 log = getLogger(__name__)
42
43
44 -class Handler(ContentHandler):
45 """ sax hanlder """
46
49
63
75
77 name = unicode(name)
78 current = self.top()
79 if len(current.charbuffer):
80 current.text = Text(u''.join(current.charbuffer))
81 del current.charbuffer
82 if len(current):
83 current.trim()
84 currentqname = current.qname()
85 if name == currentqname:
86 self.pop()
87 else:
88 raise Exception('malformed document')
89
94
95 - def push(self, node):
98
100 return self.nodes.pop()
101
103 return self.nodes[len(self.nodes)-1]
104
107 """ SAX Parser """
108
109 @classmethod
111 p = make_parser()
112 p.setFeature(feature_external_ges, 0)
113 h = Handler()
114 p.setContentHandler(h)
115 return (p, h)
116
117 - def parse(self, file=None, string=None):
118 """
119 SAX parse XML text.
120 @param file: Parse a python I{file-like} object.
121 @type file: I{file-like} object.
122 @param string: Parse string XML.
123 @type string: str
124 """
125 timer = metrics.Timer()
126 timer.start()
127 sax, handler = self.saxparser()
128 if file is not None:
129 sax.parse(file)
130 timer.stop()
131 metrics.log.debug('sax (%s) duration: %s', file, timer)
132 return handler.nodes[0]
133 if string is not None:
134 source = InputSource(None)
135 source.setByteStream(StringIO(string))
136 sax.parse(source)
137 timer.stop()
138 metrics.log.debug('%s\nsax duration: %s', string, timer)
139 return handler.nodes[0]
140