
    Sh;                        S r SrSrSSKrSSKr\R
                  S   S:  a2  \R                  " \5      S   r\\4r	\R                  " S5      S   rS rO\r	S	 rSS
K7  SSKJrJr  SSKJrJrJrJrJrJrJrJrJrJr   SSKr " S S\R@                  5      r  " S S\RB                  5      r"S r#g! \ a    \" S\R>                  " 5       S   -  5      ef = f)a  A SAX2 driver for libxml2, on top of it's XmlReader API

USAGE
    # put this file (drv_libxml2.py) in PYTHONPATH
    import xml.sax
    reader = xml.sax.make_parser(["drv_libxml2"])
    # ...and the rest is standard python sax.

CAVEATS
    - Lexical handlers are supported, except for start/endEntity
      (waiting for XmlReader.ResolveEntity) and start/endDTD
    - Error callbacks are not exactly synchronous, they tend
      to be invoked before the corresponding content callback,
      because the underlying reader interface parses
      data by chunks of 512 bytes
    
TODO
    - search for TODO
    - some ErrorHandler events (warning)
    - some ContentHandler events (setDocumentLocator, skippedEntity)
    - EntityResolver (using libxml2.?)
    - DTDHandler (if/when libxml2 exposes such node types)
    - DeclHandler (if/when libxml2 exposes such node types)
    - property_xml_string?
    - feature_string_interning?
    - Incremental parser
    - additional performance tuning:
      - one might cache callbacks to avoid some name lookups
      - one might implement a smarter way to pass attributes to startElement
        (some kind of lazy evaluation?)
      - there might be room for improvement in start/endPrefixMapping
      - other?

u    Stéphane Bidoul <sbi@skynet.be>z0.3    N   utf8   c                 (    U c  U $ [        U 5      S   $ )Nr   )_decoderss    -/usr/lib/python3/dist-packages/drv_libxml2.py_dr   1   s    9HA;q>!    c                     U $ N r   s    r
   r   r   9   s    r   )*)	xmlreadersaxutils)
feature_namespacesfeature_namespace_prefixesfeature_string_interningfeature_validationfeature_external_gesfeature_external_pesproperty_lexical_handlerproperty_declaration_handlerproperty_dom_nodeproperty_xml_stringz+libxml2 not available: import error was: %sc                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
LocatorP   z4SAX Locator adapter for libxml2.xmlTextReaderLocatorc                     Xl         g r   )_Locator__locator)selflocators     r
   __init__Locator.__init__S   s     r   c                     g)z6Return the column number where the current event ends.r   r"   s    r
   getColumnNumberLocator.getColumnNumberV   s    r   c                 6    U R                   R                  5       $ )z4Return the line number where the current event ends.)r!   
LineNumberr(   s    r
   getLineNumberLocator.getLineNumberZ   s    ~~((**r   c                     g)z3Return the public identifier for the current event.Nr   r(   s    r
   getPublicIdLocator.getPublicId^   s    r   c                 6    U R                   R                  5       $ )z3Return the system identifier for the current event.)r!   BaseURIr(   s    r
   getSystemIdLocator.getSystemIdb   s    ~~%%''r   )	__locatorN)__name__
__module____qualname____firstlineno____doc__r$   r)   r-   r0   r4   __static_attributes__r   r   r
   r   r   P   s    >!+(r   r   c                   P    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rSrg)LibXml2Readerf   c                     [         R                  R                  U 5        SU l        SU l        SU l        SU l        SU l        S U l        S U l	        S U l
        g )Nr   r   )r   	XMLReaderr$   _LibXml2Reader__ns_LibXml2Reader__nspfx_LibXml2Reader__validate_LibXml2Reader__extparams_LibXml2Reader__parsing_LibXml2Reader__lex_handler_LibXml2Reader__decl_handler_LibXml2Reader__errorsr(   s    r
   r$   LibXml2Reader.__init__h   sQ    $$T*	!"r   c           
          U R                   c  / U l         U R                   R                  U[        US [        U5      5      45        g r   )rI   appendSAXParseExceptionr   )r"   argmsgseverityr#   s        r
   _errorHandlerLibXml2Reader._errorHandlerw   sC    == DMh/D070@BC 	Dr   c                 d   U R                    H  u  p#U[        R                  [        R                  4;   a  U R                  R                  U5        MF  U(       a1  X0R                   S   S   L a  U R                  R                  U5        M~  U R                  R                  U5        M     S U l         g )Nr'   r   )rI   libxml2 PARSER_SEVERITY_VALIDITY_WARNINGPARSER_SEVERITY_WARNING_err_handlerwarning
fatalErrorerror)r"   fatalrP   	exceptions       r
   _reportErrorsLibXml2Reader._reportErrors~   s    "&--HGDD#;;= =!!)))4
 Y--*;A*>>%%00;%%++I6 #0 r   c                    SU l          [        U[        5      (       a  [        R                  " U5      nOY[
        R                  " U5      n[        R                  " UR                  5       5      nUR                  UR                  5       5      nUR                  U R                  S 5        U R                  (       a  UR                  [        R                  S5        UR                  [        R                   S5        UR                  [        R"                  S5        UR                  [        R$                  U R&                  5        O UR                  [        R                  S5        U R(                  (       a  [*        R,                  " 0 0 5      nO[*        R.                  " 0 5      n/ nU R0                  R3                  5          UR5                  5       nUS:X  a  U R6                  b  U R9                  S5        OkUS:X  a   U R6                  b  U R9                  S5        GOU R6                  b  U R9                  S5        O$U R:                  R=                  [?        S5      5        GOfURA                  5       nUS:X  Ga  U R(                  (       Ga  [C        URE                  5       5      [C        URG                  5       5      4n	[C        URI                  5       5      n
0 =Wl%        n0 =Ul&        n/ nURO                  5       (       a  [C        URI                  5       5      n[C        URQ                  5       5      nURS                  S5      (       aW  [U        U5      S:  a  USS  nOS nURW                  U5        U R0                  RY                  UU5        U RZ                  (       d  M  [C        URE                  5       5      [C        URG                  5       5      4nXU'   XU'   URO                  5       (       a  M  UR]                  5         U R0                  R_                  XU5        URa                  5       (       aA  U R0                  Rc                  X5        U H  nU R0                  Re                  U5        M      GOTURW                  U5        GOA[C        URI                  5       5      n	0 =Wl%        nURO                  5       (       aL  [C        URI                  5       5      n[C        URQ                  5       5      UU'   URO                  5       (       a  ML  UR]                  5         U R0                  Rg                  X5        URa                  5       (       a  U R0                  Ri                  U	5        GOaUS:X  a  U R(                  (       a  U R0                  Rc                  [C        URE                  5       5      [C        URG                  5       5      4[C        URI                  5       5      5        URk                  5        H  nU R0                  Re                  U5        M      GOU R0                  Ri                  [C        URI                  5       5      5        GOUS:X  a4  U R0                  Rm                  [C        URQ                  5       5      5        GOEUS	:X  a4  U R0                  Ro                  [C        URQ                  5       5      5        GOUS
:X  a4  U R0                  Rm                  [C        URQ                  5       5      5        GOUS:X  a  U Rp                  b  U Rp                  Rs                  5         U R0                  Rm                  [C        URQ                  5       5      5        U Rp                  b  U Rp                  Ru                  5         GOIUS:X  aF  U Rp                  b(  U Rw                  [C        URI                  5       5      5        URy                  5         OUS:X  a6  U Rp                  b(  U R{                  [C        URI                  5       5      5        OUS:X  aK  U R0                  R}                  [C        URI                  5       5      [C        URQ                  5       5      5        OpUS:X  a@  U Rp                  b2  U Rp                  R                  [C        URQ                  5       5      5        O*US:X  a  O#US:X  a  OUS:X  a  OUS:X  a  O[?        SU-  5      eGM  US:X  a  U R0                  R                  5         UR                  5         SU l         g ! SU l         f = f)Nr   r   z"Read failed (no details available)xmlns         r                     
         zUnexpected node type %d)BrF   
isinstanceStringTypesrT   newTextReaderFilenamer   prepare_input_sourceinputBuffergetByteStreamnewTextReaderr4   SetErrorHandlerrQ   rE   SetParserPropPARSER_LOADDTDPARSER_DEFAULTATTRSPARSER_SUBST_ENTITIESPARSER_VALIDATErD   rB   r   AttributesNSImplAttributesImpl_cont_handlerstartDocumentReadrI   r]   rW   rY   SAXExceptionNodeTyper   NamespaceUri	LocalNameName_attrs_qnamesMoveToNextAttributeValue
startswithlenrL   startPrefixMappingrC   MoveToElementstartElementNSIsEmptyElementendElementNSendPrefixMappingstartElement
endElementpop
charactersignorableWhitespacerG   
startCDATAendCDATAstartEntityResolveEntity	endEntityprocessingInstructioncommentendDocumentClose)r"   sourcereaderinputattributesNSImplattributesImplprefixesrnodeTypeeltNameeltQNameattrsqnamesnewPrefixesqnamevalue	newPrefixattNameprefixs                      r
   parseLibXml2Reader.parse   s   g	&+.. 66v>!66v>++F,@,@,BC,,V-?-?-AB""4#5#5d;$$W%;%;A>$$W%@%@C$$W%B%B1E$$W%<%<T__M$$W%;%;Q?yy#,#=#=b#D !*!9!9"!=H,,.KKM6==0**1-!V==0**1-==0**1-))44()MNP!??,q=yyy#%f&9&9&;#<#%f&6&6&8#9#;#%fkkm#4:<<(/%<>>(06&($88::$&v{{}$5E$&v||~$6E$//88#&u:>05ab	I04I + 2 29 = $ 2 2 E E$-e!5'+||$,')&*=*=*?'@')&*:*:*<'='?G.37O-2'N! %88::" ,,.**99#-=?!0022 ..;;GM-8	 $ 2 2 C CI N .9 %OOK8"$V[[]"38::-$88::&(&7G-/-?E'N %88:: ,,.**77#4!0022 ..99'B^yy**77 !4!4!676;K;K;M8NO.0 '/llnF ..??G '5 **55b6GH]&&11"V\\^2DE^&&::2flln;MN^&&11"V\\^2DE]--5**557&&11"V\\^2DE--5**335]--5((FKKM):;((*^--5r&++-'89]&&<<6;;=)"V\\^*<> ]--5**222flln3EF^ ^]^ ''@8'KLLQ R Av""..0LLNDNQDNs   O$d  /Dd  O1d   	d	c                     [        S5      e)NzDTDHandler not supportedSAXNotSupportedException)r"   handlers     r
   setDTDHandlerLibXml2Reader.setDTDHandler8  s    &'ABBr   c                     [        S5      e)NzEntityResolver not supportedr   )r"   resolvers     r
   setEntityResolverLibXml2Reader.setEntityResolver<  s    &'EFFr   c                     U[         :X  a  U R                  $ U[        :X  a  U R                  $ U[        :X  a  U R
                  $ U[        :X  a  gU[        :X  a  U R                  $ [        SU-  5      e)Nr   Feature '%s' not recognized)
r   rB   r   rC   r   rD   r   r   rE   SAXNotRecognizedExceptionr"   names     r
   
getFeatureLibXml2Reader.getFeature@  st    %%99//<<''??"))))###+,I,0-1 2 2r   c                 "   U R                   (       a  [        SU-  5      eU[        :X  a  X l        g U[        :X  a  X l        g U[        :X  a  X l        g U[        :X  a  US:X  a  [        SU-  5      eg U[        :X  a  X l
        g [        SU-  5      e)Nz#Cannot set feature %s while parsingr   zFeature '%s' not supportedr   )rF   r   r   rB   r   rC   r   rD   r   r   rE   r   )r"   r   states      r
   
setFeatureLibXml2Reader.setFeatureO  s    >>* ,;=A,B C C%%I// L''#O))z./K/304 5 5  ))$+,I,0-1 2 2r   c                 v    U[         :X  a  U R                  $ U[        :X  a  U R                  $ [	        SU-  5      e)NProperty '%s' not recognized)r   rG   r   rH   r   r   s     r
   getPropertyLibXml2Reader.getPropertyd  sD    ++%%%11&&&+,J,0-1 2 2r   c                 p    U[         :X  a  X l        g U[        :X  a  [        SU-  5      e[        SU-  5      e)NzProperty '%s' not supportedr   )r   rG   r   r   rH   r   )r"   r   r   s      r
   setPropertyLibXml2Reader.setPropertym  sL    ++!&11*+H+/,0 1 1 ,,J,0-1 2 2r   )__decl_handler__errors__extparams__lex_handler__ns__nspfx	__parsing
__validateN)r7   r8   r9   r:   r$   rQ   r]   r   r   r   r   r   r   r   r<   r   r   r
   r>   r>   f   s9    DiVCG22*2
2r   r>   c                      [        5       $ r   )r>   r   r   r
   create_parserr   y  s
    ?r   )$r;   
__author____version__syscodecsversion_infounicode_escape_decodestrunicodern   lookupr   r   xml.sax._exceptionsxml.saxr   r   xml.sax.handlerr   r   r   r   r   r   r   r   r   r   rT   ImportErrorSAXReaderNotAvailableexc_infor   rA   r>   r   r   r   r
   <module>r      s   !F 1
 
 A..z:1=J.K}}V$Q'H" K " '
 
 
L
(i (,Q2I'' Q2f[	  L
 !79<9J!K L LLs   ?B1 1$C