summaryrefslogtreecommitdiff
path: root/examples/dom-structure-dump.c
diff options
context:
space:
mode:
authorMichael Drake <tlsa@netsurf-browser.org>2010-12-05 11:20:24 +0000
committerMichael Drake <tlsa@netsurf-browser.org>2010-12-05 11:20:24 +0000
commit2b0588ef4418a43c48b3b7cbc0c92fce007ef5ae (patch)
tree5473c291b2f1518163dc6f7127b422dd92d1f83f /examples/dom-structure-dump.c
parent64e9feb8d0e62a4428d1db7a18cf00fdc2917252 (diff)
downloadlibdom-2b0588ef4418a43c48b3b7cbc0c92fce007ef5ae.tar.gz
libdom-2b0588ef4418a43c48b3b7cbc0c92fce007ef5ae.tar.bz2
Update for latest lib versions.
svn path=/trunk/dom/; revision=10996
Diffstat (limited to 'examples/dom-structure-dump.c')
-rw-r--r--examples/dom-structure-dump.c280
1 files changed, 144 insertions, 136 deletions
diff --git a/examples/dom-structure-dump.c b/examples/dom-structure-dump.c
index f1ca731..4e7f77c 100644
--- a/examples/dom-structure-dump.c
+++ b/examples/dom-structure-dump.c
@@ -14,7 +14,6 @@
* 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>
@@ -49,8 +48,6 @@
#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);
@@ -71,95 +68,73 @@ void test_msg(uint32_t severity, void *ctx, const char *msg, ...)
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;
- }
+/**
+ * Generate a LibDOM document DOM from an HTML file
+ *
+ * \param file The file path
+ * \return pointer to DOM document, or NULL on error
+ */
+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];
- /* 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;
+ /* Create Hubbub parser */
+ parser = dom_hubbub_parser_create(NULL, true, test_realloc, NULL,
+ test_msg, NULL);
+ if (parser == NULL) {
+ printf("Can't create Hubbub Parser\n");
+ return NULL;
}
- /* 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;
+ /* 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;
}
- /* 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;
+ /* 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;
+ }
}
- /* 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;
+ /* 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;
}
- 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++;
+ /* Get the document */
+ doc = dom_hubbub_parser_get_document(parser);
- /* Loop though all node's children */
- do {
- /* Visit node's descendents */
- dump_dom_structure(child, depth);
+ /* Finished with parser */
+ dom_hubbub_parser_destroy(parser);
- /* 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 */
+ /* Close input file */
+ if (close(handle) == -1) {
+ printf("Can't close test input file: %s\n", file);
+ return NULL;
}
+
+ return doc;
}
@@ -168,42 +143,44 @@ dump_dom_structure(dom_node_internal *node, int depth)
*
* \param node The node to dump
* \param depth The node's depth
+ * \return true on success, or false on error
*/
-dump_dom_element(dom_node_internal *node, int depth)
+bool 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;
+ const char *string;
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;
+ return false;
} else if (type != DOM_ELEMENT_NODE) {
- return;
+ /* Nothing to print */
+ return true;
}
- /* Get root element name */
+ /* Get 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;
+ return false;
} else if (node_name == NULL) {
printf("Broken: root_name == NULL\n");
- return EXIT_FAILURE;
+ return false;
}
/* 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;
+ return false;
}
/* Print ASCII tree structure for current node */
@@ -217,74 +194,105 @@ dump_dom_element(dom_node_internal *node, int depth)
/* Get string data and print element name */
string = lwc_string_data(lwcstr);
length = lwc_string_length(lwcstr);
- printf("%*s\n", length, string);
+ printf("%*s", length, string);
+
+ /* TODO: Print the element's class, if it has one */
+
+ printf("\n");
+ return true;
}
/**
- * Generate a LibDOM document DOM from an HTML file
+ * Walk though a DOM (sub)tree, in depth first order, printing DOM structure.
*
- * \param file The file path
+ * \param node The root node to start from
+ * \param depth The depth of 'node' in the (sub)tree
*/
-dom_document *create_doc_dom_from_file(char *file)
+bool dump_dom_structure(dom_node_internal *node, int depth)
{
- 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];
+ dom_exception exc;
+ dom_node_internal *child;
- /* 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;
+ /* Print this node's entry */
+ if (dump_dom_element(node, depth) == false) {
+ /* There was an error; return */
+ return false;
}
- /* 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;
+ /* 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 false;
+ } 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 false;
+ }
+ } while (child != NULL); /* No more children */
}
+ return true;
+}
- /* 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;
- }
+
+/**
+ * Main entry point from OS.
+ */
+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_node_internal *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;
}
- /* 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;
+ /* 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 the document */
- doc = dom_hubbub_parser_get_document(parser);
+ /* 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;
+ }
- /* Finished with parser */
- dom_hubbub_parser_destroy(parser);
+ /* Dump DOM structure */
+ if (dump_dom_structure(root, 0) == false) {
+ return EXIT_FAILURE;
+ }
- /* Close input file */
- if (close(handle) == -1) {
- printf("Can't close test input file: %s\n", file);
- return NULL;
+ /* Finalise the DOM library */
+ exc = dom_finalise();
+ if (exc != DOM_NO_ERR) {
+ printf("Failed to finalise DOM library.\n");
+ return EXIT_FAILURE;
}
- return doc;
+ return EXIT_SUCCESS;
}