summaryrefslogtreecommitdiffstats
path: root/rteval/xmlout.py
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2013-03-13 19:56:22 +0100
committerDavid Sommerseth <davids@redhat.com>2013-03-13 19:56:22 +0100
commit99253169b347346a96cc08d767f04dea615db79d (patch)
treebbf4ecc2bf54f234f27013e121f61ff48dc909d7 /rteval/xmlout.py
parent27783aa01e41bce0e2b7b6e3db5d4b453f438ffe (diff)
downloadrteval-99253169b347346a96cc08d767f04dea615db79d.tar.gz
rteval-99253169b347346a96cc08d767f04dea615db79d.tar.xz
rteval-99253169b347346a96cc08d767f04dea615db79d.zip
Migrated from libxslt to lxml
To avoid depending on libxslt-python, use the more standard Python lxml module for XSLT processing. Signed-off-by: David Sommerseth <davids@redhat.com>
Diffstat (limited to 'rteval/xmlout.py')
-rw-r--r--rteval/xmlout.py49
1 files changed, 40 insertions, 9 deletions
diff --git a/rteval/xmlout.py b/rteval/xmlout.py
index 454a452..ddc2964 100644
--- a/rteval/xmlout.py
+++ b/rteval/xmlout.py
@@ -26,11 +26,35 @@
import os
import sys
import libxml2
-import libxslt
+import lxml.etree
import codecs
import re
from string import maketrans
+
+def convert_libxml2_to_lxml_doc(inxml):
+ "Converts a libxml2.xmlDoc into a lxml.etree document object"
+
+ if not isinstance(inxml, libxml2.xmlDoc):
+ raise TypeError('Function requires an libxml2.xmlDoc as input')
+
+ root = inxml.getRootElement()
+ ret = lxml.etree.XML(root.serialize('UTF-8'))
+ del root
+ return ret
+
+
+
+def convert_lxml_to_libxml2_nodes(inlxml):
+ "Converts a lxml.etree elements tree into a libxml2.xmlNode object"
+
+ if not isinstance(inlxml,lxml.etree._Element) and not isinstance(inlxml, lxml.etree._XSLTResultTree):
+ raise TypeError('Function requires an lxml.etree object as input')
+
+ return libxml2.parseDoc(lxml.etree.tostring(inlxml)).getRootElement()
+
+
+
class XMLOut(object):
'''Class to create XML output'''
def __init__(self, roottag, version, attr = None, encoding='UTF-8'):
@@ -171,6 +195,7 @@ class XMLOut(object):
self.status = 2 # Confirm that we have loaded a report from file
+
def Write(self, filename, xslt = None):
if self.status != 2 and self.status != 3:
raise RuntimeError, "XMLOut: XML document is not closed"
@@ -181,8 +206,10 @@ class XMLOut(object):
return
else:
# Load XSLT file and prepare the XSLT parser
- xsltdoc = libxml2.parseFile(xslt)
- parser = libxslt.parseStylesheetDoc(xsltdoc)
+ xsltfile = open(xslt, 'r')
+ xsltdoc = lxml.etree.parse(xsltfile)
+ parser = lxml.etree.XSLT(xsltdoc)
+ xsltfile.close()
# imitate libxml2's filename interpretation
if filename != "-":
@@ -192,18 +219,22 @@ class XMLOut(object):
#
# Parse XML+XSLT and write the result to file
#
- resdoc = parser.applyStylesheet(self.xmldoc, None)
- # Decode the result string according to the charset declared in the XSLT file
- xsltres = parser.saveResultToString(resdoc).decode(parser.encoding())
+ xmldoc = convert_libxml2_to_lxml_doc(self.xmldoc)
+ resdoc = parser(xmldoc)
+
# Write the file with the requested output encoding
- dstfile.write(xsltres.encode(self.encoding))
+ dstfile.write(unicode(resdoc).encode(self.encoding))
if dstfile != sys.stdout:
dstfile.close()
# Clean up
- resdoc.freeDoc()
- xsltdoc.freeDoc()
+ del resdoc
+ del xsltdoc
+ del parser
+ del xsltfile
+ del xmldoc
+
def GetXMLdocument(self):
if self.status != 2 and self.status != 3: