source: TI02-CSML/trunk/newparser/parseTest.py @ 880

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/newparser/parseTest.py@880
Revision 880, 3.0 KB checked in by domlowe, 13 years ago (diff)

Python CGI script for test parsing see  http://glue.badc.rl.ac.uk/cgi-bin/csml/parseTest.py

Line 
1#!/usr/local/NDG/ActivePython-2.4/bin/python2.4
2
3import cgi
4from cElementTree import *
5from Parser import *
6from xml.dom.minidom import parseString
7import sys
8import parser_extra
9import elementtree.ElementTree as etree
10import traceback
11"""Test for GML/CSML parsing code
12"""
13
14def main():
15    sys.stderr = sys.stdout
16    print "Content-type: text/html"
17    print 
18    print "<title>CSML Test Parser</title>"
19    print "<h1>CSML Test Parser</h1>"
20    print "<p>Please note this is experimental, any questions please contact d.lowe@rl.ac.uk</p>"
21    print "<h2>Load a CSML file to test:</h2>"
22   
23    form = cgi.FieldStorage()
24    if not form:
25        print """<form action="http://glue.badc.rl.ac.uk/cgi-bin/csml/parseTest.py" method="POST" enctype="multipart/form-data">
26        <input type="file" name="filename">
27        <input type="submit">
28        </form>"""
29    elif form.has_key("filename"):
30        item = form["filename"]
31        if item.file:
32            data = item.file.read()
33            #print cgi.escape(data)
34            #data1 = cgi.escape(data)
35            f = open("/tmp/tempcsml.xml","wb")
36            f.write(data)
37            f.close()
38           
39            #NOW TRY AND PARSE THE FILE
40            #this is a fix to the  ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc.
41            etree._namespace_map.update({
42                    'http://www.opengis.net/om': 'om',  'http://www.opengis.net/gml': 'gml','http://ndg.nerc.ac.uk/csml' : 'csml', 'http://www.w3.org/1999/xlink':'xlink'})
43           
44           
45            #######################################################
46            # ElementTree-based parser test ROUND TRIP
47            #######################################################
48            # 1. Test parsing from CSML file
49            try:
50                tree = ElementTree(file='/tmp/tempcsml.xml')
51                #tree = ElementTree(file='example.xml')
52            except:
53                traceback.print_exc()
54                print "<p>Could not parse CSML</p>"
55                sys.exit()
56            #tree = ElementTree(file='era40gridseries.xml')
57            dataset = Dataset()
58           
59            #Calling the fromXML method reads the CSML into memory.
60            try:
61                dataset.fromXML(tree.getroot())
62            except:
63                traceback.print_exc()
64                print "<p>Could not parse CSML</p>"
65                sys.exit()
66           
67
68           
69            #This creates a new CSML document string from the CSML objects in memory.
70
71            try:
72                csml = dataset.toXML()
73            except:
74                traceback.print_exc()
75                print "<p>Could not parse CSML</p>"
76                sys.exit()
77           
78            strCSML=parser_extra.PrettyPrint(csml)
79            strCSML=parser_extra.removeInlineNS(strCSML)
80            print strCSML
81            print "<h2>CSML Parsed OK - Note, this does not automatically mean it is valid CSML.. this is not a CSML validator..</h2>"
82   
83main()
84
Note: See TracBrowser for help on using the repository browser.