Package pygccxml :: Package parser :: Module etree_scanner

# Source Code for Module pygccxml.parser.etree_scanner

     1  # Copyright 2004-2008 Roman Yakovenko.
     2  # Distributed under the Boost Software License, Version 1.0. (See
     3  # accompanying file LICENSE_1_0.txt or copy at
     4  # http://www.boost.org/LICENSE_1_0.txt)
     5
     6  import scanner
     7  import xml.etree.cElementTree as ElementTree
     8
     9 -class etree_saxifier_t(object):
    10 -    def __init__(self, etree, handler):
    11          self.__root_elem = etree.getroot()
    12          self.__handler = handler
    13
    14 -    def saxify(self):
    15          self.__handler.startDocument()
    16          self.__recursive_saxify( self.__root_elem )
    17          self.__handler.endDocument()
    18
    19 -    def __recursive_saxify(self, element ):
    20          self.__handler.startElement( element.tag, element.attrib )
    21          map( self.__recursive_saxify, element )
    22          self.__handler.endElement( element.tag )
    23
    24 -class etree_scanner_t( scanner.scanner_t ):
    25 -    def __init__(self, gccxml_file, decl_factory, *args ):
    26          scanner.scanner_t.__init__( self, gccxml_file, decl_factory, *args )
    27
    28 -    def read( self ):
    29          tree = ElementTree.parse( self.gccxml_file )
    30          saxifier = etree_saxifier_t( tree, self )
    31          saxifier.saxify()
    32
    33 -class ietree_scanner_t( scanner.scanner_t ):
    34 -    def __init__(self, gccxml_file, decl_factory, *args ):
    35          scanner.scanner_t.__init__( self, gccxml_file, decl_factory, *args )
    36
    37 -    def read( self ):
    38          context = ElementTree.iterparse(self.gccxml_file, events=("start", "end"))
    39          for event, elem in context:
    40              if event == 'start':
    41                  self.startElement( elem.tag, elem.attrib )
    42              else:
    43                  self.endElement( elem.tag )
    44                  elem.clear()
    45          self.endDocument()
    46
    47  etree_scanner_t = ietree_scanner_t
    48