]> git.0d.be Git - jack_mixer.git/blob - serialization_xml.py
Replace the "see above" by the most probable explanation
[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 import xml.dom.ext
24
25 class xml_serialization(serialization_backend):
26     def __init__(self):
27         self.doctype = xml.dom.implementation.createDocumentType("pyserialzation", None, None)
28
29     def get_root_serialization_object(self, name):
30         self.doc = xml.dom.implementation.createDocument(xml.dom.EMPTY_NAMESPACE, name, self.doctype)
31         return xml_serialization_object(self.doc, self.doc.documentElement)
32
33     def get_root_unserialization_object(self, name):
34         if name != self.doc.documentElement.nodeName:
35             return None
36         return xml_serialization_object(self.doc, self.doc.documentElement)
37
38     def get_child_serialization_object(self, name, backend_object):
39         child = self.doc.createElement(name)
40         backend_object.element.appendChild(child)
41         return xml_serialization_object(self.doc, child)
42
43     def save(self, file):
44         xml.dom.ext.PrettyPrint(self.doc, file)
45
46     def load(self, file):
47         self.doc = xml.dom.minidom.parse(file)
48         #rint "Loaded:"
49         #xml.dom.ext.PrettyPrint(self.doc)
50
51 class xml_serialization_object:
52     def __init__(self, doc, element):
53         self.doc = doc
54         self.element = element
55
56     def add_property(self, name, value):
57         self.add_property_as_attribute(name, value)
58
59     def add_property_as_attribute(self, name, value):
60         self.element.setAttribute(name, value)
61
62     #def add_property_as_child_element(self, name, value):
63     #    child = self.doc.createElement(name)
64     #    value = self.doc.createTextNode(value)
65     #    child.appendChild(value)
66     #    self.element.appendChild(child)
67
68     def get_childs(self):
69         child_elements = self.element.childNodes
70         childs = []
71         for child in child_elements:
72             if child.nodeType == child.ELEMENT_NODE:
73                 childs.append(xml_serialization_object(self.doc, child))
74         return childs
75
76     def get_properties(self):
77         properties = self.element.attributes
78         dictionary = {}
79         for i in range(properties.length):
80             dictionary[properties.item(i).name] = properties.item(i).nodeValue
81         return dictionary
82
83     def serialization_name(self):
84         return self.element.nodeName