File: xbl_process.c

package info (click to toggle)
gpac 0.7.1+dfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 33,112 kB
  • sloc: ansic: 454,042; xml: 47,905; cpp: 13,115; sh: 5,548; makefile: 3,401; objc: 321
file content (242 lines) | stat: -rw-r--r-- 7,051 bytes parent folder | download
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 *			GPAC - Multimedia Framework C SDK
 *
 *			Authors: Cyril Concolato
 *			Copyright (c) Telecom ParisTech 2008-2012
 *					All rights reserved
 *
 *  This file is part of GPAC / Scene Management sub-project
 *
 *  GPAC is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  GPAC is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
#include <gpac/scene_manager.h>
#include <gpac/constants.h>
#include <gpac/utf.h>
#include <gpac/xml.h>
#include <gpac/scenegraph_svg.h>

#ifndef GPAC_DISABLE_SVG

#include <gpac/nodes_xbl.h>

typedef struct
{
	GF_SceneLoader *load;
	GF_Err last_error;
	GF_SAXParser *sax_parser;
	XBL_Element *root;

	/* stack of XBL nodes*/
	GF_List *node_stack;
} GF_XBL_Parser;


typedef struct
{
	/*top of parsed sub-tree*/
	XBL_Element *node;
	/*depth of unknown elements being skipped*/
	u32 unknown_depth;
	/*last child added, used to speed-up parsing*/
	GF_ChildNodeItem *last_child;
} XBL_NodeStack;

static GF_Err xbl_parse_report(GF_XBL_Parser *parser, GF_Err e, char *format, ...)
{
#ifndef GPAC_DISABLE_LOG
	if (gf_log_tool_level_on(GF_LOG_PARSER, e ? GF_LOG_ERROR : GF_LOG_WARNING)) {
		char szMsg[2048];
		va_list args;
		va_start(args, format);
		vsnprintf(szMsg, 2048, format, args);
		va_end(args);
		GF_LOG((u32) (e ? GF_LOG_ERROR : GF_LOG_WARNING), GF_LOG_PARSER, ("[XBL Parsing] line %d - %s\n", gf_xml_sax_get_line(parser->sax_parser), szMsg));
	}
#endif
	if (e) parser->last_error = e;
	return e;
}

static void xbl_parse_progress(void *cbk, u64 done, u64 total)
{
	gf_set_progress("XBL Parsing", done, total);
}

XBL_Element *gf_xbl_create_node(u32 ElementTag)
{
	XBL_Element *p;
	GF_SAFEALLOC(p, SVG_Element);
	gf_node_setup((GF_Node *)p, ElementTag);
	gf_sg_parent_setup((GF_Node *) p);
	return p;
}

static XBL_Element *xbl_parse_element(GF_XBL_Parser *parser, const char *name, const char *name_space, const GF_XMLAttribute *attributes, u32 nb_attributes, XBL_NodeStack *parent)
{
	u32	tag, attribute_tag, i;
	XBL_Element *elt = NULL;

//	if (name_space && strlen(name_space)) return NULL;

	tag = gf_sg_node_get_tag_by_class_name(name, 0);
	if (tag != TAG_UndefinedNode) {
		elt = (XBL_Element *)gf_node_new(parser->load->scene_graph, tag);
	} else {
		elt = (XBL_Element *)gf_node_new(parser->load->scene_graph, TAG_DOMFullNode);
	}
	gf_node_register((GF_Node *)elt, (parent ? (GF_Node *)parent->node : NULL));
	if (parent && elt) gf_node_list_add_child_last( & parent->node->children, (GF_Node*)elt, & parent->last_child);

	for (i=0; i<nb_attributes; i++) {
		GF_XMLAttribute *att = (GF_XMLAttribute *)&attributes[i];
		if (!att->value || !strlen(att->value)) continue;
		attribute_tag = gf_xml_get_attribute_tag((GF_Node *)elt, att->name, 0);
		if (attribute_tag!=TAG_DOM_ATT_any) {
			/*FIXME do we need to check if the attribute is specified several times*/
			GF_DOMAttribute *dom_att = gf_xml_create_attribute((GF_Node*)elt, attribute_tag);
			dom_att->data = gf_strdup(att->value);
		} else {
			xbl_parse_report(parser, GF_OK, "Skipping attribute %s on node %s", att->name, name);
		}
	}

	return elt;
}

static void xbl_node_start(void *sax_cbck, const char *name, const char *name_space, const GF_XMLAttribute *attributes, u32 nb_attributes)
{
	GF_XBL_Parser *parser = (GF_XBL_Parser *)sax_cbck;
	XBL_NodeStack *stack, *parent;
	XBL_Element *elt;

	parent = (XBL_NodeStack *)gf_list_last(parser->node_stack);

	elt = xbl_parse_element(parser, name, name_space, attributes, nb_attributes, parent);
	if (!elt) {
		if (parent) parent->unknown_depth++;
		xbl_parse_report(parser, GF_OK, "Ignoring unknown element %s", name);
		return;
	}
	if (!parser->root) parser->root = elt;

	GF_SAFEALLOC(stack, XBL_NodeStack);
	if (!stack) {
		return;
	}
	stack->node = elt;
	gf_list_add(parser->node_stack, stack);

}

static void xbl_text_content(void *sax_cbck, const char *text_content, Bool is_cdata)
{
	/* FIXME : this function does nothing
	GF_XBL_Parser *parser = (GF_XBL_Parser *)sax_cbck;
	XBL_NodeStack *top = (XBL_NodeStack *)gf_list_last(parser->node_stack);

	*/
}

static void xbl_node_end(void *sax_cbck, const char *name, const char *name_space)
{
	GF_XBL_Parser *parser = (GF_XBL_Parser *)sax_cbck;
	XBL_NodeStack *top = (XBL_NodeStack *)gf_list_last(parser->node_stack);

	if (!top) return;
	if (/*!name_space && */gf_sg_node_get_tag_by_class_name(name, 0) != TAG_UndefinedNode) {
		const char *the_name;
		XBL_Element *node = top->node;
		/*check node name...*/
		the_name = gf_node_get_class_name((GF_Node *) node);
		if (strcmp(the_name, name)) {
			if (top->unknown_depth) {
				top->unknown_depth--;
				return;
			} else {
				xbl_parse_report(parser, GF_BAD_PARAM, "depth mismatch");
				return;
			}
		}
		gf_free(top);
		gf_list_rem_last(parser->node_stack);
	} else if (top) {
		if (top->unknown_depth) {
			top->unknown_depth--;
		} else {
			xbl_parse_report(parser, GF_BAD_PARAM, "depth mismatch");
		}
	}

}

static GF_XBL_Parser *xbl_new_parser(GF_SceneLoader *load)
{
	GF_XBL_Parser *parser;
	if (load->type==GF_SM_LOAD_XBL) {
		if (!load->ctx) return NULL;
	} else if (load->type!=GF_SM_LOAD_XBL) return NULL;

	GF_SAFEALLOC(parser, GF_XBL_Parser);
	if (!parser) return NULL;
	parser->node_stack = gf_list_new();
	parser->sax_parser = gf_xml_sax_new(xbl_node_start, xbl_node_end, xbl_text_content, parser);
	parser->load = load;
	load->loader_priv = parser;

	return parser;
}


GF_Err gf_sm_load_init_xbl(GF_SceneLoader *load)
{
	GF_Err e;
	GF_XBL_Parser *parser;

	if (!load->fileName) return GF_BAD_PARAM;
	parser = xbl_new_parser(load);
	GF_LOG(GF_LOG_INFO, GF_LOG_PARSER, ( "[Parser] XBL Parsing\n") );
	e = gf_xml_sax_parse_file(parser->sax_parser, (const char *)load->fileName, xbl_parse_progress);
	if (e<0) return xbl_parse_report(parser, e, "Unable to parse file %s: %s", load->fileName, gf_xml_sax_get_error(parser->sax_parser) );
	return parser->last_error;
}

GF_Err gf_sm_load_run_xbl(GF_SceneLoader *load)
{
	return GF_OK;
}

void gf_sm_load_done_xbl(GF_SceneLoader *load)
{
	GF_XBL_Parser *parser = (GF_XBL_Parser *)load->loader_priv;
	if (!parser) return;
	while (gf_list_count(parser->node_stack)) {
		XBL_NodeStack *st = (XBL_NodeStack *)gf_list_last(parser->node_stack);
		gf_list_rem_last(parser->node_stack);
		gf_free(st);
	}
	gf_list_del(parser->node_stack);
	if (parser->sax_parser) gf_xml_sax_del(parser->sax_parser);
	gf_free(parser);
	load->loader_priv = NULL;
	return;
}

void apply(GF_Node *bound_doc, GF_Node *binding_doc)
{

}

#endif