def to_xml(self, encoding="UTF-8", pretty=True): dom = Document() dom.version = "1.0" dom.encoding = "UTF-8" doc_type = DocumentType("plist") doc_type.publicId = "-//Apple Computer//DTD PLIST 1.0//EN" doc_type.systemId = "http://www.apple.com/DTDs/PropertyList-1.0.dtd" dom.appendChild(doc_type) plist_node = dom.createElement("plist") plist_node.setAttribute("version", "1.0") dom.appendChild(plist_node) plist_root = dom.createElement("dict") plist_node.appendChild(plist_root) temp_pairs = [(self, plist_root)] while temp_pairs: data, root = temp_pairs.pop(0) if isinstance(data, dict): for k, v in data.items(): k_node = dom.createElement("key") text_node = dom.createTextNode(k) k_node.appendChild(text_node) root.appendChild(k_node) if isinstance(v, dict): v_node = dom.createElement("dict") temp_pairs.append((v, v_node)) elif isinstance(v, list): v_node = dom.createElement("array") temp_pairs.append((v, v_node)) else: v_node = self._to_dom_node(v, dom) root.appendChild(v_node) elif isinstance(data, list): for v in data: if isinstance(v, dict): v_node = dom.createElement("dict") temp_pairs.append((v, v_node)) elif isinstance(v, list): v_node = dom.createElement("array") temp_pairs.append((v, v_node)) else: v_node = self._to_dom_node(v, dom) root.appendChild(v_node) else: data_node = self._to_dom_node(data, dom) root.appendChild(data_node) if pretty: xml_content = dom.toprettyxml(encoding=encoding) else: xml_content = dom.toxml(encoding=encoding) return xml_content
def toXHTML(self): """ Print a message into XHTML+RDFa format """ #root nodes doc = getDOMImplementation().createDocument(None, 'html', None) doctype = DocumentType("html") doctype.publicId = "-//W3C//DTD XHTML+RDFa 1.0//EN" doctype.systemId = "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd" doc.doctype = doctype root = doc.documentElement root.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml') root.setAttribute('xmlns:sioc', str(SIOC)) root.setAttribute('xmlns:foaf', str(FOAF)) root.setAttribute('xmlns:dc', str(DC)) root.setAttribute('xmlns:dct', str(DCT)) root.setAttribute('xmlns:mvcb', str(MVCB)) root.setAttribute('xmlns:xsd', str(XSD)) head = doc.createElement('head') root.appendChild(head) head.setAttribute('profile', 'http://www.w3.org/2003/g/data-view') link = doc.createElement('link') link.setAttribute('rel', 'transformation') link.setAttribute( 'href', 'http://www-sop.inria.fr/acacia/soft/RDFa2RDFXML.xsl') head.appendChild(link) link = doc.createElement('link') link.setAttribute('rel', 'meta') link.setAttribute('type', 'application/rdf+xml') link.setAttribute('title', 'SIOC') link.setAttribute('href', self.getRdfUrl()) head.appendChild(link) link = doc.createElement('link') link.setAttribute('rel', 'stylesheet') link.setAttribute('type', 'text/css') link.setAttribute('href', self.config.get('base') + 'swaml.css') head.appendChild(link) title = doc.createElement('title') title.appendChild(doc.createTextNode(self.getSubject())) head.appendChild(title) #body body = doc.createElement('body') body.setAttribute('typeof', 'foaf:Document') body.setAttribute('about', self.getXhtmlUrl()) root.appendChild(body) p = doc.createElement('p') span = doc.createElement('span') span.setAttribute('rel', 'foaf:primaryTopic') span.setAttribute('href', self.getUri()) body.appendChild(p) p.appendChild(span) #post div div = doc.createElement('div') body.appendChild(div) div.setAttribute('typeof', 'sioc:Post') div.setAttribute('about', self.getUri()) #post fields try: h1 = doc.createElement('h1') div.appendChild(h1) h1.setAttribute('property', 'dc:title') h1.appendChild(doc.createTextNode(self.getSubject())) p = doc.createElement('p') div.appendChild(p) strong = doc.createElement('strong') p.appendChild(strong) strong.appendChild(doc.createTextNode('From: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:has_creator') a.setAttribute('href', self.getSender().getUri()) a.appendChild(doc.createTextNode(self.getSender().getName())) p.appendChild(a) p = doc.createElement('p') div.appendChild(p) strong = doc.createElement('strong') p.appendChild(strong) strong.appendChild(doc.createTextNode('To: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:has_container') a.setAttribute('href', self.config.get('base') + 'forum') if (len(self.config.get('title')) > 0): a.appendChild(doc.createTextNode(self.config.get('title'))) else: a.appendChild( doc.createTextNode(self.config.get('base') + 'forum')) p.appendChild(a) p = doc.createElement('p') div.appendChild(p) strong = doc.createElement('strong') p.appendChild(strong) strong.appendChild(doc.createTextNode('Date: ')) span = doc.createElement('span') span.setAttribute('property', 'dct:created') span.setAttribute('datatype', 'xsd:dateTime') span.appendChild(doc.createTextNode(self.getDate())) p.appendChild(span) #p = doc.createElement('p') #div.appendChild(p) #strong = doc.createElement('strong') #p.appendChild(strong) #strong.appendChild(doc.createTextNode('Message-Id: ')) #span = doc.createElement('span') #span.setAttribute('property', 'sioc:id') #span.appendChild(doc.createTextNode(self.getSwamlId())) #p.appendChild(span) pre = doc.createElement('pre') div.appendChild(pre) pre.setAttribute('property', 'sioc:content') pre.appendChild(doc.createTextNode( self.getBody())) #FIXME: parse URLs p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('URI: ')) a = doc.createElement('a') a.setAttribute('href', self.getUri()) a.appendChild(doc.createTextNode(self.getUri())) p.appendChild(a) p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('Link: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:link') a.setAttribute('href', self.getXhtmlUrl()) a.appendChild(doc.createTextNode(self.getXhtmlUrl())) p.appendChild(a) parent = self.getParent() if (parent != None): p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('Reply of: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:reply_of') a.setAttribute('href', parent) a.appendChild(doc.createTextNode(parent)) p.appendChild(a) if (len(self.childs) > 0): for child in self.childs: p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('Has reply: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:has_reply') a.setAttribute('href', child) a.appendChild(doc.createTextNode(child)) p.appendChild(a) previous = self.getPreviousByDate() if (previous != None): p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('Previous by Date: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:previous_by_date') a.setAttribute('href', previous) a.appendChild(doc.createTextNode(previous)) p.appendChild(a) next = self.getNextByDate() if (next != None): p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('Next by Date: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:next_by_date') a.setAttribute('href', next) a.appendChild(doc.createTextNode(next)) p.appendChild(a) except Exception, detail: print 'Error exporting to XHTML message ' + str( self.getId()) + ': ' + str(detail)
def toXHTML(self): """ Print a message into XHTML+RDFa format """ #root nodes doc = getDOMImplementation().createDocument(None, 'html', None) doctype = DocumentType("html") doctype.publicId = "-//W3C//DTD XHTML+RDFa 1.0//EN" doctype.systemId = "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd" doc.doctype = doctype root = doc.documentElement root.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml') root.setAttribute('xmlns:sioc', str(SIOC)) root.setAttribute('xmlns:foaf', str(FOAF)) root.setAttribute('xmlns:dc', str(DC)) root.setAttribute('xmlns:dct', str(DCT)) root.setAttribute('xmlns:mvcb', str(MVCB)) root.setAttribute('xmlns:xsd', str(XSD)) head = doc.createElement('head') root.appendChild(head) head.setAttribute('profile', 'http://www.w3.org/2003/g/data-view') link = doc.createElement('link') link.setAttribute('rel', 'transformation') link.setAttribute('href', 'http://www-sop.inria.fr/acacia/soft/RDFa2RDFXML.xsl') head.appendChild(link) link = doc.createElement('link') link.setAttribute('rel', 'meta') link.setAttribute('type', 'application/rdf+xml') link.setAttribute('title', 'SIOC') link.setAttribute('href', self.getRdfUrl()) head.appendChild(link) link = doc.createElement('link') link.setAttribute('rel', 'stylesheet') link.setAttribute('type', 'text/css') link.setAttribute('href', self.config.get('base')+'swaml.css') head.appendChild(link) title = doc.createElement('title') title.appendChild(doc.createTextNode(self.getSubject())) head.appendChild(title) #body body = doc.createElement('body') body.setAttribute('typeof', 'foaf:Document') body.setAttribute('about', self.getXhtmlUrl()) root.appendChild(body) p = doc.createElement('p') span = doc.createElement('span') span.setAttribute('rel', 'foaf:primaryTopic') span.setAttribute('href', self.getUri()) body.appendChild(p) p.appendChild(span) #post div div = doc.createElement('div') body.appendChild(div) div.setAttribute('typeof', 'sioc:Post') div.setAttribute('about', self.getUri()) #post fields try: h1 = doc.createElement('h1') div.appendChild(h1) h1.setAttribute('property', 'dc:title') h1.appendChild(doc.createTextNode(self.getSubject())) p = doc.createElement('p') div.appendChild(p) strong = doc.createElement('strong') p.appendChild(strong) strong.appendChild(doc.createTextNode('From: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:has_creator') a.setAttribute('href', self.getSender().getUri()) a.appendChild(doc.createTextNode(self.getSender().getName())) p.appendChild(a) p = doc.createElement('p') div.appendChild(p) strong = doc.createElement('strong') p.appendChild(strong) strong.appendChild(doc.createTextNode('To: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:has_container') a.setAttribute('href', self.config.get('base')+'forum') if (len(self.config.get('title'))>0): a.appendChild(doc.createTextNode(self.config.get('title'))) else: a.appendChild(doc.createTextNode(self.config.get('base')+'forum')) p.appendChild(a) p = doc.createElement('p') div.appendChild(p) strong = doc.createElement('strong') p.appendChild(strong) strong.appendChild(doc.createTextNode('Date: ')) span = doc.createElement('span') span.setAttribute('property', 'dct:created') span.setAttribute('datatype', 'xsd:dateTime') span.appendChild(doc.createTextNode(self.getDate())) p.appendChild(span) #p = doc.createElement('p') #div.appendChild(p) #strong = doc.createElement('strong') #p.appendChild(strong) #strong.appendChild(doc.createTextNode('Message-Id: ')) #span = doc.createElement('span') #span.setAttribute('property', 'sioc:id') #span.appendChild(doc.createTextNode(self.getSwamlId())) #p.appendChild(span) pre = doc.createElement('pre') div.appendChild(pre) pre.setAttribute('property', 'sioc:content') pre.appendChild(doc.createTextNode(self.getBody())) #FIXME: parse URLs p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('URI: ')) a = doc.createElement('a') a.setAttribute('href', self.getUri()) a.appendChild(doc.createTextNode(self.getUri())) p.appendChild(a) p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('Link: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:link') a.setAttribute('href', self.getXhtmlUrl()) a.appendChild(doc.createTextNode(self.getXhtmlUrl())) p.appendChild(a) parent = self.getParent() if (parent != None): p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('Reply of: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:reply_of') a.setAttribute('href', parent) a.appendChild(doc.createTextNode(parent)) p.appendChild(a) if (len(self.childs) > 0): for child in self.childs: p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('Has reply: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:has_reply') a.setAttribute('href', child) a.appendChild(doc.createTextNode(child)) p.appendChild(a) previous = self.getPreviousByDate() if (previous != None): p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('Previous by Date: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:previous_by_date') a.setAttribute('href', previous) a.appendChild(doc.createTextNode(previous)) p.appendChild(a) next = self.getNextByDate() if (next != None): p = doc.createElement('p') div.appendChild(p) p.appendChild(doc.createTextNode('Next by Date: ')) a = doc.createElement('a') a.setAttribute('rel', 'sioc:next_by_date') a.setAttribute('href', next) a.appendChild(doc.createTextNode(next)) p.appendChild(a) except Exception, detail: print 'Error exporting to XHTML message ' + str(self.getId()) + ': ' + str(detail)