def save(self, root, namespace_project, namespace_common): xmlVocabulary = etree.SubElement(root, "{" + namespace_project + "}vocabulary") # Messages xmlMessages = etree.SubElement(xmlVocabulary, "{" + namespace_project + "}messages") for message in self.messages: AbstractMessageFactory.save(message, xmlMessages, namespace_project, namespace_common) # Symbols xmlSymbols = etree.SubElement(xmlVocabulary, "{" + namespace_project + "}symbols") for symbol in self.symbols: symbol.save(xmlSymbols, namespace_project, namespace_common) # Sessions xmlSessions = etree.SubElement(xmlVocabulary, "{" + namespace_project + "}sessions") for session in self.sessions: session.save(xmlSessions, namespace_project, namespace_common)
def save(self, root, namespace_workspace, namespace_common, pathOfTraces, override=False): xmlTrace = etree.SubElement(root, "{" + namespace_workspace + "}trace") xmlTrace.set( "date", str(TypeConvertor.pythonDatetime2XSDDatetime(self.getDate()))) xmlTrace.set("type", str(self.getType())) xmlTrace.set("description", str(self.getDescription())) xmlTrace.set("name", str(self.getName())) xmlTrace.set("id", str(self.getID())) # Register the namespace (2 way depending on the version) try: etree.register_namespace('netzob-common', namespace_common) except AttributeError: etree._namespace_map[namespace_common] = 'netzob-common' # Save the messages root = etree.Element("{" + namespace_workspace + "}trace") root.set("id", str(self.getID())) xmlMessages = etree.SubElement(root, "{" + namespace_workspace + "}messages") for message in self.getMessages(): AbstractMessageFactory.save(message, xmlMessages, namespace_workspace, namespace_common) # Save the sessions xmlSessions = etree.SubElement(root, "{" + namespace_workspace + "}sessions") for session in self.getSessions(): session.save(xmlSessions, namespace_workspace, namespace_common) contentOfFile = str(etree.tostring(root)) # Creation of the XML File (in buffer) # Compress it using gzip and save the .gz tracesFile = os.path.join(pathOfTraces, str(self.getID()) + ".gz") if not os.path.isfile(tracesFile) or override: logging.debug("Save the trace " + str(self.getID()) + " in " + tracesFile) # Compress and write the file gzipFile = gzip.open(tracesFile, 'wb') gzipFile.write(contentOfFile) gzipFile.close()
def loadVocabulary(xmlRoot, namespace_project, namespace_common, version, project): vocabulary = Vocabulary() if version == "0.1": # Messages for xmlMessage in xmlRoot.findall("{" + namespace_project + "}messages/{" + namespace_common + "}message"): message = AbstractMessageFactory.loadFromXML( xmlMessage, namespace_common, version) if message is not None: vocabulary.addMessage(message) # Symbols for xmlSymbol in xmlRoot.findall("{" + namespace_project + "}symbols/{" + namespace_project + "}symbol"): symbol = Symbol.loadSymbol(xmlSymbol, namespace_project, namespace_common, version, project, vocabulary) if symbol is not None: vocabulary.addSymbol(symbol) # Sessions for xmlSession in xmlRoot.findall("{" + namespace_project + "}sessions/{" + namespace_common + "}session"): session = Session.loadFromXML(xmlSession, namespace_project, namespace_common, version, vocabulary) if session is not None: vocabulary.addSession(session) return vocabulary
def retrieveMessagesFromFiles(self): # We read each file and create one message for each file self.messages = [] self.lineView.get_model().clear() for file in self.filesToBeImported: from netzob.Common.ResourcesConfiguration import ResourcesConfiguration xmlSchemaPath = os.path.join(ResourcesConfiguration.getStaticResources(), "xsds/0.1/common.xsd") # If we find a version which validates the XML, we parse with the associated function if not Workspace.isSchemaValidateXML(xmlSchemaPath, file): logging.error(_("The specified XML file {0} is not valid according to the XSD ({1}).").format(str(file), str(xmlSchemaPath))) else: logging.debug(_("XML file valid according to the XSD schema")) # Parse the XML Document as 0.1 version tree = ElementTree() tree.parse(file) xmlFile = tree.getroot() for xmlMessage in xmlFile.findall("{" + Project.COMMON_NAMESPACE + "}message"): message = AbstractMessageFactory.loadFromXML(xmlMessage, Project.COMMON_NAMESPACE, "0.1") logging.debug(_("XML String data: {0}").format(message.getStringData())) self.messages.append(message) self.lineView.get_model().append(None, [str(message.getID()), message.getType(), message.getStringData()])
def save(self, root, namespace_workspace, namespace_common, pathOfTraces): xmlTrace = etree.SubElement(root, "{" + namespace_workspace + "}trace") xmlTrace.set("date", str(TypeConvertor.pythonDatetime2XSDDatetime(self.getDate()))) xmlTrace.set("type", str(self.getType())) xmlTrace.set("description", str(self.getDescription())) xmlTrace.set("name", str(self.getName())) xmlTrace.set("id", str(self.getID())) # Creation of the XML File (in buffer) # Compress it using gzip and save the .gz tracesFile = os.path.join(pathOfTraces, str(self.getID()) + ".gz") logging.info("Save the trace " + str(self.getID()) + " in " + tracesFile) # Register the namespace (2 way depending on the version) try: etree.register_namespace('netzob-common', namespace_common) except AttributeError: etree._namespace_map[namespace_common] = 'netzob-common' # Save the messages root = etree.Element("{" + namespace_workspace + "}trace") root.set("id", str(self.getID())) xmlMessages = etree.SubElement(root, "{" + namespace_workspace + "}messages") for message in self.getMessages(): AbstractMessageFactory.save(message, xmlMessages, namespace_workspace, namespace_common) # Save the sessions xmlSessions = etree.SubElement(root, "{" + namespace_workspace + "}sessions") for session in self.getSessions(): session.save(xmlSessions, namespace_workspace, namespace_common) tree = ElementTree(root) contentOfFile = str(etree.tostring(tree.getroot())) # if outputfile already exists we delete it if os.path.isfile(tracesFile): logging.debug("The compressed version (" + tracesFile + ") of the file already exists, we replace it with the new one") os.remove(tracesFile) # Compress and write the file gzipFile = gzip.open(tracesFile, 'wb') gzipFile.write(contentOfFile) gzipFile.close()
def save(self, root, namespace_project, namespace_common): xmlVocabulary = etree.SubElement( root, "{" + namespace_project + "}vocabulary") # Messages xmlMessages = etree.SubElement(xmlVocabulary, "{" + namespace_project + "}messages") for message in self.messages: AbstractMessageFactory.save(message, xmlMessages, namespace_project, namespace_common) # Symbols xmlSymbols = etree.SubElement(xmlVocabulary, "{" + namespace_project + "}symbols") for symbol in self.symbols: symbol.save(xmlSymbols, namespace_project, namespace_common) # Sessions xmlSessions = etree.SubElement(xmlVocabulary, "{" + namespace_project + "}sessions") for session in self.sessions: session.save(xmlSessions, namespace_project, namespace_common)
def loadTrace(xmlRoot, namespace_workspace, namespace_common, version, pathOfTraces): if version == "0.1": date = TypeConvertor.xsdDatetime2PythonDatetime( str(xmlRoot.get("date"))) type = xmlRoot.get("type") description = xmlRoot.get("description", "") id = str(xmlRoot.get("id")) name = xmlRoot.get("name") importedTrace = ImportedTrace(id, date, type, description, name) tracesFile = os.path.join(pathOfTraces, "{0}.gz".format(id)) if not os.path.isfile(tracesFile): logging.warn( "The trace file {0} is referenced but doesn't exist.". format(tracesFile)) else: gzipFile = gzip.open(tracesFile, 'rb') xml_content = gzipFile.read() gzipFile.close() tree = etree.parse(StringIO(xml_content)) xmlRoot = tree.getroot() # We retrieve the pool of messages xmlMessages = xmlRoot.find("{" + namespace_workspace + "}messages") if xmlMessages is not None: for xmlMessage in xmlMessages.findall("{" + namespace_common + "}message"): message = AbstractMessageFactory.loadFromXML( xmlMessage, namespace_common, version) if message is not None: importedTrace.addMessage(message) # We retrieve the sessions if xmlRoot.find("{" + namespace_workspace + "}sessions") is not None: xmlSessions = xmlRoot.find("{" + namespace_workspace + "}sessions") for xmlSession in xmlSessions.findall("{" + namespace_common + "}session"): session = Session.loadFromXML(xmlSession, namespace_workspace, namespace_common, version, importedTrace) if session is not None: importedTrace.addSession(session) return importedTrace return None
def _readMessagesFromFile(self, filePath): from netzob.Common.ResourcesConfiguration import ResourcesConfiguration xmlSchemaPath = os.path.join(ResourcesConfiguration.getStaticResources(), "xsds/0.1/common.xsd") # If we find a version which validates the XML, we parse with the associated function if not Workspace.isSchemaValidateXML(xmlSchemaPath, filePath): logging.error("The specified XML file {0} is not valid " "according to the XSD ({1}).".format(filePath, xmlSchemaPath)) else: logging.debug("XML file valid according to the XSD schema") # Parse the XML Document as 0.1 version tree = ElementTree() tree.parse(filePath) xmlFile = tree.getroot() for xmlMessage in xmlFile.findall("{" + Project.COMMON_NAMESPACE + "}message"): message = AbstractMessageFactory.loadFromXML(xmlMessage, Project.COMMON_NAMESPACE, "0.1") logging.debug("XML String data: " + message.getStringData()) self.messages.append(message)
def loadVocabulary(xmlRoot, namespace_project, namespace_common, version, project): vocabulary = Vocabulary() if version == "0.1": # Messages for xmlMessage in xmlRoot.findall("{" + namespace_project + "}messages/{" + namespace_common + "}message"): message = AbstractMessageFactory.loadFromXML(xmlMessage, namespace_common, version) if message is not None: vocabulary.addMessage(message) # Symbols for xmlSymbol in xmlRoot.findall("{" + namespace_project + "}symbols/{" + namespace_project + "}symbol"): symbol = Symbol.loadSymbol(xmlSymbol, namespace_project, namespace_common, version, project, vocabulary) if symbol is not None: vocabulary.addSymbol(symbol) # Sessions for xmlSession in xmlRoot.findall("{" + namespace_project + "}sessions/{" + namespace_common + "}session"): session = Session.loadFromXML(xmlSession, namespace_project, namespace_common, version, vocabulary) if session is not None: vocabulary.addSession(session) return vocabulary
def loadTrace(xmlRoot, namespace_workspace, namespace_common, version, pathOfTraces): if version == "0.1": date = TypeConvertor.xsdDatetime2PythonDatetime(str(xmlRoot.get("date"))) type = xmlRoot.get("type") description = xmlRoot.get("description", "") id = xmlRoot.get("id") name = xmlRoot.get("name") importedTrace = ImportedTrace(id, date, type, description, name) tracesFile = os.path.join(pathOfTraces, str(id) + ".gz") if not os.path.isfile(tracesFile): logging.warn("The trace file " + str(tracesFile) + " is referenced but doesn't exist.") else: gzipFile = gzip.open(tracesFile, 'rb') xml_content = gzipFile.read() gzipFile.close() tree = etree.parse(StringIO(xml_content)) xmlRoot = tree.getroot() # We retrieve the pool of messages if xmlRoot.find("{" + namespace_workspace + "}messages") != None: xmlMessages = xmlRoot.find("{" + namespace_workspace + "}messages") for xmlMessage in xmlMessages.findall("{" + namespace_common + "}message"): message = AbstractMessageFactory.loadFromXML(xmlMessage, namespace_common, version) if message != None: importedTrace.addMessage(message) # We retrieve the sessions if xmlRoot.find("{" + namespace_workspace + "}sessions") != None: xmlSessions = xmlRoot.find("{" + namespace_workspace + "}sessions") for xmlSession in xmlSessions.findall("{" + namespace_common + "}session"): session = Session.loadFromXML(xmlSession, namespace_workspace, namespace_common, version, importedTrace) if session != None: importedTrace.addSession(session) return importedTrace return None
def retrieveMessagesFromFiles(self): # We read each file and create one message for each file self.messages = [] self.lineView.get_model().clear() for file in self.filesToBeImported: from netzob.Common.ResourcesConfiguration import ResourcesConfiguration xmlSchemaPath = os.path.join( ResourcesConfiguration.getStaticResources(), "xsds/0.1/common.xsd") # If we find a version which validates the XML, we parse with the associated function if not Workspace.isSchemaValidateXML(xmlSchemaPath, file): logging.error( "The specified XML file {0} is not valid according to the XSD ({1})." .format(str(file), str(xmlSchemaPath))) else: logging.debug("XML file valid according to the XSD schema") # Parse the XML Document as 0.1 version tree = ElementTree() tree.parse(file) xmlFile = tree.getroot() for xmlMessage in xmlFile.findall("{" + Project.COMMON_NAMESPACE + "}message"): message = AbstractMessageFactory.loadFromXML( xmlMessage, Project.COMMON_NAMESPACE, "0.1") logging.debug("XML String data: {0}".format( message.getStringData())) self.messages.append(message) self.lineView.get_model().append(None, [ str(message.getID()), message.getType(), message.getStringData() ])