2009-27-09 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / tests / apps / atk-object-xml-loader.c
1 /*
2  * Copyright 2008 Codethink Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <glib.h>
23 #include <libxml/parser.h>
24 #include <libxml/tree.h>
25
26 #include <my-atk.h>
27
28 #define ACCESSIBLE_NODE ((const xmlChar *) "accessible")
29 #define INTERFACE_NODE  ((const xmlChar *) "interface")
30
31 #define NAME_ATTR ((const xmlChar *) "name")
32 #define DESC_ATTR ((const xmlChar *) "description")
33 #define ROLE_ATTR ((const xmlChar *) "role")
34
35 static MyAtkObject *
36 create_atk_object_from_element(xmlNode *element)
37 {
38   xmlNode *child_node;
39
40   MyAtkObject *obj = NULL;
41   MyAtkObject *child_obj;
42
43   xmlChar *name;
44   xmlChar *description;
45   xmlChar *role_text; 
46   gint role;
47
48   name = xmlGetProp(element, NAME_ATTR);
49   description = xmlGetProp(element, DESC_ATTR);
50   role_text = xmlGetProp(element, ROLE_ATTR);
51   role = atoi(role_text);
52
53   obj = MY_ATK_OBJECT(g_object_new(MY_TYPE_ATK_OBJECT,
54                                    "accessible-name", name,
55                                    "accessible-description", description,
56                                    "accessible-role", role,
57                                    NULL));
58
59   child_node = element->xmlChildrenNode;
60   while (child_node != NULL)
61     {
62       if (!xmlStrcmp(child_node->name, ACCESSIBLE_NODE))  
63         {
64           child_obj = create_atk_object_from_element(child_node); 
65           my_atk_object_add_child(obj, child_obj);
66         }
67       child_node = child_node->next;
68     }
69   return obj;
70 }
71
72 /*
73  * Reads the XML from filename and uses it
74  * to create a tree of MyAtkObjects.
75  * 
76  * returns: The root object of the tree.
77  */
78 MyAtkObject *
79 atk_object_xml_parse(gchar *filename)
80 {
81   xmlDoc *doc;
82   xmlNode *root_element;
83   MyAtkObject *new_atk_object = NULL;
84
85   doc = xmlReadFile(filename, NULL, 0);
86   g_assert(doc != NULL);
87
88   root_element = xmlDocGetRootElement(doc);
89
90   if (!xmlStrcmp(root_element->name, ACCESSIBLE_NODE))  
91     new_atk_object = create_atk_object_from_element(root_element); 
92
93   xmlFreeDoc(doc);
94   return new_atk_object;
95 }