summaryrefslogtreecommitdiff
path: root/qpid/python/qpid/xmlutil.py
blob: 585516b44feb80919fc7eb7118ce76fe6cff62ba (plain)
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
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

"""
XML utilities used by spec.py
"""

import xml.sax
from xml.sax.handler import ContentHandler

def parse(file):
  doc = Node("root")
  xml.sax.parse(file, Builder(doc))
  return doc

class Node:

  def __init__(self, name, attrs = None, text = None, parent = None):
    self.name = name
    self.attrs = attrs
    self.text = text
    self.parent = parent
    self.children = []
    if parent != None:
      parent.children.append(self)

  def get_bool(self, key, default = False):
    v = self.get(key)
    if v == None:
      return default
    else:
      return bool(int(v))

  def index(self):
    if self.parent:
      return self.parent.children.index(self)
    else:
      return 0

  def has(self, key):
    try:
      result = self[key]
      return True
    except KeyError:
      return False
    except IndexError:
      return False

  def get(self, key, default = None):
    if self.has(key):
      return self[key]
    else:
      return default

  def __getitem__(self, key):
    if callable(key):
      return filter(key, self.children)
    else:
      t = key.__class__
      meth = "__get%s__" % t.__name__
      if hasattr(self, meth):
        return getattr(self, meth)(key)
      else:
        raise KeyError(key)

  def __getstr__(self, name):
    if name[:1] == "@":
      return self.attrs[name[1:]]
    else:
      return self[lambda nd: nd.name == name]

  def __getint__(self, index):
    return self.children[index]

  def __iter__(self):
    return iter(self.children)

  def path(self):
    if self.parent == None:
      return "/%s" % self.name
    else:
      return "%s/%s" % (self.parent.path(), self.name)

class Builder(ContentHandler):

  def __init__(self, start = None):
    self.node = start

  def __setitem__(self, element, type):
    self.types[element] = type

  def startElement(self, name, attrs):
    self.node = Node(name, attrs, None, self.node)

  def endElement(self, name):
    self.node = self.node.parent

  def characters(self, content):
    if self.node.text == None:
      self.node.text = content
    else:
      self.node.text += content