<s> """<STR_LIT>""" <EOL> from xml . dom import xmlbuilder , minidom , Node <EOL> from xml . dom import EMPTY_NAMESPACE , EMPTY_PREFIX , XMLNS_NAMESPACE <EOL> from xml . parsers import expat <EOL> from xml . dom . minidom import _append_child , _set_attribute_node <EOL> from xml . dom . NodeFilter import NodeFilter <EOL> from xml . dom . minicompat import * <EOL> TEXT_NODE = Node . TEXT_NODE <EOL> CDATA_SECTION_NODE = Node . CDATA_SECTION_NODE <EOL> DOCUMENT_NODE = Node . DOCUMENT_NODE <EOL> FILTER_ACCEPT = xmlbuilder . DOMBuilderFilter . FILTER_ACCEPT <EOL> FILTER_REJECT = xmlbuilder . DOMBuilderFilter . FILTER_REJECT <EOL> FILTER_SKIP = xmlbuilder . DOMBuilderFilter . FILTER_SKIP <EOL> FILTER_INTERRUPT = xmlbuilder . DOMBuilderFilter . FILTER_INTERRUPT <EOL> theDOMImplementation = minidom . getDOMImplementation ( ) <EOL> _typeinfo_map = { <EOL> "<STR_LIT>" : minidom . TypeInfo ( None , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : minidom . TypeInfo ( None , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : minidom . TypeInfo ( None , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : minidom . TypeInfo ( None , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : minidom . TypeInfo ( None , "<STR_LIT:id>" ) , <EOL> "<STR_LIT>" : minidom . TypeInfo ( None , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : minidom . TypeInfo ( None , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : minidom . TypeInfo ( None , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : minidom . TypeInfo ( None , "<STR_LIT>" ) , <EOL> } <EOL> class ElementInfo ( NewStyle ) : <EOL> __slots__ = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> def __init__ ( self , tagName , model = None ) : <EOL> self . tagName = tagName <EOL> self . _attr_info = [ ] <EOL> self . _model = model <EOL> def __getstate__ ( self ) : <EOL> return self . _attr_info , self . _model , self . tagName <EOL> def __setstate__ ( self , state ) : <EOL> self . _attr_info , self . _model , self . tagName = state <EOL> def getAttributeType ( self , aname ) : <EOL> for info in self . _attr_info : <EOL> if info [ <NUM_LIT:1> ] == aname : <EOL> t = info [ - <NUM_LIT:2> ] <EOL> if t [ <NUM_LIT:0> ] == "<STR_LIT:(>" : <EOL> return _typeinfo_map [ "<STR_LIT>" ] <EOL> else : <EOL> return _typeinfo_map [ info [ - <NUM_LIT:2> ] ] <EOL> return minidom . _no_type <EOL> def getAttributeTypeNS ( self , namespaceURI , localName ) : <EOL> return minidom . _no_type <EOL> def isElementContent ( self ) : <EOL> if self . _model : <EOL> type = self . _model [ <NUM_LIT:0> ] <EOL> return type not in ( expat . model . XML_CTYPE_ANY , <EOL> expat . model . XML_CTYPE_MIXED ) <EOL> else : <EOL> return False <EOL> def isEmpty ( self ) : <EOL> if self . _model : <EOL> return self . _model [ <NUM_LIT:0> ] == expat . model . XML_CTYPE_EMPTY <EOL> else : <EOL> return False <EOL> def isId ( self , aname ) : <EOL> for info in self . _attr_info : <EOL> if info [ <NUM_LIT:1> ] == aname : <EOL> return info [ - <NUM_LIT:2> ] == "<STR_LIT>" <EOL> return False <EOL> def isIdNS ( self , euri , ename , auri , aname ) : <EOL> return self . isId ( ( auri , aname ) ) <EOL> def _intern ( builder , s ) : <EOL> return builder . _intern_setdefault ( s , s ) <EOL> def _parse_ns_name ( builder , name ) : <EOL> assert '<STR_LIT:U+0020>' in name <EOL> parts = name . split ( '<STR_LIT:U+0020>' ) <EOL> intern = builder . _intern_setdefault <EOL> if len ( parts ) == <NUM_LIT:3> : <EOL> uri , localname , prefix = parts <EOL> prefix = intern ( prefix , prefix ) <EOL> qname = "<STR_LIT>" % ( prefix , localname ) <EOL> qname = intern ( qname , qname ) <EOL> localname = intern ( localname , localname ) <EOL> else : <EOL> uri , localname = parts <EOL> prefix = EMPTY_PREFIX <EOL> qname = localname = intern ( localname , localname ) <EOL> return intern ( uri , uri ) , localname , prefix , qname <EOL> class ExpatBuilder : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , options = None ) : <EOL> if options is None : <EOL> options = xmlbuilder . Options ( ) <EOL> self . _options = options <EOL> if self . _options . filter is not None : <EOL> self . _filter = FilterVisibilityController ( self . _options . filter ) <EOL> else : <EOL> self . _filter = None <EOL> self . _finish_start_element = id <EOL> self . _parser = None <EOL> self . reset ( ) <EOL> def createParser ( self ) : <EOL> """<STR_LIT>""" <EOL> return expat . ParserCreate ( ) <EOL> def getParser ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _parser : <EOL> self . _parser = self . createParser ( ) <EOL> self . _intern_setdefault = self . _parser . intern . setdefault <EOL> self . _parser . buffer_text = True <EOL> self . _parser . ordered_attributes = True <EOL> self . _parser . specified_attributes = True <EOL> self . install ( self . _parser ) <EOL> return self . _parser <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . document = theDOMImplementation . createDocument ( <EOL> EMPTY_NAMESPACE , None , None ) <EOL> self . curNode = self . document <EOL> self . _elem_info = self . document . _elem_info <EOL> self . _cdata = False <EOL> def install ( self , parser ) : <EOL> """<STR_LIT>""" <EOL> parser . StartDoctypeDeclHandler = self . start_doctype_decl_handler <EOL> parser . StartElementHandler = self . first_element_handler <EOL> parser . EndElementHandler = self . end_element_handler <EOL> parser . ProcessingInstructionHandler = self . pi_handler <EOL> if self . _options . entities : <EOL> parser . EntityDeclHandler = self . entity_decl_handler <EOL> parser . NotationDeclHandler = self . notation_decl_handler <EOL> if self . _options . comments : <EOL> parser . CommentHandler = self . comment_handler <EOL> if self . _options . cdata_sections : <EOL> parser . StartCdataSectionHandler = self . start_cdata_section_handler <EOL> parser . EndCdataSectionHandler = self . end_cdata_section_handler <EOL> parser . CharacterDataHandler = self . character_data_handler_cdata <EOL> else : <EOL> parser . CharacterDataHandler = self . character_data_handler <EOL> parser . ExternalEntityRefHandler = self . external_entity_ref_handler <EOL> parser . XmlDeclHandler = self . xml_decl_handler <EOL> parser . ElementDeclHandler = self . element_decl_handler <EOL> parser . AttlistDeclHandler = self . attlist_decl_handler <EOL> def parseFile ( self , file ) : <EOL> """<STR_LIT>""" <EOL> parser = self . getParser ( ) <EOL> first_buffer = True <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> buffer = file . read ( <NUM_LIT:16> * <NUM_LIT> ) <EOL> if not buffer : <EOL> break <EOL> parser . Parse ( buffer , <NUM_LIT:0> ) <EOL> if first_buffer and self . document . documentElement : <EOL> self . _setup_subset ( buffer ) <EOL> first_buffer = False <EOL> parser . Parse ( "<STR_LIT>" , True ) <EOL> except ParseEscape : <EOL> pass <EOL> doc = self . document <EOL> self . reset ( ) <EOL> self . _parser = None <EOL> return doc <EOL> def parseString ( self , string ) : <EOL> """<STR_LIT>""" <EOL> parser = self . getParser ( ) <EOL> try : <EOL> parser . Parse ( string , True ) <EOL> self . _setup_subset ( string ) <EOL> except ParseEscape : <EOL> pass <EOL> doc = self . document <EOL> self . reset ( ) <EOL> self . _parser = None <EOL> return doc <EOL> def _setup_subset ( self , buffer ) : <EOL> """<STR_LIT>""" <EOL> if self . document . doctype : <EOL> extractor = InternalSubsetExtractor ( ) <EOL> extractor . parseString ( buffer ) <EOL> subset = extractor . getSubset ( ) <EOL> self . document . doctype . internalSubset = subset <EOL> def start_doctype_decl_handler ( self , doctypeName , systemId , publicId , <EOL> has_internal_subset ) : <EOL> doctype = self . document . implementation . createDocumentType ( <EOL> doctypeName , publicId , systemId ) <EOL> doctype . ownerDocument = self . document <EOL> self . document . childNodes . append ( doctype ) <EOL> self . document . doctype = doctype <EOL> if self . _filter and self . _filter . acceptNode ( doctype ) == FILTER_REJECT : <EOL> self . document . doctype = None <EOL> del self . document . childNodes [ - <NUM_LIT:1> ] <EOL> doctype = None <EOL> self . _parser . EntityDeclHandler = None <EOL> self . _parser . NotationDeclHandler = None <EOL> if has_internal_subset : <EOL> if doctype is not None : <EOL> doctype . entities . _seq = [ ] <EOL> doctype . notations . _seq = [ ] <EOL> self . _parser . CommentHandler = None <EOL> self . _parser . ProcessingInstructionHandler = None <EOL> self . _parser . EndDoctypeDeclHandler = self . end_doctype_decl_handler <EOL> def end_doctype_decl_handler ( self ) : <EOL> if self . _options . comments : <EOL> self . _parser . CommentHandler = self . comment_handler <EOL> self . _parser . ProcessingInstructionHandler = self . pi_handler <EOL> if not ( self . _elem_info or self . _filter ) : <EOL> self . _finish_end_element = id <EOL> def pi_handler ( self , target , data ) : <EOL> node = self . document . createProcessingInstruction ( target , data ) <EOL> _append_child ( self . curNode , node ) <EOL> if self . _filter and self . _filter . acceptNode ( node ) == FILTER_REJECT : <EOL> self . curNode . removeChild ( node ) <EOL> def character_data_handler_cdata ( self , data ) : <EOL> childNodes = self . curNode . childNodes <EOL> if self . _cdata : <EOL> if ( self . _cdata_continue <EOL> and childNodes [ - <NUM_LIT:1> ] . nodeType == CDATA_SECTION_NODE ) : <EOL> childNodes [ - <NUM_LIT:1> ] . appendData ( data ) <EOL> return <EOL> node = self . document . createCDATASection ( data ) <EOL> self . _cdata_continue = True <EOL> elif childNodes and childNodes [ - <NUM_LIT:1> ] . nodeType == TEXT_NODE : <EOL> node = childNodes [ - <NUM_LIT:1> ] <EOL> value = node . data + data <EOL> d = node . __dict__ <EOL> d [ '<STR_LIT:data>' ] = d [ '<STR_LIT>' ] = value <EOL> return <EOL> else : <EOL> node = minidom . Text ( ) <EOL> d = node . __dict__ <EOL> d [ '<STR_LIT:data>' ] = d [ '<STR_LIT>' ] = data <EOL> d [ '<STR_LIT>' ] = self . document <EOL> _append_child ( self . curNode , node ) <EOL> def character_data_handler ( self , data ) : <EOL> childNodes = self . curNode . childNodes <EOL> if childNodes and childNodes [ - <NUM_LIT:1> ] . nodeType == TEXT_NODE : <EOL> node = childNodes [ - <NUM_LIT:1> ] <EOL> d = node . __dict__ <EOL> d [ '<STR_LIT:data>' ] = d [ '<STR_LIT>' ] = node . data + data <EOL> return <EOL> node = minidom . Text ( ) <EOL> d = node . __dict__ <EOL> d [ '<STR_LIT:data>' ] = d [ '<STR_LIT>' ] = node . data + data <EOL> d [ '<STR_LIT>' ] = self . document <EOL> _append_child ( self . curNode , node ) <EOL> def entity_decl_handler ( self , entityName , is_parameter_entity , value , <EOL> base , systemId , publicId , notationName ) : <EOL> if is_parameter_entity : <EOL> return <EOL> if not self . _options . entities : <EOL> return <EOL> node = self . document . _create_entity ( entityName , publicId , <EOL> systemId , notationName ) <EOL> if value is not None : <EOL> child = self . document . createTextNode ( value ) <EOL> node . childNodes . append ( child ) <EOL> self . document . doctype . entities . _seq . append ( node ) <EOL> if self . _filter and self . _filter . acceptNode ( node ) == FILTER_REJECT : <EOL> del self . document . doctype . entities . _seq [ - <NUM_LIT:1> ] <EOL> def notation_decl_handler ( self , notationName , base , systemId , publicId ) : <EOL> node = self . document . _create_notation ( notationName , publicId , systemId ) <EOL> self . document . doctype . notations . _seq . append ( node ) <EOL> if self . _filter and self . _filter . acceptNode ( node ) == FILTER_ACCEPT : <EOL> del self . document . doctype . notations . _seq [ - <NUM_LIT:1> ] <EOL> def comment_handler ( self , data ) : <EOL> node = self . document . createComment ( data ) <EOL> _append_child ( self . curNode , node ) <EOL> if self . _filter and self . _filter . acceptNode ( node ) == FILTER_REJECT : <EOL> self . curNode . removeChild ( node ) <EOL> def start_cdata_section_handler ( self ) : <EOL> self . _cdata = True <EOL> self . _cdata_continue = False <EOL> def end_cdata_section_handler ( self ) : <EOL> self . _cdata = False <EOL> self . _cdata_continue = False <EOL> def external_entity_ref_handler ( self , context , base , systemId , publicId ) : <EOL> return <NUM_LIT:1> <EOL> def first_element_handler ( self , name , attributes ) : <EOL> if self . _filter is None and not self . _elem_info : <EOL> self . _finish_end_element = id <EOL> self . getParser ( ) . StartElementHandler = self . start_element_handler <EOL> self . start_element_handler ( name , attributes ) <EOL> def start_element_handler ( self , name , attributes ) : <EOL> node = self . document . createElement ( name ) <EOL> _append_child ( self . curNode , node ) <EOL> self . curNode = node <EOL> if attributes : <EOL> for i in range ( <NUM_LIT:0> , len ( attributes ) , <NUM_LIT:2> ) : <EOL> a = minidom . Attr ( attributes [ i ] , EMPTY_NAMESPACE , <EOL> None , EMPTY_PREFIX ) <EOL> value = attributes [ i + <NUM_LIT:1> ] <EOL> d = a . childNodes [ <NUM_LIT:0> ] . __dict__ <EOL> d [ '<STR_LIT:data>' ] = d [ '<STR_LIT>' ] = value <EOL> d = a . __dict__ <EOL> d [ '<STR_LIT:value>' ] = d [ '<STR_LIT>' ] = value <EOL> d [ '<STR_LIT>' ] = self . document <EOL> _set_attribute_node ( node , a ) <EOL> if node is not self . document . documentElement : <EOL> self . _finish_start_element ( node ) <EOL> def _finish_start_element ( self , node ) : <EOL> if self . _filter : <EOL> if node is self . document . documentElement : <EOL> return <EOL> filt = self . _filter . startContainer ( node ) <EOL> if filt == FILTER_REJECT : <EOL> Rejecter ( self ) <EOL> elif filt == FILTER_SKIP : <EOL> Skipper ( self ) <EOL> else : <EOL> return <EOL> self . curNode = node . parentNode <EOL> node . parentNode . removeChild ( node ) <EOL> node . unlink ( ) <EOL> def end_element_handler ( self , name ) : <EOL> curNode = self . curNode <EOL> self . curNode = curNode . parentNode <EOL> self . _finish_end_element ( curNode ) <EOL> def _finish_end_element ( self , curNode ) : <EOL> info = self . _elem_info . get ( curNode . tagName ) <EOL> if info : <EOL> self . _handle_white_text_nodes ( curNode , info ) <EOL> if self . _filter : <EOL> if curNode is self . document . documentElement : <EOL> return <EOL> if self . _filter . acceptNode ( curNode ) == FILTER_REJECT : <EOL> self . curNode . removeChild ( curNode ) <EOL> curNode . unlink ( ) <EOL> def _handle_white_text_nodes ( self , node , info ) : <EOL> if ( self . _options . whitespace_in_element_content <EOL> or not info . isElementContent ( ) ) : <EOL> return <EOL> L = [ ] <EOL> for child in node . childNodes : <EOL> if child . nodeType == TEXT_NODE and not child . data . strip ( ) : <EOL> L . append ( child ) <EOL> for child in L : <EOL> node . removeChild ( child ) <EOL> def element_decl_handler ( self , name , model ) : <EOL> info = self . _elem_info . get ( name ) <EOL> if info is None : <EOL> self . _elem_info [ name ] = ElementInfo ( name , model ) <EOL> else : <EOL> assert info . _model is None <EOL> info . _model = model <EOL> def attlist_decl_handler ( self , elem , name , type , default , required ) : <EOL> info = self . _elem_info . get ( elem ) <EOL> if info is None : <EOL> info = ElementInfo ( elem ) <EOL> self . _elem_info [ elem ] = info <EOL> info . _attr_info . append ( <EOL> [ None , name , None , None , default , <NUM_LIT:0> , type , required ] ) <EOL> def xml_decl_handler ( self , version , encoding , standalone ) : <EOL> self . document . version = version <EOL> self . document . encoding = encoding <EOL> if standalone >= <NUM_LIT:0> : <EOL> if standalone : <EOL> self . document . standalone = True <EOL> else : <EOL> self . document . standalone = False <EOL> _ALLOWED_FILTER_RETURNS = ( FILTER_ACCEPT , FILTER_REJECT , FILTER_SKIP ) <EOL> class FilterVisibilityController ( NewStyle ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = '<STR_LIT>' , <EOL> def __init__ ( self , filter ) : <EOL> self . filter = filter <EOL> def startContainer ( self , node ) : <EOL> mask = self . _nodetype_mask [ node . nodeType ] <EOL> if self . filter . whatToShow & mask : <EOL> val = self . filter . startContainer ( node ) <EOL> if val == FILTER_INTERRUPT : <EOL> raise ParseEscape <EOL> if val not in _ALLOWED_FILTER_RETURNS : <EOL> raise ValueError , "<STR_LIT>" + repr ( val ) <EOL> return val <EOL> else : <EOL> return FILTER_ACCEPT <EOL> def acceptNode ( self , node ) : <EOL> mask = self . _nodetype_mask [ node . nodeType ] <EOL> if self . filter . whatToShow & mask : <EOL> val = self . filter . acceptNode ( node ) <EOL> if val == FILTER_INTERRUPT : <EOL> raise ParseEscape <EOL> if val == FILTER_SKIP : <EOL> parent = node . parentNode <EOL> for child in node . childNodes [ : ] : <EOL> parent . appendChild ( child ) <EOL> return FILTER_REJECT <EOL> if val not in _ALLOWED_FILTER_RETURNS : <EOL> raise ValueError , "<STR_LIT>" + repr ( val ) <EOL> return val <EOL> else : <EOL> return FILTER_ACCEPT <EOL> _nodetype_mask = { <EOL> Node . ELEMENT_NODE : NodeFilter . SHOW_ELEMENT , <EOL> Node . ATTRIBUTE_NODE : NodeFilter . SHOW_ATTRIBUTE , <EOL> Node . TEXT_NODE : NodeFilter . SHOW_TEXT , <EOL> Node . CDATA_SECTION_NODE : NodeFilter . SHOW_CDATA_SECTION , <EOL> Node . ENTITY_REFERENCE_NODE : NodeFilter . SHOW_ENTITY_REFERENCE , <EOL> Node . ENTITY_NODE : NodeFilter . SHOW_ENTITY , <EOL> Node . PROCESSING_INSTRUCTION_NODE : NodeFilter . SHOW_PROCESSING_INSTRUCTION , <EOL> Node . COMMENT_NODE : NodeFilter . SHOW_COMMENT , <EOL> Node . DOCUMENT_NODE : NodeFilter . SHOW_DOCUMENT , <EOL> Node . DOCUMENT_TYPE_NODE : NodeFilter . SHOW_DOCUMENT_TYPE , <EOL> Node . DOCUMENT_FRAGMENT_NODE : NodeFilter . SHOW_DOCUMENT_FRAGMENT , <EOL> Node . NOTATION_NODE : NodeFilter . SHOW_NOTATION , <EOL> } <EOL> class FilterCrutch ( NewStyle ) : <EOL> __slots__ = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> def __init__ ( self , builder ) : <EOL> self . _level = <NUM_LIT:0> <EOL> self . _builder = builder <EOL> parser = builder . _parser <EOL> self . _old_start = parser . StartElementHandler <EOL> self . _old_end = parser . EndElementHandler <EOL> parser . StartElementHandler = self . start_element_handler <EOL> parser . EndElementHandler = self . end_element_handler <EOL> class Rejecter ( FilterCrutch ) : <EOL> __slots__ = ( ) <EOL> def __init__ ( self , builder ) : <EOL> FilterCrutch . __init__ ( self , builder ) <EOL> parser = builder . _parser <EOL> for name in ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) : <EOL> setattr ( parser , name , None ) <EOL> def start_element_handler ( self , * args ) : <EOL> self . _level = self . _level + <NUM_LIT:1> <EOL> def end_element_handler ( self , * args ) : <EOL> if self . _level == <NUM_LIT:0> : <EOL> parser = self . _builder . _parser <EOL> self . _builder . install ( parser ) <EOL> parser . StartElementHandler = self . _old_start <EOL> parser . EndElementHandler = self . _old_end <EOL> else : <EOL> self . _level = self . _level - <NUM_LIT:1> <EOL> class Skipper ( FilterCrutch ) : <EOL> __slots__ = ( ) <EOL> def start_element_handler ( self , * args ) : <EOL> node = self . _builder . curNode <EOL> self . _old_start ( * args ) <EOL> if self . _builder . curNode is not node : <EOL> self . _level = self . _level + <NUM_LIT:1> <EOL> def end_element_handler ( self , * args ) : <EOL> if self . _level == <NUM_LIT:0> : <EOL> self . _builder . _parser . StartElementHandler = self . _old_start <EOL> self . _builder . _parser . EndElementHandler = self . _old_end <EOL> self . _builder = None <EOL> else : <EOL> self . _level = self . _level - <NUM_LIT:1> <EOL> self . _old_end ( * args ) <EOL> _FRAGMENT_BUILDER_INTERNAL_SYSTEM_ID = "<STR_LIT>" <EOL> _FRAGMENT_BUILDER_TEMPLATE = ( <EOL> '''<STR_LIT>''' <EOL> % _FRAGMENT_BUILDER_INTERNAL_SYSTEM_ID ) <EOL> class FragmentBuilder ( ExpatBuilder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , context , options = None ) : <EOL> if context . nodeType == DOCUMENT_NODE : <EOL> self . originalDocument = context <EOL> self . context = context <EOL> else : <EOL> self . originalDocument = context . ownerDocument <EOL> self . context = context <EOL> ExpatBuilder . __init__ ( self , options ) <EOL> def reset ( self ) : <EOL> ExpatBuilder . reset ( self ) <EOL> self . fragment = None <EOL> def parseFile ( self , file ) : <EOL> """<STR_LIT>""" <EOL> return self . parseString ( file . read ( ) ) <EOL> def parseString ( self , string ) : <EOL> """<STR_LIT>""" <EOL> self . _source = string <EOL> parser = self . getParser ( ) <EOL> doctype = self . originalDocument . doctype <EOL> ident = "<STR_LIT>" <EOL> if doctype : <EOL> subset = doctype . internalSubset or self . _getDeclarations ( ) <EOL> if doctype . publicId : <EOL> ident = ( '<STR_LIT>' <EOL> % ( doctype . publicId , doctype . systemId ) ) <EOL> elif doctype . systemId : <EOL> ident = '<STR_LIT>' % doctype . systemId <EOL> else : <EOL> subset = "<STR_LIT>" <EOL> nsattrs = self . _getNSattrs ( ) <EOL> document = _FRAGMENT_BUILDER_TEMPLATE % ( ident , subset , nsattrs ) <EOL> try : <EOL> parser . Parse ( document , <NUM_LIT:1> ) <EOL> except : <EOL> self . reset ( ) <EOL> raise <EOL> fragment = self . fragment <EOL> self . reset ( ) <EOL> return fragment <EOL> def _getDeclarations ( self ) : <EOL> """<STR_LIT>""" <EOL> doctype = self . context . ownerDocument . doctype <EOL> s = "<STR_LIT>" <EOL> if doctype : <EOL> for i in range ( doctype . notations . length ) : <EOL> notation = doctype . notations . item ( i ) <EOL> if s : <EOL> s = s + "<STR_LIT>" <EOL> s = "<STR_LIT>" % ( s , notation . nodeName ) <EOL> if notation . publicId : <EOL> s = '<STR_LIT>' % ( s , notation . publicId , notation . systemId ) <EOL> else : <EOL> s = '<STR_LIT>' % ( s , notation . systemId ) <EOL> for i in range ( doctype . entities . length ) : <EOL> entity = doctype . entities . item ( i ) <EOL> if s : <EOL> s = s + "<STR_LIT>" <EOL> s = "<STR_LIT>" % ( s , entity . nodeName ) <EOL> if entity . publicId : <EOL> s = '<STR_LIT>' % ( s , entity . publicId , entity . systemId ) <EOL> elif entity . systemId : <EOL> s = '<STR_LIT>' % ( s , entity . systemId ) <EOL> else : <EOL> s = '<STR_LIT>' % ( s , entity . firstChild . data ) <EOL> if entity . notationName : <EOL> s = "<STR_LIT>" % ( s , entity . notationName ) <EOL> s = s + "<STR_LIT:>>" <EOL> return s <EOL> def _getNSattrs ( self ) : <EOL> return "<STR_LIT>" <EOL> def external_entity_ref_handler ( self , context , base , systemId , publicId ) : <EOL> if systemId == _FRAGMENT_BUILDER_INTERNAL_SYSTEM_ID : <EOL> old_document = self . document <EOL> old_cur_node = self . curNode <EOL> parser = self . _parser . ExternalEntityParserCreate ( context ) <EOL> self . document = self . originalDocument <EOL> self . fragment = self . document . createDocumentFragment ( ) <EOL> self . curNode = self . fragment <EOL> try : <EOL> parser . Parse ( self . _source , <NUM_LIT:1> ) <EOL> finally : <EOL> self . curNode = old_cur_node <EOL> self . document = old_document <EOL> self . _source = None <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> return ExpatBuilder . external_entity_ref_handler ( <EOL> self , context , base , systemId , publicId ) <EOL> class Namespaces : <EOL> """<STR_LIT>""" <EOL> def _initNamespaces ( self ) : <EOL> self . _ns_ordered_prefixes = [ ] <EOL> def createParser ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = expat . ParserCreate ( namespace_separator = "<STR_LIT:U+0020>" ) <EOL> parser . namespace_prefixes = True <EOL> return parser <EOL> def install ( self , parser ) : <EOL> """<STR_LIT>""" <EOL> ExpatBuilder . install ( self , parser ) <EOL> if self . _options . namespace_declarations : <EOL> parser . StartNamespaceDeclHandler = ( <EOL> self . start_namespace_decl_handler ) <EOL> def start_namespace_decl_handler ( self , prefix , uri ) : <EOL> """<STR_LIT>""" <EOL> self . _ns_ordered_prefixes . append ( ( prefix , uri ) ) <EOL> def start_element_handler ( self , name , attributes ) : <EOL> if '<STR_LIT:U+0020>' in name : <EOL> uri , localname , prefix , qname = _parse_ns_name ( self , name ) <EOL> else : <EOL> uri = EMPTY_NAMESPACE <EOL> qname = name <EOL> localname = None <EOL> prefix = EMPTY_PREFIX <EOL> node = minidom . Element ( qname , uri , prefix , localname ) <EOL> node . ownerDocument = self . document <EOL> _append_child ( self . curNode , node ) <EOL> self . curNode = node <EOL> if self . _ns_ordered_prefixes : <EOL> for prefix , uri in self . _ns_ordered_prefixes : <EOL> if prefix : <EOL> a = minidom . Attr ( _intern ( self , '<STR_LIT>' + prefix ) , <EOL> XMLNS_NAMESPACE , prefix , "<STR_LIT>" ) <EOL> else : <EOL> a = minidom . Attr ( "<STR_LIT>" , XMLNS_NAMESPACE , <EOL> "<STR_LIT>" , EMPTY_PREFIX ) <EOL> d = a . childNodes [ <NUM_LIT:0> ] . __dict__ <EOL> d [ '<STR_LIT:data>' ] = d [ '<STR_LIT>' ] = uri <EOL> d = a . __dict__ <EOL> d [ '<STR_LIT:value>' ] = d [ '<STR_LIT>' ] = uri <EOL> d [ '<STR_LIT>' ] = self . document <EOL> _set_attribute_node ( node , a ) <EOL> del self . _ns_ordered_prefixes [ : ] <EOL> if attributes : <EOL> _attrs = node . _attrs <EOL> _attrsNS = node . _attrsNS <EOL> for i in range ( <NUM_LIT:0> , len ( attributes ) , <NUM_LIT:2> ) : <EOL> aname = attributes [ i ] <EOL> value = attributes [ i + <NUM_LIT:1> ] <EOL> if '<STR_LIT:U+0020>' in aname : <EOL> uri , localname , prefix , qname = _parse_ns_name ( self , aname ) <EOL> a = minidom . Attr ( qname , uri , localname , prefix ) <EOL> _attrs [ qname ] = a <EOL> _attrsNS [ ( uri , localname ) ] = a <EOL> else : <EOL> a = minidom . Attr ( aname , EMPTY_NAMESPACE , <EOL> aname , EMPTY_PREFIX ) <EOL> _attrs [ aname ] = a <EOL> _attrsNS [ ( EMPTY_NAMESPACE , aname ) ] = a <EOL> d = a . childNodes [ <NUM_LIT:0> ] . __dict__ <EOL> d [ '<STR_LIT:data>' ] = d [ '<STR_LIT>' ] = value <EOL> d = a . __dict__ <EOL> d [ '<STR_LIT>' ] = self . document <EOL> d [ '<STR_LIT:value>' ] = d [ '<STR_LIT>' ] = value <EOL> d [ '<STR_LIT>' ] = node <EOL> if __debug__ : <EOL> def end_element_handler ( self , name ) : <EOL> curNode = self . curNode <EOL> if '<STR_LIT:U+0020>' in name : <EOL> uri , localname , prefix , qname = _parse_ns_name ( self , name ) <EOL> assert ( curNode . namespaceURI == uri <EOL> and curNode . localName == localname <EOL> and curNode . prefix == prefix ) , "<STR_LIT>" <EOL> else : <EOL> assert curNode . nodeName == name , "<STR_LIT>" <EOL> assert curNode . namespaceURI == EMPTY_NAMESPACE , "<STR_LIT>" <EOL> self . curNode = curNode . parentNode <EOL> self . _finish_end_element ( curNode ) <EOL> class ExpatBuilderNS ( Namespaces , ExpatBuilder ) : <EOL> """<STR_LIT>""" <EOL> def reset ( self ) : <EOL> ExpatBuilder . reset ( self ) <EOL> self . _initNamespaces ( ) <EOL> class FragmentBuilderNS ( Namespaces , FragmentBuilder ) : <EOL> """<STR_LIT>""" <EOL> def reset ( self ) : <EOL> FragmentBuilder . reset ( self ) <EOL> self . _initNamespaces ( ) <EOL> def _getNSattrs ( self ) : <EOL> """<STR_LIT>""" <EOL> attrs = "<STR_LIT>" <EOL> context = self . context <EOL> L = [ ] <EOL> while context : <EOL> if hasattr ( context , '<STR_LIT>' ) : <EOL> for prefix , uri in context . _ns_prefix_uri . items ( ) : <EOL> if prefix in L : <EOL> continue <EOL> L . append ( prefix ) <EOL> if prefix : <EOL> declname = "<STR_LIT>" + prefix <EOL> else : <EOL> declname = "<STR_LIT>" <EOL> if attrs : <EOL> attrs = "<STR_LIT>" % ( attrs , declname , uri ) <EOL> else : <EOL> attrs = "<STR_LIT>" % ( declname , uri ) <EOL> context = context . parentNode <EOL> return attrs <EOL> class ParseEscape ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class InternalSubsetExtractor ( ExpatBuilder ) : <EOL> """<STR_LIT>""" <EOL> subset = None <EOL> def getSubset ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . subset <EOL> def parseFile ( self , file ) : <EOL> try : <EOL> ExpatBuilder . parseFile ( self , file ) <EOL> except ParseEscape : <EOL> pass <EOL> def parseString ( self , string ) : <EOL> try : <EOL> ExpatBuilder . parseString ( self , string ) <EOL> except ParseEscape : <EOL> pass <EOL> def install ( self , parser ) : <EOL> parser . StartDoctypeDeclHandler = self . start_doctype_decl_handler <EOL> parser . StartElementHandler = self . start_element_handler <EOL> def start_doctype_decl_handler ( self , name , publicId , systemId , <EOL> has_internal_subset ) : <EOL> if has_internal_subset : <EOL> parser = self . getParser ( ) <EOL> self . subset = [ ] <EOL> parser . DefaultHandler = self . subset . append <EOL> parser . EndDoctypeDeclHandler = self . end_doctype_decl_handler <EOL> else : <EOL> raise ParseEscape ( ) <EOL> def end_doctype_decl_handler ( self ) : <EOL> s = '<STR_LIT>' . join ( self . subset ) . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) . replace ( '<STR_LIT:\r>' , '<STR_LIT:\n>' ) <EOL> self . subset = s <EOL> raise ParseEscape ( ) <EOL> def start_element_handler ( self , name , attrs ) : <EOL> raise ParseEscape ( ) <EOL> def parse ( file , namespaces = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if namespaces : <EOL> builder = ExpatBuilderNS ( ) <EOL> else : <EOL> builder = ExpatBuilder ( ) <EOL> if isinstance ( file , StringTypes ) : <EOL> fp = open ( file , '<STR_LIT:rb>' ) <EOL> try : <EOL> result = builder . parseFile ( fp ) <EOL> finally : <EOL> fp . close ( ) <EOL> else : <EOL> result = builder . parseFile ( file ) <EOL> return result <EOL> def parseString ( string , namespaces = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if namespaces : <EOL> builder = ExpatBuilderNS ( ) <EOL> else : <EOL> builder = ExpatBuilder ( ) <EOL> return builder . parseString ( string ) <EOL> def parseFragment ( file , context , namespaces = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if namespaces : <EOL> builder = FragmentBuilderNS ( context ) <EOL> else : <EOL> builder = FragmentBuilder ( context ) <EOL> if isinstance ( file , StringTypes ) : <EOL> fp = open ( file , '<STR_LIT:rb>' ) <EOL> try : <EOL> result = builder . parseFile ( fp ) <EOL> finally : <EOL> fp . close ( ) <EOL> else : <EOL> result = builder . parseFile ( file ) <EOL> return result <EOL> def parseFragmentString ( string , context , namespaces = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if namespaces : <EOL> builder = FragmentBuilderNS ( context ) <EOL> else : <EOL> builder = FragmentBuilder ( context ) <EOL> return builder . parseString ( string ) <EOL> def makeBuilder ( options ) : <EOL> """<STR_LIT>""" <EOL> if options . namespaces : <EOL> return ExpatBuilderNS ( options ) <EOL> else : <EOL> return ExpatBuilder ( options ) </s>
