summaryrefslogtreecommitdiff
path: root/xps/xpsresource.c
blob: d4deb6b2e67e7b12bfb8a934217052dad789a8dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "ghostxps.h"

int
xps_resolve_resource_reference(xps_context_t *ctx, xps_resource_t *dict,
	char **attp, xps_item_t **tagp)
{
    if (*attp)
    {
	xps_item_t *rsrc = xps_parse_resource_reference(ctx, dict, *attp);
	if (rsrc)
	{
	    *attp = NULL;
	    *tagp = rsrc;
	}
    }
    return 0;
}

xps_item_t *
xps_find_resource(xps_context_t *ctx, xps_resource_t *dict, char *name)
{
    xps_resource_t *head, *node;
    for (head = dict; head; head = head->parent)
	for (node = head; node; node = node->next)
	    if (!strcmp(node->name, name))
		return node->data;
    return NULL;
}

xps_item_t *
xps_parse_resource_reference(xps_context_t *ctx, xps_resource_t *dict, char *att)
{
    char name[1024];
    char *s;

    if (strstr(att, "{StaticResource ") != att)
	return NULL;

    strcpy(name, att + 16);
    s = strrchr(name, '}');
    if (s)
	*s = 0;

    return xps_find_resource(ctx, dict, name);
}

xps_resource_t *
xps_parse_remote_resource_dictionary(xps_context_t *ctx, char *source_att)
{
    char part_name[1024];
    xps_part_t *part;
    xps_resource_t *dict;
    xps_item_t *root;

    xps_absolute_path(part_name, ctx->pwd, source_att);
    part = xps_find_part(ctx, part_name);
    if (!part)
    {
	gs_throw1(-1, "cannot find remote resource part '%s'", part_name);
	return NULL;
    }

    root = xps_parse_xml(ctx, part->data, part->size);
    if (!root)
    {
	gs_rethrow(-1, "cannot parse xml");
	return NULL;
    }

    if (strcmp(xps_tag(root), "ResourceDictionary"))
    {
	gs_throw1(-1, "expected ResourceDictionary element (found %s)", xps_tag(root));
	return NULL;
    }

    dict = xps_parse_resource_dictionary(ctx, root);

    // TODO: xps_free_item(dict); -- need to add reference counting so dict can keep nodes

    return dict;
}

xps_resource_t *
xps_parse_resource_dictionary(xps_context_t *ctx, xps_item_t *root)
{
    xps_resource_t *head;
    xps_resource_t *entry;
    xps_item_t *node;
    char *source;
    char *key;

    source = xps_att(root, "Source");
    if (source)
	return xps_parse_remote_resource_dictionary(ctx, source);

    head = NULL;

    for (node = xps_down(root); node; node = xps_next(node))
    {
	key = xps_att(node, "x:Key");
	if (key)
	{
	    entry = xps_alloc(ctx, sizeof(xps_resource_t));
	    if (!entry)
	    {
		gs_throw(-1, "cannot allocate resource entry");
		return head;
	    }
	    entry->name = key;
	    entry->data = node;
	    entry->next = head;
	    entry->parent = NULL;
	    head = entry;
	}
    }

    return head;
}

void xps_free_resource_dictionary(xps_context_t *ctx, xps_resource_t *dict)
{
    xps_resource_t *next;
    while (dict)
    {
	next = dict->next;
	xps_free(ctx, dict);
	dict = next;
    }
}