Changeset 3097 for TI02-CSML


Ignore:
Timestamp:
03/12/07 15:16:23 (12 years ago)
Author:
domlowe
Message:

replaced cgi.escape with Bryan/Alan?'s escaping class that handles double escaping problem

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TI02-CSML/trunk/csml/parser.py

    r3089 r3097  
    2323import sys, traceback 
    2424import csml 
    25 import cgi #(for escaping text) 
     25import re 
    2626'''CSML v2 Parser '''  
    2727 
     
    9191FILEFORMATS=[CSML('NetCDFExtract'),CSML('NASAAmesExtract'), CSML('GRIBExtract'),CSML('CDMLExtract'), CSML('RawFileExtract'), CSML('ImageFileExtract'), CSML('AggregatedArray')] 
    9292 
     93class subAI: 
     94    ''' This is Alan Iwi's substitute and replace orphan <> and & code ''' 
     95    def __init__(self): 
     96        self.r1=re.compile('<([^>]*(<|$))') 
     97        self.r2=re.compile('((^|>)[^<]*)>') 
     98        self.r3=re.compile('&(?!(amp|lt|gt);)') 
     99    def sub(self,s): 
     100        old='' 
     101        s=self.r3.sub(r'&amp;',s) 
     102        s=s.replace(';amp',';') 
     103        while s != old: 
     104            old=s 
     105            s=self.r1.sub(r'&lt;\1',s) 
     106            s=self.r2.sub(r'\1&gt;',s) 
     107        return s 
     108 
     109 
    93110class csElement(object): 
    94111    ''' main csElement class - all other elements inherit from this baseclass   
     
    104121        if not hasattr(self, 'CHILDREN'): 
    105122            self.__dict__['CHILDREN']={} 
     123        self.cleanup=subAI() 
    106124    #The __setattr__ and __getattribute__ special methods have been overridden. 
    107125    #This is so that attributes can be called by their fully qualified name internally 
     
    186204        #process self and convert to XML       
    187205        if 'CONTENT' in self.__dict__: 
    188             if self.CONTENT is not None: csmlfrag.text=cgi.escape(self.CONTENT) 
     206            if self.CONTENT is not None: csmlfrag.text=self.cleanup.sub(self.CONTENT) 
    189207        if 'ATTRIBUTES' in self.__dict__: 
    190208            for item in self.__dict__: 
    191209                if item in self.ATTRIBUTES:                     
    192                     csmlfrag.set(item, cgi.escape(str((self.__dict__[item])))) 
     210                    csmlfrag.set(item, self.cleanup.sub(str((self.__dict__[item])))) 
    193211    # self.CHILDREN (recursive - calls the toXML method of children 
    194212        for child in self.ELEMORDER: 
Note: See TracChangeset for help on using the changeset viewer.