Upstream version 11.39.244.0
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / handle_xml.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2014 Intel Corporation. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 """
8 The public interface functions of handling xml.
9 """
10
11
12 def AddElementAttribute(doc, node, name, value):
13   root = doc.documentElement
14   item = doc.createElement(node)
15   item.setAttribute(name, value)
16   root.appendChild(item)
17
18
19 def EditElementAttribute(doc, node, name, value):
20   item = doc.getElementsByTagName(node)[0]
21   if item.hasAttribute(name):
22     item.attributes[name].value = value
23   else:
24     item.setAttribute(name, value)
25
26
27 def EditElementValueByNodeName(doc, node, name, value):
28   items = doc.getElementsByTagName(node)
29   for item in items:
30     if item.attributes['name'].value == name:
31       item.firstChild.data = value
32       break
33
34
35 def AddElementAttributeAndText(doc, node, name, value, data):
36   root = doc.documentElement
37   item = doc.createElement(node)
38   item.setAttribute(name, value)
39   text = doc.createTextNode(data)
40   item.appendChild(text)
41   root.appendChild(item)
42
43
44 def CompareNodes(node1, node2):
45   if node1.tagName != node2.tagName or node1.attributes is None:
46     return False
47   if node2.attributes is None:
48     return True
49
50   for item in node2.attributes.items():
51     if not item in node1.attributes.items():
52       return False
53   return True
54
55
56 def MergeNodes(node1, node2):
57   tmp_node_list = []
58   for item2 in node2.childNodes:
59     if item2.nodeType != item2.ELEMENT_NODE:
60       continue
61     item1 = None
62     for tmp_item in node1.childNodes:
63       if tmp_item.nodeType != tmp_item.ELEMENT_NODE:
64         continue
65       if CompareNodes(tmp_item, item2):
66         item1 = tmp_item
67         break
68     if item1 is not None:
69       MergeNodes(item1, item2)
70     else:
71       tmp_node_list.append(item2)
72
73   for item in tmp_node_list:
74     node1.appendChild(item)