from django.utils.feedgenerator import Rss201rev2Feed
from django.utils.xmlutils import SimplerXMLGenerator

class MediaElement(object):
    """
    """
    def __init__(self, tag_name, attrs=None, contents=None, children=None):
        self.tag_name = tag_name
        self.attrs = attrs or {}
        self.contents = contents
        self.children = children or []

    def add_to(self, handler):
        handler.startElement(self.tag_name, self.attrs)
        for element in self.children:
            element.add_to(handler)
        if self.contents is not None:
            # TODO: CDATA
            handler.characters(self.contents)
        handler.endElement(self.tag_name)

class CDataXMLGenerator(SimplerXMLGenerator):
    """
    """
    def addCDataElement(self, name, contents=None, attrs=None):
        if attrs is None: attrs = {}
        self.startElement(name, attrs)
        if contents is not None:
            self._write('<![CDATA[' + contents + ']]>')
        self.endElement(name)

class MediaRSSFeed(Rss201rev2Feed):
    """
    """
    # TODO: Use CDataXMLGenerator as handler.
    #def write(self, outfile, encoding): pass

    def rss_attributes(self):
        attrs = super(MediaRSSFeed, self).rss_attributes()
        attrs['xmlns:media'] = 'http://search.yahoo.com/mrss/'
        return attrs

    def add_item_elements(self, handler, item):
        super(MediaRSSFeed, self).add_item_elements(handler, item)
        if 'media_list' in item:
            for media_elem in item['media_list']:
                media_elem.add_to(handler)

@@ -1,24 +1,64 @@
+from xml.sax.saxutils import quoteattr%0A%0A
 from django.utils.feedge
@@ -142,16 +142,42 @@
 erator%0A%0A
+CDATA_CHARS = set('&%3C%3E')%0A%0A
 class Me
@@ -248,20 +248,8 @@
 ame,
- attrs=None,
  con
@@ -260,24 +260,21 @@
 s=None, 
-children
+attrs
 =None):%0A
@@ -306,16 +306,49 @@
 ag_name%0A
+        self.contents = contents%0A
         
@@ -390,98 +390,214 @@
 lf.c
-ontents = contents%0A        self.children = children or %5B%5D%0A%0A    def add_to(self, handler):%0A
+hildren = %5B%5D%0A        #self.append = self.children.append%0A%0A    def append(self, element):%0A        self.children.append(element)%0A%0A    def add_to(self, handler):%0A        if self.contents or self.children:%0A    
     
@@ -652,24 +652,28 @@
 rs)%0A        
+    
 for element 
@@ -702,16 +702,20 @@
         
+    
 element.
@@ -730,16 +730,20 @@
 andler)%0A
+    
         
@@ -783,27 +783,8 @@
     
-     # TODO: CDATA%0A
     
@@ -780,35 +780,32 @@
                 
-   
 handler.characte
@@ -822,16 +822,20 @@
 ntents)%0A
+    
         
@@ -863,24 +863,101 @@
 lf.tag_name)
+%0A        else:%0A            handler.addEmptyElement(self.tag_name, self.attrs)
 %0A%0Aclass CDat
@@ -953,20 +953,21 @@
 %0Aclass C
-Data
+ustom
 XMLGener
@@ -1020,21 +1020,21 @@
  def add
-CData
+Empty
 Element(
@@ -1040,39 +1040,24 @@
 (self, name,
- contents=None,
  attrs=None)
@@ -1112,65 +1112,321 @@
 elf.
-startElement(name, attrs)%0A        if contents is not None
+_write('%3C' + name)%0A        for name, value in attrs.iteritems():%0A            self._write(' %25s=%25s' %25 (name, quoteattr(value)))%0A        self._write('/%3E')%0A%0A    def characters(self, content):%0A        # TODO: figure out how to do this explicitly rather than implicitly%0A        if any(c in content for c in CDATA_CHARS)
 :%0A  
@@ -1468,17 +1468,16 @@
  content
-s
  + '%5D%5D%3E'
@@ -1490,174 +1490,592 @@
     
-self.endElement(name)%0A%0Aclass MediaRSSFeed(Rss201rev2Feed):%0A    %22%22%22%0A    %22%22%22%0A    # TODO: Use CDataXMLGenerator as handler.%0A    #def write(self, outfile, encoding): pass
+else:%0A            # can't use super, XMLGenerator is an old style class%0A            SimplerXMLGenerator.characters(self, content)%0A%0Aclass MediaRSSFeed(Rss201rev2Feed):%0A    %22%22%22%0A    %22%22%22%0A    def write(self, outfile, encoding):%0A        handler = CustomXMLGenerator(outfile, encoding)%0A        handler.startDocument()%0A        handler.startElement(u'rss', self.rss_attributes())%0A        handler.startElement(u'channel', self.root_attributes())%0A        self.add_root_elements(handler)%0A        self.write_items(handler)%0A        self.endChannelElement(handler)%0A        handler.endElement(u'rss')
 %0A%0A  
@@ -2365,45 +2365,8 @@
 em)%0A
-        if 'media_list' in item:%0A    
     
@@ -2391,17 +2391,21 @@
  in item
-%5B
+.get(
 'media_l
@@ -2412,15 +2412,15 @@
 ist'
-%5D:%0A    
+, ()):%0A
     

Enable handling of Character DATA implicitly. Refs #8862