]> git.0d.be Git - jack_mixer.git/blob - serialization_xml.py
Remove dependency on python-xml (PyXML) as it's unmaintained (#14894)
[jack_mixer.git] / serialization_xml.py
1 #!/usr/bin/env python
2 #
3 # This file is part of jack_mixer
4 #
5 # Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
6 #  
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; version 2 of the License
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 from serialization import *
21 import xml.dom
22 import xml.dom.minidom
23
24 class xml_serialization(serialization_backend):
25     def __init__(self):
26         self.doctype = xml.dom.getDOMImplementation().createDocumentType("pyserialization", None, None)
27
28     def get_root_serialization_object(self, name):
29         self.doc = xml.dom.getDOMImplementation().createDocument(xml.dom.EMPTY_NAMESPACE, name, self.doctype)
30         return xml_serialization_object(self.doc, self.doc.documentElement)
31
32     def get_root_unserialization_object(self, name):
33         if name != self.doc.documentElement.nodeName:
34             return None
35         return xml_serialization_object(self.doc, self.doc.documentElement)
36
37     def get_child_serialization_object(self, name, backend_object):
38         child = self.doc.createElement(name)
39         backend_object.element.appendChild(child)
40         return xml_serialization_object(self.doc, child)
41
42     def save(self, file):
43         print >>file, self.doc.toprettyxml()
44
45     def load(self, file):
46         self.doc = xml.dom.minidom.parse(file)
47
48 class xml_serialization_object:
49     def __init__(self, doc, element):
50         self.doc = doc
51         self.element = element
52
53     def add_property(self, name, value):
54         self.add_property_as_attribute(name, value)
55
56     def add_property_as_attribute(self, name, value):
57         self.element.setAttribute(name, value)
58
59     #def add_property_as_child_element(self, name, value):
60     #    child = self.doc.createElement(name)
61     #    value = self.doc.createTextNode(value)
62     #    child.appendChild(value)
63     #    self.element.appendChild(child)
64
65     def get_childs(self):
66         child_elements = self.element.childNodes
67         childs = []
68         for child in child_elements:
69             if child.nodeType == child.ELEMENT_NODE:
70                 childs.append(xml_serialization_object(self.doc, child))
71         return childs
72
73     def get_properties(self):
74         properties = self.element.attributes
75         dictionary = {}
76         for i in range(properties.length):
77             dictionary[properties.item(i).name] = properties.item(i).nodeValue
78         return dictionary
79
80     def serialization_name(self):
81         return self.element.nodeName