# Path: easymode/utils/xmlutils.py
# class XmlPrinter(SimplerXMLGenerator):
#     """
#     XmlPrinter can be used as a contenthandler for the 
#     :class:`~easymode.utils.xmlutils.XmlScanner`.
#     
#     It will convert all skippedEntities to their unicode characters
#     and copy these to the output stream.
#     
#     You can use it as a simple xml generator, to create xml::
#     
#         stream = StringIO.StringIO()
#         xml = XmlPrinter(stream, settings.DEFAULT_CHARSET)
# 
#         def _render_page(page):
#             xml.startElement('title', {})
#             xml.characters(page.title)
#             xml.endElement('title')
#             xml.startElement('template', {})
#             xml.characters(page.template)
#             xml.endElement('template')
#         
#         for child in Page.children.all():
#             xml.startElement('page', {})
#             _render_page(child)
#             xml.endElement('page')
#     """
#     def skippedEntity(self, name):
#         self._out.write(_unicode_for_entity_with_name(name).encode('utf-8'))


import sys
from StringIO import StringIO
from django.conf import settings
from django.core.serializers import xml_serializer
from django.utils.encoding import smart_unicode, force_unicode
from easymode.tree.introspection import get_default_field_descriptors, \
    get_foreign_key_desciptors, get_generic_relation_descriptors
from easymode.utils import recursion_depth
from easymode.utils.xmlutils import XmlPrinter
        #         related_objects.append(related_object)
        #     except Exception as e:
        #         pass
        # 
        #     s = RecursiveXmlSerializer()                
        #     s.serialize( related_objects, xml=self.xml, stream=self.stream)

        # add generic relations
        for (generic_relation_descriptor_name, generic_relation_descriptor) in get_generic_relation_descriptors(obj):
            # generic relations always have serialize set to False so we always include them.
            bound_generic_relation_descriptor = generic_relation_descriptor.__get__(obj)
            s = RecursiveXmlSerializer()                
            s.serialize( bound_generic_relation_descriptor.all(), xml=self.xml, stream=self.stream)
        
        #serialize the default field descriptors:
        for (default_field_descriptor_name, default_field_descriptor) in get_default_field_descriptors(obj):
            if default_field_descriptor.serialize:
                self.handle_field(obj, default_field_descriptor)
        
        for field in obj._meta.many_to_many:
            if field.serialize:
                if self.selected_fields is None or field.attname in self.selected_fields:
                    self.handle_m2m_field(obj, field)
        self.end_object(obj)
        
    def start_serialization(self):
        """
        Start serialization -- open the XML document and the root element.
        """
        if (self.root):

# Path: easymode/utils/xmlutils.py
# class XmlPrinter(SimplerXMLGenerator):
#     """
#     XmlPrinter can be used as a contenthandler for the 
#     :class:`~easymode.utils.xmlutils.XmlScanner`.
#     
#     It will convert all skippedEntities to their unicode characters
#     and copy these to the output stream.
#     
#     You can use it as a simple xml generator, to create xml::
#     
#         stream = StringIO.StringIO()
#         xml = XmlPrinter(stream, settings.DEFAULT_CHARSET)
# 
#         def _render_page(page):
#             xml.startElement('title', {})
#             xml.characters(page.title)
#             xml.endElement('title')
#             xml.startElement('template', {})
#             xml.characters(page.template)
#             xml.endElement('template')
#         
#         for child in Page.children.all():
#             xml.startElement('page', {})
#             _render_page(child)
#             xml.endElement('page')
#     """
#     def skippedEntity(self, name):
#         self._out.write(_unicode_for_entity_with_name(name).encode('utf-8'))


# Path: easymode/tree/xml/serializers.py
import sys
from StringIO import StringIO
from django.conf import settings
from django.core.serializers import xml_serializer
from django.utils.encoding import smart_unicode, force_unicode
from easymode.tree.introspection import get_default_field_descriptors, \
    get_foreign_key_desciptors, get_generic_relation_descriptors
from easymode.utils import recursion_depth
from easymode.utils.xmlutils import XmlPrinter

        #         related_objects.append(related_object)
        #     except Exception as e:
        #         pass
        # 
        #     s = RecursiveXmlSerializer()                
        #     s.serialize( related_objects, xml=self.xml, stream=self.stream)

        # add generic relations
        for (generic_relation_descriptor_name, generic_relation_descriptor) in get_generic_relation_descriptors(obj):
            # generic relations always have serialize set to False so we always include them.
            bound_generic_relation_descriptor = generic_relation_descriptor.__get__(obj)
            s = RecursiveXmlSerializer()                
            s.serialize( bound_generic_relation_descriptor.all(), xml=self.xml, stream=self.stream)
        
        #serialize the default field descriptors:
        for (default_field_descriptor_name, default_field_descriptor) in get_default_field_descriptors(obj):
            if default_field_descriptor.serialize:
                self.handle_field(obj, default_field_descriptor)
        
        for field in obj._meta.many_to_many:
            if field.serialize:
                if self.selected_fields is None or field.attname in self.selected_fields:
                    self.handle_m2m_field(obj, field)
        self.end_object(obj)
        
    def start_serialization(self):
        """
        Start serialization -- open the XML document and the root element.
        """
        if (self.root):

            self.xml = XmlPrinter(self.stream, self.options.get("encoding", settings.DEFAULT_CHARSET))