8 #include <boost/filesystem/operations.hpp> 9 #include <libxml/xmlversion.h> 10 #ifndef LIBXML_SCHEMAS_ENABLED 11 #warning "Error libxml schemas disabled" 13 #include <libxml/tree.h> 14 #include <libxml/parser.h> 15 #include <libxml/xinclude.h> 16 #include <libxml/xmlschemas.h> 17 #include <libxml/xmlschemastypes.h> 19 #include "fwRuntime/RuntimeException.hpp" 20 #include "fwRuntime/io/Validator.hpp" 22 #include <fwCore/base.hpp> 33 m_xsd_content = validator.m_xsd_content;
34 m_schemaParserContext = validator.m_schemaParserContext;
35 m_schema = validator.m_schema;
42 m_xsd_content = buffer;
49 std::string strPath( path.string() );
51 if( ::boost::filesystem::exists(path) ==
false || ::boost::filesystem::is_directory(path) )
53 throw RuntimeException( strPath +
": is not a valid path to an xml schema file." );
55 m_xsd_content = strPath;
68 m_errorLog.str( std::string() );
75 return m_errorLog.str();
80 void Validator::initializeContext()
82 if(m_schemaValidContext)
87 if ( !m_schemaParserContext )
89 if (!(m_schemaParserContext = SchemaParserCtxtSptr (
90 xmlSchemaNewParserCtxt(m_xsd_content.c_str()),
91 xmlSchemaFreeParserCtxt)
97 xmlSchemaSetParserStructuredErrors(m_schemaParserContext.get(), Validator::ErrorHandler, this );
103 m_schema = SchemaSptr ( xmlSchemaParse(m_schemaParserContext.get()), xmlSchemaFree );
111 if ( (m_schemaValidContext = SchemaValidCtxtSptr( xmlSchemaNewValidCtxt(m_schema.get()), xmlSchemaFreeValidCtxt)) )
114 xmlSchemaSetValidStructuredErrors( m_schemaValidContext.get(), Validator::ErrorHandler, this );
126 xmlDocPtr xmlDoc = xmlParseFile ( xmlFile.string().c_str () );
129 throw std::ios_base::failure(
"Unable to parse the XML file " + xmlFile.string() );
131 xmlNodePtr xmlRoot = xmlDocGetRootElement (xmlDoc);
132 if (xmlXIncludeProcessTreeFlags (xmlRoot,XML_PARSE_NOBASEFIX) == -1)
135 throw std::ios_base::failure(std::string (
"Unable to manage xinclude !"));
138 if(!m_schemaValidContext)
143 result = xmlSchemaValidateDoc(m_schemaValidContext.get(), xmlDoc );
149 OSLM_WARN(
"Validator::validation NOK, xml = " << xmlFile.string() );
165 if(!m_schemaValidContext)
170 result = xmlSchemaValidateOneElement( m_schemaValidContext.get(), node );
174 xmlBufferPtr buffer = xmlBufferCreate();
175 xmlNodeDump( buffer, node->doc, node, 1, 1 );
176 OSLM_WARN(
"Validator::validation NOK, node :\n " << buffer->content);
177 xmlBufferFree( buffer );
187 void Validator::ErrorHandler(
void * userData, xmlErrorPtr error )
191 validator->m_errorLog <<
"At line " << error->line <<
": " << error->message;
198 return m_xsd_content;
Defines the runtime exception class.
void clearErrorLog()
Clears the error log.
FWRUNTIME_API const std::string getErrorLog() const
Retrieves the error log content.
FWRUNTIME_API bool validate(const boost::filesystem::path &xmlFile)
Validates the given file.
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
#define OSLM_ERROR(message)
#define OSLM_WARN(message)
Implements an XML validator.
FWRUNTIME_API Validator(const Validator &validator)
Copy Constructor.
FWRUNTIME_API ~Validator()
Destructor.
FWRUNTIME_API std::string getXsdContent()
Returns the xsd content in string format.