]>
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 # This program and the accompanying materials are licensed and made available
8 # under the terms and conditions of the BSD License which accompanies this
9 # distribution. The full text of the license may be found at
10 # http://opensource.org/licenses/bsd-license.php
12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 import xml
.dom
.minidom
26 from Logger
.ToolError
import PARSER_ERROR
27 import Logger
.Log
as Logger
29 ## Create a element of XML
34 # @param AttributeList
36 def CreateXmlElement(Name
, String
, NodeList
, AttributeList
):
37 Doc
= xml
.dom
.minidom
.Document()
38 Element
= Doc
.createElement(Name
)
39 if String
!= '' and String
is not None:
40 Element
.appendChild(Doc
.createTextNode(String
))
43 if isinstance(Item
, type([])):
46 if Key
!= '' and Key
is not None and Value
!= '' and Value
is not None:
47 Node
= Doc
.createElement(Key
)
48 Node
.appendChild(Doc
.createTextNode(Value
))
49 Element
.appendChild(Node
)
51 Element
.appendChild(Item
)
52 for Item
in AttributeList
:
55 if Key
!= '' and Key
is not None and Value
!= '' and Value
is not None:
56 Element
.setAttribute(Key
, Value
)
60 ## Get a list of XML nodes using XPath style syntax.
62 # Return a list of XML DOM nodes from the root Dom specified by XPath String.
63 # If the input Dom or String is not valid, then an empty list is returned.
65 # @param Dom The root XML DOM node.
66 # @param String A XPath style path.
68 def XmlList(Dom
, String
):
69 if String
is None or String
== "" or Dom
is None or Dom
== "":
71 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
72 Dom
= Dom
.documentElement
75 TagList
= String
.split('/')
78 End
= len(TagList
) - 1
82 if Node
.nodeType
== Node
.ELEMENT_NODE
and Node
.tagName
== \
85 ChildNodes
.extend(Node
.childNodes
)
87 ChildNodes
.append(Node
)
95 ## Get a single XML node using XPath style syntax.
97 # Return a single XML DOM node from the root Dom specified by XPath String.
98 # If the input Dom or String is not valid, then an empty string is returned.
100 # @param Dom The root XML DOM node.
101 # @param String A XPath style path.
103 def XmlNode(Dom
, String
):
104 if String
is None or String
== "" or Dom
is None or Dom
== "":
106 if Dom
.nodeType
== Dom
.DOCUMENT_NODE
:
107 Dom
= Dom
.documentElement
110 TagList
= String
.split('/')
112 End
= len(TagList
) - 1
115 for Node
in ChildNodes
:
116 if Node
.nodeType
== Node
.ELEMENT_NODE
and \
117 Node
.tagName
== TagList
[Index
]:
119 ChildNodes
= Node
.childNodes
127 ## Get a single XML element using XPath style syntax.
129 # Return a single XML element from the root Dom specified by XPath String.
130 # If the input Dom or String is not valid, then an empty string is returned.
132 # @param Dom The root XML DOM object.
133 # @param Strin A XPath style path.
135 def XmlElement(Dom
, String
):
137 return XmlNode(Dom
, String
).firstChild
.data
.strip()
138 except BaseException
:
141 ## Get a single XML element using XPath style syntax.
143 # Similar with XmlElement, but do not strip all the leading and tailing space
144 # and newline, instead just remove the newline and spaces introduced by
147 # @param Dom The root XML DOM object.
148 # @param Strin A XPath style path.
150 def XmlElement2(Dom
, String
):
152 HelpStr
= XmlNode(Dom
, String
).firstChild
.data
153 gRemovePrettyRe
= re
.compile(r
"""(?:(\n *) )(.*)\1""", re
.DOTALL
)
154 HelpStr
= re
.sub(gRemovePrettyRe
, r
"\2", HelpStr
)
156 except BaseException
:
160 ## Get a single XML element of the current node.
162 # Return a single XML element specified by the current root Dom.
163 # If the input Dom is not valid, then an empty string is returned.
165 # @param Dom The root XML DOM object.
167 def XmlElementData(Dom
):
169 return Dom
.firstChild
.data
.strip()
170 except BaseException
:
174 ## Get a list of XML elements using XPath style syntax.
176 # Return a list of XML elements from the root Dom specified by XPath String.
177 # If the input Dom or String is not valid, then an empty list is returned.
179 # @param Dom The root XML DOM object.
180 # @param String A XPath style path.
182 def XmlElementList(Dom
, String
):
183 return map(XmlElementData
, XmlList(Dom
, String
))
186 ## Get the XML attribute of the current node.
188 # Return a single XML attribute named Attribute from the current root Dom.
189 # If the input Dom or Attribute is not valid, then an empty string is returned.
191 # @param Dom The root XML DOM object.
192 # @param Attribute The name of Attribute.
194 def XmlAttribute(Dom
, Attribute
):
196 return Dom
.getAttribute(Attribute
)
197 except BaseException
:
201 ## Get the XML node name of the current node.
203 # Return a single XML node name from the current root Dom.
204 # If the input Dom is not valid, then an empty string is returned.
206 # @param Dom The root XML DOM object.
208 def XmlNodeName(Dom
):
210 return Dom
.nodeName
.strip()
211 except BaseException
:
214 ## Parse an XML file.
216 # Parse the input XML file named FileName and return a XML DOM it stands for.
217 # If the input File is not a valid XML file, then an empty string is returned.
219 # @param FileName The XML file name.
221 def XmlParseFile(FileName
):
223 XmlFile
= codecs
.open(FileName
, 'rb')
224 Dom
= xml
.dom
.minidom
.parse(XmlFile
)
227 except BaseException
as XExcept
:
229 Logger
.Error('\nUPT', PARSER_ERROR
, XExcept
, File
=FileName
, RaiseError
=True)