]>
Commit | Line | Data |
---|---|---|
4234283c LG |
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, 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 | from Logger.ToolError import PARSER_ERROR\r | |
26 | import Logger.Log as Logger\r | |
27 | \r | |
28 | ## Create a element of XML\r | |
29 | #\r | |
30 | # @param Name\r | |
31 | # @param String\r | |
32 | # @param NodeList\r | |
33 | # @param AttributeList\r | |
34 | #\r | |
35 | def CreateXmlElement(Name, String, NodeList, AttributeList):\r | |
36 | Doc = xml.dom.minidom.Document()\r | |
37 | Element = Doc.createElement(Name)\r | |
38 | if String != '' and String != None:\r | |
39 | Element.appendChild(Doc.createTextNode(String))\r | |
40 | \r | |
41 | for Item in NodeList:\r | |
42 | if type(Item) == type([]):\r | |
43 | Key = Item[0]\r | |
44 | Value = Item[1]\r | |
45 | if Key != '' and Key != None and Value != '' and Value != None:\r | |
46 | Node = Doc.createElement(Key)\r | |
47 | Node.appendChild(Doc.createTextNode(Value))\r | |
48 | Element.appendChild(Node)\r | |
49 | else:\r | |
50 | Element.appendChild(Item)\r | |
51 | for Item in AttributeList:\r | |
52 | Key = Item[0]\r | |
53 | Value = Item[1]\r | |
54 | if Key != '' and Key != None and Value != '' and Value != None:\r | |
55 | Element.setAttribute(Key, Value)\r | |
56 | \r | |
57 | return Element\r | |
58 | \r | |
59 | ## Get a list of XML nodes using XPath style syntax.\r | |
60 | #\r | |
61 | # Return a list of XML DOM nodes from the root Dom specified by XPath String.\r | |
62 | # If the input Dom or String is not valid, then an empty list is returned.\r | |
63 | #\r | |
64 | # @param Dom The root XML DOM node.\r | |
65 | # @param String A XPath style path.\r | |
66 | #\r | |
67 | def XmlList(Dom, String):\r | |
68 | if String == None or String == "" or Dom == None or Dom == "":\r | |
69 | return []\r | |
70 | if Dom.nodeType == Dom.DOCUMENT_NODE:\r | |
71 | Dom = Dom.documentElement\r | |
72 | if String[0] == "/":\r | |
73 | String = String[1:]\r | |
74 | TagList = String.split('/')\r | |
75 | Nodes = [Dom]\r | |
76 | Index = 0\r | |
77 | End = len(TagList) - 1\r | |
78 | while Index <= End:\r | |
79 | ChildNodes = []\r | |
80 | for Node in Nodes:\r | |
81 | if Node.nodeType == Node.ELEMENT_NODE and Node.tagName == \\r | |
82 | TagList[Index]:\r | |
83 | if Index < End:\r | |
84 | ChildNodes.extend(Node.childNodes)\r | |
85 | else:\r | |
86 | ChildNodes.append(Node)\r | |
87 | Nodes = ChildNodes\r | |
88 | ChildNodes = []\r | |
89 | Index += 1\r | |
90 | \r | |
91 | return Nodes\r | |
92 | \r | |
93 | \r | |
94 | ## Get a single XML node using XPath style syntax.\r | |
95 | #\r | |
96 | # Return a single XML DOM node from the root Dom specified by XPath String.\r | |
97 | # If the input Dom or String is not valid, then an empty string is returned.\r | |
98 | #\r | |
99 | # @param Dom The root XML DOM node.\r | |
100 | # @param String A XPath style path.\r | |
101 | #\r | |
102 | def XmlNode(Dom, String):\r | |
103 | if String == None or String == "" or Dom == None or Dom == "":\r | |
104 | return None\r | |
105 | if Dom.nodeType == Dom.DOCUMENT_NODE:\r | |
106 | Dom = Dom.documentElement\r | |
107 | if String[0] == "/":\r | |
108 | String = String[1:]\r | |
109 | TagList = String.split('/')\r | |
110 | Index = 0\r | |
111 | End = len(TagList) - 1\r | |
112 | ChildNodes = [Dom]\r | |
113 | while Index <= End:\r | |
114 | for Node in ChildNodes:\r | |
115 | if Node.nodeType == Node.ELEMENT_NODE and \\r | |
116 | Node.tagName == TagList[Index]:\r | |
117 | if Index < End:\r | |
118 | ChildNodes = Node.childNodes\r | |
119 | else:\r | |
120 | return Node\r | |
121 | break\r | |
122 | Index += 1\r | |
123 | return None\r | |
124 | \r | |
125 | \r | |
126 | ## Get a single XML element using XPath style syntax.\r | |
127 | #\r | |
128 | # Return a single XML element from the root Dom specified by XPath String.\r | |
129 | # If the input Dom or String is not valid, then an empty string is returned.\r | |
130 | #\r | |
131 | # @param Dom The root XML DOM object.\r | |
132 | # @param Strin A XPath style path.\r | |
133 | #\r | |
134 | def XmlElement(Dom, String):\r | |
135 | try:\r | |
136 | return XmlNode(Dom, String).firstChild.data.strip()\r | |
137 | except BaseException:\r | |
138 | return ""\r | |
139 | \r | |
140 | ## Get a single XML element using XPath style syntax.\r | |
141 | #\r | |
142 | # Similar with XmlElement, but do not strip all the leading and tailing space\r | |
143 | # and newline, instead just remove the newline and spaces introduced by \r | |
144 | # toprettyxml() \r | |
145 | #\r | |
146 | # @param Dom The root XML DOM object.\r | |
147 | # @param Strin A XPath style path.\r | |
148 | #\r | |
149 | def XmlElement2(Dom, String):\r | |
150 | try:\r | |
151 | HelpStr = XmlNode(Dom, String).firstChild.data\r | |
152 | gRemovePrettyRe = re.compile(r"""(?:(\n *) )(.*)\1""", re.DOTALL)\r | |
153 | HelpStr = re.sub(gRemovePrettyRe, r"\2", HelpStr)\r | |
154 | return HelpStr\r | |
155 | except BaseException:\r | |
156 | return ""\r | |
157 | \r | |
158 | \r | |
159 | ## Get a single XML element of the current node.\r | |
160 | #\r | |
161 | # Return a single XML element specified by the current root Dom.\r | |
162 | # If the input Dom is not valid, then an empty string is returned.\r | |
163 | #\r | |
164 | # @param Dom The root XML DOM object.\r | |
165 | #\r | |
166 | def XmlElementData(Dom):\r | |
167 | try:\r | |
168 | return Dom.firstChild.data.strip()\r | |
169 | except BaseException:\r | |
170 | return ""\r | |
171 | \r | |
172 | \r | |
173 | ## Get a list of XML elements using XPath style syntax.\r | |
174 | #\r | |
175 | # Return a list of XML elements from the root Dom specified by XPath String.\r | |
176 | # If the input Dom or String is not valid, then an empty list is returned.\r | |
177 | #\r | |
178 | # @param Dom The root XML DOM object.\r | |
179 | # @param String A XPath style path.\r | |
180 | #\r | |
181 | def XmlElementList(Dom, String):\r | |
182 | return map(XmlElementData, XmlList(Dom, String))\r | |
183 | \r | |
184 | \r | |
185 | ## Get the XML attribute of the current node.\r | |
186 | #\r | |
187 | # Return a single XML attribute named Attribute from the current root Dom.\r | |
188 | # If the input Dom or Attribute is not valid, then an empty string is returned.\r | |
189 | #\r | |
190 | # @param Dom The root XML DOM object.\r | |
191 | # @param Attribute The name of Attribute.\r | |
192 | #\r | |
193 | def XmlAttribute(Dom, Attribute):\r | |
194 | try:\r | |
195 | return Dom.getAttribute(Attribute)\r | |
196 | except BaseException:\r | |
197 | return ''\r | |
198 | \r | |
199 | \r | |
200 | ## Get the XML node name of the current node.\r | |
201 | #\r | |
202 | # Return a single XML node name from the current root Dom.\r | |
203 | # If the input Dom is not valid, then an empty string is returned.\r | |
204 | #\r | |
205 | # @param Dom The root XML DOM object.\r | |
206 | #\r | |
207 | def XmlNodeName(Dom):\r | |
208 | try:\r | |
209 | return Dom.nodeName.strip()\r | |
210 | except BaseException:\r | |
211 | return ''\r | |
212 | \r | |
213 | ## Parse an XML file.\r | |
214 | #\r | |
215 | # Parse the input XML file named FileName and return a XML DOM it stands for.\r | |
216 | # If the input File is not a valid XML file, then an empty string is returned.\r | |
217 | #\r | |
218 | # @param FileName The XML file name.\r | |
219 | #\r | |
220 | def XmlParseFile(FileName):\r | |
221 | try:\r | |
222 | XmlFile = open(FileName)\r | |
223 | Dom = xml.dom.minidom.parse(XmlFile)\r | |
224 | XmlFile.close()\r | |
225 | return Dom\r | |
226 | except BaseException, XExcept:\r | |
227 | XmlFile.close()\r | |
228 | Logger.Error('\nUPT', PARSER_ERROR, XExcept, File=FileName, RaiseError=True)\r |