summaryrefslogtreecommitdiff
path: root/giscanner/xmlwriter.py
diff options
context:
space:
mode:
authorJohan Dahlin <jdahlin@async.com.br>2008-04-18 20:37:51 +0000
committerJohan Dahlin <johan@src.gnome.org>2008-04-18 20:37:51 +0000
commite1d5d57efaa35e1454942a364a1d7188cadc6a89 (patch)
treec45bf02c57372ec1882f8813615e3ac690b5a84a /giscanner/xmlwriter.py
parentfe379f2f17ea6ccbb3eaf929eb87d5fc2b4b5782 (diff)
downloadgobject-introspection-e1d5d57efaa35e1454942a364a1d7188cadc6a89.tar.gz
Add a simplistic gidl writer, which can't do too much.
2008-04-18 Johan Dahlin <jdahlin@async.com.br> * giscanner/gidlwriter.py: * giscanner/xmlwriter.py: * tools/g-ir-scanner: Add a simplistic gidl writer, which can't do too much. svn path=/trunk/; revision=175
Diffstat (limited to 'giscanner/xmlwriter.py')
-rw-r--r--giscanner/xmlwriter.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/giscanner/xmlwriter.py b/giscanner/xmlwriter.py
new file mode 100644
index 00000000..2f80603d
--- /dev/null
+++ b/giscanner/xmlwriter.py
@@ -0,0 +1,55 @@
+from cStringIO import StringIO
+from xml.sax.saxutils import quoteattr
+
+
+class XMLWriter(object):
+ def __init__(self):
+ self._data = StringIO()
+ self._tag_stack = []
+ self._indent = 0
+
+ # Private
+
+ def _collect_attributes(self, attributes):
+ attrValue = ''
+ if attributes:
+ for attr, value in attributes:
+ assert value is not None, attr
+ attrValue += ' %s=%s' % (attr, quoteattr(value))
+ return attrValue
+
+ def _open_tag(self, tag_name, attributes=None):
+ attrs = self._collect_attributes(attributes)
+ self.write_line('<%s%s>' % (tag_name, attrs))
+
+ def _close_tag(self, tag_name):
+ self.write_line('</%s>' % (tag_name,))
+
+ # Public API
+
+ def get_xml(self):
+ return self._data.getvalue()
+
+ def write_line(self, line=''):
+ self._data.write('%s%s\n' % (' ' * self._indent, line))
+
+ def write_tag(self, tag_name, attributes):
+ attrs = self._collect_attributes(attributes)
+ self.write_line('<%s%s/>' % (tag_name, attrs))
+
+ def write_tag_with_data(self, tag_name, data, attributes=None):
+ if data is None:
+ self.write_tag(tag_name, attributes)
+ attrs = self._collect_attributes(attributes)
+ self.write_line('<%s%s>%s</%s>' % (tag_name, attrs, data, tag_name))
+
+ def push_tag(self, tag_name, attributes=None):
+ self._open_tag(tag_name, attributes)
+ self._tag_stack.append(tag_name)
+ self._indent += 2
+
+ def pop_tag(self):
+ self._indent -= 2
+ tag_name = self._tag_stack.pop()
+ self._close_tag(tag_name)
+ return tag_name