1 # ====================================================================
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 # ====================================================================
18 class Digester(xml.sax.ContentHandler):
23 def addSetProperty(self, path, property, attribute=None):
25 if attribute is not None:
26 pairs = self.attributes.get(path)
28 self.attributes[path] = pairs = { attribute: property }
30 pairs[property] = attribute
33 self.tags[path] = property
35 def parse(self, input):
37 xml.sax.parse(input, self)
38 return self.properties
40 def startDocument(self):
45 def startElement(self, tag, attrs):
48 pairs = self.attributes.get('/'.join(self.path))
50 for name, value in attrs.items():
51 property = pairs.get(name)
52 if property is not None:
53 self.properties[property] = value
55 def characters(self, data):
57 self.data = data.strip()
59 def endElement(self, tag):
62 property = self.tags.get('/'.join(self.path))
63 if property is not None:
64 self.properties[property] = self.data