def start_element(self, name, attrs): if name == self.VULNERABILITY: self.vul = Vulnerability() self.vul.set_name(attrs[self.VULNERABILITY_NAME]) elif name == self.VULNERABILITY_DESCRIPTION: self.tag = self.VULNERABILITY_DESCRIPTION elif name == self.VULNERABILITY_SOLUTION: # self.tag = self.VULNERABILITY_SOLUTION self.vul.set_solution(attrs["text"]) elif name == self.VULNERABILITY_REFERENCES: self.references = {} elif name == self.VULNERABILITY_REFERENCE: self.tag = self.VULNERABILITY_REFERENCE elif name == self.VULNERABILITY_REFERENCE_TITLE: self.tag = self.VULNERABILITY_REFERENCE_TITLE elif name == self.VULNERABILITY_REFERENCE_URL: self.tag = self.VULNERABILITY_REFERENCE_URL
def start_element(self, name, attrs): if name == self.VULNERABILITY: self.vul = Vulnerability() self.vul.setName(attrs[self.VULNERABILITY_NAME]) elif name == self.VULNERABILITY_DESCRIPTION: self.tag = self.VULNERABILITY_DESCRIPTION elif name == self.VULNERABILITY_SOLUTION: #self.tag = self.VULNERABILITY_SOLUTION self.vul.setSolution(attrs["text"]) elif name == self.VULNERABILITY_REFERENCES: self.references = {} elif name == self.VULNERABILITY_REFERENCE: self.tag = self.VULNERABILITY_REFERENCE elif name == self.VULNERABILITY_REFERENCE_TITLE: self.tag = self.VULNERABILITY_REFERENCE_TITLE elif name == self.VULNERABILITY_REFERENCE_URL: self.tag = self.VULNERABILITY_REFERENCE_URL
class VulnerabilityXMLParser: VULNERABILITY = "vulnerability" VULNERABILITY_NAME = "name" VULNERABILITY_DESCRIPTION = "description" VULNERABILITY_SOLUTION = "solution" VULNERABILITY_REFERENCE = "reference" VULNERABILITY_REFERENCES = "references" VULNERABILITY_REFERENCE_TITLE = "title" VULNERABILITY_REFERENCE_URL = "url" def __init__(self): self._parser = expat.ParserCreate() self._parser.StartElementHandler = self.start_element self._parser.EndElementHandler = self.end_element self._parser.CharacterDataHandler = self.char_data self.vulnerabilities = [] self.vul = None self.references = {} self.title = "" self.url = "" self.tag = "" def parse(self, filename): with open(filename) as f: content = f.read() self.feed(content) def feed(self, data): self._parser.Parse(data, 0) def close(self): self._parser.Parse("", 1) del self._parser def start_element(self, name, attrs): if name == self.VULNERABILITY: self.vul = Vulnerability() self.vul.set_name(attrs[self.VULNERABILITY_NAME]) elif name == self.VULNERABILITY_DESCRIPTION: self.tag = self.VULNERABILITY_DESCRIPTION elif name == self.VULNERABILITY_SOLUTION: # self.tag = self.VULNERABILITY_SOLUTION self.vul.set_solution(attrs["text"]) elif name == self.VULNERABILITY_REFERENCES: self.references = {} elif name == self.VULNERABILITY_REFERENCE: self.tag = self.VULNERABILITY_REFERENCE elif name == self.VULNERABILITY_REFERENCE_TITLE: self.tag = self.VULNERABILITY_REFERENCE_TITLE elif name == self.VULNERABILITY_REFERENCE_URL: self.tag = self.VULNERABILITY_REFERENCE_URL def end_element(self, name): if name == self.VULNERABILITY: self.vulnerabilities.append(self.vul) elif name == self.VULNERABILITY_REFERENCE: self.references[self.title] = self.url elif name == self.VULNERABILITY_REFERENCES: self.vul.set_references(self.references) def char_data(self, data): if self.tag == self.VULNERABILITY_DESCRIPTION: self.vul.set_description(data) # elif self.tag==self.VULNERABILITY_SOLUTION: # self.vul.set_solution(data) elif self.tag == self.VULNERABILITY_REFERENCE_TITLE: self.title = data elif self.tag == self.VULNERABILITY_REFERENCE_URL: self.url = data self.tag = "" def get_vulnerabilities(self): return self.vulnerabilities
class VulnerabilityXMLParser(object): VULNERABILITY = "vulnerability" VULNERABILITY_NAME = "name" VULNERABILITY_DESCRIPTION = "description" VULNERABILITY_SOLUTION = "solution" VULNERABILITY_REFERENCE = "reference" VULNERABILITY_REFERENCES = "references" VULNERABILITY_REFERENCE_TITLE = "title" VULNERABILITY_REFERENCE_URL = "url" def __init__(self): self._parser = expat.ParserCreate() self._parser.StartElementHandler = self.start_element self._parser.EndElementHandler = self.end_element self._parser.CharacterDataHandler = self.char_data self.vulnerabilities = [] self.vul = None self.references = {} self.title = "" self.url = "" self.tag = "" def parse(self, filename): f = None try: f = open(filename) content = f.read() self.feed(content) finally: if f is not None: f.close() def feed(self, data): self._parser.Parse(data, 0) def close(self): self._parser.Parse("", 1) del self._parser def start_element(self, name, attrs): if name == self.VULNERABILITY: self.vul = Vulnerability() self.vul.setName(attrs[self.VULNERABILITY_NAME]) elif name == self.VULNERABILITY_DESCRIPTION: self.tag = self.VULNERABILITY_DESCRIPTION elif name == self.VULNERABILITY_SOLUTION: #self.tag = self.VULNERABILITY_SOLUTION self.vul.setSolution(attrs["text"]) elif name == self.VULNERABILITY_REFERENCES: self.references = {} elif name == self.VULNERABILITY_REFERENCE: self.tag = self.VULNERABILITY_REFERENCE elif name == self.VULNERABILITY_REFERENCE_TITLE: self.tag = self.VULNERABILITY_REFERENCE_TITLE elif name == self.VULNERABILITY_REFERENCE_URL: self.tag = self.VULNERABILITY_REFERENCE_URL def end_element(self, name): if name == self.VULNERABILITY: self.vulnerabilities.append(self.vul) elif name == self.VULNERABILITY_REFERENCE: self.references[self.title] = self.url elif name == self.VULNERABILITY_REFERENCES: self.vul.setReferences(self.references) def char_data(self, data): if self.tag == self.VULNERABILITY_DESCRIPTION: self.vul.setDescription(data) # elif self.tag==self.VULNERABILITY_SOLUTION: # self.vul.setSolution(data) elif self.tag == self.VULNERABILITY_REFERENCE_TITLE: self.title = data elif self.tag == self.VULNERABILITY_REFERENCE_URL: self.url = data self.tag = "" def getVulnerabilities(self): return self.vulnerabilities