blob: a399b9c616434acfc71d927aea685df7eef9a9af [file] [log] [blame]
Patrick Benavoli68a91282011-08-31 11:23:23 +02001/* <auto_header>
2 * <FILENAME>
3 *
4 * INTEL CONFIDENTIAL
5 * Copyright © 2011 Intel
6 * Corporation All Rights Reserved.
7 *
8 * The source code contained or described herein and all documents related to
9 * the source code ("Material") are owned by Intel Corporation or its suppliers
10 * or licensors. Title to the Material remains with Intel Corporation or its
11 * suppliers and licensors. The Material contains trade secrets and proprietary
12 * and confidential information of Intel or its suppliers and licensors. The
13 * Material is protected by worldwide copyright and trade secret laws and
14 * treaty provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed, or
16 * disclosed in any way without Intel’s prior express written permission.
17 *
18 * No license under any patent, copyright, trade secret or other intellectual
19 * property right is granted to or conferred upon you by disclosure or delivery
20 * of the Materials, either expressly, by implication, inducement, estoppel or
21 * otherwise. Any license under such intellectual property rights must be
22 * express and approved by Intel in writing.
23 *
24 * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com)
25 * CREATED: 2011-06-01
26 * UPDATED: 2011-07-27
27 *
28 *
29 * </auto_header>
30 */
31/**
32 * section: Tree
33 * synopsis: Navigates a tree to print element names
34 * purpose: Parse a file to a tree, use xmlDocGetRootElement() to
35 * get the root element, then walk the document and print
36 * all the element name in document order.
37 * usage: tree1 filename_or_URL
38 * test: tree1 test2.xml > tree1.tmp ; diff tree1.tmp tree1.res ; rm tree1.tmp
39 * author: Dodji Seketeli
40 * copy: see Copyright for the status of this software.
41 */
42#include <stdio.h>
43#include <stdarg.h>
44#include <libxml/parser.h>
45#include <libxml/tree.h>
46#include <libxml/xmlschemas.h>
47
48/*
49 *To compile this file using gcc you can type
50 *gcc `xml2-config --cflags --libs` -o xmlexample libxml2-example.c
51 */
52static void schemaValidityErrorFunc(void *ctx, const char *msg, ...)
53{
54 char acBuffer[256];
55 va_list listPointer;
56
57 (void)ctx;
58
59 va_start( listPointer, msg );
60 //vprintf(msg, listPointer);
61
62 vsnprintf(acBuffer, sizeof(acBuffer), msg, listPointer);
63
64 va_end( listPointer );
65
66 puts(acBuffer);
67}
68
69static void schemaValidityWarningFunc(void *ctx, const char *msg, ...)
70{
71 char acBuffer[256];
72 va_list listPointer;
73
74 (void)ctx;
75
76 va_start( listPointer, msg );
77
78 vsnprintf(acBuffer, sizeof(acBuffer), msg, listPointer);
79
80 va_end( listPointer );
81
82 puts(acBuffer);
83}
84
85static int is_valid(const xmlDocPtr doc, const char *schema_filename)
86{
87#ifdef LIBXML_SCHEMAS_ENABLED
88 xmlDocPtr schema_doc = xmlReadFile(schema_filename, NULL, XML_PARSE_NONET);
89 if (schema_doc == NULL) {
90 /* the schema cannot be loaded or is not well-formed */
91 return -1;
92 }
93 xmlSchemaParserCtxtPtr parser_ctxt = xmlSchemaNewDocParserCtxt(schema_doc);
94 if (parser_ctxt == NULL) {
95 /* unable to create a parser context for the schema */
96 xmlFreeDoc(schema_doc);
97 return -2;
98 }
99
100 xmlSchemaPtr schema = xmlSchemaParse(parser_ctxt);
101 if (schema == NULL) {
102 /* the schema itself is not valid */
103 xmlSchemaFreeParserCtxt(parser_ctxt);
104 xmlFreeDoc(schema_doc);
105 return -3;
106 }
107 xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema);
108 if (valid_ctxt == NULL) {
109 /* unable to create a validation context for the schema */
110 xmlSchemaFree(schema);
111 xmlSchemaFreeParserCtxt(parser_ctxt);
112 xmlFreeDoc(schema_doc);
113 return -4;
114 }
115 xmlSchemaSetValidErrors(valid_ctxt, schemaValidityErrorFunc, schemaValidityWarningFunc, NULL);
116
117 int is_valid = (xmlSchemaValidateDoc(valid_ctxt, doc) == 0);
118 xmlSchemaFreeValidCtxt(valid_ctxt);
119 xmlSchemaFree(schema);
120 xmlSchemaFreeParserCtxt(parser_ctxt);
121 xmlFreeDoc(schema_doc);
122
123 /* force the return value to be non-negative on success */
124 return is_valid ? 1 : 0;
125#else
126 return 1;
127#endif
128}
129
130/**
131 * print_element_names:
132 * @a_node: the initial xml node to consider.
133 *
134 * Prints the names of the all the xml elements
135 * that are siblings or children of a given xml node.
136 */
137static void
138print_element_names(xmlNode * a_node, int iLevel = 0)
139{
140 xmlNode *cur_node = NULL;
141
142 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
143
144 int iIndent = iLevel;
145
146 while (iIndent--) {
147 printf(" ");
148 }
149
150 if (cur_node->type == XML_ELEMENT_NODE) {
151
152 printf("node type: Element, name: %s\n", cur_node->name);
153 }
154 print_element_names(cur_node->children, iLevel + 1);
155 }
156}
157
158
159/**
160 * Simple example to parse a file called "file.xml",
161 * walk down the DOM, and print the name of the
162 * xml elements nodes.
163 */
164int
165main(int argc, char **argv)
166{
167 xmlDoc *doc = NULL;
168 xmlNode *root_element = NULL;
169
170 if (argc != 3)
171 return(1);
172
173 /*
174 * this initialize the library and check potential ABI mismatches
175 * between the version it was compiled for and the actual shared
176 * library used.
177 */
178 LIBXML_TEST_VERSION
179
180 /*parse the file and get the DOM */
181 doc = xmlReadFile(argv[1], NULL, 0);
182
183 if (doc == NULL) {
184 printf("error: could not parse file %s\n", argv[1]);
185 }
186 // Validate
187 if (!is_valid(doc, argv[2])) {
188 printf("error: doc is not valid\n");
189
190 return -1;
191 }
192
193
194 /*Get the root element node */
195 root_element = xmlDocGetRootElement(doc);
196
197
198 print_element_names(root_element);
199
200 /*free the document */
201 xmlFreeDoc(doc);
202
203 /*
204 *Free the global variables that may
205 *have been allocated by the parser.
206 */
207 xmlCleanupParser();
208
209 return 0;
210}