<s> """<STR_LIT>""" <EOL> from logging import getLogger <EOL> import suds . metrics <EOL> from suds import * <EOL> from suds . sax import * <EOL> from suds . sax . document import Document <EOL> from suds . sax . element import Element <EOL> from suds . sax . text import Text <EOL> from suds . sax . attribute import Attribute <EOL> from xml . sax import make_parser , InputSource , ContentHandler <EOL> from xml . sax . handler import feature_external_ges <EOL> from cStringIO import StringIO <EOL> log = getLogger ( __name__ ) <EOL> class Handler ( ContentHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . nodes = [ Document ( ) ] <EOL> def startElement ( self , name , attrs ) : <EOL> top = self . top ( ) <EOL> node = Element ( unicode ( name ) , parent = top ) <EOL> for a in attrs . getNames ( ) : <EOL> n = unicode ( a ) <EOL> v = unicode ( attrs . getValue ( a ) ) <EOL> attribute = Attribute ( n , v ) <EOL> if self . mapPrefix ( node , attribute ) : <EOL> continue <EOL> node . append ( attribute ) <EOL> node . charbuffer = [ ] <EOL> top . append ( node ) <EOL> self . push ( node ) <EOL> def mapPrefix ( self , node , attribute ) : <EOL> skip = False <EOL> if attribute . name == '<STR_LIT>' : <EOL> if len ( attribute . value ) : <EOL> node . expns = unicode ( attribute . value ) <EOL> skip = True <EOL> elif attribute . prefix == '<STR_LIT>' : <EOL> prefix = attribute . name <EOL> node . nsprefixes [ prefix ] = unicode ( attribute . value ) <EOL> skip = True <EOL> return skip <EOL> def endElement ( self , name ) : <EOL> name = unicode ( name ) <EOL> current = self . top ( ) <EOL> if len ( current . charbuffer ) : <EOL> current . text = Text ( u'<STR_LIT>' . join ( current . charbuffer ) ) <EOL> del current . charbuffer <EOL> if len ( current ) : <EOL> current . trim ( ) <EOL> currentqname = current . qname ( ) <EOL> if name == currentqname : <EOL> self . pop ( ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def characters ( self , content ) : <EOL> text = unicode ( content ) <EOL> node = self . top ( ) <EOL> node . charbuffer . append ( text ) <EOL> def push ( self , node ) : <EOL> self . nodes . append ( node ) <EOL> return node <EOL> def pop ( self ) : <EOL> return self . nodes . pop ( ) <EOL> def top ( self ) : <EOL> return self . nodes [ len ( self . nodes ) - <NUM_LIT:1> ] <EOL> class Parser : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def saxparser ( cls ) : <EOL> p = make_parser ( ) <EOL> p . setFeature ( feature_external_ges , <NUM_LIT:0> ) <EOL> h = Handler ( ) <EOL> p . setContentHandler ( h ) <EOL> return ( p , h ) <EOL> def parse ( self , file = None , string = None ) : <EOL> """<STR_LIT>""" <EOL> timer = metrics . Timer ( ) <EOL> timer . start ( ) <EOL> sax , handler = self . saxparser ( ) <EOL> if file is not None : <EOL> sax . parse ( file ) <EOL> timer . stop ( ) <EOL> return handler . nodes [ <NUM_LIT:0> ] <EOL> if string is not None : <EOL> source = InputSource ( None ) <EOL> source . setByteStream ( StringIO ( string ) ) <EOL> sax . parse ( source ) <EOL> timer . stop ( ) <EOL> return handler . nodes [ <NUM_LIT:0> ] </s>
