summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Drake <tlsa@netsurf-browser.org>2010-12-03 16:51:17 +0000
committerMichael Drake <tlsa@netsurf-browser.org>2010-12-03 16:51:17 +0000
commit483e4eaa3ec57807b5552f91c7a39f83d69d594b (patch)
treeae692b928f94a2673717fedfb62c23eef107e370
parentc46af7ce360697ec321284f20c26e5d98976d606 (diff)
downloadlibdom-483e4eaa3ec57807b5552f91c7a39f83d69d594b.tar.gz
libdom-483e4eaa3ec57807b5552f91c7a39f83d69d594b.tar.bz2
Add incomplete LibDOM example.
svn path=/trunk/dom/; revision=10953
-rw-r--r--examples/dom-structure-dump.c234
-rw-r--r--examples/files/test.html213
-rw-r--r--examples/makefile18
-rwxr-xr-xtest/transform.pl2
4 files changed, 466 insertions, 1 deletions
diff --git a/examples/dom-structure-dump.c b/examples/dom-structure-dump.c
new file mode 100644
index 0000000..fc1b75e
--- /dev/null
+++ b/examples/dom-structure-dump.c
@@ -0,0 +1,234 @@
+/*
+ * 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
+ *
+ * e.g. <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>
+ *
+ * gives:
+ *
+ * HTML
+ * +-BODY
+ * | +-H1 class="woo"
+ * | | +-"NetSurf"
+ * | +-P
+ * | | +-"NetSurf is "
+ * | | +-EM
+ * | | | +-"awesome"
+ * | | +-"!"
+ * | +-DIV
+ * | | +-H2
+ * | | | +-"Hubbub"
+ * | | +-P
+ * | | | +-"Hubbub is too."
+ * | | +-P
+ * | | | +-"Big time."
+ *
+ *
+ * or maybe just:
+ *
+ * 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))
+
+struct test_data {
+ int wah;
+};
+
+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)
+{
+ struct test_data test_data;
+ lwc_error err;
+ dom_exception exc;
+ dom_document *doc = NULL;
+ dom_element *root = NULL;
+ dom_string *root_name = NULL;
+ lwc_string *lwcstr = NULL;
+ const char *string;
+
+ /* Initialise the DOM library */
+ exc = dom_initialise(test_realloc, &test_data);
+ 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;
+ }
+
+ /* Dump DOM structure */
+ /* TODO: Actually just root element for now! */
+
+ /* 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;
+ }
+
+ /* Get root element name */
+ exc = dom_node_get_node_name(root, &root_name);
+ if (exc != DOM_NO_ERR) {
+ printf("Exception raised for get_node_name\n");
+ return EXIT_FAILURE;
+ } else if (root_name == NULL) {
+ printf("Broken: root_name == NULL\n");
+ return EXIT_FAILURE;
+ }
+
+ /* Get root element name's lwc_string */
+ exc = dom_string_get_intern(root_name, &lwcstr);
+ if (exc != DOM_NO_ERR) {
+ printf("Exception raised for string_get_intern\n");
+ return EXIT_FAILURE;
+ }
+
+ /* Get string data and print */
+ string = lwc_string_data(lwcstr);
+ printf("%4s\n", string);
+
+ /* 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;
+}
+
+
+
+/**
+ * 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;
+}
+
diff --git a/examples/files/test.html b/examples/files/test.html
new file mode 100644
index 0000000..7289589
--- /dev/null
+++ b/examples/files/test.html
@@ -0,0 +1,213 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
+ "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+<title>NetSurf Web Browser</title>
+<link rel="stylesheet" title="Standard" type="text/css" href="netsurf.css">
+<link rel="icon" type="image/png" href="/webimages/favicon.png">
+</head>
+
+<body>
+<h1 class="banner"><a href="/"><img src="netsurf.png" alt="NetSurf"></a></h1>
+
+<div class="navigation">
+<div class="navsection">
+<ul>
+<li><a href="/about/">About NetSurf</a></li>
+<li><a href="/downloads/">Downloads</a></li>
+<li><a href="/documentation/">Documentation</a></li>
+<li><a href="/developers/">Development area</a></li>
+<li><a href="/webmasters/">Webmaster area</a></li>
+<li><a href="/contact/">Contact</a></li>
+</ul>
+</div>
+
+<div class="navsection">
+<ul class="languages">
+<!--<li><a href="index.de">Deutsch</a></li>-->
+<li>English</li>
+<!--<li><a href="index.fr">Français</a></li>-->
+<!--<li><a href="index.nl">Nederlands</a></li>-->
+</ul>
+</div>
+
+<div class="navsection">
+<ul class="sitelinks">
+<li><a href="http://wiki.netsurf-browser.org/">Development wiki</a></li>
+<li><a href="http://source.netsurf-browser.org/">SVN repository viewer</a></li>
+</ul>
+</div>
+
+<div class="navsection">
+<h2 class="navtitle">Project Overview</h2>
+<p>NetSurf project at-a-glance.</p>
+<dl>
+<dt>Core project:</dt>
+<dd>
+<dl>
+<dt><a href="#leader">NetSurf web browser</a></dt>
+</dl>
+</dd>
+<dt>Sub-projects:</dt>
+<dd>
+<dl>
+<dt><a href="/projects/hubbub/">Hubbub</a></dt>
+<dd>HTML5 compliant parsing library</dd>
+<dt><a href="/projects/libcss/">LibCSS</a><dt>
+<dd>CSS parser and selection library<dd>
+<dt><a href="/projects/libdom/">LibDOM</a></dt>
+<dd>DOM library</dd>
+<dt><a href="/projects/libparserutils/">LibParserUtils</a><dt>
+<dd>Parser building library<dd>
+<dt><a href="/projects/libwapcaplet/">LibWapcaplet</a><dt>
+<dd>String internment library<dd>
+<dt><a href="/projects/libsvgtiny/">Libsvgtiny</a></dt>
+<dd>SVG Tiny library</dd>
+<dt><a href="/projects/libnsfb/">LibNSFB</a><dt>
+<dd>Framebuffer Abstraction library<dd>
+<dt><a href="/projects/libnsbmp/">Libnsbmp</a></dt>
+<dd>BMP and ICO decoding library<dd>
+<dt><a href="/projects/libnsgif/">Libnsgif</a></dt>
+<dd>GIF decoding library<dd>
+<dt><a href="/projects/librosprite/">LibROSprite</a></dt>
+<dd>RISC OS Sprite decoding library<dd>
+<dt><a href="/projects/iconv/">Iconv</a></dt>
+<dd>RISC OS character encoding support module</dd>
+<dt><a href="/projects/rufl/">RUfl</a></dt>
+<dd>RISC OS Unicode font library</dd>
+<dt><a href="/projects/tinct/">Tinct</a></dt>
+<dd>Advanced RISC OS Sprite plotting module</dd>
+<dt><a href="/projects/libpencil/">Libpencil</a></dt>
+<dd>RISC OS Drawfile export library</dd>
+</dl>
+</dd>
+</dl>
+</div>
+</div>
+
+<div class="content">
+<div class="frontpageintro">
+<p id="leader">Small as a mouse, fast as a cheetah and available for free. NetSurf is a multi-platform web browser for <a href="http://www.riscosopen.org/">RISC&nbsp;OS</a>, UNIX-like platforms (including Linux) and more.</p>
+
+<p>Whether you want to check your webmail, read the news or post to discussion forums, NetSurf is your lightweight gateway to the world wide web. Actively developed, NetSurf is continually evolving and improving.</p>
+
+<div class="frontscreen"><p class="frontscreen"><a href="/about/screenshots/"><img src="about/screenshots/thumbnails/rowikipedia.png" alt="NetSurf screenshot"> <span class="seemore">See more screenshots</span></a></p></div>
+
+<p>Written in C, this award winning open source project features its own layout engine. It is licensed under GPL version 2.</p>
+
+<h2 id="whynetsurf">Why choose NetSurf?</h2>
+
+<dl class="motivation">
+<dt>Speed</dt>
+<dd>Efficiency lies at the heart of the NetSurf engine, allowing it to outwit the heavyweights of the web browser world. The NetSurf team continue to squeeze more speed out of their code.</dd>
+<dt>Interface innovation</dt>
+<dd>Simple to use and easy to grasp, NetSurf significantly raised the bar for user interface design on the RISC&nbsp;OS platform. Designed carefully by RISC&nbsp;OS users and developers to integrate well with the desktop, NetSurf is seen as the benchmark for future applications. NetSurf pioneered the concept of web page thumbnailing, offering an intuitive graphical tree-like view of visited web sites.</dd>
+<dt>Lean requirements</dt>
+<dd>From a modern monster PC to a humble 30MHz ARM 6 computer with 16MB of RAM, the web browser will keep you surfing the web whatever your system. Originally written for computer hardware normally found in PDAs, cable TV boxes, mobile phones and other hand-held gadgets, NetSurf is compact and low maintenance by design.</dd>
+<dt>Portable</dt>
+<dd>NetSurf can be built for a number of modern computer platforms 'out of the box'. Written in C, with portability in mind, NetSurf is developed by programmers from a wide range of computing backgrounds, ensuring it remains available for as many users as possible.</dd>
+<dt>Standards compliant</dt>
+<dd>Despite a myriad of standards to support, NetSurf makes surfing the web enjoyable and stress-free by striving for complete standards compliancy. As an actively developed project, NetSurf aims to stay abreast of new and upcoming web technologies.</dd>
+</dl>
+
+<p>See the <a href="/about/#ProjectGoals">project goals</a> and <a href="/documentation/progress">progress</a> page for further information on where NetSurf is headed.</p>
+
+<h2 id="WantToHelp">Want to help?</h2>
+
+<p>There are always things that need doing, and not enough time in the day, so we'd be delighted if you want to help develop NetSurf. Visit the &quot;<a href="/developers/contribute">How can I help?</a>&quot; page to see ideas for contributing to the project.</p>
+
+<p>If you can program and you'd like to improve NetSurf, then we'd love to hear from you. Pick an area you'd like to improve or a feature you want to add and <a href="/contact/">contact the developers</a>. Also, take a look at the <a href="/developers/">developer and contributor</a> area of this site.</p>
+</div>
+
+<div class="frontpagelatestinfo">
+<div>
+<div class="downloadbox">
+<div class="downloadcontainer">
+<div class="downloadcontent">
+<h2 id="downloadrelease"><a href="/downloads/">Download NetSurf&nbsp;2.6</a></h2>
+<ul>
+<li><a href="/downloads/riscos/">For RISC&nbsp;OS</a></li>
+<li><a href="/downloads/gtk/">For Linux</a></li>
+<li><a href="/downloads/">Other systems</a></li>
+<li><a href="/downloads/source/">Source code</a></li>
+</ul>
+</div>
+<div class="topleft"></div>
+<div class="topright"></div>
+<div class="bottomleft"></div>
+<div class="bottomright"></div>
+</div>
+<div class="arrow"></div>
+</div>
+
+<h2 id="news">Latest news</h2>
+
+<dl class="frontnews">
+<dt><a href="/downloads/">NetSurf 2.6 released</a> <span>21 Sep 2010</span></dt>
+<dd>NetSurf 2.6 is primarily a bug fix release. It contains some improvements to page rendering, fetching &amp; caching, memory usage, as well as some front-end specific fixes. Full details in the change log. We recommend all users upgrade.</dd>
+<dt><a href="/downloads/">NetSurf 2.5 released</a> <span>24 Apr 2010</span></dt>
+<dd>NetSurf 2.5 contains many improvements over the previous release. The major changes are the use of our brand new CSS parser and selection engine (LibCSS), and a newly designed cache for fetched content. Full details in the change log. We recommend all users upgrade.</dd>
+<dt><a href="http://vlists.pepperfish.net/pipermail/netsurf-users-netsurf-browser.org/2010-January/009241.html">NetSurf at Wakefield Show 2010</a> <span>14 Jan 2010</span></dt>
+<dd>NetSurf 2.5 is expected to be released at the Wakefield RISC OS Show. The release will focus on the CSS engine, memory usage and speed, as well as fixing bugs. NetSurf 2.5 is likely to be the last release for RISC OS.</dd>
+</dl>
+<p class="more"><a href="/about/news" class="seemore">See more news</a></p>
+
+<h2 id="features">NetSurf 2.6 features</h2>
+
+<p>NetSurf 2.6 is available for: RISC OS; Linux and other UNIX-like systems; and AmigaOS 4.</p>
+
+<dl>
+<dt>General</dt>
+<dd>
+<ul>
+<li>Web standards: HTML 4.01 and CSS 2.1</li>
+<li>Image formats: PNG, GIF, JPEG, SVG, JNG, MNG and BMP</li>
+<li>HTTPS for secure online transactions</li>
+<li>Unicode text</li>
+<li>Web page thumbnailing</li>
+<li>Local history trees</li>
+<li>Global history</li>
+<li>URL completion</li>
+<li>Text selection</li>
+<li>Scale view</li>
+<li>Search-as-you-type text search highlighting</li>
+<li>Save pages complete with images</li>
+</ul>
+</dd>
+<dt>RISC OS only</dt>
+<dd>
+<ul>
+<li>Image formats: Sprite, Drawfile and ArtWorks</li>
+<li>Hotlist management (bookmarks)</li>
+<li>Cookie viewer</li>
+<li>Drawfile export</li>
+</ul>
+</dd>
+</dl>
+
+<h2 id="Awards">Awards</h2>
+
+<p class="award"><a class="award" href="http://www.iconbar.com/articles/The_Icon_Bar_Awards_2009/index1244.html"><img src="/webimages/tiba2009.png" alt=""></a>NetSurf picked up another accolade, winning in the &quot;best non-commercial product&quot; category at <a href="http://www.iconbar.com/articles/The_Icon_Bar_Awards_2009/index1244.html">The Icon Bar Awards 2009</a>. NetSurf had previously been <a href="http://www.iconbar.com/articles/Nominations_open_for_the_sDrobes_Icon_Bar_awards_2009/index1240.html">nominated</a> by <a href="http://www.iconbar.com/">The Icon Bar</a>'s readers.</p>
+
+<p class="award"><a class="award" href="http://www.drobe.co.uk/article.php?id=2389"><img src="/webimages/da2008.png" alt=""></a>At <a href="http://www.drobe.co.uk/">Drobe Launch Pad</a>'s annual awards, NetSurf triumphed again in the category for &quot;best non-commercial software&quot;, winning the <a href="http://www.drobe.co.uk/article.php?id=2389">2008 award</a>.</p>
+<p class="more"><a href="/about/awards" class="seemore">See more awards</a></p>
+
+</div>
+</div>
+
+<div class="footer">
+<p>Copyright 2003 - 2010 The NetSurf Developers</p>
+</div>
+</div>
+
+
+<form method="get" action="http://www.google.co.uk/search">
+<div class="searchbox">
+<input type="hidden" name="q" value="site:netsurf-browser.org">
+<input type="text" name="q" maxlength="255"><br>
+<input type="submit" value="Search" name="btnG">
+</div>
+</form>
+
+</body>
+</html>
diff --git a/examples/makefile b/examples/makefile
new file mode 100644
index 0000000..fc315cc
--- /dev/null
+++ b/examples/makefile
@@ -0,0 +1,18 @@
+CC := gcc
+LD := gcc
+
+CFLAGS := `pkg-config --cflags libdom` `pkg-config --cflags libwapcaplet`
+LDFLAGS := `pkg-config --libs libdom` `pkg-config --libs libwapcaplet`
+
+SRC := dom-structure-dump.c
+
+dom-structure-dump: $(SRC:.c=.o)
+ @$(LD) -o $@ $^ $(LDFLAGS)
+
+.PHONY: clean
+clean:
+ $(RM) dom-structure-dump $(SRC:.c=.o)
+
+%.o: %.c
+ @$(CC) -c $(CFLAGS) -o $@ $<
+
diff --git a/test/transform.pl b/test/transform.pl
index 4ab0b46..74d2f61 100755
--- a/test/transform.pl
+++ b/test/transform.pl
@@ -1,4 +1,4 @@
-#!/bin/perl
+#!/usr/bin/perl
# This file is part of libdom.
# It is used to generate libdom test files from the W3C DOMTS.
#