summaryrefslogtreecommitdiff
path: root/examples/dom-structure-dump.c
blob: f1ca731daefa22c587e7b2ae3cbfa9b4a70ce17e (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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * This file is part of LibDOM.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2010 Michael Drake <tlsa@netsurf-browser.org>
 */

/*
 * Load an HTML file into LibDOM with Hubbub and print out the DOM structure.
 *
 * This example demonstrates the following:
 *
 * 1. Using LibDOM's Hubbub binding to read an HTML file into LibDOM.
 * 2. Walking around the DOM tree.
 * 3. Accessing DOM node attributes.
 * 4. Using LibWapcaplet to intern and compare strings with strings in the DOM.
 *
 * Example input:
 *      <html><body><h1 class="woo">NetSurf</h1>
 *      <p>NetSurf is <em>awesome</em>!</p>
 *      <div><h2>Hubbub</h2><p>Hubbub is too.</p>
 *      <p>Big time.</p></div></body></html>
 *
 * Example output:
 *
 * HTML
 * +-BODY
 * | +-H1 class="woo"
 * | +-P
 * | | +-EM
 * | +-DIV
 * | | +-H2
 * | | +-P
 * | | +-P
 *
 */

#define _GNU_SOURCE /* for strndup */
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

#include <dom/dom.h>
#include <dom/bindings/hubbub/parser.h>

#define UNUSED(x) ((x)=(x))

dom_document *create_doc_dom_from_file(char *file);

void *test_realloc(void *ptr, size_t len, void *pw)
{
	UNUSED(pw);

	return realloc(ptr, len);
}

void test_msg(uint32_t severity, void *ctx, const char *msg, ...)
{
	va_list l;

	UNUSED(ctx);

	va_start(l, msg);

	fprintf(stderr, "%d: ", severity);
	vfprintf(stderr, msg, l);
	fprintf(stderr, "\n");
}

int main(int argc, char **argv)
{
	dom_exception exc; /* returned by libdom functions */
	lwc_error err; /* returned by libwapacplet functions */
	dom_document *doc = NULL; /* document, loaded into libdom */
	dom_element *root = NULL; /* root element of document */

	/* Initialise the DOM library */
	exc = dom_initialise(test_realloc, NULL);
	if (exc != DOM_NO_ERR) {
		printf("Failed to initialise DOM library.\n");
		return EXIT_FAILURE;
	}

	/* Initialise the string library */
	err = lwc_initialise (test_realloc, NULL, 0);
	if (err != lwc_error_ok) {
		printf("Failed to initialise string library.\n");
		return EXIT_FAILURE;
	}

	/* Load up the input HTML file */
	doc = create_doc_dom_from_file("files/test.html");
	if (doc == NULL) {
		printf("Failed to load document.\n");
		return EXIT_FAILURE;
	}

	/* Get root element */
	exc = dom_document_get_document_element(doc, &root);
	if (exc != DOM_NO_ERR) {
		printf("Exception raised for get_document_element\n");
 		return EXIT_FAILURE;
	} else if (root == NULL) {
		printf("Broken: root == NULL\n");
 		return EXIT_FAILURE;
	}

	/* Dump DOM structure */
	dump_dom_structure(root, 0);

	/* Finalise the DOM library */
	exc = dom_finalise();
	if (exc != DOM_NO_ERR) {
		printf("Failed to finalise DOM library.\n");
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}


/**
 * Walk though a DOM (sub)tree, in depth first order, printing DOM structure.
 *
 * \param node   The root node to start from
 * \param depth  The depth of 'node' in the (sub)tree
 */
dump_dom_structure(dom_node_internal *node, int depth)
{
	dom_exception exc;
	dom_node_internal *child;

	/* Print this node's entry */
	dump_dom_element(node, depth);

	/* Get the node's first child */
	exc = dom_node_get_first_child(node, &child);
	if (exc != DOM_NO_ERR) {
		printf("Exception raised for node_get_first_child\n");
		return EXIT_FAILURE;
	} else if (child != NULL) {
		/* node has children;  decend to children's depth */
		depth++;

		/* Loop though all node's children */
		do {
			/* Visit node's descendents */
			dump_dom_structure(child, depth);

			/* Go to next sibling */
			exc = dom_node_get_next_sibling(child, &child);
			if (exc != DOM_NO_ERR) {
				printf("Exception raised for "
						"node_get_next_sibling\n");
				return EXIT_FAILURE;
			}
		} while (child != NULL); /* No more children */
	}
}


/**
 * Print a line in a DOM structure dump for an element
 *
 * \param node   The node to dump
 * \param depth  The node's depth
 */
dump_dom_element(dom_node_internal *node, int depth)
{
	dom_exception exc;
	lwc_error err;
	dom_string *node_name = NULL;
	lwc_string *lwcstr = NULL;
	const char *string;
	dom_node_type type;
	int i;
	size_t length;

	/* Only interested in element nodes */
	exc = dom_node_get_node_type(node, &type);
	if (exc != DOM_NO_ERR) {
		printf("Exception raised for node_get_node_type\n");
		return EXIT_FAILURE;
	} else if (type != DOM_ELEMENT_NODE) {
 		return;
	}

	/* Get root element name */
	exc = dom_node_get_node_name(node, &node_name);
	if (exc != DOM_NO_ERR) {
		printf("Exception raised for get_node_name\n");
		return EXIT_FAILURE;
	} else if (node_name == NULL) {
		printf("Broken: root_name == NULL\n");
 		return EXIT_FAILURE;
	}

	/* Get element name's lwc_string */
	exc = dom_string_get_intern(node_name, &lwcstr);
	if (exc != DOM_NO_ERR) {
		printf("Exception raised for string_get_intern\n");
		return EXIT_FAILURE;
	}

	/* Print ASCII tree structure for current node */
	if (depth > 0) {
		for (i = 0; i < depth; i++) {
			printf("| ");
		}
		printf("+-");
	}

	/* Get string data and print element name */
	string = lwc_string_data(lwcstr);
	length = lwc_string_length(lwcstr);
	printf("%*s\n", length, string);
}


/**
 * Generate a LibDOM document DOM from an HTML file
 *
 * \param file  The file path
 */
dom_document *create_doc_dom_from_file(char *file)
{
	const unsigned int buffer_size = 1024;
	dom_hubbub_parser *parser = NULL;
	int handle;
	int chunk_length;
	dom_hubbub_error error;
	dom_document *doc;
	char buffer[buffer_size];

	/* Create Hubbub parser */
	parser = dom_hubbub_parser_create("../test/data/Aliases", NULL, true,
			test_realloc, NULL, test_msg, NULL);
	if (parser == NULL) {
		printf("Can't create Hubbub Parser\n");
		return NULL;
	}

	/* Open input file */
	handle = open(file, O_RDONLY);
	if (handle == -1) {
		dom_hubbub_parser_destroy(parser);
		printf("Can't open test input file: %s\n", file);
		return NULL;
	}

	/* Parse input file in chunks */
	chunk_length = buffer_size;
	while(chunk_length == buffer_size) {
		chunk_length = read(handle, buffer, buffer_size);
		error = dom_hubbub_parser_parse_chunk(parser, buffer,
				chunk_length);
		if (error != DOM_HUBBUB_OK) {
			dom_hubbub_parser_destroy(parser);
			printf("Parsing errors occur\n");
			return NULL;
		}
	}

	/* Done parsing file */
	error = dom_hubbub_parser_completed(parser);
	if (error != DOM_HUBBUB_OK) {
		dom_hubbub_parser_destroy(parser);
		printf("Parsing error when construct DOM\n");
		return NULL;
	}

	/* Get the document */
	doc = dom_hubbub_parser_get_document(parser);

	/* Finished with parser */
	dom_hubbub_parser_destroy(parser);

	/* Close input file */
	if (close(handle) == -1) {
		printf("Can't close test input file: %s\n", file);
		return NULL;
	}

	return doc;
}