CHANGELOG.md: Adjust for LADIOS jack_mixer
[jack_mixer.git] / jack_mixer / serialization_xml.py
blob802513e3c0bb6ca82b8d3a1fa673a8be4a60d274
1 # This file is part of jack_mixer
3 # Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
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
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.
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.
18 import xml.dom
19 import xml.dom.minidom
21 from .serialization import SerializationBackend
24 class XmlSerializationError(Exception):
25 pass
28 class InvalidDocumentTypeError(XmlSerializationError):
29 pass
32 class XmlSerialization(SerializationBackend):
33 def get_root_serialization_object(self, name):
34 self.doc = xml.dom.getDOMImplementation().createDocument(
35 xml.dom.EMPTY_NAMESPACE, name, None
37 return XmlSerializationObject(self.doc, self.doc.documentElement)
39 def get_root_unserialization_object(self, name):
40 if name != self.doc.documentElement.nodeName:
41 return None
42 return XmlSerializationObject(self.doc, self.doc.documentElement)
44 def get_child_serialization_object(self, name, backend_object):
45 child = self.doc.createElement(name)
46 backend_object.element.appendChild(child)
47 return XmlSerializationObject(self.doc, child)
49 def save(self, file):
50 file.write(self.doc.toprettyxml())
52 def load(self, file, name):
53 self.doc = xml.dom.minidom.parse(file)
55 document_type = self.doc.documentElement.nodeName
56 if document_type != name:
57 raise InvalidDocumentTypeError(
58 _("Document type '{type}' not supported.").format(type=document_type)
62 class XmlSerializationObject:
63 def __init__(self, doc, element):
64 self.doc = doc
65 self.element = element
67 def add_property(self, name, value):
68 self.add_property_as_attribute(name, value)
70 def add_property_as_attribute(self, name, value):
71 self.element.setAttribute(name, value)
73 # def add_property_as_child_element(self, name, value):
74 # child = self.doc.createElement(name)
75 # value = self.doc.createTextNode(value)
76 # child.appendChild(value)
77 # self.element.appendChild(child)
79 def get_childs(self):
80 child_elements = self.element.childNodes
81 childs = []
82 for child in child_elements:
83 if child.nodeType == child.ELEMENT_NODE:
84 childs.append(XmlSerializationObject(self.doc, child))
85 return childs
87 def get_properties(self):
88 properties = self.element.attributes
89 dictionary = {}
90 for i in range(properties.length):
91 dictionary[properties.item(i).name] = properties.item(i).nodeValue
92 return dictionary
94 def serialization_name(self):
95 return self.element.nodeName