#include "support/parse_helpers.hpp" namespace support { // Код ошибки вместо исключения — удобно проверять в тестах // An error code instead of an exception — convenient to check in tests beresta::DomErrorCode errorCodeOf(std::string_view input) { try { beresta::XmlParser().parse(input); } catch (const beresta::ParseException& error) { return error.getCode(); } return beresta::DomErrorCode::NOT_FOUND; } // Все ошибки валидности собираются CollectingErrorHandler за один проход // All validity errors are collected by CollectingErrorHandler in one pass std::size_t countValidityErrors(const std::string& document) { beresta::CollectingErrorHandler handler; beresta::ParserOptions options; options.validateDtd = true; options.errorHandler = &handler; beresta::XmlParser(options).parse(document); return handler.getDiagnostics().size(); } // Встроенный тип ищется по имени, значение проверяется без контекста префиксов // The built-in type is looked up by name, the value is checked without a prefix context bool acceptsBuiltin(const char* typeName, const char* value) { const beresta::xsd::SimpleType* type = beresta::xsd::builtinType(typeName); return type != nullptr && !beresta::xsd::validateSimpleValue(*type, value, nullptr).has_value(); } // Схема компилируется из текста, документ разбирается и проверяется // The schema is compiled from text, the document is parsed and validated std::vector schemaErrors(const std::string& xsd, const std::string& xml) { std::shared_ptr schema = beresta::xsd::Schema::fromString(xsd); beresta::Document document = beresta::XmlParser().parse(xml); return beresta::xsd::SchemaValidator(schema).validate(document); } // Тело оборачивается в xs:schema — так короче тесты загрузки схем // The body is wrapped into xs:schema — this makes the schema loading tests shorter std::shared_ptr schemaFromBody(const std::string& body) { return beresta::xsd::Schema::fromString("" + body + ""); } }