-
Notifications
You must be signed in to change notification settings - Fork 2
/
XSLT.py
executable file
·289 lines (257 loc) · 10.1 KB
/
XSLT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/python
# Copyright (c) 2005, Toby White <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * The name of Toby White may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
# * Any individuals or entities associated with the San Diego
# Supercomputing Centre may not use, modify, or redistribute this code
# in any form without the explicit permission of the copyright holder.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import cgi
import cgitb; cgitb.enable()
import mimetypes
import os
import sys
import tempfile
import time
import urllib
import subProcess
import libfinxml
xsltproc = '/usr/bin/xsltproc'
xsltCommand = xsltproc + ' -o %(output)s %(style)s %(input)s >/dev/null'
defaultStylesheet='http://www.eminerals.org/XSLT/ccViz.xsl'
# Nothing below this line should be altered.
############################################
# CGI environment variables of interest.
thisServer = os.environ['SERVER_NAME']
thisPage = os.environ['SCRIPT_NAME']
if os.environ.has_key('HTTP_ACCEPT'):
httpAccept = os.environ['HTTP_ACCEPT']
else:
httpAccept = 'text/html'
# Check if user is using IE or other dumb browser
if httpAccept.find('application/xhtml+xml') > -1:
contentType = "application/xhtml+xml"
else:
contentType = 'text/html'
contentType = 'text/html'
# Templates for output file.
#########################################
header = """
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
"""
htmlForm = """
<hr/>
<div id="inputForms">
<form method="get" action="%(thisPage)s" enctype="multipart/form-data" onsubmit="submitForm(this.id)" id="listForm">
<table>
<tr><td>XML file address</td><td><input name="xmlIn" value="" type="text"/></td></tr>
<tr><td>XSLT stylesheet</td><td><input name="xsltSheet" value="%(xsltDefault)s" type="text"/>(leave blank for internal)</td></tr>
<tr><td><input type="submit" value="SUBMIT"/></td><td><input value="CLEAR" type="reset"/></td></tr>
</table>
</form>
</div>
<hr/>
<div>
<form method="post" action="%(thisPage)s" enctype="multipart/form-data" onsubmit="submitForm(this.id)" id="putForm">
<table>
<tr><td>XML file to upload</td><td><input name="xmlUp" value="" type="file"/></td></tr>
<tr><td>XSLT stylesheet</td><td><input name="xsltSheet" value="%(xsltDefault)s" type="text"/>(leave blank for internal)</td></tr>
<tr><td><input value="SUBMIT" type="submit"/></td><td><input value="CLEAR" type="reset"/></td></tr>
</table>
</form>
</div>
"""
footer = """
</body>
</html>
"""
# Exception classes.
#########################################
class InputDataError(Exception):
def __init__(self, value):
self.value = value
def __str__ (self):
return self.value
class Page:
def __init__(self, contentType='text/plain', status=200, statusmsg='OK'):
self.headers = {}
self.headers['Content-Type'] = contentType
self.headers['Status'] = str(status)+' '+statusmsg
self.contents = []
def appendHeaders(self, headers):
for key in headers:
self.headers[key] = headers[key]
def append(self, line):
self.contents.append(line)
def write(self, output=sys.stdout):
for key in self.headers:
output.write(key+': '+self.headers[key]+'\n')
output.write('\n')
for line in self.contents:
output.write(line+'\n')
def xmlEncode(string):
tempstring = string.replace("&", '&')
tempstring = tempstring.replace('<', '<')
tempstring = tempstring.replace('"', '"')
tempstring = tempstring.replace("'", ''')
return tempstring
def errorPage(errorCode, errorString, explanation):
page = Page(contentType, errorCode, errorString)
page.append(header % {'title':'ERROR'})
page.append("<p>Error:</p>")
page.append("<p>Status " + str(errorCode) + ' ' + xmlEncode(errorString) + "</p>")
page.append("<p>"+xmlEncode(explanation)+"</p>")
page.append(footer)
return page
def generate_form():
page = Page(contentType)
page.append(header)
page.append(htmlForm % {'xsltDefault':'', 'thisPage':thisPage})
page.append(footer)
return page
def transformXML(inputFile, outputFile, stylesheet="default"):
xmlFile = urllib.urlopen(inputFile)
tfname = os.path.join(tmpDir, 'output.xml')
tf = open(tfname, 'w')
libfinxml.finishXMLFile(xmlFile, tf)
tf.close()
if stylesheet == "default":
sys.stderr.write(xsltCommand % {'xsltproc':xsltproc,
'output':outputFile,
'style':'',
'input':tfname })
process = subProcess.subProcess(xsltCommand %
{'xsltproc':xsltproc,
'output':outputFile,
'style':'',
'input':tfname })
done = process.read(300) # 1 minute time out
if done == 1:
# we timed out.
process.kill()
errdata = 'xsltproc timeout'
else:
# we finished (for better or worse) & don't want to retry.
outdata = process.outdata
errdata = process.errdata
process.cleanup()
if process.sts != 0 or stylesheet != "default":
# we have failed on the document's internal stylesheet, let's try again with the default:
stylesheet = defaultStylesheet
process = subProcess.subProcess(xsltCommand %
{'xsltproc':xsltproc,
'output':outputFile,
'style':stylesheet,
'input':tfname })
done = process.read(300) # 1 minute time out
if done == 1:
# we timed out.
process.kill()
errdata = 'xsltproc timeout'
else:
# we finished (for better or worse) & don't want to retry.
outdata = process.outdata
errdata = process.errdata
process.cleanup()
if process.sts != 0:
raise OSError(errdata)
sys.stderr.write(xsltCommand % {'xsltproc':xsltproc,
'output':outputFile,
'style':stylesheet,
'input':tfname })
sys.stderr.write(outdata)
sys.stderr.write(errdata)
os.unlink(tfname)
os.rmdir(tmpDir)
def isXML(file):
retval = os.system('xmllint '+file)
return (retval == 0)
def main():
if not (os.access(xsltproc, os.X_OK)):
raise EnvironmentError('Bad server configuration - cannot find xsltproc')
form = cgi.FieldStorage(keep_blank_values=True)
if os.environ["REQUEST_METHOD"] == "GET":
try:
xmlDoc = form['xmlIn'].value
except KeyError:
page = generate_form()
return page
elif os.environ["REQUEST_METHOD"] == "POST":
try:
postFile = form['xmlUp']
except KeyError:
page = generate_form()
return page
localFileName = os.path.join(tmpDir, 'postfile')
fUp = open(localFileName, 'wb')
while True:
chunk = postFile.file.read(100000)
if not chunk: break
fUp.write (chunk)
fUp.close()
xmlDoc = localFileName
try:
xsltSheet = form['xsltSheet'].value
except KeyError:
xsltSheet = '' # We will use stylesheet specified in document.
outFile = os.path.join(tmpDir, 'outputFile')
transformXML(xmlDoc, outFile)
#if xsltSheet == '': # then we know we will be getting xhtml
# contentType = 'application/xhtml+xml'
# else:
# unfortunately, 'file -i' - which should give us mimetypes
# seems horribly broken wrt XML files. Therefore we will just
# say text/plain & worry about it later.
# contentType = 'text/plain'
# FIXME: we should do an xmllint to check for XML, and then
# maybe also check for SVG & XHTML, and otherwise fall
# back to file
contentType = 'application/xhtml+xml'
page = Page(contentType)
fOpen = open(outFile)
for line in fOpen:
page.append(line)
return page
# Main program.
#########################################
tmpDir = tempfile.mkdtemp()
try:
page = main()
except OSError, inst:
page= errorPage(400, 'XSLT failure', 'XSLT transform failed')
except InputDataError, inst:
page = errorPage(400, 'Bad Request', 'Could not retrieve document xmlDoc')
# Clear up temporary files - all clean up is done here in one place.
for file in os.listdir(tmpDir):
os.unlink(os.path.join(tmpDir,file))
os.rmdir(tmpDir)
page.write()