Back to index

moin  1.9.0~rc2
Public Member Functions | Static Public Attributes | Private Member Functions
MoinMoin.wikixml.marshal.Marshal Class Reference
Collaboration diagram for MoinMoin.wikixml.marshal.Marshal:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def toXML

Static Public Attributes

string XML_DECL = '<?xml version="1.0" encoding="utf-8"?>\n'
string ROOT_CONTAINER = "data"
string ITEM_CONTAINER = "item"
list PRIVATE_PREFIXES = ['_']
dictionary TAG_MAP = {}

Private Member Functions

def __toXML

Detailed Description

Serialize Python data structures to XML.

    XML_DECL is the standard XML declaration.

    The class attributes ROOT_CONTAINER (default "data") and
    ITEM_CONTAINER (default "item") can be overwritten by derived
    classes; if ROOT_CONTAINER is `None`, the usually generated
    container element is omitted, the same is true for ITEM_CONTAINER.

    PRIVATE_PREFIXES is a list of prefixes of tagnames that are
    treated as private, i.e. things that should not be serialized.
    Default is to omit all properties starting with an underscore.

    TAG_MAP is a translation table for tag names, and is empty by
    default. It can also be used to suppress certain properties by
    mapping a tag name to `None`.

Definition at line 9 of file marshal.py.


Member Function Documentation

def MoinMoin.wikixml.marshal.Marshal.__toXML (   self,
  element,
  data 
) [private]
Recursive helper method that transforms an object to XML.

    Returns a list of strings, which constitute the XML document.

Definition at line 42 of file marshal.py.

00042 
00043     def __toXML(self, element, data):
00044         """ Recursive helper method that transforms an object to XML.
00045 
00046             Returns a list of strings, which constitute the XML document.
00047         """
00048         # map tag names
00049         if self.TAG_MAP:
00050             element = self.TAG_MAP.get(element, element)
00051 
00052         if element:
00053             for prefix in self.PRIVATE_PREFIXES:
00054                 if element.startswith(prefix):
00055                     return ''
00056             content = ['<%s>' % element]
00057         else:
00058             content = []
00059 
00060         # Handle the different data types
00061         add_content = content.extend
00062 
00063         if data is None:
00064             content = "<none/>"
00065 
00066         elif isinstance(data, str):
00067             content = (data.replace("&", "&amp;") # Must be done first!
00068                            .replace("<", "&lt;")
00069                            .replace(">", "&gt;"))
00070 
00071         elif isinstance(data, dict):
00072             for key, value in data.items():
00073                 add_content(self.__toXML(key, value))
00074 
00075         elif isinstance(data, (list, tuple)):
00076             for item in data:
00077                 add_content(self.__toXML(self.ITEM_CONTAINER, item))
00078 
00079         elif hasattr(data, "toXML"):
00080             add_content([data.toXML()])
00081 
00082         elif hasattr(data, "__dict__"):
00083             add_content(self.__toXML(self.ROOT_CONTAINER, data.__dict__))
00084 
00085         else:
00086             content = (str(data).replace("&", "&amp;") # Must be done first!
00087                                 .replace("<", "&lt;")
00088                                 .replace(">", "&gt;"))
00089 
00090         # Close container element
00091         if isinstance(content, str):
00092             # No Whitespace
00093             if element:
00094                 content = ['<%s>%s</%s>' % (element, content, element)]
00095             else:
00096                 content = [content]
00097         elif element:
00098             # Whitespace
00099             content.append('</%s>' % element)
00100 
00101         return content
00102 

Here is the call graph for this function:

Here is the caller graph for this function:

Transform an instance to an XML string.

Definition at line 103 of file marshal.py.

00103 
00104     def toXML(self):
00105         """ Transform an instance to an XML string.
00106         """
00107         return '\n'.join(self.__toXML(self.ROOT_CONTAINER, self.__dict__))
00108 

Here is the call graph for this function:


Member Data Documentation

Definition at line 33 of file marshal.py.

Definition at line 36 of file marshal.py.

Definition at line 32 of file marshal.py.

dictionary MoinMoin.wikixml.marshal.Marshal.TAG_MAP = {} [static]

Definition at line 39 of file marshal.py.

string MoinMoin.wikixml.marshal.Marshal.XML_DECL = '<?xml version="1.0" encoding="utf-8"?>\n' [static]

Definition at line 29 of file marshal.py.


The documentation for this class was generated from the following file: