|
WFTK_EXPORT XML * repos_get_layout (XML * repository, const char * layout_id)
{
XML * ltop;
XML * layout;
XML * this_layout;
XML * replace_layout;
XML * layout_mark;
const char * replaces;
FILE * file;
ltop = xml_loc (repository, ".layout");
if (!ltop) {
return NULL;
}
if (!layout_id) layout_id = xml_attrval (ltop, "default");
if (!*layout_id) layout_id = xml_attrval (ltop, "default");
if (!*layout_id) layout_id = xml_attrval (ltop, "id");
if (!*layout_id) {
layout_id = "top";
xml_set (ltop, "id", "top");
}
this_layout = xml_locf (repository, ".cache.layout[%s]", layout_id);
if (this_layout) {
return (xml_copy (this_layout));
}
this_layout = xml_search (ltop, "layout", "id", layout_id);
if (!this_layout) {
return NULL;
}
layout = NULL;
replace_layout = NULL;
do {
if (*xml_attrval (this_layout, "defn")) {
file = _repos_fopen (repository, xml_attrval (this_layout, "defn"), "r");
if (!file) {
layout = xml_create ("layout-error");
xml_setf (layout, "error", "Named layout definition file %s can't be opened.", xml_attrval (this_layout, "defn"));
break;
}
layout = xml_parse_general (file, (XMLAPI_DATARETRIEVE) fread);
fclose (file);
if (xml_is (layout, "xml-error")) {
xml_setf (layout, "error", "Named layout definition file %s is corrupt in line %s: [%s] %s\n", xml_attrval (this_layout, "defn"), xml_attrval (layout, "line"), xml_attrval (layout, "code"), xml_attrval (layout, "message"));
break;
}
} else {
layout = xml_copy (this_layout);
}
if (replace_layout) {
layout_mark = xml_search (layout, "template:value", "id", replaces);
if (!layout_mark) layout_mark = xml_search (layout, "template:value", "name", replaces);
if (layout_mark) {
xml_replacewithcontent (layout_mark, replace_layout);
} else xml_free (replace_layout);
replace_layout = NULL;
}
if (*xml_attrval (this_layout, "replace") && this_layout != ltop) {
replace_layout = layout;
replaces = xml_attrval (this_layout, "replace");
layout = NULL;
this_layout = xml_parent (this_layout);
}
} while (replace_layout);
layout_mark = xml_loc (repository, ".cache");
if (!layout_mark) {
layout_mark = xml_create ("cache");
xml_append (repository, layout_mark);
}
xml_set (layout, "id", layout_id);
xml_append (layout_mark, layout);
return (xml_copy (layout));
}
|