<s> """<STR_LIT>""" <EOL> import os , urlparse , urllib , types <EOL> import handler <EOL> import xmlreader <EOL> try : <EOL> _StringTypes = [ types . StringType , types . UnicodeType ] <EOL> except AttributeError : <EOL> _StringTypes = [ types . StringType ] <EOL> try : <EOL> from codecs import xmlcharrefreplace_errors <EOL> _error_handling = "<STR_LIT>" <EOL> del xmlcharrefreplace_errors <EOL> except ImportError : <EOL> _error_handling = "<STR_LIT:strict>" <EOL> def __dict_replace ( s , d ) : <EOL> """<STR_LIT>""" <EOL> for key , value in d . items ( ) : <EOL> s = s . replace ( key , value ) <EOL> return s <EOL> def escape ( data , entities = { } ) : <EOL> """<STR_LIT>""" <EOL> data = data . replace ( "<STR_LIT:&>" , "<STR_LIT>" ) <EOL> data = data . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) <EOL> data = data . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> if entities : <EOL> data = __dict_replace ( data , entities ) <EOL> return data <EOL> def unescape ( data , entities = { } ) : <EOL> """<STR_LIT>""" <EOL> data = data . replace ( "<STR_LIT>" , "<STR_LIT:<>" ) <EOL> data = data . replace ( "<STR_LIT>" , "<STR_LIT:>>" ) <EOL> if entities : <EOL> data = __dict_replace ( data , entities ) <EOL> return data . replace ( "<STR_LIT>" , "<STR_LIT:&>" ) <EOL> def quoteattr ( data , entities = { } ) : <EOL> """<STR_LIT>""" <EOL> entities = entities . copy ( ) <EOL> entities . update ( { '<STR_LIT:\n>' : '<STR_LIT>' , '<STR_LIT:\r>' : '<STR_LIT>' , '<STR_LIT:\t>' : '<STR_LIT>' } ) <EOL> data = escape ( data , entities ) <EOL> if '<STR_LIT:">' in data : <EOL> if "<STR_LIT:'>" in data : <EOL> data = '<STR_LIT>' % data . replace ( '<STR_LIT:">' , "<STR_LIT>" ) <EOL> else : <EOL> data = "<STR_LIT>" % data <EOL> else : <EOL> data = '<STR_LIT>' % data <EOL> return data <EOL> class XMLGenerator ( handler . ContentHandler ) : <EOL> def __init__ ( self , out = None , encoding = "<STR_LIT>" ) : <EOL> if out is None : <EOL> import sys <EOL> out = sys . stdout <EOL> handler . ContentHandler . __init__ ( self ) <EOL> self . _out = out <EOL> self . _ns_contexts = [ { } ] <EOL> self . _current_context = self . _ns_contexts [ - <NUM_LIT:1> ] <EOL> self . _undeclared_ns_maps = [ ] <EOL> self . _encoding = encoding <EOL> def _write ( self , text ) : <EOL> if isinstance ( text , str ) : <EOL> self . _out . write ( text ) <EOL> else : <EOL> self . _out . write ( text . encode ( self . _encoding , _error_handling ) ) <EOL> def _qname ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name [ <NUM_LIT:0> ] : <EOL> prefix = self . _current_context [ name [ <NUM_LIT:0> ] ] <EOL> if prefix : <EOL> return prefix + "<STR_LIT::>" + name [ <NUM_LIT:1> ] <EOL> return name [ <NUM_LIT:1> ] <EOL> def startDocument ( self ) : <EOL> self . _write ( '<STR_LIT>' % <EOL> self . _encoding ) <EOL> def startPrefixMapping ( self , prefix , uri ) : <EOL> self . _ns_contexts . append ( self . _current_context . copy ( ) ) <EOL> self . _current_context [ uri ] = prefix <EOL> self . _undeclared_ns_maps . append ( ( prefix , uri ) ) <EOL> def endPrefixMapping ( self , prefix ) : <EOL> self . _current_context = self . _ns_contexts [ - <NUM_LIT:1> ] <EOL> del self . _ns_contexts [ - <NUM_LIT:1> ] <EOL> def startElement ( self , name , attrs ) : <EOL> self . _write ( '<STR_LIT:<>' + name ) <EOL> for ( name , value ) in attrs . items ( ) : <EOL> self . _write ( '<STR_LIT>' % ( name , quoteattr ( value ) ) ) <EOL> self . _write ( '<STR_LIT:>>' ) <EOL> def endElement ( self , name ) : <EOL> self . _write ( '<STR_LIT>' % name ) <EOL> def startElementNS ( self , name , qname , attrs ) : <EOL> self . _write ( '<STR_LIT:<>' + self . _qname ( name ) ) <EOL> for prefix , uri in self . _undeclared_ns_maps : <EOL> if prefix : <EOL> self . _out . write ( '<STR_LIT>' % ( prefix , uri ) ) <EOL> else : <EOL> self . _out . write ( '<STR_LIT>' % uri ) <EOL> self . _undeclared_ns_maps = [ ] <EOL> for ( name , value ) in attrs . items ( ) : <EOL> self . _write ( '<STR_LIT>' % ( self . _qname ( name ) , quoteattr ( value ) ) ) <EOL> self . _write ( '<STR_LIT:>>' ) <EOL> def endElementNS ( self , name , qname ) : <EOL> self . _write ( '<STR_LIT>' % self . _qname ( name ) ) <EOL> def characters ( self , content ) : <EOL> self . _write ( escape ( content ) ) <EOL> def ignorableWhitespace ( self , content ) : <EOL> self . _write ( content ) <EOL> def processingInstruction ( self , target , data ) : <EOL> self . _write ( '<STR_LIT>' % ( target , data ) ) <EOL> class XMLFilterBase ( xmlreader . XMLReader ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent = None ) : <EOL> xmlreader . XMLReader . __init__ ( self ) <EOL> self . _parent = parent <EOL> def error ( self , exception ) : <EOL> self . _err_handler . error ( exception ) <EOL> def fatalError ( self , exception ) : <EOL> self . _err_handler . fatalError ( exception ) <EOL> def warning ( self , exception ) : <EOL> self . _err_handler . warning ( exception ) <EOL> def setDocumentLocator ( self , locator ) : <EOL> self . _cont_handler . setDocumentLocator ( locator ) <EOL> def startDocument ( self ) : <EOL> self . _cont_handler . startDocument ( ) <EOL> def endDocument ( self ) : <EOL> self . _cont_handler . endDocument ( ) <EOL> def startPrefixMapping ( self , prefix , uri ) : <EOL> self . _cont_handler . startPrefixMapping ( prefix , uri ) <EOL> def endPrefixMapping ( self , prefix ) : <EOL> self . _cont_handler . endPrefixMapping ( prefix ) <EOL> def startElement ( self , name , attrs ) : <EOL> self . _cont_handler . startElement ( name , attrs ) <EOL> def endElement ( self , name ) : <EOL> self . _cont_handler . endElement ( name ) <EOL> def startElementNS ( self , name , qname , attrs ) : <EOL> self . _cont_handler . startElementNS ( name , qname , attrs ) <EOL> def endElementNS ( self , name , qname ) : <EOL> self . _cont_handler . endElementNS ( name , qname ) <EOL> def characters ( self , content ) : <EOL> self . _cont_handler . characters ( content ) <EOL> def ignorableWhitespace ( self , chars ) : <EOL> self . _cont_handler . ignorableWhitespace ( chars ) <EOL> def processingInstruction ( self , target , data ) : <EOL> self . _cont_handler . processingInstruction ( target , data ) <EOL> def skippedEntity ( self , name ) : <EOL> self . _cont_handler . skippedEntity ( name ) <EOL> def notationDecl ( self , name , publicId , systemId ) : <EOL> self . _dtd_handler . notationDecl ( name , publicId , systemId ) <EOL> def unparsedEntityDecl ( self , name , publicId , systemId , ndata ) : <EOL> self . _dtd_handler . unparsedEntityDecl ( name , publicId , systemId , ndata ) <EOL> def resolveEntity ( self , publicId , systemId ) : <EOL> return self . _ent_handler . resolveEntity ( publicId , systemId ) <EOL> def parse ( self , source ) : <EOL> self . _parent . setContentHandler ( self ) <EOL> self . _parent . setErrorHandler ( self ) <EOL> self . _parent . setEntityResolver ( self ) <EOL> self . _parent . setDTDHandler ( self ) <EOL> self . _parent . parse ( source ) <EOL> def setLocale ( self , locale ) : <EOL> self . _parent . setLocale ( locale ) <EOL> def getFeature ( self , name ) : <EOL> return self . _parent . getFeature ( name ) <EOL> def setFeature ( self , name , state ) : <EOL> self . _parent . setFeature ( name , state ) <EOL> def getProperty ( self , name ) : <EOL> return self . _parent . getProperty ( name ) <EOL> def setProperty ( self , name , value ) : <EOL> self . _parent . setProperty ( name , value ) <EOL> def getParent ( self ) : <EOL> return self . _parent <EOL> def setParent ( self , parent ) : <EOL> self . _parent = parent <EOL> def prepare_input_source ( source , base = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if type ( source ) in _StringTypes : <EOL> source = xmlreader . InputSource ( source ) <EOL> elif hasattr ( source , "<STR_LIT>" ) : <EOL> f = source <EOL> source = xmlreader . InputSource ( ) <EOL> source . setByteStream ( f ) <EOL> if hasattr ( f , "<STR_LIT:name>" ) : <EOL> source . setSystemId ( f . name ) <EOL> if source . getByteStream ( ) is None : <EOL> sysid = source . getSystemId ( ) <EOL> basehead = os . path . dirname ( os . path . normpath ( base ) ) <EOL> sysidfilename = os . path . join ( basehead , sysid ) <EOL> if os . path . isfile ( sysidfilename ) : <EOL> source . setSystemId ( sysidfilename ) <EOL> f = open ( sysidfilename , "<STR_LIT:rb>" ) <EOL> else : <EOL> source . setSystemId ( urlparse . urljoin ( base , sysid ) ) <EOL> f = urllib . urlopen ( source . getSystemId ( ) ) <EOL> source . setByteStream ( f ) <EOL> return source </s>
