Package for XML support for Docutils. Meant to be included in the main
[docutils.git] / sandbox / odf2docutils / odf2docutilslib / parsers / xslt.py
blob4b2799fecfe69e1adf1bd2d95c0db0ef27f4a00f
1 # Copyright (C) 2013 Stefan Merten
3 # This file is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published
5 # by the Free Software Foundation; either version 2 of the License,
6 # or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
16 # 02111-1307, USA.
18 """
19 Parse and produce result string by XSLT.
20 """
22 __docformat__ = 'reStructuredText' # Formatted to be rendered by epydoc
24 ###############################################################################
25 ###############################################################################
26 # Import
28 import docutils.parsers
30 from lxml import etree
32 ###############################################################################
33 ###############################################################################
34 # Constants
36 ###############################################################################
37 ###############################################################################
38 # Variables
40 ###############################################################################
41 ###############################################################################
42 # Functions
44 ###############################################################################
45 ###############################################################################
46 # Classes
48 class XsltParser(docutils.parsers.Parser):
49 """
50 Parses XML input by XSLT and stores the result in the attribute
51 `xslt_result` of the document. Works together with `XsltWriter`.
52 """
54 def __init__(self, xsltPath):
55 """
56 See instance variables for parameter documentation.
57 """
58 self.xsltPath = xsltPath
59 """
60 Path to the XSLT to use.
61 """
62 self.xslt = None
63 """
64 :type: Return type of `etree.XSLT`()
66 The XSLT to use for parsing.
67 """
69 # Find XSLT
70 try:
71 xsltF = open(self.xsltPath)
72 except IOError, e:
73 raise Exception("Can't open main XSLT file %r: %s"
74 % ( self.xsltPath, e, ))
76 # Parse and prepare XSLT
77 try:
78 xsltDoc = etree.parse(xsltF)
79 except Exception, e:
80 raise Exception("Error parsing main XSLT file %r: %s"
81 % ( self.xsltPath, e, ))
82 xsltF.close()
83 self.xslt = etree.XSLT(xsltDoc)
85 def parse(self, inputstring, document):
86 self.setup_parse(inputstring, document)
87 inDoc = etree.fromstring(inputstring)
88 document.xslt_result = self.xslt(inDoc, sourceName="'%s'"
89 % ( document.current_source, ))
90 if self.xslt.error_log:
91 document.reporter.error(self.xslt.error_log)
92 self.finish_parse()