import xml.dom.minidom
from xml.sax.saxutils import escape
class Article:
	"""Represents a block of text and metadata created from XML."""
	
	def __init__(self):
		"""Set initial data properties."""
		self.reset( )
	
	def reset(self):
		"""Re-initialize data properties."""
		self.title       = ""
		self.size        =0
		self.time        = ""    # pretty-printing time string
		self.author      = ""
		self.contributor = ""
		self.contents    = ""
	
	def getXML(self):
		"""Returns XML after re-assembling from data
		members that may have changed."""
		attr = ''
		if self.title:
			attr = ' title="%s"' % escape(self.title)
		s = '<?xml version="1.0"?>\n<article%s>\n' % attr
		if self.author:
			s = '%s <author name="%s" />\n' % (s, escape(self.author))
		if self.contributor:
			s = '%s <contributor name="%s" />\n' % (s, escape(self.contributor))
		if self.contents:
			s = ('%s <contents>\n%s\n </contents>\n' % (s, escape(self.contents)))
		return s + "</article>\n"
	
	def fromXML(self, data):
		"""Initialize using an XML document passed as a string."""
		self.reset( )
		#print data
		dom = xml.dom.minidom.parseString(data)
		self.title       = self.get_attribute(dom, "article", "title")
		self.size        = int(self.get_attribute(dom, "size", "bytes") or 0)
		self.time        = self.get_attribute(dom, "time", "stime")
		self.author      = self.get_attribute(dom, "author", "name")
		self.contributor = self.get_attribute(dom, "contributor", "name")
		
		print 'title: ' + self.title + '\n'
		#print 'size: ' + self.size + '\n'
		#print 'time: ' + self.time + '\n'
		#print 'author: ' + self.author + '\n'
		#print 'contributor: ' + self.contributor + '\n'
		
		nodelist         = dom.getElementsByTagName("contents")
		if nodelist:
			assert len(nodelist) == 1
			contents = nodelist[0]
			contents.normalize( )
			if contents.childNodes:
				self.contents = contents.firstChild.data.strip( )
				print 'contents: ' + self.contents + '\n'
	
	# Helper function:
	def get_attribute(self, dom, tagname, attrname):
		"""Return the value of a solitary element & attribute,
		if available."""
		nodelist = dom.getElementsByTagName(tagname)
		if nodelist:
			assert len(nodelist) == 1
			node = nodelist[0]
			return node.getAttribute(attrname).strip( )
		else:
			return ""
		
if __name__ == "__main__":
	a = Article()
	a.fromXML('<?xml version="1.0"?><article title="FirstPost"><contents>This is the first article.</contents></article>')
	#a.fromXML('<?xml version="1.0"?><article title="FirstPost"><contents>This is the first article.</contents></article><article title="SecondPost"><contents>This is the second article.</contents></article>')
	#a.fromXML('<?xml version="1.0"?><article title="FirstPost"><contents>This is the first article.</contents></article><barticle btitle="SecondPost"><bcontents>This is the second article.</bcontents></barticle>')