#!/usr/local/Python-2.4/bin/python
#!/usr/bin/env python
"""
CGI interface to xsltproc

Share and Enjoy.
"""
import cgi, sys, os, urllib
from urlparse import urlparse, urlunparse

XSLTPROC='/usr/bin/xsltproc'

Page = """
<!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" xml:lang="en-US">
<head><title>xsltproc service</title>
<link rel="stylesheet" href="http://www.w3.org/StyleSheets/base" />
</head>
<body>

<h1>Process your XSLT</h1>
"""
Page2 = """
<form method="GET">
<p>Address of XSL: <input name="xslAddr" value="%s"/></p>
<p>Address of document: <input name="docAddr" value="%s"/></p>
<p><input type="submit" value="get xsl results"/></p>
</form>
</body>
</html>
"""

def serveRequest():
    fields = cgi.FieldStorage()

    if not fields.has_key('docAddr'):
        print "Content-Type: text/html"
        print
        print Page
        print Page2 % ("", "")
    else:
        docAddr = urlunparse(urlparse(fields['docAddr'].value))
        xslAddr = urlunparse(urlparse(fields['xslAddr'].value))

        print "Content-Type: text/xml"
        print ""
        print ""
        sys.stdout.flush()
        os.system(" ".join((XSLTPROC, xslAddr, docAddr)))

if __name__ == '__main__':
    if os.environ.has_key('SCRIPT_NAME'):
        serveRequest()
