]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Python/XmlRoutines.py
1. Fix EDKT413: EnumerationData.java should use defined final static string
[mirror_edk2.git] / Tools / Python / XmlRoutines.py
CommitLineData
77afa5eb 1#!/usr/bin/env python
2
3# This is an XML API that uses a syntax similar to XPath, but it is written in
4# standard python so that no extra python packages are required to use it.
5
6import xml.dom.minidom
7
8def XmlList(Dom, String):
9 """Get a list of XML Elements using XPath style syntax."""
10 if Dom.nodeType==Dom.DOCUMENT_NODE:
11 return XmlList(Dom.documentElement, String)
12 if String[0] == "/":
13 return XmlList(Dom, String[1:])
14 if String == "" :
15 return []
16 TagList = String.split('/')
17 nodes = []
18 if Dom.nodeType == Dom.ELEMENT_NODE and Dom.tagName.strip() == TagList[0]:
19 if len(TagList) == 1:
20 nodes = [Dom]
21 else:
22 restOfPath = "/".join(TagList[1:])
23 for child in Dom.childNodes:
24 nodes = nodes + XmlList(child, restOfPath)
25 return nodes
26
27def XmlElement (Dom, String):
28 """Return a single element that matches the String which is XPath style syntax."""
29 try:
8d4243f1 30 return XmlList (Dom, String)[0].firstChild.data.strip()
77afa5eb 31 except:
32 return ''
33
34def XmlElementData (Dom):
35 """Get the text for this element."""
8d4243f1 36 return Dom.firstChild.data.strip()
77afa5eb 37
38def XmlAttribute (Dom, String):
39 """Return a single attribute that named by String."""
40 try:
41 return Dom.getAttribute(String)
42 except:
43 return ''
44
e853a9d4 45def XmlTopTag(Dom):
46 """Return the name of the Root or top tag in the XML tree."""
8d4243f1 47 return Dom.firstChild.nodeName
48
49def XmlParseFile (FileName):
50 """Parse an XML file into a DOM and return the DOM."""
51 try:
52 f = open(FileName, 'r')
53 Dom = xml.dom.minidom.parse(f)
54 f.close()
55 return Dom
56 except:
57 return xml.dom.minidom.parseString('<empty/>')
58
59def XmlParseFileSection (FileName, Tag):
60 """Parse a section of an XML file into a DOM(Document Object Model) and return the DOM."""
61 try:
62 f = open(FileName, 'r')
63 except:
64 return xml.dom.minidom.parseString('<empty/>')
65 Start = '<' + Tag
66 End = '</' + Tag + '>'
67 File = ''
68 while File.find(Start) < 0 or File.find(End) < 0:
69 Section = f.read(0x1000)
70 if Section == '':
71 break
72 File += Section
73 f.close()
74 if File.find(Start) < 0 or File.find(End) < 0:
75 return xml.dom.minidom.parseString('<empty/>')
76 File = File[File.find(Start):File.find(End)+len(End)]
77 try:
78 return xml.dom.minidom.parseString(File)
79 except:
80 return xml.dom.minidom.parseString('<empty/>')
81
82def XmlSaveFile (Dom, FileName, Encoding='UTF-8'):
83 """Save a DOM(Document Object Model) into an XML file."""
84 try:
85 f = open(FileName, 'w')
86 f.write(Dom.toxml(Encoding).replace('&quot;','"').replace('&gt;','>'))
87 f.close()
88 return True
89 except:
90 return False
91
92def XmlRemoveElement(Node):
93 """Remove an element node from DOM(Document Object Model) tree."""
94 ParentNode = Node.parentNode
95 if ParentNode == None:
96 return False
97 PreviousSibling = Node.previousSibling
98 while PreviousSibling != None and PreviousSibling.nodeType == PreviousSibling.TEXT_NODE and PreviousSibling.data.strip() == '':
99 Temp = PreviousSibling
100 PreviousSibling = PreviousSibling.previousSibling
101 ParentNode.removeChild(Temp)
102 ParentNode.removeChild(Node)
103 return True
104
105def XmlAppendChildElement(ParentNode, TagName, ElementText='', AttributeDictionary = {}):
106 """Add a child element to a DOM(Document Object Model) tree with optional Attributes."""
107 TagName = TagName.strip()
108 if TagName == '':
109 return False
110 Depth = 0
111 Dom = ParentNode
112 while Dom != None and Dom.nodeType != Dom.DOCUMENT_NODE:
113 Dom = Dom.parentNode
114 Depth += 1
115 if Dom == None:
116 return False
117 ParentNode.appendChild(Dom.createTextNode('\n%*s' % (Depth * 2, '')))
118 ElementNode = Dom.createElement(TagName)
119 if ElementText != '':
120 ElementNode.appendChild(Dom.createTextNode(ElementText))
121 for Item in AttributeDictionary:
122 ElementNode.setAttribute(Item, AttributeDictionary[Item])
123 ParentNode.appendChild(ElementNode)
124 return True
e853a9d4 125
126
77afa5eb 127# This acts like the main() function for the script, unless it is 'import'ed into another
128# script.
129if __name__ == '__main__':
130
131 # Nothing to do here. Could do some unit tests.
132 pass