summaryrefslogtreecommitdiff
path: root/docs/examples/selection-example-1.c
blob: ab944926e33a518f8192faff6769ad5c2f817a8b (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
/**
 * 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);
	  ws->selector = NULL ;
  }
  if (ws->cascade) 
  {
	  cr_cascade_destroy(ws->cascade);
	  ws->cascade = NULL ;
  }

  if (ws->result) 
  {	  
	  xmlXPathFreeObject(ws->result);
	  ws->result = NULL ;
  }
  if (ws->xpath) 
  {
	  xmlXPathFreeContext(ws->xpath); 
	  ws->xpath = NULL ;
  }
  if (ws->document) 
  {
	  xmlFreeDoc(ws->document);
	  ws->document = NULL ;
  }  
}

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;
}