]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/UPT/Library/Xml/XmlRoutines.py
Sync BaseTools Branch (version r2271) to EDKII main trunk.
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Library / Xml / XmlRoutines.py
diff --git a/BaseTools/Source/Python/UPT/Library/Xml/XmlRoutines.py b/BaseTools/Source/Python/UPT/Library/Xml/XmlRoutines.py
new file mode 100644 (file)
index 0000000..7029e59
--- /dev/null
@@ -0,0 +1,228 @@
+## @file\r
+# This is an XML API that uses a syntax similar to XPath, but it is written in\r
+# standard python so that no extra python packages are required to use it.\r
+#\r
+# Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+#\r
+# This program and the accompanying materials are licensed and made available \r
+# under the terms and conditions of the BSD License which accompanies this \r
+# distribution. The full text of the license may be found at \r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+\r
+'''\r
+XmlRoutines\r
+'''\r
+\r
+##\r
+# Import Modules\r
+#\r
+import xml.dom.minidom\r
+import re\r
+from Logger.ToolError import PARSER_ERROR\r
+import Logger.Log as Logger\r
+\r
+## Create a element of XML\r
+#\r
+# @param Name\r
+# @param String\r
+# @param NodeList\r
+# @param AttributeList\r
+#\r
+def CreateXmlElement(Name, String, NodeList, AttributeList):\r
+    Doc = xml.dom.minidom.Document()\r
+    Element = Doc.createElement(Name)\r
+    if String != '' and String != None:\r
+        Element.appendChild(Doc.createTextNode(String))\r
+\r
+    for Item in NodeList:\r
+        if type(Item) == type([]):\r
+            Key = Item[0]\r
+            Value = Item[1]\r
+            if Key != '' and Key != None and Value != '' and Value != None:\r
+                Node = Doc.createElement(Key)\r
+                Node.appendChild(Doc.createTextNode(Value))\r
+                Element.appendChild(Node)\r
+        else:\r
+            Element.appendChild(Item)\r
+    for Item in AttributeList:\r
+        Key = Item[0]\r
+        Value = Item[1]\r
+        if Key != '' and Key != None and Value != '' and Value != None:\r
+            Element.setAttribute(Key, Value)\r
+\r
+    return Element\r
+\r
+## Get a list of XML nodes using XPath style syntax.\r
+#\r
+# Return a list of XML DOM nodes from the root Dom specified by XPath String.\r
+# If the input Dom or String is not valid, then an empty list is returned.\r
+#\r
+# @param  Dom                The root XML DOM node.\r
+# @param  String             A XPath style path.\r
+#\r
+def XmlList(Dom, String):\r
+    if String == None or String == "" or Dom == None or Dom == "":\r
+        return []\r
+    if Dom.nodeType == Dom.DOCUMENT_NODE:\r
+        Dom = Dom.documentElement\r
+    if String[0] == "/":\r
+        String = String[1:]\r
+    TagList = String.split('/')\r
+    Nodes = [Dom]\r
+    Index = 0\r
+    End = len(TagList) - 1\r
+    while Index <= End:\r
+        ChildNodes = []\r
+        for Node in Nodes:\r
+            if Node.nodeType == Node.ELEMENT_NODE and Node.tagName == \\r
+            TagList[Index]:\r
+                if Index < End:\r
+                    ChildNodes.extend(Node.childNodes)\r
+                else:\r
+                    ChildNodes.append(Node)\r
+        Nodes = ChildNodes\r
+        ChildNodes = []\r
+        Index += 1\r
+\r
+    return Nodes\r
+\r
+\r
+## Get a single XML node using XPath style syntax.\r
+#\r
+# Return a single XML DOM node from the root Dom specified by XPath String.\r
+# If the input Dom or String is not valid, then an empty string is returned.\r
+#\r
+# @param  Dom                The root XML DOM node.\r
+# @param  String             A XPath style path.\r
+#\r
+def XmlNode(Dom, String):\r
+    if String == None or String == ""  or Dom == None or Dom == "":\r
+        return None\r
+    if Dom.nodeType == Dom.DOCUMENT_NODE:\r
+        Dom = Dom.documentElement\r
+    if String[0] == "/":\r
+        String = String[1:]\r
+    TagList = String.split('/')\r
+    Index = 0\r
+    End = len(TagList) - 1\r
+    ChildNodes = [Dom]\r
+    while Index <= End:\r
+        for Node in ChildNodes:\r
+            if Node.nodeType == Node.ELEMENT_NODE and \\r
+               Node.tagName == TagList[Index]:\r
+                if Index < End:\r
+                    ChildNodes = Node.childNodes\r
+                else:\r
+                    return Node\r
+                break\r
+        Index += 1\r
+    return None\r
+\r
+\r
+## Get a single XML element using XPath style syntax.\r
+#\r
+# Return a single XML element from the root Dom specified by XPath String.\r
+# If the input Dom or String is not valid, then an empty string is returned.\r
+#\r
+# @param  Dom                The root XML DOM object.\r
+# @param  Strin              A XPath style path.\r
+#\r
+def XmlElement(Dom, String):\r
+    try:\r
+        return XmlNode(Dom, String).firstChild.data.strip()\r
+    except BaseException:\r
+        return ""\r
+\r
+## Get a single XML element using XPath style syntax.\r
+#\r
+# Similar with XmlElement, but do not strip all the leading and tailing space\r
+# and newline, instead just remove the newline and spaces introduced by \r
+# toprettyxml() \r
+#\r
+# @param  Dom                The root XML DOM object.\r
+# @param  Strin              A XPath style path.\r
+#\r
+def XmlElement2(Dom, String):\r
+    try:\r
+        HelpStr = XmlNode(Dom, String).firstChild.data\r
+        gRemovePrettyRe = re.compile(r"""(?:(\n *)  )(.*)\1""", re.DOTALL)\r
+        HelpStr = re.sub(gRemovePrettyRe, r"\2", HelpStr)\r
+        return HelpStr\r
+    except BaseException:\r
+        return ""\r
+\r
+\r
+## Get a single XML element of the current node.\r
+#\r
+# Return a single XML element specified by the current root Dom.\r
+# If the input Dom is not valid, then an empty string is returned.\r
+#\r
+# @param  Dom                The root XML DOM object.\r
+#\r
+def XmlElementData(Dom):\r
+    try:\r
+        return Dom.firstChild.data.strip()\r
+    except BaseException:\r
+        return ""\r
+\r
+\r
+## Get a list of XML elements using XPath style syntax.\r
+#\r
+# Return a list of XML elements from the root Dom specified by XPath String.\r
+# If the input Dom or String is not valid, then an empty list is returned.\r
+#\r
+# @param  Dom                The root XML DOM object.\r
+# @param  String             A XPath style path.\r
+#\r
+def XmlElementList(Dom, String):\r
+    return map(XmlElementData, XmlList(Dom, String))\r
+\r
+\r
+## Get the XML attribute of the current node.\r
+#\r
+# Return a single XML attribute named Attribute from the current root Dom.\r
+# If the input Dom or Attribute is not valid, then an empty string is returned.\r
+#\r
+# @param  Dom                The root XML DOM object.\r
+# @param  Attribute          The name of Attribute.\r
+#\r
+def XmlAttribute(Dom, Attribute):\r
+    try:\r
+        return Dom.getAttribute(Attribute)\r
+    except BaseException:\r
+        return ''\r
+\r
+\r
+## Get the XML node name of the current node.\r
+#\r
+# Return a single XML node name from the current root Dom.\r
+# If the input Dom is not valid, then an empty string is returned.\r
+#\r
+# @param  Dom                The root XML DOM object.\r
+#\r
+def XmlNodeName(Dom):\r
+    try:\r
+        return Dom.nodeName.strip()\r
+    except BaseException:\r
+        return ''\r
+\r
+## Parse an XML file.\r
+#\r
+# Parse the input XML file named FileName and return a XML DOM it stands for.\r
+# If the input File is not a valid XML file, then an empty string is returned.\r
+#\r
+# @param  FileName           The XML file name.\r
+#\r
+def XmlParseFile(FileName):\r
+    try:\r
+        XmlFile = open(FileName)\r
+        Dom = xml.dom.minidom.parse(XmlFile)\r
+        XmlFile.close()\r
+        return Dom\r
+    except BaseException, XExcept:\r
+        XmlFile.close()\r
+        Logger.Error('\nUPT', PARSER_ERROR, XExcept, File=FileName, RaiseError=True)\r