diff options
author | Dodji Seketeli <dodji@gnome.org> | 2003-12-11 21:14:05 +0000 |
---|---|---|
committer | Dodji Seketeli <dodji@src.gnome.org> | 2003-12-11 21:14:05 +0000 |
commit | 61ade384b3856379e82bceba7768762d04a015ef (patch) | |
tree | 1491ca77c9990fac776faee8e18adda439fb9495 /docs/examples | |
parent | 7da465f33ed5cd06d322eb0cdf174f313a2cf6e6 (diff) | |
download | libcroco-61ade384b3856379e82bceba7768762d04a015ef.tar.gz |
Fixed more build system stuffs here. Make these compile with g++. another
2003-12-11 Dodji Seketeli <dodji@gnome.org>
* src/layeng/Makefile.am,src/seleng/Makefile.am: Fixed more
build system stuffs here.
* docs/examples/cssom-example-1.c,docs/examples/sac-example-1.c: Make these
compile with g++.
* src/layeng/Makefile.am: another build system cleanup.
* docs/examples/selection-example-1.c,docs/examples/selection-example-1.css,
docs/examples/selection-example-1.xml: added this code example
from Stefan Seefeld.
Diffstat (limited to 'docs/examples')
-rw-r--r-- | docs/examples/cssom-example-1.c | 9 | ||||
-rw-r--r-- | docs/examples/report.c | 0 | ||||
-rw-r--r-- | docs/examples/report.css | 0 | ||||
-rw-r--r-- | docs/examples/report.xml | 0 | ||||
-rw-r--r-- | docs/examples/sac-example-1.c | 18 | ||||
-rw-r--r-- | docs/examples/selection-example-1.c | 140 | ||||
-rw-r--r-- | docs/examples/selection-example-1.css | 31 | ||||
-rw-r--r-- | docs/examples/selection-example-1.xml | 12 |
8 files changed, 197 insertions, 13 deletions
diff --git a/docs/examples/cssom-example-1.c b/docs/examples/cssom-example-1.c index 01e93ed..663f86f 100644 --- a/docs/examples/cssom-example-1.c +++ b/docs/examples/cssom-example-1.c @@ -3,10 +3,12 @@ /** *This is an example that shows how to use *the CSSOM api of the libcroco CSS2 parsing library. + *It just parses the CSS document given in argument and + *it dumps it (serializes) it on the screen. * *To compile it using gcc, type * - *gcc `croco-config --cflags` `croco-config --libs` -o cssom-example cssom-example.c + *gcc -g `croco-config --cflags` `croco-config --libs` -o cssom-example-1 cssom-example-1.c * *Prior to that, you must have compiled and installed libcroco, of course. * @@ -52,7 +54,6 @@ main (int argc, char **argv) { short i = 0 ; enum CRStatus status = CR_OK ; - CROMParser *parser = NULL ; CRStyleSheet *stylesheet = NULL ; /*first parse command line arguments*/ @@ -78,7 +79,7 @@ main (int argc, char **argv) return 0; } - /***************************************************** + /***************************************************** *Enough plumbering... now, the real libcroco stuffs. ***************************************************/ @@ -86,7 +87,7 @@ main (int argc, char **argv) *What we want here is to simply parse *a CSS document using the cssom api. */ - status = cr_om_parser_simply_parse_file (argv[i] /*sheet*/, + status = cr_om_parser_simply_parse_file ((const guchar*)argv[i] /*sheet*/, CR_ASCII /*the encoding*/, &stylesheet) ; if (status == CR_OK && stylesheet) diff --git a/docs/examples/report.c b/docs/examples/report.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/docs/examples/report.c diff --git a/docs/examples/report.css b/docs/examples/report.css new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/docs/examples/report.css diff --git a/docs/examples/report.xml b/docs/examples/report.xml new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/docs/examples/report.xml diff --git a/docs/examples/sac-example-1.c b/docs/examples/sac-example-1.c index 7ba8dcf..e9be128 100644 --- a/docs/examples/sac-example-1.c +++ b/docs/examples/sac-example-1.c @@ -64,7 +64,7 @@ display_usage (unsigned char *a_prog_name) if (!prog_name) { - prog_name = "sac-example-1" ; + prog_name = (unsigned char*)"sac-example-1" ; } printf ("usage: %s [--help] | <css file name>\n", prog_name) ; @@ -80,7 +80,7 @@ main (int argc, char **argv) if (argc <= 1) { - display_usage (argv[0]) ; + display_usage ((unsigned char*)argv[0]) ; return -1 ; } @@ -97,8 +97,8 @@ main (int argc, char **argv) if (!strcmp (argv[i], "--help") || !strcmp (argv[i], "-h")) { - display_usage (argv[0]) ; - return ; + display_usage ((unsigned char*)argv[0]) ; + return -1; } else { @@ -107,7 +107,7 @@ main (int argc, char **argv) *available now, so this is *a bit redundant... */ - display_usage (argv[0]) ; + display_usage ((unsigned char*)argv[0]) ; } } @@ -117,14 +117,14 @@ main (int argc, char **argv) *no file name has been given *in parameter, go out. */ - return ; + return -1 ; } /**************************************** *Now, the real libcroco related stuffs... ****************************************/ - file_path = argv[i] ; + file_path = (unsigned char*)argv[i] ; /* *Instanciate the libcroco parser. @@ -136,7 +136,7 @@ main (int argc, char **argv) /* *Damned, something bad happened ... */ - return ; + return -1; } /* @@ -152,7 +152,7 @@ main (int argc, char **argv) */ cr_parser_destroy (parser) ; - return ; + return -1; } /****************** diff --git a/docs/examples/selection-example-1.c b/docs/examples/selection-example-1.c new file mode 100644 index 0000000..0e0f111 --- /dev/null +++ b/docs/examples/selection-example-1.c @@ -0,0 +1,140 @@ +/** + * This example looks up a node from a document with an + * xpath expression, then reports all properties that apply + * from a given stylesheet. + * + * To compile it using gcc, type + * + * gcc `croco-config --cflags` `croco-config --libs` -o selection-example-1 selection-example-1.c + * + * @author Stefan Seefeld <seefeld@sympatico.ca> + */ + +#include <libcroco.h> +#include <libxml/tree.h> +#include <libxml/xpath.h> + +void usage_and_exit(char *progname) +{ + fprintf(stderr, "Usage: %s <xml doc> <stylesheet> <xpath>\n", progname); + exit(-1); +} + +struct workspace +{ + xmlDoc *document; + xmlXPathContext *xpath; + xmlXPathObject *result; + CRStyleSheet *stylesheet; + CRCascade *cascade; + CRSelEng *selector; +}; + +/** + *construct workspace members in order... + * return 0 on success and -1 on error + */ +int init(struct workspace *ws, char **args) +{ + short i = 0; + enum CRStatus status = CR_OK; + + ws->document = 0; + ws->xpath = 0; + ws->result = 0; + ws->stylesheet = 0; + ws->cascade = 0; + ws->selector = 0; + + + ws->document = xmlParseFile(args[0]); + if (!ws->document) + { + fprintf(stderr, "could not parse the document %s", args[0]); + return -1; + } + ws->xpath = xmlXPathNewContext(ws->document); + if (!ws->xpath) + { + fprintf(stderr, "Error: unable to create new XPath context\n"); + return -1; + } + ws->result = xmlXPathEvalExpression((xmlChar *)args[2], ws->xpath); + if (!ws->result) + { + fprintf(stderr, "Error: unable to evaluate xpath expression\n"); + return -1; + } + if (ws->result->type != XPATH_NODESET || !ws->result->nodesetval) + { + fprintf(stderr, "Error: xpath does not evaluate to a node set\n"); + return -1; + } + + status = cr_om_parser_simply_parse_file((const guchar*)args[1] /*sheet*/, + CR_ASCII /*the encoding*/, + &ws->stylesheet); + if (status != CR_OK || !ws->stylesheet) + { + fprintf(stderr, "could not parse the stylesheet %s", args[1]); + return -1; + } + ws->cascade = cr_cascade_new(ws->stylesheet, 0, 0); + ws->selector = cr_sel_eng_new(); +} + +/* ...and destruct in reverse order*/ +void fini(struct workspace *ws) +{ + if (ws->selector) cr_sel_eng_destroy(ws->selector); + if (ws->cascade) cr_cascade_destroy(ws->cascade); + if (ws->stylesheet) cr_stylesheet_destroy(ws->stylesheet); + if (ws->result) xmlXPathFreeObject(ws->result); + if (ws->xpath) xmlXPathFreeContext(ws->xpath); + if (ws->document) xmlFreeDoc(ws->document); +} + +void print_property(gpointer name, gpointer decl, gpointer data) +{ + CRDeclaration *declaration = (CRDeclaration *)decl; + printf("%s\n", (char *)cr_declaration_to_string(declaration, 0)); +} + +void print_properties(struct workspace *ws) +{ + enum CRStatus status; + GHashTable *table = g_hash_table_new(g_str_hash, g_str_equal); + xmlNode *node = ws->result->nodesetval->nodeTab[0]; + if (!table) + { + fprintf(stderr, "unable to allocate a hash table\n"); + return; + } + status = cr_sel_eng_get_matched_properties_from_cascade(ws->selector, + ws->cascade, + node, + &table); + if (status != CR_OK) + fprintf(stderr, "Error retrieving properties\n"); + else + { + printf("properties for node %s :\n", (char *)xmlGetNodePath(node)); + g_hash_table_foreach(table, print_property, 0); + } + g_hash_table_destroy(table); +} + +int main(int argc, char **argv) +{ + struct workspace ws; + if (argc != 4) usage_and_exit(argv[0]); + if (!init(&ws, argv + 1)) fini(&ws); + + if (ws.result->nodesetval->nodeNr == 0) + printf("no matching nodes found\n"); + else + print_properties(&ws); + + fini(&ws); + return 0; +} diff --git a/docs/examples/selection-example-1.css b/docs/examples/selection-example-1.css new file mode 100644 index 0000000..8bf681b --- /dev/null +++ b/docs/examples/selection-example-1.css @@ -0,0 +1,31 @@ +report +{ + color : red; +} + +report > entry +{ + color : blue; +} + +note +{ + color : green; + frame : solid; +} + +note[id] +{ + color : blue; + frame : solid; +} + +note[id=bar] +{ + margin : 5px; +} + +entry[type=notice] note[id=bar] +{ + padding : 5px; +}
\ No newline at end of file diff --git a/docs/examples/selection-example-1.xml b/docs/examples/selection-example-1.xml new file mode 100644 index 0000000..90724d3 --- /dev/null +++ b/docs/examples/selection-example-1.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" ?> +<report> + <entry type="notice"> + <note id="foo"/> + <note id="bar"/> + </entry> + <entry> + <note/> + <note id="foo"/> + <note id="bar"/> + </entry> +</report>
\ No newline at end of file |