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