<s> """<STR_LIT>""" <EOL> version = "<STR_LIT>" <EOL> revision = "<STR_LIT>" <EOL> import string <EOL> from xml . sax import xmlreader , saxutils <EOL> from xml . sax . handler import feature_namespaces , feature_namespace_prefixes <EOL> from xml . sax import _exceptions <EOL> import sys <EOL> if sys . platform [ : <NUM_LIT:4> ] != "<STR_LIT>" : <EOL> raise _exceptions . SAXReaderNotAvailable ( "<STR_LIT>" , None ) <EOL> del sys <EOL> try : <EOL> from org . python . core import FilelikeInputStream <EOL> from org . xml . sax . helpers import XMLReaderFactory <EOL> from org . xml import sax as javasax <EOL> from org . xml . sax . ext import LexicalHandler <EOL> except ImportError : <EOL> raise _exceptions . SAXReaderNotAvailable ( "<STR_LIT>" , None ) <EOL> try : <EOL> from javax . xml . parsers import SAXParserFactory , ParserConfigurationException <EOL> factory = SAXParserFactory . newInstance ( ) <EOL> jaxp = <NUM_LIT:1> <EOL> except ImportError : <EOL> jaxp = <NUM_LIT:0> <EOL> from java . lang import String <EOL> def _wrap_sax_exception ( e ) : <EOL> return _exceptions . SAXParseException ( e . message , <EOL> e . exception , <EOL> SimpleLocator ( e . columnNumber , <EOL> e . lineNumber , <EOL> e . publicId , <EOL> e . systemId ) ) <EOL> class JyErrorHandlerWrapper ( javasax . ErrorHandler ) : <EOL> def __init__ ( self , err_handler ) : <EOL> self . _err_handler = err_handler <EOL> def error ( self , exc ) : <EOL> self . _err_handler . error ( _wrap_sax_exception ( exc ) ) <EOL> def fatalError ( self , exc ) : <EOL> self . _err_handler . fatalError ( _wrap_sax_exception ( exc ) ) <EOL> def warning ( self , exc ) : <EOL> self . _err_handler . warning ( _wrap_sax_exception ( exc ) ) <EOL> class JyInputSourceWrapper ( javasax . InputSource ) : <EOL> def __init__ ( self , source ) : <EOL> if isinstance ( source , basestring ) : <EOL> javasax . InputSource . __init__ ( self , source ) <EOL> elif hasattr ( source , "<STR_LIT>" ) : <EOL> f = source <EOL> javasax . InputSource . __init__ ( self , FilelikeInputStream ( f ) ) <EOL> if hasattr ( f , "<STR_LIT:name>" ) : <EOL> self . setSystemId ( f . name ) <EOL> else : <EOL> if source . getByteStream ( ) : <EOL> javasax . InputSource . __init__ ( self , <EOL> FilelikeInputStream ( source . getByteStream ( ) ) ) <EOL> else : <EOL> javasax . InputSource . __init__ ( self ) <EOL> if source . getSystemId ( ) : <EOL> self . setSystemId ( source . getSystemId ( ) ) <EOL> self . setPublicId ( source . getPublicId ( ) ) <EOL> self . setEncoding ( source . getEncoding ( ) ) <EOL> class JyEntityResolverWrapper ( javasax . EntityResolver ) : <EOL> def __init__ ( self , entityResolver ) : <EOL> self . _resolver = entityResolver <EOL> def resolveEntity ( self , pubId , sysId ) : <EOL> return JyInputSourceWrapper ( self . _resolver . resolveEntity ( pubId , sysId ) ) <EOL> class JyDTDHandlerWrapper ( javasax . DTDHandler ) : <EOL> def __init__ ( self , dtdHandler ) : <EOL> self . _handler = dtdHandler <EOL> def notationDecl ( self , name , publicId , systemId ) : <EOL> self . _handler . notationDecl ( name , publicId , systemId ) <EOL> def unparsedEntityDecl ( self , name , publicId , systemId , notationName ) : <EOL> self . _handler . unparsedEntityDecl ( name , publicId , systemId , notationName ) <EOL> class SimpleLocator ( xmlreader . Locator ) : <EOL> def __init__ ( self , colNum , lineNum , pubId , sysId ) : <EOL> self . colNum = colNum <EOL> self . lineNum = lineNum <EOL> self . pubId = pubId <EOL> self . sysId = sysId <EOL> def getColumnNumber ( self ) : <EOL> return self . colNum <EOL> def getLineNumber ( self ) : <EOL> return self . lineNum <EOL> def getPublicId ( self ) : <EOL> return self . pubId <EOL> def getSystemId ( self ) : <EOL> return self . sysId <EOL> class JavaSAXParser ( xmlreader . XMLReader , javasax . ContentHandler , LexicalHandler ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , jdriver = None ) : <EOL> xmlreader . XMLReader . __init__ ( self ) <EOL> self . _parser = create_java_parser ( jdriver ) <EOL> self . _parser . setFeature ( feature_namespaces , <NUM_LIT:0> ) <EOL> self . _parser . setFeature ( feature_namespace_prefixes , <NUM_LIT:0> ) <EOL> self . _parser . setContentHandler ( self ) <EOL> self . _nsattrs = AttributesNSImpl ( ) <EOL> self . _attrs = AttributesImpl ( ) <EOL> self . setEntityResolver ( self . getEntityResolver ( ) ) <EOL> self . setErrorHandler ( self . getErrorHandler ( ) ) <EOL> self . setDTDHandler ( self . getDTDHandler ( ) ) <EOL> try : <EOL> self . _parser . setProperty ( "<STR_LIT>" , self ) <EOL> except Exception , x : <EOL> pass <EOL> def parse ( self , source ) : <EOL> "<STR_LIT>" <EOL> self . _parser . parse ( JyInputSourceWrapper ( source ) ) <EOL> def getFeature ( self , name ) : <EOL> return self . _parser . getFeature ( name ) <EOL> def setFeature ( self , name , state ) : <EOL> self . _parser . setFeature ( name , state ) <EOL> def getProperty ( self , name ) : <EOL> return self . _parser . getProperty ( name ) <EOL> def setProperty ( self , name , value ) : <EOL> self . _parser . setProperty ( name , value ) <EOL> def setEntityResolver ( self , resolver ) : <EOL> self . _parser . entityResolver = JyEntityResolverWrapper ( resolver ) <EOL> xmlreader . XMLReader . setEntityResolver ( self , resolver ) <EOL> def setErrorHandler ( self , err_handler ) : <EOL> self . _parser . errorHandler = JyErrorHandlerWrapper ( err_handler ) <EOL> xmlreader . XMLReader . setErrorHandler ( self , err_handler ) <EOL> def setDTDHandler ( self , dtd_handler ) : <EOL> self . _parser . setDTDHandler ( JyDTDHandlerWrapper ( dtd_handler ) ) <EOL> xmlreader . XMLReader . setDTDHandler ( self , dtd_handler ) <EOL> def setDocumentLocator ( self , locator ) : <EOL> self . _cont_handler . setDocumentLocator ( locator ) <EOL> def startDocument ( self ) : <EOL> self . _cont_handler . startDocument ( ) <EOL> self . _namespaces = self . _parser . getFeature ( feature_namespaces ) <EOL> def startElement ( self , uri , lname , qname , attrs ) : <EOL> if self . _namespaces : <EOL> self . _nsattrs . _attrs = attrs <EOL> self . _cont_handler . startElementNS ( ( uri or None , lname ) , qname , <EOL> self . _nsattrs ) <EOL> else : <EOL> self . _attrs . _attrs = attrs <EOL> self . _cont_handler . startElement ( qname , self . _attrs ) <EOL> def startPrefixMapping ( self , prefix , uri ) : <EOL> self . _cont_handler . startPrefixMapping ( prefix , uri ) <EOL> def characters ( self , char , start , len ) : <EOL> self . _cont_handler . characters ( unicode ( String ( char , start , len ) ) ) <EOL> def ignorableWhitespace ( self , char , start , len ) : <EOL> self . _cont_handler . ignorableWhitespace ( unicode ( String ( char , start , <EOL> len ) ) ) <EOL> def endElement ( self , uri , lname , qname ) : <EOL> if self . _namespaces : <EOL> self . _cont_handler . endElementNS ( ( uri or None , lname ) , qname ) <EOL> else : <EOL> self . _cont_handler . endElement ( qname ) <EOL> def endPrefixMapping ( self , prefix ) : <EOL> self . _cont_handler . endPrefixMapping ( prefix ) <EOL> def endDocument ( self ) : <EOL> self . _cont_handler . endDocument ( ) <EOL> def processingInstruction ( self , target , data ) : <EOL> self . _cont_handler . processingInstruction ( target , data ) <EOL> def comment ( self , char , start , len ) : <EOL> try : <EOL> self . _cont_handler . comment ( unicode ( String ( char , start , len ) ) ) <EOL> except : <EOL> pass <EOL> def startCDATA ( self ) : <EOL> pass <EOL> def endCDATA ( self ) : <EOL> pass <EOL> def startDTD ( self , name , publicId , systemId ) : <EOL> pass <EOL> def endDTD ( self ) : <EOL> pass <EOL> def startEntity ( self , name ) : <EOL> pass <EOL> def endEntity ( self , name ) : <EOL> pass <EOL> def _fixTuple ( nsTuple , frm , to ) : <EOL> if isinstance ( nsTuple , tuple ) and len ( nsTuple ) == <NUM_LIT:2> : <EOL> nsUri , localName = nsTuple <EOL> if nsUri == frm : <EOL> nsUri = to <EOL> return ( nsUri , localName ) <EOL> return nsTuple <EOL> def _makeJavaNsTuple ( nsTuple ) : <EOL> return _fixTuple ( nsTuple , None , '<STR_LIT>' ) <EOL> def _makePythonNsTuple ( nsTuple ) : <EOL> return _fixTuple ( nsTuple , '<STR_LIT>' , None ) <EOL> class AttributesImpl : <EOL> def __init__ ( self , attrs = None ) : <EOL> self . _attrs = attrs <EOL> def getLength ( self ) : <EOL> return self . _attrs . getLength ( ) <EOL> def getType ( self , name ) : <EOL> return self . _attrs . getType ( _makeJavaNsTuple ( name ) ) <EOL> def getValue ( self , name ) : <EOL> value = self . _attrs . getValue ( _makeJavaNsTuple ( name ) ) <EOL> if value == None : <EOL> raise KeyError ( name ) <EOL> return value <EOL> def getNames ( self ) : <EOL> return [ _makePythonNsTuple ( self . _attrs . getQName ( index ) ) for index in range ( len ( self ) ) ] <EOL> def getQNames ( self ) : <EOL> return [ self . _attrs . getQName ( index ) for index in range ( len ( self ) ) ] <EOL> def getValueByQName ( self , qname ) : <EOL> idx = self . _attrs . getIndex ( qname ) <EOL> if idx == - <NUM_LIT:1> : <EOL> raise KeyError , qname <EOL> return self . _attrs . getValue ( idx ) <EOL> def getNameByQName ( self , qname ) : <EOL> idx = self . _attrs . getIndex ( qname ) <EOL> if idx == - <NUM_LIT:1> : <EOL> raise KeyError , qname <EOL> return qname <EOL> def getQNameByName ( self , name ) : <EOL> idx = self . _attrs . getIndex ( _makeJavaNsTuple ( name ) ) <EOL> if idx == - <NUM_LIT:1> : <EOL> raise KeyError , name <EOL> return name <EOL> def __len__ ( self ) : <EOL> return self . _attrs . getLength ( ) <EOL> def __getitem__ ( self , name ) : <EOL> return self . getValue ( name ) <EOL> def keys ( self ) : <EOL> return self . getNames ( ) <EOL> def copy ( self ) : <EOL> return self . __class__ ( self . _attrs ) <EOL> def items ( self ) : <EOL> return [ ( name , self [ name ] ) for name in self . getNames ( ) ] <EOL> def values ( self ) : <EOL> return map ( self . getValue , self . getNames ( ) ) <EOL> def get ( self , name , alt = None ) : <EOL> try : <EOL> return self . getValue ( name ) <EOL> except KeyError : <EOL> return alt <EOL> def has_key ( self , name ) : <EOL> try : <EOL> self . getValue ( name ) <EOL> return True <EOL> except KeyError : <EOL> return False <EOL> class AttributesNSImpl ( AttributesImpl ) : <EOL> def __init__ ( self , attrs = None ) : <EOL> AttributesImpl . __init__ ( self , attrs ) <EOL> def getType ( self , name ) : <EOL> name = _makeJavaNsTuple ( name ) <EOL> return self . _attrs . getType ( name [ <NUM_LIT:0> ] , name [ <NUM_LIT:1> ] ) <EOL> def getValue ( self , name ) : <EOL> jname = _makeJavaNsTuple ( name ) <EOL> value = self . _attrs . getValue ( jname [ <NUM_LIT:0> ] , jname [ <NUM_LIT:1> ] ) <EOL> if value == None : <EOL> raise KeyError ( name ) <EOL> return value <EOL> def getNames ( self ) : <EOL> names = [ ] <EOL> for idx in range ( len ( self ) ) : <EOL> names . append ( _makePythonNsTuple ( ( self . _attrs . getURI ( idx ) , self . _attrs . getLocalName ( idx ) ) ) ) <EOL> return names <EOL> def getNameByQName ( self , qname ) : <EOL> idx = self . _attrs . getIndex ( qname ) <EOL> if idx == - <NUM_LIT:1> : <EOL> raise KeyError , qname <EOL> return _makePythonNsTuple ( ( self . _attrs . getURI ( idx ) , self . _attrs . getLocalName ( idx ) ) ) <EOL> def getQNameByName ( self , name ) : <EOL> name = _makeJavaNsTuple ( name ) <EOL> idx = self . _attrs . getIndex ( name [ <NUM_LIT:0> ] , name [ <NUM_LIT:1> ] ) <EOL> if idx == - <NUM_LIT:1> : <EOL> raise KeyError , name <EOL> return self . _attrs . getQName ( idx ) <EOL> def getQNames ( self ) : <EOL> return [ self . _attrs . getQName ( idx ) for idx in range ( len ( self ) ) ] <EOL> def create_java_parser ( jdriver = None ) : <EOL> try : <EOL> if jdriver : <EOL> return XMLReaderFactory . createXMLReader ( jdriver ) <EOL> elif jaxp : <EOL> return factory . newSAXParser ( ) . getXMLReader ( ) <EOL> else : <EOL> return XMLReaderFactory . createXMLReader ( ) <EOL> except ParserConfigurationException , e : <EOL> raise _exceptions . SAXReaderNotAvailable ( e . getMessage ( ) ) <EOL> except javasax . SAXException , e : <EOL> raise _exceptions . SAXReaderNotAvailable ( e . getMessage ( ) ) <EOL> def create_parser ( jdriver = None ) : <EOL> return JavaSAXParser ( jdriver ) </s>
