import os from SupportedType import SupportedType from xml.dom import Node from xml.dom.ext.reader.Sax import FromXmlFile """ This class parses all of the information in the config file, and makes it available through various methods. """ class XmlDataParser: def __init__(self, xml_file_path): self.__supported_types = [] xml_doc = FromXmlFile(xml_file_path) supported_type_elements = xml_doc.getElementsByTagName('supported_type') for current_node in supported_type_elements: name = self.GetChildrenText(current_node, 'name')[0] descriptions = self.GetChildrenText(current_node, 'description') extensions = self.GetChildrenText(current_node, 'extension') template_files = self.GetChildrenText(current_node, 'template_file') template_data = self.GetChildrenText(current_node, 'template_data') current_type = SupportedType() current_type.SetName(name) current_type.SetExtensions(extensions) if (len(descriptions) > 0): current_type.SetDescription(descriptions[0]) if (len(template_files) > 0): # Allow for relative paths in the template field root_dir = os.path.dirname(xml_file_path) template_file_path = os.path.join(root_dir, template_files[0]) current_type.SetTemplateFile(template_file_path) if (len(template_data) > 0): current_type.SetTemplateData(template_data[0]) self.__supported_types.append(current_type) return def GetAllExtensions(self): all_extensions = [] for current_type in self.__supported_types: current_type_extensions = current_type.GetExtensions() for extension in current_type_extensions: all_extensions.append(extension) return all_extensions def GetTemplateByExtension(self, extension): # Get the (x)html template which corresponds to a mimetype # having the supplied extension. for current_type in self.__supported_types: current_type_extensions = current_type.GetExtensions() for current_extension in current_type_extensions: if (current_extension.lower() == extension.lower()): return current_type.GetTemplate() return None def GetNameByExtension(self, extension): # Get the name of the mimetype that a particular extension # belongs to. for current_type in self.__supported_types: current_type_extensions = current_type.GetExtensions() for current_extension in current_type_extensions: if (current_extension.lower() == extension.lower()): return current_type.GetName() return None def GetChildNodesByTagName(self, node, tag_name): found_children = [] for child in node.childNodes: if (child.nodeName == tag_name): found_children.append(child) return found_children def GetChildrenText(self, node, child_tag_name): children_text = [] children = self.GetChildNodesByTagName(node, child_tag_name) for child_node in children: for sub_child in child_node.childNodes: if (sub_child.nodeType == Node.TEXT_NODE): children_text.append(sub_child.nodeValue) break return children_text