]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/UPT/Library/Xml/XmlRoutines.py
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.
5 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
7 # SPDX-License-Identifier: BSD-2-Clause-Patent
17 import xml
.dom
.minidom
20 from Logger
.ToolError
import PARSER_ERROR
21 import Logger
.Log
as Logger
23 ## Create a element of XML
28 # @param AttributeList
30 def CreateXmlElement(Name
, String
, NodeList
, AttributeList
):
31 Doc
= xml
.dom
.minidom
.Document()
32 Element
= Doc
.createElement(Name
)
33 if String
!= '' and String
is not None:
34 Element
.appendChild(Doc
.createTextNode(String
))
37 if isinstance(Item
, type([])):
40 if Key
!= '' and Key
is not None and Value
!= '' and Value
is not None:
41 Node
= Doc
.createElement(Key
)
42 Node
.appendChild(Doc
.createTextNode(Value
))
43 Element
.appendChild(Node
)
45 Element
.appendChild(Item
)
46 for Item
in AttributeList
:
49 if Key
!= '' and Key
is not None and Value
!= '' and Value
is not None:
50 Element
.setAttribute(Key
, Value
)
54 ## Get a list of XML nodes using XPath style syntax.
56 # Return a list of XML DOM nodes from the root Dom specified by XPath String.
57 # If the input Dom or String is not valid, then an empty list is returned.
59 # @param Dom The root XML DOM node.
60 # @param String A XPath style path.
62 def XmlList(Dom
, String
):
63 if String
is None or String
== "" or Dom
is None or Dom
== "":
65 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
66 Dom
= Dom
.documentElement
69 TagList
= String
.split('/')
72 End
= len(TagList
) - 1
76 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== \
79 ChildNodes
.extend(Node
.childNodes
)
81 ChildNodes
.append(Node
)
89 ## Get a single XML node using XPath style syntax.
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.
94 # @param Dom The root XML DOM node.
95 # @param String A XPath style path.
97 def XmlNode(Dom
, String
):
98 if String
is None or String
== "" or Dom
is None or Dom
== "":
100 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
101 Dom
= Dom
.documentElement
104 TagList
= String
.split('/')
106 End
= len(TagList
) - 1
109 for Node
in ChildNodes
:
110 if Node
.nodeType
== Node
.ELEMENT_NODE
and \
111 Node
.tagName
== TagList
[Index
]:
113 ChildNodes
= Node
.childNodes
121 ## Get a single XML element using XPath style syntax.
123 # Return a single XML element from the root Dom specified by XPath String.
124 # If the input Dom or String is not valid, then an empty string is returned.
126 # @param Dom The root XML DOM object.
127 # @param Strin A XPath style path.
129 def XmlElement(Dom
, String
):
131 return XmlNode(Dom
, String
).firstChild
.data
.strip()
132 except BaseException
:
135 ## Get a single XML element using XPath style syntax.
137 # Similar with XmlElement, but do not strip all the leading and tailing space
138 # and newline, instead just remove the newline and spaces introduced by
141 # @param Dom The root XML DOM object.
142 # @param Strin A XPath style path.
144 def XmlElement2(Dom
, String
):
146 HelpStr
= XmlNode(Dom
, String
).firstChild
.data
147 gRemovePrettyRe
= re
.compile(r
"""(?:(\n *) )(.*)\1""", re
.DOTALL
)
148 HelpStr
= re
.sub(gRemovePrettyRe
, r
"\2", HelpStr
)
150 except BaseException
:
154 ## Get a single XML element of the current node.
156 # Return a single XML element specified by the current root Dom.
157 # If the input Dom is not valid, then an empty string is returned.
159 # @param Dom The root XML DOM object.
161 def XmlElementData(Dom
):
163 return Dom
.firstChild
.data
.strip()
164 except BaseException
:
168 ## Get a list of XML elements using XPath style syntax.
170 # Return a list of XML elements from the root Dom specified by XPath String.
171 # If the input Dom or String is not valid, then an empty list is returned.
173 # @param Dom The root XML DOM object.
174 # @param String A XPath style path.
176 def XmlElementList(Dom
, String
):
177 return list(map(XmlElementData
, XmlList(Dom
, String
)))
180 ## Get the XML attribute of the current node.
182 # Return a single XML attribute named Attribute from the current root Dom.
183 # If the input Dom or Attribute is not valid, then an empty string is returned.
185 # @param Dom The root XML DOM object.
186 # @param Attribute The name of Attribute.
188 def XmlAttribute(Dom
, Attribute
):
190 return Dom
.getAttribute(Attribute
)
191 except BaseException
:
195 ## Get the XML node name of the current node.
197 # Return a single XML node name from the current root Dom.
198 # If the input Dom is not valid, then an empty string is returned.
200 # @param Dom The root XML DOM object.
202 def XmlNodeName(Dom
):
204 return Dom
.nodeName
.strip()
205 except BaseException
:
208 ## Parse an XML file.
210 # Parse the input XML file named FileName and return a XML DOM it stands for.
211 # If the input File is not a valid XML file, then an empty string is returned.
213 # @param FileName The XML file name.
215 def XmlParseFile(FileName
):
217 XmlFile
= codecs
.open(FileName
, 'rb')
218 Dom
= xml
.dom
.minidom
.parse(XmlFile
)
221 except BaseException
as XExcept
:
223 Logger
.Error('\nUPT', PARSER_ERROR
, XExcept
, File
=FileName
, RaiseError
=True)