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
33 #include <sys/types.h>
36 #include <libxml/xmlschemas.h>
37 #include <libxml/parser.h>
39 #include <lttng/lttng-error.h>
41 struct validation_ctx
{
42 xmlSchemaParserCtxtPtr parser_ctx
;
44 xmlSchemaValidCtxtPtr schema_validation_ctx
;
47 enum command_err_code
{
53 void xml_error_handler(void *ctx
, const char *format
, ...)
59 va_start(args
, format
);
60 ret
= vasprintf(&err_msg
, format
, args
);
63 fprintf(stderr
, "ERR: %s\n",
64 "String allocation failed in xml error handle");
68 fprintf(stderr
, "XML Error: %s\n", err_msg
);
73 void fini_validation_ctx(
74 struct validation_ctx
*ctx
)
76 if (ctx
->parser_ctx
) {
77 xmlSchemaFreeParserCtxt(ctx
->parser_ctx
);
81 xmlSchemaFree(ctx
->schema
);
84 if (ctx
->schema_validation_ctx
) {
85 xmlSchemaFreeValidCtxt(ctx
->schema_validation_ctx
);
88 memset(ctx
, 0, sizeof(struct validation_ctx
));
92 int init_validation_ctx(
93 struct validation_ctx
*ctx
, char *xsd_path
)
98 ret
= -LTTNG_ERR_NOMEM
;
102 ctx
->parser_ctx
= xmlSchemaNewParserCtxt(xsd_path
);
103 if (!ctx
->parser_ctx
) {
104 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
107 xmlSchemaSetParserErrors(ctx
->parser_ctx
, xml_error_handler
,
108 xml_error_handler
, NULL
);
110 ctx
->schema
= xmlSchemaParse(ctx
->parser_ctx
);
112 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
116 ctx
->schema_validation_ctx
= xmlSchemaNewValidCtxt(ctx
->schema
);
117 if (!ctx
->schema_validation_ctx
) {
118 ret
= -LTTNG_ERR_LOAD_INVALID_CONFIG
;
122 xmlSchemaSetValidErrors(ctx
->schema_validation_ctx
, xml_error_handler
,
123 xml_error_handler
, NULL
);
128 fini_validation_ctx(ctx
);
133 static int validate_xml(const char *xml_file_path
, struct validation_ctx
*ctx
)
136 xmlDocPtr doc
= NULL
;
138 assert(xml_file_path
);
141 /* Open the document */
142 doc
= xmlParseFile(xml_file_path
);
144 ret
= LTTNG_ERR_MI_IO_FAIL
;
148 /* Validate against the validation ctx (xsd) */
149 ret
= xmlSchemaValidateDoc(ctx
->schema_validation_ctx
, doc
);
151 fprintf(stderr
, "ERR: %s\n", "XML is not valid againt provided XSD");
162 int main(int argc
, char **argv
, char *env
[])
165 struct validation_ctx ctx
= { 0 };
167 /* Check if we have all argument */
169 fprintf(stderr
, "ERR: %s\n", "Missing arguments");
174 /* Check if xsd file exist */
175 ret
= access(argv
[1], F_OK
);
177 fprintf(stderr
, "ERR: %s\n", "Xsd path not valid");
181 /* Check if xml to validate exist */
182 ret
= access(argv
[2], F_OK
);
184 fprintf(stderr
, "ERR: %s\n", "XML path not valid");
188 /* initialize the validation ctx */
189 ret
= init_validation_ctx(&ctx
, argv
[1]);
194 ret
= validate_xml(argv
[2], &ctx
);
196 fini_validation_ctx(&ctx
);