import os,sys,string
from xml.sax import *
dico = {}
# Handle the XML file with a SAX Parser
class ResultHandler(ContentHandler):
	def __init__(self):
		global dico
		self.inElmt = False
		self.nbInputs = False
		self.nbDiffInputs = False
		self.nbDiffInputsSunked = False
		self.sinks = False
		self.variables = False
		self.functions = False
		self.classes = False
		self.inFile = False
		self.nLoc = -1
		self.sName, self.tmp   = "",""
		self.temp = {}
	def startElement(self, name, attrs):
		if name == 'instance':
			self.inElmt = True
			self.temp = {}
			self.sName = ""
		elif name == 'file':
			self.inFile = True
			self.sName = ""
			if attrs.getValue('loc'):
				self.nLoc = attrs.getValue('loc')
		elif name == "variables":
			self.variables = True
			self.tmp = ""
		elif name == "classes":
			self.classes = True
			self.tmp = ""
		elif name == "functions":
			self.functions = True
			self.tmp = ""
		elif name == "sinks":
			self.sinks = True
			self.tmp = ""
		elif name == "nbInputs":
			self.nbInputs = True
			self.tmp = ""
		elif name == "nbDiffInputs":
			self.nbDiffInputs = True
			self.tmp = ""
		elif name == "nbDiffInputsSunked":
			self.nbDiffInputsSunked = True
			self.tmp = ""
	def characters(self, ch):
		if self.inFile:
			self.sName = self.sName + ch
		else:
			self.tmp = self.tmp + ch
	def endElement(self, name):
		global attack_list
		if name == 'nbInputs':
			self.nbInputs = False
			self.temp['nbInputs'] = self.tmp
		elif name == 'nbDiffInputs':
			self.nbDiffInputs = False
			self.temp['nbDiffInputs'] = self.tmp
		elif name == 'nbDiffInputsSunked':
			self.nbDiffInputsSunked = False
			self.temp['nbDiffInputsSunked'] = self.tmp
		elif name == 'functions':
			self.functions = False
			self.temp['functions'] = self.tmp
		elif name == 'sinks':
			self.sinks = False
			self.temp['sinks'] = self.tmp
		elif name == 'classes':
			self.classes = False
			self.temp['classes'] = self.tmp
		elif name == 'variables':
			self.variables = False
			self.temp['variables'] = self.tmp
		elif name == 'file':
			self.inFile = False
			self.temp['loc'] = self.nLoc
		elif name == 'instance':
			self.inElmt = False
			if '.xml' in self.sName:
				self.sName = self.sName.replace(".xml", "")
			dico[self.sName] = self.temp

from pychart import *

def parse(f):
	global dico
	print f
	dico = {}
	parser = make_parser()
	def_handler = ResultHandler()
	parser.setContentHandler(def_handler)
	parser.parse(f)
	sample = []
	for a in dico:
		fname = a.replace("./study/metrics/",'')
		isunk = int(dico[a]['nbDiffInputsSunked'])
		entry = int(dico[a]['nbDiffInputs'])
		sinks = int(dico[a]['sinks'])
		sample.append([fname, isunk, entry * sinks])
	return sample

def get_bar(string, value, ratio):
	s=  ""
	s += string
	s += "] .|"
	n = int(value * ratio)
	for k in range(0,n):
		s += "-"
	s += "| (%d)" % value
	return s

if __name__ == "__main__":
	fname = sys.argv[1]
	data = parse(fname)
	max_bar1 = 0
	max_bar2 = 0
	max_trait= 100.0
	for a in data:
		if a[1] > max_bar1:
			max_bar1 = float(a[1])
		if a[2] > max_bar2:
			max_bar2 = float(a[2])
	ratio_1 = float(max_trait / max_bar1)
	ratio_2 = float(max_trait / max_bar2)
	for a in data:
		print a[0].replace(fname[:fname.find('.xml')], '')
		print get_bar("[ df ", a[1], ratio_1)
		print get_bar("[ S  ", a[2], ratio_2)

