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 from serialization
import *
20 import xml
.dom
.minidom
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
)
27 def get_root_unserialization_object(self
, name
):
28 if name
!= self
.doc
.documentElement
.nodeName
:
30 return XmlSerializationObject(self
.doc
, self
.doc
.documentElement
)
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
)
38 file.write(self
.doc
.toprettyxml())
41 self
.doc
= xml
.dom
.minidom
.parse(file)
43 class XmlSerializationObject
:
44 def __init__(self
, doc
, element
):
46 self
.element
= element
48 def add_property(self
, name
, value
):
49 self
.add_property_as_attribute(name
, value
)
51 def add_property_as_attribute(self
, name
, value
):
52 self
.element
.setAttribute(name
, value
)
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)
61 child_elements
= self
.element
.childNodes
63 for child
in child_elements
:
64 if child
.nodeType
== child
.ELEMENT_NODE
:
65 childs
.append(XmlSerializationObject(self
.doc
, child
))
68 def get_properties(self
):
69 properties
= self
.element
.attributes
71 for i
in range(properties
.length
):
72 dictionary
[properties
.item(i
).name
] = properties
.item(i
).nodeValue
75 def serialization_name(self
):
76 return self
.element
.nodeName