<s> from xml . sax import make_parser , handler <EOL> import xml as pyxml <EOL> from nevow . stan import xml , Tag , directive , slot <EOL> import nevow <EOL> try : <EOL> bad_version = pyxml . version_info < ( <NUM_LIT:0> , <NUM_LIT:8> , <NUM_LIT:2> ) <EOL> bad_startdtd_args = pyxml . version_info < ( <NUM_LIT:0> , <NUM_LIT:8> , <NUM_LIT:3> ) <EOL> except : <EOL> import sys <EOL> bad_version = sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> bad_startdtd_args = sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:4> ) <EOL> class nscontext ( object ) : <EOL> def __init__ ( self , parent = None ) : <EOL> self . parent = parent <EOL> if parent is not None : <EOL> self . nss = dict ( parent . nss ) <EOL> else : <EOL> self . nss = { '<STR_LIT>' : '<STR_LIT>' } <EOL> def get ( self , k , d = None ) : <EOL> return self . nss . get ( k , d ) <EOL> def __setitem__ ( self , k , v ) : <EOL> self . nss . __setitem__ ( k , v ) <EOL> def __getitem__ ( self , k ) : <EOL> return self . nss . __getitem__ ( k ) <EOL> class ToStan ( handler . ContentHandler , handler . EntityResolver ) : <EOL> directiveMapping = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:data>' : '<STR_LIT:data>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> attributeList = [ <EOL> '<STR_LIT>' , '<STR_LIT:key>' , <EOL> ] <EOL> def __init__ ( self , ignoreDocType , ignoreComment , sourceFilename ) : <EOL> self . ignoreDocType = ignoreDocType <EOL> self . ignoreComment = ignoreComment <EOL> self . sourceFilename = sourceFilename <EOL> self . prefixMap = nscontext ( ) <EOL> self . inCDATA = False <EOL> def setDocumentLocator ( self , locator ) : <EOL> self . locator = locator <EOL> def resolveEntity ( self , publicId , systemId ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def skippedEntity ( self , name ) : <EOL> self . current . append ( xml ( "<STR_LIT>" % name ) ) <EOL> def startDocument ( self ) : <EOL> self . document = [ ] <EOL> self . current = self . document <EOL> self . stack = [ ] <EOL> self . xmlnsAttrs = [ ] <EOL> def endDocument ( self ) : <EOL> pass <EOL> def processingInstruction ( self , target , data ) : <EOL> self . current . append ( xml ( "<STR_LIT>" % ( target , data ) ) ) <EOL> def startPrefixMapping ( self , prefix , uri ) : <EOL> self . prefixMap = nscontext ( self . prefixMap ) <EOL> self . prefixMap [ uri ] = prefix <EOL> if uri == nevow . namespace : <EOL> return <EOL> if prefix is None : <EOL> self . xmlnsAttrs . append ( ( '<STR_LIT>' , uri ) ) <EOL> else : <EOL> self . xmlnsAttrs . append ( ( '<STR_LIT>' % prefix , uri ) ) <EOL> def endPrefixMapping ( self , prefix ) : <EOL> self . prefixMap = self . prefixMap . parent <EOL> def startElementNS ( self , ns_and_name , qname , attrs ) : <EOL> filename = self . sourceFilename <EOL> lineNumber = self . locator . getLineNumber ( ) <EOL> columnNumber = self . locator . getColumnNumber ( ) <EOL> ns , name = ns_and_name <EOL> if ns == nevow . namespace : <EOL> if name == '<STR_LIT>' : <EOL> name = '<STR_LIT>' <EOL> elif name == '<STR_LIT>' : <EOL> try : <EOL> default = attrs [ ( None , '<STR_LIT:default>' ) ] <EOL> except KeyError : <EOL> default = None <EOL> el = slot ( <EOL> attrs [ ( None , '<STR_LIT:name>' ) ] , default = default , <EOL> filename = filename , lineNumber = lineNumber , <EOL> columnNumber = columnNumber ) <EOL> self . stack . append ( el ) <EOL> self . current . append ( el ) <EOL> self . current = el . children <EOL> return <EOL> attrs = dict ( attrs ) <EOL> specials = { } <EOL> attributes = self . attributeList <EOL> directives = self . directiveMapping <EOL> for k , v in attrs . items ( ) : <EOL> att_ns , nons = k <EOL> if att_ns != nevow . namespace : <EOL> continue <EOL> if nons in directives : <EOL> specials [ directives [ nons ] ] = directive ( v ) <EOL> del attrs [ k ] <EOL> if nons in attributes : <EOL> specials [ nons ] = v <EOL> del attrs [ k ] <EOL> no_ns_attrs = { } <EOL> for ( attrNs , attrName ) , v in attrs . items ( ) : <EOL> nsPrefix = self . prefixMap . get ( attrNs ) <EOL> if nsPrefix is None : <EOL> no_ns_attrs [ attrName ] = v <EOL> else : <EOL> no_ns_attrs [ '<STR_LIT>' % ( nsPrefix , attrName ) ] = v <EOL> if ns == nevow . namespace and name == '<STR_LIT>' : <EOL> if not self . stack : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> if '<STR_LIT:name>' not in no_ns_attrs : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> el = Tag ( '<STR_LIT>' , specials = specials , filename = filename , <EOL> lineNumber = lineNumber , columnNumber = columnNumber ) <EOL> self . stack [ - <NUM_LIT:1> ] . attributes [ no_ns_attrs [ '<STR_LIT:name>' ] ] = el <EOL> self . stack . append ( el ) <EOL> self . current = el . children <EOL> return <EOL> if self . xmlnsAttrs : <EOL> no_ns_attrs . update ( dict ( self . xmlnsAttrs ) ) <EOL> self . xmlnsAttrs = [ ] <EOL> if ns != nevow . namespace and ns is not None : <EOL> prefix = self . prefixMap [ ns ] <EOL> if prefix is not None : <EOL> name = '<STR_LIT>' % ( self . prefixMap [ ns ] , name ) <EOL> el = Tag ( <EOL> name , attributes = dict ( no_ns_attrs ) , specials = specials , <EOL> filename = filename , lineNumber = lineNumber , <EOL> columnNumber = columnNumber ) <EOL> self . stack . append ( el ) <EOL> self . current . append ( el ) <EOL> self . current = el . children <EOL> def characters ( self , ch ) : <EOL> if self . inCDATA : <EOL> ch = xml ( ch ) <EOL> self . current . append ( ch ) <EOL> def endElementNS ( self , name , qname ) : <EOL> me = self . stack . pop ( ) <EOL> if self . stack : <EOL> self . current = self . stack [ - <NUM_LIT:1> ] . children <EOL> else : <EOL> self . current = self . document <EOL> def startDTD ( self , name , publicId , systemId ) : <EOL> if self . ignoreDocType : <EOL> return <EOL> if bad_startdtd_args : <EOL> systemId , publicId = publicId , systemId <EOL> doctype = '<STR_LIT>' % ( name , publicId , systemId ) <EOL> self . current . append ( xml ( doctype ) ) <EOL> def endDTD ( self , * args ) : <EOL> pass <EOL> def startCDATA ( self ) : <EOL> self . inCDATA = True <EOL> self . current . append ( xml ( '<STR_LIT>' ) ) <EOL> def endCDATA ( self ) : <EOL> self . inCDATA = False <EOL> self . current . append ( xml ( '<STR_LIT>' ) ) <EOL> def comment ( self , content ) : <EOL> if self . ignoreComment : <EOL> return <EOL> self . current . append ( ( xml ( '<STR_LIT>' ) , xml ( content ) , xml ( '<STR_LIT>' ) ) ) <EOL> def parse ( fl , ignoreDocType = False , ignoreComment = False ) : <EOL> if bad_version : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> parser = make_parser ( ) <EOL> parser . setFeature ( handler . feature_validation , <NUM_LIT:0> ) <EOL> parser . setFeature ( handler . feature_namespaces , <NUM_LIT:1> ) <EOL> parser . setFeature ( handler . feature_external_ges , <NUM_LIT:0> ) <EOL> parser . setFeature ( handler . feature_external_pes , <NUM_LIT:0> ) <EOL> s = ToStan ( ignoreDocType , ignoreComment , getattr ( fl , "<STR_LIT:name>" , None ) ) <EOL> parser . setContentHandler ( s ) <EOL> parser . setEntityResolver ( s ) <EOL> parser . setProperty ( handler . property_lexical_handler , s ) <EOL> parser . parse ( fl ) <EOL> return s . document <EOL> def parseString ( t , ignoreDocType = False , ignoreComment = False ) : <EOL> from cStringIO import StringIO <EOL> return parse ( StringIO ( t ) , ignoreDocType , ignoreComment ) </s>
