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