Changeset 2641
- Timestamp:
- 23/06/07 19:33:10 (14 years ago)
- Location:
- TI05-delivery/ows_framework/trunk/ows_server/ows_server/models
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/ndgRetrieve.py
r2640 r2641 2 2 import cgi,time 3 3 from Utilities import myConfig 4 from ndgSecurity import HandleSecurity 5 4 6 debug=1 5 def ndgRetrieve(uri,config,logger=0,requestor='test',format='' ):7 def ndgRetrieve(uri,config,logger=0,requestor='test',format='',securityTokens=None): 6 8 7 9 ''' Given an ndgObject, uri, retrieve it, with security in place ''' 8 10 9 11 10 def getws(config,uri ):12 def getws(config,uri,): 11 13 ''' Get a ws connection to the local exist database ''' 12 14 # The logic here is that … … 71 73 pass 72 74 elif uri.schema =='NDG-A0': 73 pass 74 75 75 76 s=x.tree.find('{http://ndg.nerc.ac.uk/csml}AccessControlPolicy/{http://ndg.nerc.ac.uk/csml}dgSecurityCondition') 77 if s is not None: 78 status,message=HandleSecurity(s,securityTokens) 79 if not status: return 0,'<p> Access Denied </p><p>%s</p>'%message 76 80 return 1,x 77 81 … … 139 143 status,xml=ndgRetrieve(uri,self.c) 140 144 if status: s=str(xml) 141 print s 145 146 147 def testSecurityCSML(self): 148 ''' Make sure that CSML security is respected ''' 149 securityTokens=None 150 self.setup() 151 doc='badc.nerc.ac.uk__NDG-A0__AfEj15o6' 152 uri=ndgObject.ndgObject(doc) 153 status,xml=ndgRetrieve(uri,self.c) 154 self.assertEqual(status,0) 155 142 156 143 157 -
TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/xmlHandler2.py
r2640 r2641 41 41 self.tree=ET.parse(xmlf).getroot() 42 42 except SyntaxError: 43 self. __fixXML()43 self.xmls=self.__fixXML(self.xmls) 44 44 xmlf=StringIO.StringIO(self.xmls.encode('utf-8')) 45 45 self.tree=ET.parse(xmlf).getroot() … … 53 53 # or bettter yet, use iterparse in the first place, but that seemed slow. 54 54 55 print self.xmls[0:19] 55 56 56 if self.xmls[0:19]=='<?xml version="1.0"': 57 57 self.root=1 … … 85 85 def span(x,c): return '<span class="%s">%s</span>'%(c,x) 86 86 def div(x,c): return '<div class="%s">%s</div>'%(c,x) 87 def fix(x): 88 if x is None: return '' 89 return x 87 90 def et2html(elem): 88 91 strAttrib='' 89 92 for att in elem.attrib: 90 93 strAttrib+=' %s="%s"'%(span(att,'xmlAttrTyp'),span(elem.attrib[att],'xmlAttrVal')) 91 result='%s%s%s%s%s'%(lt,span(elem.tag,"xmlElemTag"),strAttrib,gt, elem.tail)94 result='%s%s%s%s%s'%(lt,span(elem.tag,"xmlElemTag"),strAttrib,gt,fix(elem.text)) 92 95 children=len(elem) 93 96 if children: 94 97 for item in elem: 95 98 result+=et2html(item) 96 result+='%s%s/%s%s'%( lt,span(elem.tag,'xmlElemTag'),gt,elem.tail)99 result+='%s%s/%s%s'%(fix(elem.tail),lt,span(elem.tag,'xmlElemTag'),gt) 97 100 else: 98 101 result+='%s/%s%s'%(lt,span(elem.tag,'xmlElemTag'),gt) … … 103 106 if self.root:h='%s%s %s="%s" %s="%s"%s'%( 104 107 lt,'?xml',span('version','xmlAttrTyp'),'1.0',span('encoding','xmlAttrTyp'),'utf-8',gt) 108 ss=self.__fixXML(ss) 105 109 if self.realns=={}: return h+ss 106 return self.__fixns(h,ss) 107 110 return h+self.__nsfixpretty(ss,span) 111 112 def __nsfixpretty(self,s,span): 113 ''' Yet another careful fix ''' 114 for ns in self.realns: 115 r='{%s}'%ns 116 if self.realns[ns]=='default': 117 s=s.replace(r,'') 118 else: 119 s=s.replace(r,'%s:'%self.realns[ns]) 120 if self.realns=={}: return s 121 # at this point we have no namespace list at the top 122 rightArrow=s.find('</span>') # this is just after the tag, where we do want the namespace list 123 nslist={} 124 for ns in self.realns: nslist[self.realns[ns]]=ns 125 r=' %s="%s"'%(span('xmlns','xmlAttrTyp'),span(nslist['default'],'xmlAttrVal')) 126 for ns in nslist: 127 if ns<>'default': r+=' %s="%s"'%(span('xmlns:%s'%ns,'xmlAttrTyp'),span(nslist[ns],'xmlAttrVal')) 128 return s[:rightArrow]+r+s[rightArrow:] 129 108 130 def __updatens(self): 109 131 ''' Update the element tree namespace map with our own map ''' 110 # *c*ElementTree doesn't have this update method (or at 111 # least I can't find it), so you have to import ElementTree and call it on 112 # that, then it all mysteriously works in cElementTree... 113 132 # *c*ElementTree doesn't have this update method (or at 133 # least I can't find it), so you have to import ElementTree and call it on 134 # that, then it all mysteriously works in cElementTree... 135 114 136 pET._namespace_map.update(self.realns) 115 137 … … 126 148 127 149 def __fixns(self,h,ss): 128 ''' Fix the namespaces '''150 ''' Fix the namespaces after ET has produced a string ''' 129 151 ss=ss.replace('default:','') 130 152 for ns in self.realns: … … 137 159 nslist={} 138 160 for ns in self.realns: nslist[self.realns[ns]]=ns 139 r='xmlns :"%s"'%nslist['default']161 r='xmlns="%s"'%nslist['default'] 140 162 for ns in nslist: 141 163 if ns<>'default': r+=' xmlns:%s="%s"'%(ns,nslist[ns]) 142 164 h+=ss[:rightArrow]+r+ss[rightArrow:] 143 165 return h 144 145 166 146 def __fixXML(self ):167 def __fixXML(self,s): 147 168 #first those nasty ampersands 148 s elf.xmls=re.sub(r'&(?!\w+;)', '&', self.xmls)169 s=re.sub(r'&(?!\w+;)', '&', s) 149 170 #and now orphan > < signs 150 171 if self.r1 is None: 151 172 self.r1=re.compile('<([^>]*(<|$))') 152 173 self.r2=re.compile('((^|>)[^<]*)>') 153 s=self.xmls154 174 old='' 155 175 while s != old: … … 157 177 s=self.r1.sub(r'<\1',s) 158 178 s=self.r2.sub(r'\1>',s) 159 self.xmls=s179 return s 160 180 161 181 … … 171 191 <Dataset xmlns:swe="http://www.opengis.net/swe" xmlns:gml="http://www.opengis.net/gml" 172 192 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:moles="http://ndg.nerc.ac.uk/moles" 173 xmlns:om="http://www.opengis.net/om" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://ndg.nerc.ac.uk/csml" 193 xmlns:om="http://www.opengis.net/om" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://ndg.nerc.ac.uk/csml" 174 194 id="FGPfF9i0"><CSMLFeatureCollection gml:id="AfEj15o6"/><om:blah>blahvalue</om:blah><foo>foovalue</foo></Dataset>''' 175 195 … … 187 207 self.setup() 188 208 x=xmlHandler(self.ss,string=1) 189 self.assertEqual('<?xml version="1.0" encoding="utf-8"><Dataset id="FGPfF9i0" xmlns :"http://ndg.nerc.ac.uk/csml" xmlns:om="http://www.opengis.net/om" xmlns:gml="http://www.opengis.net/gml" xmlns:swe="http://www.opengis.net/swe" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:moles="http://ndg.nerc.ac.uk/moles"><CSMLFeatureCollection gml:id="AfEj15o6" /><om:blah >blahvalue</om:blah><foo>foovalue</foo></Dataset>',str(x))209 self.assertEqual('<?xml version="1.0" encoding="utf-8"><Dataset id="FGPfF9i0" xmlns="http://ndg.nerc.ac.uk/csml" xmlns:om="http://www.opengis.net/om" xmlns:gml="http://www.opengis.net/gml" xmlns:swe="http://www.opengis.net/swe" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:moles="http://ndg.nerc.ac.uk/moles"><CSMLFeatureCollection gml:id="AfEj15o6" /><om:blah >blahvalue</om:blah><foo>foovalue</foo></Dataset>',str(x)) 190 210 191 211 def testorphans(self): … … 211 231 s='<?xml version="1.0" encoding="utf-8"?><data><element>stuff</element></data>' 212 232 x=xmlHandler(s,string=1) 213 h=x.tohtml() 214 print h233 h=x.tohtml() # only testing the mechanics, not the result 234 215 235 216 236 #turn off the test … … 222 242 223 243 244 def testDIF(self): 245 s='''<DIF xmlns="http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Entry_ID>badc.nerc.ac.uk__DIF__dataent_11738019833217179</Entry_ID></DIF>''' 246 x=xmlHandler(s,string=1) 247 print x.realns 248 print str(x) 249 h=x.tohtml() 250 print h 251 224 252 unittest.main() 225 253
Note: See TracChangeset
for help on using the changeset viewer.