"Initial commit to Gerrit"
[profile/ivi/libcroco.git] / tests / test5-main.c
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  * This file is part of The Croco Library
5  *
6  * Copyright (C) 2002-2003 Dodji Seketeli <dodji@seketeli.org>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of version 2.1 of the GNU Lesser General Public
10  * License as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */
22
23 #include <string.h>
24 #include "cr-test-utils.h"
25 #include "libcroco.h"
26
27 /**
28  *@file
29  *Some test facilities for the #CRParser class.
30  */
31
32 CRDocHandler *gv_test_handler = { 0 };
33
34 const guchar *xml_content =
35         "<document>"
36         "<E0>text0</E0> "
37         "<E1><E1-1>text1</E1-1></E1>"
38         "<E2 attr2=\"val2\">text2</E2>"
39         "<E3 attr3=\"val3_1 val3_2 val3_3\">text3</E3>"
40         "<E4 attr4=\"val4_1-val4_2-val4_3\">text4</E4>"
41         "<E5 class=\"class5\">text5</E5>"
42         "<E6 id=\"id6\">text6</E6>"
43         "<E7 lang=\"fr\">text7</E7>" "</document>";
44
45 static void
46   display_help (char *prg_name);
47
48 static void
49   display_about (char *prg_name);
50 static enum CRStatus
51   test_sel_eng (guchar * a_file_uri);
52
53 static void
54   walk_xml_tree_and_lookup_rules (CRSelEng * a_sel_eng,
55                                   CRStyleSheet * a_sheet, xmlNode * a_node);
56
57 /**
58  *Displays the usage of the test
59  *facility.
60  *@param a_argc the argc variable passed to the main function.
61  *@param a_argv the argv variable passed to the main function.
62  */
63 static void
64 display_help (char *prg_name)
65 {
66         g_print ("\n\n");
67         g_print ("usage: %s <file-to-parse>\n", prg_name);
68         g_print ("\t <file-to-parse>: the file to parse\n");
69         g_print ("\n\n");
70         g_print ("Test the selection engine");
71         g_print ("Returns OK if the status is CR_OK, KO otherwise\n");
72         g_print ("\n\n");
73 }
74
75 /**
76  *Displays the about text.
77  *@param a_argc the argc variable passed to the main function.
78  *@param a_argv the argv variable passed to the main function.
79  */
80 static void
81 display_about (char *prg_name)
82 {
83         g_print ("\n\n");
84         g_print ("%s is a libcroco CROMParser class test program.\n",
85                  prg_name);
86         g_print ("%s Parses a file and builds a CSS object model", prg_name);
87         g_print ("It should run on GNU compliants systems.\n");
88         g_print ("\n\n");
89         g_print ("Initial author: Dodji Seketeli <dodji@seketeli.org>.\n");
90         g_print ("\n\n");
91 }
92
93 static void
94 walk_xml_tree_and_lookup_rules (CRSelEng * a_sel_eng,
95                                 CRStyleSheet * a_sheet, xmlNode * a_node)
96 {
97         CRStatement **stmts_tab = NULL;
98         gulong tab_len = 0,
99                 i = 0;
100         enum CRStatus status = CR_OK;
101
102         xmlNode *cur_node = a_node;
103
104         for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
105                 status = cr_sel_eng_get_matched_rulesets
106                         (a_sel_eng, a_sheet, cur_node, &stmts_tab, &tab_len);
107
108                 if (status == CR_OK && tab_len) {
109                         g_print ("'''''''''''''''''''''''''\n");
110                         g_print ("xml start element: %s\n", cur_node->name);
111
112                         for (i = 0; i < tab_len; i++) {
113                                 if (stmts_tab[i]) {
114                                         g_print ("\n");
115                                         cr_statement_dump (stmts_tab[i],
116                                                            stdout, 2);
117                                 }
118                         }
119                         g_print ("\n\nxml end element: %s\n", cur_node->name);
120                         g_print ("'''''''''''''''''''''''''\n");
121                 }
122
123                 if (stmts_tab) {
124                         g_free (stmts_tab);
125                         stmts_tab = NULL;
126                 }
127                 if (cur_node->children) {
128                         walk_xml_tree_and_lookup_rules
129                                 (a_sel_eng, a_sheet, cur_node->children);
130                 }
131         }
132 }
133
134 /**
135  *The test of the cr_input_read_byte() method.
136  *Reads the each byte of a_file_uri using the
137  *cr_input_read_byte() method. Each byte is send to
138  *stdout.
139  *@param a_file_uri the file to read.
140  *@return CR_OK upon successfull completion of the
141  *function, an error code otherwise.
142  */
143 static enum CRStatus
144 test_sel_eng (guchar * a_file_uri)
145 {
146         enum CRStatus status = CR_OK;
147         CROMParser *parser = NULL;
148         CRStyleSheet *stylesheet = NULL;
149         xmlDoc *xml_doc = NULL;
150         xmlNode *cur_node = NULL;
151         CRSelEng *selection_engine = NULL;
152
153         g_return_val_if_fail (a_file_uri, CR_BAD_PARAM_ERROR);
154
155         parser = cr_om_parser_new (NULL);
156         status = cr_om_parser_parse_file (parser, a_file_uri, CR_ASCII,
157                                           &stylesheet);
158         if (status != CR_OK || !stylesheet) {
159                 cr_utils_trace_info ("Could not parse xml content");
160                 goto error;
161         }
162
163         xml_doc = xmlParseMemory (xml_content, strlen (xml_content));
164         if (!xml_doc) {
165                 cr_utils_trace_info ("Could not parse xml content");
166                 goto error;
167
168         }
169
170         selection_engine = cr_sel_eng_new ();
171
172         cur_node = xml_doc->children;
173
174         walk_xml_tree_and_lookup_rules (selection_engine,
175                                         stylesheet, cur_node);
176
177         if (parser) {
178                 cr_om_parser_destroy (parser);
179                 parser = NULL;
180         }
181
182         if (xml_doc) {
183                 xmlFreeDoc (xml_doc);
184                 xml_doc = NULL;
185         }
186
187         if (stylesheet) {
188                 cr_stylesheet_destroy (stylesheet);
189                 stylesheet = NULL;
190         }
191         if (selection_engine) {
192                 cr_sel_eng_destroy (selection_engine) ;
193                 selection_engine = NULL ;
194         }
195         xmlCleanupParser ();
196         return status;
197
198       error:
199
200         if (parser) {
201                 cr_om_parser_destroy (parser);
202                 parser = NULL;
203         }
204
205         if (xml_doc) {
206                 xmlFreeDoc (xml_doc);
207                 xml_doc = NULL;
208         }
209
210         if (stylesheet) {
211                 cr_stylesheet_destroy (stylesheet);
212                 stylesheet = NULL;
213         }
214
215         xmlCleanupParser ();
216         return CR_ERROR;
217 }
218
219 /**
220  *The entry point of the testing routine.
221  */
222 int
223 main (int argc, char **argv)
224 {
225         struct Options options;
226         enum CRStatus status = CR_OK;
227
228         cr_test_utils_parse_cmd_line (argc, argv, &options);
229
230         if (options.display_help == TRUE) {
231                 display_help (argv[0]);
232                 return 0;
233         }
234
235         if (options.display_about == TRUE) {
236                 display_about (argv[0]);
237                 return 0;
238         }
239
240         if (options.files_list == NULL) {
241                 display_help (argv[0]);
242                 return 0;
243         }
244
245         status = test_sel_eng (options.files_list[0]);
246
247         if (status != CR_OK) {
248                 g_print ("\nKO\n");
249         }
250
251         return 0;
252 }