def validate_against_dtd(view, document): """Validate a document against the DTD in its DOCTYPE. TODO: Add support for external subsets and system identifiers. """ docinfo = document.docinfo internal_subset = docinfo.internalDTD system_url = docinfo.system_url if internal_subset is None and system_url is None: declare_valid(view) return False if internal_subset.external_id is None and system_url is not None: try: file = utils.resolve_file_path(system_url, view.file_name()) validator = _get_validator(system_url, etree.DTD, file=file) return validate(view, document, validator) except etree.DTDParseError as e: return vu.show_error(view, e) elif internal_subset.external_id is not None: # <!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd"> id = bytes(internal_subset.external_id, encodings.UTF8) try: validator = _get_validator(id, etree.DTD, external_id=id) return validate(view, document, validator) except etree.DTDParseError as e: return vu.show_error(view, e) else: # <!DOCTYPE people_list [ <!ELEMENT people_list (person)*> ]> try: return validate(view, document, internal_subset) except etree.DTDParseError as e: return vu.show_error(view, e)
def run(self, edit): view = self.view if not vu.is_xml(view) or len(vu.get_content(view).strip()) == 0: return try: parser = parsetools.get_parser(view, encoding=encodings.UTF8, load_dtd=True) doc = parsetools.parse_string(view, parser, vu.get_content(view)) if vu.is_xslt(view): version = doc.getroot().get(constants.VERSION) relax_ng = validator.get_xslt_relaxng_path(version) v = validator.get_validator_for_namespace( isoschematron.RELAXNG_NS)(view, doc, relax_ng) invoke_async(lambda: v, 0) else: invoke_async(lambda: validator.try_validate(view, doc), 0) except etree.XMLSyntaxError as e: message = str(e) if constants.LXML_NO_DTD_FOUND not in message: error = parser.error_log.filter_from_errors()[0] vu.show_error(view, message, error)
def validate(view, document, validator): try: validator.assertValid(document) return declare_valid(view) except etree.DocumentInvalid as e: if type(validator) == isoschematron.Schematron: message = _get_schematron_error_message(e) else: message = e vu.show_error(view, message, validator.error_log[0]) return True except OSError: vu.set_status(view, messages.SCHEMA_RESOLVE_ERROR % id) return False
def run(self, edit): view = self.view if not vu.is_xml(view) or len(vu.get_content(view).strip()) == 0: return try: parser = parsetools.get_parser(view, encoding=encodings.UTF8) doc = parsetools.parse_string(view, parser, vu.get_content(view)) if vu.is_xslt(view): version = doc.getroot().get(constants.VERSION) relax_ng = validator.get_xslt_relaxng_path(version) v = validator.get_validator_for_namespace( isoschematron.RELAXNG_NS)(view, doc, relax_ng) invoke_async(lambda: v, 0) else: invoke_async(lambda: validator.try_validate(view, doc), 0) except etree.XMLSyntaxError as e: message = str(e) if constants.LXML_NO_DTD_FOUND not in message: error = parser.error_log.filter_from_errors()[0] return vu.show_error(view, message, error)
def validate_against_schema(parser, error, view, document, schema_path): """Validate document against schema using parser and throw error if validation fails.""" current_file = view.file_name() # If the schema file URL is a relative URL and the file doesn't have # a name (as in, it hasn't been saved), bail out. if utils.is_relative_path(schema_path) and not current_file: return False file = utils.resolve_file_path(schema_path, current_file) try: validator = _get_validator(file, parser, file=file) return validate(view, document, validator) except (error, etree.XSLTApplyError) as e: vu.show_error(view, e) return False
def validate(view, document, validator): try: validator.assertValid(document) return declare_valid(view) except etree.DocumentInvalid as e: if type(validator) == isoschematron.Schematron: message = _get_schematron_error_message(e) else: message = e return vu.show_error(view, message, validator.error_log[0]) except OSError: vu.set_status(view, messages.SCHEMA_RESOLVE_ERROR % id) return False
def validate_against_dtd(view, document): """Validate a document against the DTD in its DOCTYPE. TODO: Add support for external subsets and system identifiers. """ internal_subset = document.docinfo.internalDTD if internal_subset is None: return False elif internal_subset.external_id is not None: # <!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd"> id = bytes(internal_subset.external_id, encodings.UTF8) try: validator = _get_validator(id, etree.DTD, external_id=id) return validate(view, document, validator) except etree.DTDParseError as e: return vu.show_error(view, e) else: # <!DOCTYPE people_list [ <!ELEMENT people_list (person)*> ]> try: return validate(view, document, internal_subset) except etree.DTDParseError as e: return vu.show_error(view, e)
def validate_against_schema(parser, error, view, document, schema_path): """Validate document against schema using parser and throw error if validation fails.""" current_file = view.file_name() # If the schema file URL is a relative URL and the file doesn't have # a name (as in, it hasn't been saved), bail out. if utils.is_relative_path(schema_path) and not current_file: return False file = utils.resolve_file_path(schema_path, current_file) try: validator = _get_validator(file, parser, file=file) return validate(view, document, validator) except (error, etree.XSLTApplyError) as e: return vu.show_error(view, e)