2 * Copyright (C) 2014 Jonathan Rajotte <jonathan.r.julien@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; version 2.1 of
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * This script validate and xml from an xsd.
21 * argv[1] Path of the xsd
22 * argv[2] Path to the XML to be validated
34 #include <sys/types.h>
37 #include <libxml/xmlschemas.h>
38 #include <libxml/parser.h>
40 #include <lttng/lttng-error.h>
42 struct validation_ctx
{
43 xmlSchemaParserCtxtPtr parser_ctx
;
45 xmlSchemaValidCtxtPtr schema_validation_ctx
;
48 enum command_err_code
{
54 void xml_error_handler(void *ctx
, const char *format
, ...)
60 va_start(args
, format
);
61 ret
= vasprintf(&err_msg
, format
, args
);
64 fprintf(stderr
, "ERR: %s\n",
65 "String allocation failed in xml error handle");
69 fprintf(stderr
, "XML Error: %s\n", err_msg
);
74 void fini_validation_ctx(
75 struct validation_ctx
*ctx
)
77 if (ctx
->parser_ctx
) {
78 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
82 xmlSchemaFree(ctx
->schema
);
85 if (ctx
->schema_validation_ctx
) {
86 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
89 memset(ctx
, 0, sizeof(struct validation_ctx
));
93 int init_validation_ctx(
94 struct validation_ctx
*ctx
, char *xsd_path
)
99 ret
= -LTTNG_ERR_NOMEM
;
103 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
104 if (!ctx
->parser_ctx
) {
105 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
108 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
109 xml_error_handler
, NULL
);
111 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
113 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
117 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
118 if (!ctx
->schema_validation_ctx
) {
119 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
123 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
124 xml_error_handler
, NULL
);
129 fini_validation_ctx(ctx
);
134 static int validate_xml(const char *xml_file_path
, struct validation_ctx
*ctx
)
137 xmlDocPtr doc
= NULL
;
139 assert(xml_file_path
);
142 /* Open the document */
143 doc
= xmlParseFile(xml_file_path
);
145 ret
= LTTNG_ERR_MI_IO_FAIL
;
149 /* Validate against the validation ctx (xsd) */
150 ret
= xmlSchemaValidateDoc(ctx
->schema_validation_ctx
, doc
);
152 fprintf(stderr
, "ERR: %s\n", "XML is not valid againt provided XSD");
163 int main(int argc
, char **argv
, char *env
[])
166 struct validation_ctx ctx
= { 0 };
168 /* Check if we have all argument */
170 fprintf(stderr
, "ERR: %s\n", "Missing arguments");
175 /* Check if xsd file exist */
176 ret
= access(argv
[1], F_OK
);
178 fprintf(stderr
, "ERR: %s\n", "Xsd path not valid");
182 /* Check if xml to validate exist */
183 ret
= access(argv
[2], F_OK
);
185 fprintf(stderr
, "ERR: %s\n", "XML path not valid");
189 /* initialize the validation ctx */
190 ret
= init_validation_ctx(&ctx
, argv
[1]);
195 ret
= validate_xml(argv
[2], &ctx
);
197 fini_validation_ctx(&ctx
);