]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/XmlRoutines.py
Sync EDKII BaseTools to BaseTools project r1971
[mirror_edk2.git] / BaseTools / Source / Python / Common / XmlRoutines.py
1 ## @file
2 # This is an XML API that uses a syntax similar to XPath, but it is written in
3 # standard python so that no extra python packages are required to use it.
4 #
5 # Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
10 #
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 #
14
15 ##
16 # Import Modules
17 #
18 import xml.dom.minidom
19
20 ## Create a element of XML
21 #
22 # @param Name
23 # @param String
24 # @param NodeList
25 # @param AttributeList
26 #
27 # @revel Element
28 #
29 def CreateXmlElement(Name, String, NodeList, AttributeList):
30 Doc = xml.dom.minidom.Document()
31 Element = Doc.createElement(Name)
32 if String != '' and String != None:
33 Element.appendChild(Doc.createTextNode(String))
34
35 for Item in NodeList:
36 if type(Item) == type([]):
37 Key = Item[0]
38 Value = Item[1]
39 if Key != '' and Key != None and Value != '' and Value != None:
40 Node = Doc.createElement(Key)
41 Node.appendChild(Doc.createTextNode(Value))
42 Element.appendChild(Node)
43 else:
44 Element.appendChild(Item)
45 for Item in AttributeList:
46 Key = Item[0]
47 Value = Item[1]
48 if Key != '' and Key != None and Value != '' and Value != None:
49 Element.setAttribute(Key, Value)
50
51 return Element
52
53 ## Get a list of XML nodes using XPath style syntax.
54 #
55 # Return a list of XML DOM nodes from the root Dom specified by XPath String.
56 # If the input Dom or String is not valid, then an empty list is returned.
57 #
58 # @param Dom The root XML DOM node.
59 # @param String A XPath style path.
60 #
61 # @revel Nodes A list of XML nodes matching XPath style Sting.
62 #
63 def XmlList(Dom, String):
64 if String == None or String == "" or Dom == None or Dom == "":
65 return []
66 if Dom.nodeType == Dom.DOCUMENT_NODE:
67 Dom = Dom.documentElement
68 if String[0] == "/":
69 String = String[1:]
70 TagList = String.split('/')
71 Nodes = [Dom]
72 Index = 0
73 End = len(TagList) - 1
74 while Index <= End:
75 ChildNodes = []
76 for Node in Nodes:
77 if Node.nodeType == Node.ELEMENT_NODE and Node.tagName == TagList[Index]:
78 if Index < End:
79 ChildNodes.extend(Node.childNodes)
80 else:
81 ChildNodes.append(Node)
82 Nodes = ChildNodes
83 ChildNodes = []
84 Index += 1
85
86 return Nodes
87
88
89 ## Get a single XML node using XPath style syntax.
90 #
91 # Return a single XML DOM node from the root Dom specified by XPath String.
92 # If the input Dom or String is not valid, then an empty string is returned.
93 #
94 # @param Dom The root XML DOM node.
95 # @param String A XPath style path.
96 #
97 # @revel Node A single XML node matching XPath style Sting.
98 #
99 def XmlNode(Dom, String):
100 if String == None or String == "" or Dom == None or Dom == "":
101 return ""
102 if Dom.nodeType == Dom.DOCUMENT_NODE:
103 Dom = Dom.documentElement
104 if String[0] == "/":
105 String = String[1:]
106 TagList = String.split('/')
107 Index = 0
108 End = len(TagList) - 1
109 ChildNodes = [Dom]
110 while Index <= End:
111 for Node in ChildNodes:
112 if Node.nodeType == Node.ELEMENT_NODE and Node.tagName == TagList[Index]:
113 if Index < End:
114 ChildNodes = Node.childNodes
115 else:
116 return Node
117 break
118 Index += 1
119 return ""
120
121
122 ## Get a single XML element using XPath style syntax.
123 #
124 # Return a single XML element from the root Dom specified by XPath String.
125 # If the input Dom or String is not valid, then an empty string is returned.
126 #
127 # @param Dom The root XML DOM object.
128 # @param Strin A XPath style path.
129 #
130 # @revel Element An XML element matching XPath style Sting.
131 #
132 def XmlElement(Dom, String):
133 try:
134 return XmlNode(Dom, String).firstChild.data.strip()
135 except:
136 return ""
137
138
139 ## Get a single XML element of the current node.
140 #
141 # Return a single XML element specified by the current root Dom.
142 # If the input Dom is not valid, then an empty string is returned.
143 #
144 # @param Dom The root XML DOM object.
145 #
146 # @revel Element An XML element in current root Dom.
147 #
148 def XmlElementData(Dom):
149 try:
150 return Dom.firstChild.data.strip()
151 except:
152 return ""
153
154
155 ## Get a list of XML elements using XPath style syntax.
156 #
157 # Return a list of XML elements from the root Dom specified by XPath String.
158 # If the input Dom or String is not valid, then an empty list is returned.
159 #
160 # @param Dom The root XML DOM object.
161 # @param String A XPath style path.
162 #
163 # @revel Elements A list of XML elements matching XPath style Sting.
164 #
165 def XmlElementList(Dom, String):
166 return map(XmlElementData, XmlList(Dom, String))
167
168
169 ## Get the XML attribute of the current node.
170 #
171 # Return a single XML attribute named Attribute from the current root Dom.
172 # If the input Dom or Attribute is not valid, then an empty string is returned.
173 #
174 # @param Dom The root XML DOM object.
175 # @param Attribute The name of Attribute.
176 #
177 # @revel Element A single XML element matching XPath style Sting.
178 #
179 def XmlAttribute(Dom, Attribute):
180 try:
181 return Dom.getAttribute(Attribute).strip()
182 except:
183 return ''
184
185
186 ## Get the XML node name of the current node.
187 #
188 # Return a single XML node name from the current root Dom.
189 # If the input Dom is not valid, then an empty string is returned.
190 #
191 # @param Dom The root XML DOM object.
192 #
193 # @revel Element A single XML element matching XPath style Sting.
194 #
195 def XmlNodeName(Dom):
196 try:
197 return Dom.nodeName.strip()
198 except:
199 return ''
200
201 ## Parse an XML file.
202 #
203 # Parse the input XML file named FileName and return a XML DOM it stands for.
204 # If the input File is not a valid XML file, then an empty string is returned.
205 #
206 # @param FileName The XML file name.
207 #
208 # @revel Dom The Dom object achieved from the XML file.
209 #
210 def XmlParseFile(FileName):
211 try:
212 XmlFile = open(FileName)
213 Dom = xml.dom.minidom.parse(XmlFile)
214 XmlFile.close()
215 return Dom
216 except Exception, X:
217 print X
218 return ""
219
220 # This acts like the main() function for the script, unless it is 'import'ed
221 # into another script.
222 if __name__ == '__main__':
223 # Nothing to do here. Could do some unit tests.
224 A = CreateXmlElement('AAA', 'CCC', [['AAA', '111'], ['BBB', '222']], [['A', '1'], ['B', '2']])
225 B = CreateXmlElement('ZZZ', 'CCC', [['XXX', '111'], ['YYY', '222']], [['A', '1'], ['B', '2']])
226 C = CreateXmlList('DDD', 'EEE', [A, B], ['FFF', 'GGG'])
227 print C.toprettyxml(indent = " ")
228 pass