]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Python/buildgen/XmlRoutines.py
Change parameter id in UserExtension element from Integer to String according to...
[mirror_edk2.git] / Tools / Python / buildgen / XmlRoutines.py
CommitLineData
28972318 1#!/usr/bin/env python
2
3# Copyright (c) 2007, Intel Corporation
4# All rights reserved. This program and the accompanying materials
5# are licensed and made available under the terms and conditions of the BSD License
6# which accompanies this distribution. The full text of the license may be found at
7# http://opensource.org/licenses/bsd-license.php
8#
9# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12"""This is an XML API that uses a syntax similar to XPath, but it is written in
13 standard python so that no extra python packages are required to use it."""
14
15import xml.dom.minidom
16
17def XmlList(Dom, String):
18 """Get a list of XML Elements using XPath style syntax."""
19 if String == "" or Dom == None or not isinstance(Dom, xml.dom.Node):
20 return []
21
22 if String[0] == "/":
23 String = String[1:]
24
25 if Dom.nodeType==Dom.DOCUMENT_NODE:
26 Dom = Dom.documentElement
27
28 tagList = String.split('/')
29 nodes = [Dom]
30 childNodes = []
31 index = 0
32 end = len(tagList) - 1
33 while index <= end:
34 for node in nodes:
35 if node.nodeType == node.ELEMENT_NODE and node.tagName == tagList[index]:
36 if index < end:
37 childNodes.extend(node.childNodes)
38 else:
39 childNodes.append(node)
40
41 nodes = childNodes
42 childNodes = []
43 index += 1
44
45 return nodes
46
47def XmlElement (Dom, String):
48 """Return a single element that matches the String which is XPath style syntax."""
49 if String == "" or Dom == None or not isinstance(Dom, xml.dom.Node):
50 return ""
51
52 if String[0] == "/":
53 String = String[1:]
54
55 if Dom.nodeType==Dom.DOCUMENT_NODE:
56 Dom = Dom.documentElement
57
58 tagList = String.split('/')
59 childNodes = [Dom]
60 index = 0
61 end = len(tagList) - 1
62 while index <= end:
63 for node in childNodes:
64 if node.nodeType == node.ELEMENT_NODE and node.tagName == tagList[index]:
65 if index < end:
66 childNodes = node.childNodes
67 else:
68 return node
69 break
70
71 index += 1
72
73 return ""
74
75def XmlElementData (Dom):
76 """Get the text for this element."""
77 if Dom == None or Dom == '' or Dom.firstChild == None:
78 return ''
79
80 return Dom.firstChild.data.strip(' ')
81
82def XmlAttribute (Dom, String):
83 """Return a single attribute that named by String."""
84 if Dom == None or Dom == '':
85 return ''
86
87 try:
88 return Dom.getAttribute(String).strip(' ')
89 except:
90 return ''
91
92def XmlTopTag(Dom):
93 """Return the name of the Root or top tag in the XML tree."""
94 if Dom == None or Dom == '' or Dom.firstChild == None:
95 return ''
96 return Dom.firstChild.nodeName
97
98
99# This acts like the main() function for the script, unless it is 'import'ed into another
100# script.
101if __name__ == '__main__':
102
103 # Nothing to do here. Could do some unit tests.
104 pass