1 | """Test for GML/CSML parsing code |
---|
2 | """ |
---|
3 | |
---|
4 | from cElementTree import * |
---|
5 | from Parser import * |
---|
6 | from xml.dom.minidom import parseString |
---|
7 | import elementtree.ElementTree as etree |
---|
8 | import sys |
---|
9 | |
---|
10 | |
---|
11 | #this is a fix to the ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc. |
---|
12 | etree._namespace_map.update({ |
---|
13 | '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'}) |
---|
14 | |
---|
15 | |
---|
16 | def removeInlineNS(csmlstring): |
---|
17 | # removeInlineNS: function removes "inline" namespaces and declares them as part of the Dataset element. |
---|
18 | #remove all |
---|
19 | csmlstring=csmlstring.replace(' xmlns:csml="http://ndg.nerc.ac.uk/csml"','') |
---|
20 | csmlstring=csmlstring.replace(' xmlns:xlink="http://www.w3.org/1999/xlink"','') |
---|
21 | csmlstring=csmlstring.replace('xmlns:om="http://www.opengis.net/om"','') |
---|
22 | csmlstring=csmlstring.replace('xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd"','') |
---|
23 | csmlstring=csmlstring.replace('xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"','') |
---|
24 | |
---|
25 | #remove cmsl: prefixes |
---|
26 | csmlstring=csmlstring.replace('<csml:','<') |
---|
27 | csmlstring=csmlstring.replace('</csml:','</') |
---|
28 | #add namespace declarations at top of document, after gml namespace declaration |
---|
29 | csmlstring=csmlstring.replace('xmlns:gml="http://www.opengis.net/gml">', 'xmlns:gml="http://www.opengis.net/gml" xmlns:om="http://www.opengis.net/om" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd">') |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | return csmlstring |
---|
37 | ############################################################################## |
---|
38 | # ElementTree-based parser test |
---|
39 | ############################################################################## |
---|
40 | # 1. Test parsing from CSML file |
---|
41 | tree = ElementTree(file='example.xml') |
---|
42 | #tree = ElementTree(file='/home/users/domlowe/CSML/Coapec/Coapec.v2.xml') |
---|
43 | #tree = ElementTree(file='era40gridseries.xml') |
---|
44 | dataset = Dataset() |
---|
45 | dataset.fromXML(tree.getroot()) |
---|
46 | #csml = dataset.toXML(Element('Dataset')) |
---|
47 | csml = dataset.toXML() |
---|
48 | |
---|
49 | strCSML= parseString(tostring(csml)).toprettyxml() |
---|
50 | strCSML=removeInlineNS(strCSML) |
---|
51 | print strCSML |
---|
52 | sys.exit() # stop here. |
---|
53 | |
---|
54 | ############################################################################## |
---|
55 | #2. Test construction of objects from scratch |
---|
56 | |
---|
57 | # AW changes made to DL code: |
---|
58 | # (NB: all these derive from the underlying UML model) |
---|
59 | # * AbstractFileExtract.arraySize is an array [] |
---|
60 | # * Dataset.metaDataProperty is an array [] |
---|
61 | # * Omit empty attributes entirely |
---|
62 | # * Dataset.arrayDescriptors is an array holding all file extract objects |
---|
63 | |
---|
64 | #MetaDataProperty Object |
---|
65 | md = MetaDataProperty() |
---|
66 | setattr(md, 'href', 'http://ndg.nerc.ac.uk/Metadata/home/users/domlowe/CSML/demodata') |
---|
67 | |
---|
68 | #An empty list to hold a collection of NASAAmesExtracts: |
---|
69 | nalist=[] |
---|
70 | |
---|
71 | #Create a NASAAmesExtract |
---|
72 | na1 = NASAAmesExtract() |
---|
73 | #set attributes: |
---|
74 | setattr(na1, 'id', 'naextract_one') |
---|
75 | setattr(na1, 'variableName', 'temp') |
---|
76 | setattr(na1, 'index', '1') |
---|
77 | setattr(na1, 'fileName', '/blah/testfile.na') |
---|
78 | setattr(na1, 'arraySize', [50]) |
---|
79 | setattr(na1, 'uom', 'celsius') |
---|
80 | #setattr(na1, 'numericType', None) |
---|
81 | #setattr(na1, 'numericTransform', None) |
---|
82 | #setattr(na1,'regExpTransform',None) |
---|
83 | #append NASAAmesExtract to list |
---|
84 | nalist.append(na1) |
---|
85 | |
---|
86 | #Create a second NASAAmesExtract |
---|
87 | na2 = NASAAmesExtract() |
---|
88 | #set attributes: |
---|
89 | setattr(na2, 'id', 'naextract_two') |
---|
90 | setattr(na2, 'variableName', 'pressure') |
---|
91 | setattr(na2, 'index', '1') |
---|
92 | setattr(na2, 'fileName', '/blah/testfile2.na') |
---|
93 | setattr(na2, 'arraySize', [50]) |
---|
94 | setattr(na2, 'uom', 'mb') |
---|
95 | #setattr(na2, 'numericType', None) |
---|
96 | #setattr(na2, 'numericTransform', None) |
---|
97 | #setattr(na2,'regExpTransform',None) |
---|
98 | #append second NASAAmesExtract to list |
---|
99 | nalist.append(na2) |
---|
100 | |
---|
101 | #An empty list to hold a collection of NetCDFExtracts: |
---|
102 | nclist=[] |
---|
103 | |
---|
104 | #Create a NetCDFExtract |
---|
105 | nc1 = NetCDFExtract() |
---|
106 | #set attributes: |
---|
107 | setattr(nc1, 'id', 'ncextract_one') |
---|
108 | setattr(nc1, 'variableName', 'temp2') |
---|
109 | setattr(nc1, 'fileName', '/blah/testfile.nc') |
---|
110 | setattr(nc1, 'arraySize', [100]) |
---|
111 | setattr(nc1, 'uom', 'celsius') |
---|
112 | #setattr(nc1, 'numericType', None) |
---|
113 | #setattr(nc1, 'numericTransform', None) |
---|
114 | #setattr(nc1,'regExpTransform',None) |
---|
115 | #append NetCDFExtract to list |
---|
116 | nclist.append(nc1) |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | #Create an Empty Dataset |
---|
121 | ds = Dataset() |
---|
122 | #Set objects as attributes of dataset |
---|
123 | setattr(ds, 'metaDataProperty', [md]) |
---|
124 | setattr(ds, 'arrayDescriptors', nalist+nclist) |
---|
125 | |
---|
126 | |
---|
127 | csml = ds.toXML() |
---|
128 | print parseString(tostring(csml)).toprettyxml() |
---|