home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / pulldom.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2003-12-30  |  18KB  |  441 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. import xml.sax as xml
  5. import xml.sax.handler as xml
  6. import types
  7.  
  8. try:
  9.     _StringTypes = [
  10.         types.StringType,
  11.         types.UnicodeType]
  12. except AttributeError:
  13.     _StringTypes = [
  14.         types.StringType]
  15.  
  16. START_ELEMENT = 'START_ELEMENT'
  17. END_ELEMENT = 'END_ELEMENT'
  18. COMMENT = 'COMMENT'
  19. START_DOCUMENT = 'START_DOCUMENT'
  20. END_DOCUMENT = 'END_DOCUMENT'
  21. PROCESSING_INSTRUCTION = 'PROCESSING_INSTRUCTION'
  22. IGNORABLE_WHITESPACE = 'IGNORABLE_WHITESPACE'
  23. CHARACTERS = 'CHARACTERS'
  24.  
  25. class PullDOM(xml.sax.ContentHandler):
  26.     _locator = None
  27.     document = None
  28.     
  29.     def __init__(self, documentFactory = None):
  30.         XML_NAMESPACE = XML_NAMESPACE
  31.         import xml.dom
  32.         self.documentFactory = documentFactory
  33.         self.firstEvent = [
  34.             None,
  35.             None]
  36.         self.lastEvent = self.firstEvent
  37.         self.elementStack = []
  38.         self.push = self.elementStack.append
  39.         
  40.         try:
  41.             self.pop = self.elementStack.pop
  42.         except AttributeError:
  43.             pass
  44.  
  45.         self._ns_contexts = [
  46.             {
  47.                 XML_NAMESPACE: 'xml' }]
  48.         self._current_context = self._ns_contexts[-1]
  49.         self.pending_events = []
  50.  
  51.     
  52.     def pop(self):
  53.         result = self.elementStack[-1]
  54.         del self.elementStack[-1]
  55.         return result
  56.  
  57.     
  58.     def setDocumentLocator(self, locator):
  59.         self._locator = locator
  60.  
  61.     
  62.     def startPrefixMapping(self, prefix, uri):
  63.         if not hasattr(self, '_xmlns_attrs'):
  64.             self._xmlns_attrs = []
  65.         
  66.         if not prefix:
  67.             pass
  68.         self._xmlns_attrs.append(('xmlns', uri))
  69.         self._ns_contexts.append(self._current_context.copy())
  70.         if not prefix:
  71.             pass
  72.         self._current_context[uri] = None
  73.  
  74.     
  75.     def endPrefixMapping(self, prefix):
  76.         self._current_context = self._ns_contexts.pop()
  77.  
  78.     
  79.     def startElementNS(self, name, tagName, attrs):
  80.         xmlns_uri = 'http://www.w3.org/2000/xmlns/'
  81.         xmlns_attrs = getattr(self, '_xmlns_attrs', None)
  82.         if xmlns_attrs is not None:
  83.             for aname, value in xmlns_attrs:
  84.                 attrs._attrs[(xmlns_uri, aname)] = value
  85.             
  86.             self._xmlns_attrs = []
  87.         
  88.         (uri, localname) = name
  89.         if uri:
  90.             if tagName is None:
  91.                 prefix = self._current_context[uri]
  92.                 if prefix:
  93.                     tagName = prefix + ':' + localname
  94.                 else:
  95.                     tagName = localname
  96.             
  97.             if self.document:
  98.                 node = self.document.createElementNS(uri, tagName)
  99.             else:
  100.                 node = self.buildDocument(uri, tagName)
  101.         elif self.document:
  102.             node = self.document.createElement(localname)
  103.         else:
  104.             node = self.buildDocument(None, localname)
  105.         for aname, value in attrs.items():
  106.             (a_uri, a_localname) = aname
  107.             if a_uri == xmlns_uri:
  108.                 if a_localname == 'xmlns':
  109.                     qname = a_localname
  110.                 else:
  111.                     qname = 'xmlns:' + a_localname
  112.                 attr = self.document.createAttributeNS(a_uri, qname)
  113.                 node.setAttributeNodeNS(attr)
  114.             elif a_uri:
  115.                 prefix = self._current_context[a_uri]
  116.                 if prefix:
  117.                     qname = prefix + ':' + a_localname
  118.                 else:
  119.                     qname = a_localname
  120.                 attr = self.document.createAttributeNS(a_uri, qname)
  121.                 node.setAttributeNodeNS(attr)
  122.             else:
  123.                 attr = self.document.createAttribute(a_localname)
  124.                 node.setAttributeNode(attr)
  125.             attr.value = value
  126.         
  127.         self.lastEvent[1] = [
  128.             (START_ELEMENT, node),
  129.             None]
  130.         self.lastEvent = self.lastEvent[1]
  131.         self.push(node)
  132.  
  133.     
  134.     def endElementNS(self, name, tagName):
  135.         self.lastEvent[1] = [
  136.             (END_ELEMENT, self.pop()),
  137.             None]
  138.         self.lastEvent = self.lastEvent[1]
  139.  
  140.     
  141.     def startElement(self, name, attrs):
  142.         if self.document:
  143.             node = self.document.createElement(name)
  144.         else:
  145.             node = self.buildDocument(None, name)
  146.         for aname, value in attrs.items():
  147.             attr = self.document.createAttribute(aname)
  148.             attr.value = value
  149.             node.setAttributeNode(attr)
  150.         
  151.         self.lastEvent[1] = [
  152.             (START_ELEMENT, node),
  153.             None]
  154.         self.lastEvent = self.lastEvent[1]
  155.         self.push(node)
  156.  
  157.     
  158.     def endElement(self, name):
  159.         self.lastEvent[1] = [
  160.             (END_ELEMENT, self.pop()),
  161.             None]
  162.         self.lastEvent = self.lastEvent[1]
  163.  
  164.     
  165.     def comment(self, s):
  166.         if self.document:
  167.             node = self.document.createComment(s)
  168.             self.lastEvent[1] = [
  169.                 (COMMENT, node),
  170.                 None]
  171.             self.lastEvent = self.lastEvent[1]
  172.         else:
  173.             event = [
  174.                 (COMMENT, s),
  175.                 None]
  176.             self.pending_events.append(event)
  177.  
  178.     
  179.     def processingInstruction(self, target, data):
  180.         if self.document:
  181.             node = self.document.createProcessingInstruction(target, data)
  182.             self.lastEvent[1] = [
  183.                 (PROCESSING_INSTRUCTION, node),
  184.                 None]
  185.             self.lastEvent = self.lastEvent[1]
  186.         else:
  187.             event = [
  188.                 (PROCESSING_INSTRUCTION, target, data),
  189.                 None]
  190.             self.pending_events.append(event)
  191.  
  192.     
  193.     def ignorableWhitespace(self, chars):
  194.         node = self.document.createTextNode(chars)
  195.         self.lastEvent[1] = [
  196.             (IGNORABLE_WHITESPACE, node),
  197.             None]
  198.         self.lastEvent = self.lastEvent[1]
  199.  
  200.     
  201.     def characters(self, chars):
  202.         node = self.document.createTextNode(chars)
  203.         self.lastEvent[1] = [
  204.             (CHARACTERS, node),
  205.             None]
  206.         self.lastEvent = self.lastEvent[1]
  207.  
  208.     
  209.     def startDocument(self):
  210.         if self.documentFactory is None:
  211.             import xml.dom.minidom as xml
  212.             self.documentFactory = xml.dom.minidom.Document.implementation
  213.         
  214.  
  215.     
  216.     def buildDocument(self, uri, tagname):
  217.         node = self.documentFactory.createDocument(uri, tagname, None)
  218.         self.document = node
  219.         self.lastEvent[1] = [
  220.             (START_DOCUMENT, node),
  221.             None]
  222.         self.lastEvent = self.lastEvent[1]
  223.         self.push(node)
  224.         for e in self.pending_events:
  225.             if e[0][0] == PROCESSING_INSTRUCTION:
  226.                 (_, target, data) = e[0]
  227.                 n = self.document.createProcessingInstruction(target, data)
  228.                 e[0] = (PROCESSING_INSTRUCTION, n)
  229.             elif e[0][0] == COMMENT:
  230.                 n = self.document.createComment(e[0][1])
  231.                 e[0] = (COMMENT, n)
  232.             else:
  233.                 raise AssertionError('Unknown pending event ', e[0][0])
  234.             self.lastEvent[1] = e
  235.             self.lastEvent = e
  236.         
  237.         self.pending_events = None
  238.         return node.firstChild
  239.  
  240.     
  241.     def endDocument(self):
  242.         self.lastEvent[1] = [
  243.             (END_DOCUMENT, self.document),
  244.             None]
  245.         self.pop()
  246.  
  247.     
  248.     def clear(self):
  249.         '''clear(): Explicitly release parsing structures'''
  250.         self.document = None
  251.  
  252.  
  253.  
  254. class ErrorHandler:
  255.     
  256.     def warning(self, exception):
  257.         print exception
  258.  
  259.     
  260.     def error(self, exception):
  261.         raise exception
  262.  
  263.     
  264.     def fatalError(self, exception):
  265.         raise exception
  266.  
  267.  
  268.  
  269. class DOMEventStream:
  270.     
  271.     def __init__(self, stream, parser, bufsize):
  272.         self.stream = stream
  273.         self.parser = parser
  274.         self.bufsize = bufsize
  275.         if not hasattr(self.parser, 'feed'):
  276.             self.getEvent = self._slurp
  277.         
  278.         self.reset()
  279.  
  280.     
  281.     def reset(self):
  282.         self.pulldom = PullDOM()
  283.         self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
  284.         self.parser.setContentHandler(self.pulldom)
  285.  
  286.     
  287.     def __getitem__(self, pos):
  288.         rc = self.getEvent()
  289.         if rc:
  290.             return rc
  291.         
  292.         raise IndexError
  293.  
  294.     
  295.     def next(self):
  296.         rc = self.getEvent()
  297.         if rc:
  298.             return rc
  299.         
  300.         raise StopIteration
  301.  
  302.     
  303.     def __iter__(self):
  304.         return self
  305.  
  306.     
  307.     def expandNode(self, node):
  308.         event = self.getEvent()
  309.         parents = [
  310.             node]
  311.         while event:
  312.             (token, cur_node) = event
  313.             if cur_node is node:
  314.                 return None
  315.             
  316.             if token != END_ELEMENT:
  317.                 parents[-1].appendChild(cur_node)
  318.             
  319.             if token == START_ELEMENT:
  320.                 parents.append(cur_node)
  321.             elif token == END_ELEMENT:
  322.                 del parents[-1]
  323.             
  324.             event = self.getEvent()
  325.  
  326.     
  327.     def getEvent(self):
  328.         if not self.pulldom.firstEvent[1]:
  329.             self.pulldom.lastEvent = self.pulldom.firstEvent
  330.         
  331.         while not self.pulldom.firstEvent[1]:
  332.             buf = self.stream.read(self.bufsize)
  333.             if not buf:
  334.                 self.parser.close()
  335.                 return None
  336.             
  337.             self.parser.feed(buf)
  338.         rc = self.pulldom.firstEvent[1][0]
  339.         self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
  340.         return rc
  341.  
  342.     
  343.     def _slurp(self):
  344.         ''' Fallback replacement for getEvent() using the
  345.             standard SAX2 interface, which means we slurp the
  346.             SAX events into memory (no performance gain, but
  347.             we are compatible to all SAX parsers).
  348.         '''
  349.         self.parser.parse(self.stream)
  350.         self.getEvent = self._emit
  351.         return self._emit()
  352.  
  353.     
  354.     def _emit(self):
  355.         ''' Fallback replacement for getEvent() that emits
  356.             the events that _slurp() read previously.
  357.         '''
  358.         rc = self.pulldom.firstEvent[1][0]
  359.         self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
  360.         return rc
  361.  
  362.     
  363.     def clear(self):
  364.         '''clear(): Explicitly release parsing objects'''
  365.         self.pulldom.clear()
  366.         del self.pulldom
  367.         self.parser = None
  368.         self.stream = None
  369.  
  370.  
  371.  
  372. class SAX2DOM(PullDOM):
  373.     
  374.     def startElementNS(self, name, tagName, attrs):
  375.         PullDOM.startElementNS(self, name, tagName, attrs)
  376.         curNode = self.elementStack[-1]
  377.         parentNode = self.elementStack[-2]
  378.         parentNode.appendChild(curNode)
  379.  
  380.     
  381.     def startElement(self, name, attrs):
  382.         PullDOM.startElement(self, name, attrs)
  383.         curNode = self.elementStack[-1]
  384.         parentNode = self.elementStack[-2]
  385.         parentNode.appendChild(curNode)
  386.  
  387.     
  388.     def processingInstruction(self, target, data):
  389.         PullDOM.processingInstruction(self, target, data)
  390.         node = self.lastEvent[0][1]
  391.         parentNode = self.elementStack[-1]
  392.         parentNode.appendChild(node)
  393.  
  394.     
  395.     def ignorableWhitespace(self, chars):
  396.         PullDOM.ignorableWhitespace(self, chars)
  397.         node = self.lastEvent[0][1]
  398.         parentNode = self.elementStack[-1]
  399.         parentNode.appendChild(node)
  400.  
  401.     
  402.     def characters(self, chars):
  403.         PullDOM.characters(self, chars)
  404.         node = self.lastEvent[0][1]
  405.         parentNode = self.elementStack[-1]
  406.         parentNode.appendChild(node)
  407.  
  408.  
  409. default_bufsize = 2 ** 14 - 20
  410.  
  411. def parse(stream_or_string, parser = None, bufsize = None):
  412.     if bufsize is None:
  413.         bufsize = default_bufsize
  414.     
  415.     if type(stream_or_string) in _StringTypes:
  416.         stream = open(stream_or_string)
  417.     else:
  418.         stream = stream_or_string
  419.     if not parser:
  420.         parser = xml.sax.make_parser()
  421.     
  422.     return DOMEventStream(stream, parser, bufsize)
  423.  
  424.  
  425. def parseString(string, parser = None):
  426.     
  427.     try:
  428.         StringIO = StringIO
  429.         import cStringIO
  430.     except ImportError:
  431.         StringIO = StringIO
  432.         import StringIO
  433.  
  434.     bufsize = len(string)
  435.     buf = StringIO(string)
  436.     if not parser:
  437.         parser = xml.sax.make_parser()
  438.     
  439.     return DOMEventStream(buf, parser, bufsize)
  440.  
  441.