3 # This file is part of jack_mixer
5 # Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
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
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.
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.
20 from serialization import *
22 import xml.dom.minidom
24 class xml_serialization(serialization_backend):
25 def get_root_serialization_object(self, name):
26 self.doc = xml.dom.getDOMImplementation().createDocument(xml.dom.EMPTY_NAMESPACE, name, None)
27 return xml_serialization_object(self.doc, self.doc.documentElement)
29 def get_root_unserialization_object(self, name):
30 if name != self.doc.documentElement.nodeName:
32 return xml_serialization_object(self.doc, self.doc.documentElement)
34 def get_child_serialization_object(self, name, backend_object):
35 child = self.doc.createElement(name)
36 backend_object.element.appendChild(child)
37 return xml_serialization_object(self.doc, child)
40 file.write(self.doc.toprettyxml())
43 self.doc = xml.dom.minidom.parse(file)
45 class xml_serialization_object:
46 def __init__(self, doc, element):
48 self.element = element
50 def add_property(self, name, value):
51 self.add_property_as_attribute(name, value)
53 def add_property_as_attribute(self, name, value):
54 self.element.setAttribute(name, value)
56 #def add_property_as_child_element(self, name, value):
57 # child = self.doc.createElement(name)
58 # value = self.doc.createTextNode(value)
59 # child.appendChild(value)
60 # self.element.appendChild(child)
63 child_elements = self.element.childNodes
65 for child in child_elements:
66 if child.nodeType == child.ELEMENT_NODE:
67 childs.append(xml_serialization_object(self.doc, child))
70 def get_properties(self):
71 properties = self.element.attributes
73 for i in range(properties.length):
74 dictionary[properties.item(i).name] = properties.item(i).nodeValue
77 def serialization_name(self):
78 return self.element.nodeName