d706fb8904
with test.xml as input it produces the same output as clay-demo (except for different order of properties in the debug print)
71 lines
2 KiB
C
71 lines
2 KiB
C
#include <libxml/tree.h>
|
|
#include <assert.h>
|
|
#include <stdbool.h>
|
|
#include "clay.h"
|
|
|
|
|
|
clay make_clay_from_xml(clay_ctx, xmlNodePtr);
|
|
|
|
int main(int argc, char **argv) {
|
|
assert(argc == 2);
|
|
const char * input_file_name = argv[1];
|
|
|
|
xmlDocPtr doc;
|
|
xmlNodePtr cur;
|
|
doc = xmlParseFile(input_file_name);
|
|
assert(doc != NULL);
|
|
|
|
xmlNodePtr rootNode = xmlDocGetRootElement(doc);
|
|
assert(rootNode != NULL);
|
|
assert(xmlStrcmp(rootNode->name, (const xmlChar *) "clay") == 0);
|
|
|
|
cur = rootNode->children;
|
|
xmlNodePtr documentNode = NULL;
|
|
while(cur != NULL) {
|
|
if (cur->type == XML_ELEMENT_NODE) {
|
|
assert(documentNode == NULL);
|
|
assert(xmlStrcmp(cur->name, (const xmlChar *) "document") == 0);
|
|
documentNode = cur;
|
|
}
|
|
cur = cur->next;
|
|
}
|
|
assert(documentNode != NULL);
|
|
|
|
clay_ctx ctx = clay_create_context();
|
|
|
|
clay document_element = make_clay_from_xml(ctx, documentNode);
|
|
|
|
xmlFreeDoc(doc);
|
|
|
|
clay_print_layout_tree(document_element);
|
|
|
|
clay_destroy_context(ctx);
|
|
}
|
|
|
|
|
|
clay make_clay_from_xml(clay_ctx ctx, xmlNodePtr xml_node) {
|
|
clay layout = clay_create(ctx, (const char *)xml_node->name);
|
|
xmlAttrPtr attribute = xml_node->properties;
|
|
while (attribute != NULL) {
|
|
const char * key = (const char *) attribute->name;
|
|
char * value = (char *) xmlNodeListGetString(xml_node->doc, attribute->children, 1);
|
|
clay_set(layout, key, value);
|
|
attribute = attribute->next;
|
|
}
|
|
|
|
xmlNodePtr child = xml_node->children;
|
|
bool has_element_children = false;
|
|
while(child != NULL) {
|
|
if (child->type == XML_ELEMENT_NODE) {
|
|
clay child_layout = make_clay_from_xml(ctx, child);
|
|
clay_append_child(layout, child_layout);
|
|
has_element_children = true;
|
|
}
|
|
child = child->next;
|
|
}
|
|
if (!has_element_children) {
|
|
clay_set(layout, "text", (char*) xml_node->children->content);
|
|
}
|
|
|
|
return layout;
|
|
} |