def check_xml_file_is_valid(data, request=None): """Check if xml data is valid against a given schema. Args: data: request: Returns: """ template = data.template try: xml_tree = XSDTree.build_tree(data.xml_content) except Exception as e: raise exceptions.XMLError(str(e)) try: xsd_tree = XSDTree.build_tree(template.content) except Exception as e: raise exceptions.XSDError(str(e)) error = validate_xml_data(xsd_tree, xml_tree, request=request) if error is not None: raise exceptions.XMLError(error) else: return True
def raw_xml_to_dict(raw_xml, postprocessor=None, force_list=None): """Transform a raw xml to dict. Returns an empty dict if the parsing failed. Args: raw_xml: postprocessor: force_list: Returns: """ try: if postprocessor: # set postprocessor function if found in the list (XML_POST_PROCESSORS) postprocessor = (XML_POST_PROCESSORS[postprocessor] if postprocessor in XML_POST_PROCESSORS else postprocessor) # check if postprocessor is callable if not callable(postprocessor): raise exceptions.CoreError("postprocessor is not callable") # convert xml to dict return xmltodict.parse(raw_xml, postprocessor=postprocessor, force_list=force_list) except xmltodict.expat.ExpatError: raise exceptions.XMLError( "An unexpected error happened during the XML parsing.")
def validate_xpath(xpath): """Validate a provided xpath. Args: xpath: Raises: core_main_app.commons.exceptions.CoreError """ try: xml_utils_xpath.validate_xpath(xpath) except xml_utils_exceptions.XPathError as e: raise exceptions.XMLError(str(e))
def format_content_xml(xml_string): """Format XML content. Args: xml_string: Returns: """ try: xml_tree = XSDTree.build_tree(xml_string) return XSDTree.tostring(xml_tree, pretty=True) except Exception: raise exceptions.XMLError("Content is not well formatted XML.")
def raw_xml_to_dict(raw_xml, postprocessor=None): """Transform a raw xml to dict. Returns an empty dict if the parsing failed. Args: raw_xml: postprocessor: Returns: """ try: dict_raw = xmltodict.parse(raw_xml, postprocessor=postprocessor) return dict_raw except xmltodict.expat.ExpatError: raise exceptions.XMLError( "An unexpected error happened during the XML parsing.")
def is_schema_valid(xsd_string, *args, **kwargs): """Test if the schema is valid to be uploaded. Args: xsd_string: Returns: """ if not is_well_formed_xml(xsd_string): raise exceptions.XMLError("Uploaded file is not well formatted XML.") # Check schema support by the core errors = _check_core_support(xsd_string) if len(errors) > 0: errors_str = ", ".join(errors) raise exceptions.CoreError(errors_str) error = validate_xml_schema(XSDTree.build_tree(xsd_string), *args, **kwargs) if error is not None: raise exceptions.XSDError(error)