Changeset View
Changeset View
Standalone View
Standalone View
source/ps/XML/RelaxNG.cpp
/* Copyright (C) 2015 Wildfire Games. | /* Copyright (C) 2018 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | public: | ||||
xmlRelaxNGPtr m_Schema; | xmlRelaxNGPtr m_Schema; | ||||
RelaxNGSchema(const std::string& grammar) | RelaxNGSchema(const std::string& grammar) | ||||
{ | { | ||||
xmlRelaxNGParserCtxtPtr ctxt = xmlRelaxNGNewMemParserCtxt(grammar.c_str(), (int)grammar.size()); | xmlRelaxNGParserCtxtPtr ctxt = xmlRelaxNGNewMemParserCtxt(grammar.c_str(), (int)grammar.size()); | ||||
m_Schema = xmlRelaxNGParse(ctxt); | m_Schema = xmlRelaxNGParse(ctxt); | ||||
xmlRelaxNGFreeParserCtxt(ctxt); | xmlRelaxNGFreeParserCtxt(ctxt); | ||||
if (m_Schema == NULL) | if (m_Schema == nullptr) | ||||
LOGERROR("RelaxNGValidator: Failed to compile schema"); | LOGERROR("RelaxNGValidator: Failed to compile schema"); | ||||
} | } | ||||
~RelaxNGSchema() | ~RelaxNGSchema() | ||||
{ | { | ||||
if (m_Schema) | if (m_Schema) | ||||
xmlRelaxNGFree(m_Schema); | xmlRelaxNGFree(m_Schema); | ||||
} | } | ||||
}; | }; | ||||
RelaxNGValidator::RelaxNGValidator() : | RelaxNGValidator::RelaxNGValidator() : | ||||
m_Schema(NULL) | m_Schema(nullptr) | ||||
{ | { | ||||
} | } | ||||
RelaxNGValidator::~RelaxNGValidator() | RelaxNGValidator::~RelaxNGValidator() | ||||
{ | { | ||||
} | } | ||||
bool RelaxNGValidator::LoadGrammar(const std::string& grammar) | bool RelaxNGValidator::LoadGrammar(const std::string& grammar) | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | bool RelaxNGValidator::ValidateEncoded(const std::wstring& filename, const std::string& document) const | ||||
TIMER_ACCRUE(xml_validation); | TIMER_ACCRUE(xml_validation); | ||||
if (!m_Schema) | if (!m_Schema) | ||||
{ | { | ||||
LOGERROR("RelaxNGValidator: No grammar loaded"); | LOGERROR("RelaxNGValidator: No grammar loaded"); | ||||
return false; | return false; | ||||
} | } | ||||
xmlDocPtr doc = xmlReadMemory(document.c_str(), (int)document.size(), utf8_from_wstring(filename).c_str(), NULL, XML_PARSE_NONET); | xmlDocPtr doc = xmlReadMemory(document.c_str(), (int)document.size(), utf8_from_wstring(filename).c_str(), nullptr, XML_PARSE_NONET); | ||||
if (doc == NULL) | if (doc == nullptr) | ||||
{ | { | ||||
LOGERROR("RelaxNGValidator: Failed to parse document '%s'", utf8_from_wstring(filename).c_str()); | LOGERROR("RelaxNGValidator: Failed to parse document '%s'", utf8_from_wstring(filename).c_str()); | ||||
return false; | return false; | ||||
} | } | ||||
bool ret = ValidateEncoded(doc); | bool ret = ValidateEncoded(doc); | ||||
xmlFreeDoc(doc); | xmlFreeDoc(doc); | ||||
return ret; | return ret; | ||||
} | } | ||||
bool RelaxNGValidator::ValidateEncoded(xmlDocPtr doc) const | bool RelaxNGValidator::ValidateEncoded(xmlDocPtr doc) const | ||||
{ | { | ||||
xmlRelaxNGValidCtxtPtr ctxt = xmlRelaxNGNewValidCtxt(m_Schema); | xmlRelaxNGValidCtxtPtr ctxt = xmlRelaxNGNewValidCtxt(m_Schema); | ||||
xmlRelaxNGSetValidStructuredErrors(ctxt, &relaxNGErrorHandler, NULL); | xmlRelaxNGSetValidStructuredErrors(ctxt, &relaxNGErrorHandler, nullptr); | ||||
int ret = xmlRelaxNGValidateDoc(ctxt, doc); | int ret = xmlRelaxNGValidateDoc(ctxt, doc); | ||||
xmlRelaxNGFreeValidCtxt(ctxt); | xmlRelaxNGFreeValidCtxt(ctxt); | ||||
if (ret == 0) | if (ret == 0) | ||||
{ | { | ||||
return true; | return true; | ||||
} | } | ||||
else if (ret > 0) | else if (ret > 0) | ||||
{ | { | ||||
LOGERROR("RelaxNGValidator: Validation failed for '%s'", doc->name); | LOGERROR("RelaxNGValidator: Validation failed for '%s'", doc->name); | ||||
return false; | return false; | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
LOGERROR("RelaxNGValidator: Internal error %d", ret); | LOGERROR("RelaxNGValidator: Internal error %d", ret); | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
bool RelaxNGValidator::CanValidate() const | bool RelaxNGValidator::CanValidate() const | ||||
{ | { | ||||
return m_Schema != NULL; | return m_Schema != nullptr; | ||||
} | } |
Wildfire Games · Phabricator