1 | #parser_extra.py |
---|
2 | # DL 12 april 2006 |
---|
3 | #v2 DL 22 November 2006: changed namespaces to reflect v2 schema |
---|
4 | # 'extra' functions to enable parsing. |
---|
5 | #contains: |
---|
6 | #. Namespace fix |
---|
7 | # Function to resolve references in CSML doc. |
---|
8 | |
---|
9 | import csml.parser |
---|
10 | import sys |
---|
11 | import elementtree.ElementTree as etree |
---|
12 | import string |
---|
13 | import cElementTree as ET |
---|
14 | import codecs |
---|
15 | |
---|
16 | |
---|
17 | #some xml/string manipulation functions. may as well go in this file for now: |
---|
18 | |
---|
19 | def encodingParser(file, encoding): |
---|
20 | f = codecs.open(file, "r", encoding) |
---|
21 | p = ET.XMLParser(encoding="utf-8") |
---|
22 | while 1: |
---|
23 | s = f.read(65536) |
---|
24 | if not s: |
---|
25 | break |
---|
26 | p.feed(s.encode("utf-8")) |
---|
27 | return ET.ElementTree(p.close()) |
---|
28 | |
---|
29 | |
---|
30 | def PrettyPrint(elem,indent='',html=0,space=' '): |
---|
31 | '''Lightweight pretty printing of elementTree elements''' |
---|
32 | def estrip(elem): |
---|
33 | ''' Just want to get rid of unwanted whitespace ''' |
---|
34 | if elem is None: |
---|
35 | return '' |
---|
36 | else: |
---|
37 | return elem.strip() |
---|
38 | strAttrib='' |
---|
39 | for att in elem.attrib: |
---|
40 | strAttrib+=' %s="%s"'%(att,elem.attrib[att]) |
---|
41 | result='%s<%s%s>%s'%(indent,elem.tag,strAttrib,estrip(elem.text)) |
---|
42 | children=len(elem) |
---|
43 | if children: |
---|
44 | for item in elem: |
---|
45 | result+='\n'+PrettyPrint(item,indent=indent+space) |
---|
46 | result+='\n%s%s</%s>'%(indent,estrip(item.tail),elem.tag) |
---|
47 | else: |
---|
48 | result+='</%s>'%(elem.tag) |
---|
49 | return result |
---|
50 | |
---|
51 | # fixing up namespaces: |
---|
52 | def removeInlineNS(csmlstring): |
---|
53 | # removeInlineNS: function removes "inline" namespaces and declares them as part of the Dataset element. |
---|
54 | #TODO: This whole thing is hardcoded and needs reviewing. |
---|
55 | |
---|
56 | #first need to update the ElementTree namespace map: |
---|
57 | etree._namespace_map.update({ |
---|
58 | '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'}) |
---|
59 | |
---|
60 | #replace any fully qualified namespaces |
---|
61 | csmlstring=csmlstring.replace('{http://www.opengis.net/gml}', 'gml:') |
---|
62 | csmlstring=csmlstring.replace('{http://ndg.nerc.ac.uk/csml}','') |
---|
63 | csmlstring=csmlstring.replace('{http://www.w3.org/1999/xlink}','xlink:') |
---|
64 | csmlstring=csmlstring.replace('{http://www.opengis.net/om}','om:') |
---|
65 | csmlstring=csmlstring.replace('{http://www.opengis.net/swe}','swe:') |
---|
66 | csmlstring=csmlstring.replace('{http://ndg.nerc.ac.uk/moles}','moles:') |
---|
67 | #remove cmsl: prefixes |
---|
68 | csmlstring=csmlstring.replace('<csml:','<') |
---|
69 | csmlstring=csmlstring.replace('</csml:','</') |
---|
70 | |
---|
71 | #add namespace declarations at top of document |
---|
72 | csmlstring=csmlstring.replace('<Dataset', '<Dataset xmlns="http://ndg.nerc.ac.uk/csml" xmlns:gml="http://www.opengis.net/gml" xmlns:om="http://www.opengis.net/om" xmlns:swe="http://www.opengis.net/swe" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:moles="http://ndg.nerc.ac.uk/moles" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://ndg.nerc.ac.uk/csml/XMLSchemas/CSMLAppSchema.xsd"') |
---|
73 | |
---|
74 | #this is only used when creating a new csml document |
---|
75 | csmlstring=csmlstring.replace('ns0', 'gml') |
---|
76 | csmlstring=csmlstring.replace('xmlns:ns1', 'xmlns:xlink') |
---|
77 | csmlstring=csmlstring.replace('ns1:href', 'xlink:href') |
---|
78 | csmlstring=csmlstring.replace('ns1:moles', 'xlink:moles') |
---|
79 | csmlstring=csmlstring.replace('ns1:', '') #the rest of the ns1s are CSML namespaces... due to the way it has been parsed. |
---|
80 | #what about SWE - need to check this ********TODO***** |
---|
81 | return csmlstring |
---|
82 | |
---|
83 | |
---|
84 | def isURI(uri): |
---|
85 | """ a very simple function to test if a string is a uri |
---|
86 | if ;// appears in the first 12 characters it is probably a uri """ |
---|
87 | #TODO - a decent uri check! |
---|
88 | result = False |
---|
89 | if string.find(uri[:12], '://') != -1: |
---|
90 | result = True |
---|
91 | return result |
---|
92 | |
---|
93 | |
---|
94 | class ParserPostProcessor: |
---|
95 | def __init__(self,dataset): |
---|
96 | #Needs rewriting for CSML v2 see V1original. |
---|
97 | self.dataset=dataset |
---|
98 | |
---|
99 | def resolveReferences(self): |
---|
100 | pass |
---|
101 | # return self.dataset |
---|