1 | #include <string.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <glib.h> |
---|
5 | #include <sys/stat.h> |
---|
6 | #include "service.h" |
---|
7 | #include "service_internal.h" |
---|
8 | #include <libxml/tree.h> |
---|
9 | #include <libxml/xmlmemory.h> |
---|
10 | #include <libxml/parser.h> |
---|
11 | #include <libxml/xpath.h> |
---|
12 | #include <libxml/xpathInternals.h> |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | static int SCAN_DEPTH = 7; |
---|
17 | |
---|
18 | |
---|
19 | static GList *serviceCfgList; |
---|
20 | |
---|
21 | static int |
---|
22 | scanServiceCfg (char *rootDir, GList ** serviceList, int level) |
---|
23 | { |
---|
24 | if (level >= SCAN_DEPTH) |
---|
25 | return 1; |
---|
26 | struct dirent *d; |
---|
27 | DIR *dir = opendir (rootDir); |
---|
28 | if (dir != NULL) |
---|
29 | { |
---|
30 | while ((d = readdir (dir))) |
---|
31 | { |
---|
32 | if ((d->d_type == DT_DIR || d->d_type == DT_LNK) |
---|
33 | && d->d_name[0] != '.' && strstr (d->d_name, ".") == NULL) |
---|
34 | { |
---|
35 | char *name = |
---|
36 | (char *) malloc (strlen (rootDir) + strlen (d->d_name) + 2); |
---|
37 | name[0] = '\0'; |
---|
38 | strncat (name, rootDir, strlen (rootDir)); |
---|
39 | strncat (name, "/", strlen ("/")); |
---|
40 | strncat (name, d->d_name, strlen (d->d_name)); |
---|
41 | scanServiceCfg (name, serviceList, level + 1); |
---|
42 | } |
---|
43 | else |
---|
44 | { |
---|
45 | if (d->d_name[0] != '.') |
---|
46 | if (strstr (d->d_name, ".zcfg") != NULL) |
---|
47 | { |
---|
48 | char *name = |
---|
49 | (char *) malloc (strlen (rootDir) + strlen (d->d_name) + |
---|
50 | 2); |
---|
51 | name[0] = '\0'; |
---|
52 | strncat (name, rootDir, strlen (rootDir)); |
---|
53 | strncat (name, "/", strlen ("/")); |
---|
54 | strncat (name, d->d_name, strlen (d->d_name)); |
---|
55 | *serviceList = g_list_append (*serviceList, name); |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | if (level > 0) |
---|
60 | free(rootDir); |
---|
61 | } |
---|
62 | closedir (dir); |
---|
63 | return 1; |
---|
64 | } |
---|
65 | |
---|
66 | static int |
---|
67 | get_identifier (char *root_dir, char *zcfg_path, char **identifier) |
---|
68 | { |
---|
69 | // On extrait le repertoire racine ainsi que l'extention .zcfg pour contruire l'identifiant |
---|
70 | // root_dir = /var/www/zoo/cgi-bin/ zcfg_path=/var/www/zoo/cgi-bin/gdal/ndvi/ExtractNDVI.zcfg ===> gdal.ndvi.ExtractNDVI |
---|
71 | char *identifier_tmp = |
---|
72 | (char *) malloc ((strlen (zcfg_path) + 1) * sizeof (char)); |
---|
73 | identifier_tmp[0] = '\0'; |
---|
74 | int ext_len = strlen (".zcfg"); |
---|
75 | int s_tmp_len = strlen (zcfg_path) - ext_len - strlen (root_dir); |
---|
76 | if (s_tmp_len > 1) |
---|
77 | { |
---|
78 | char *s_tmp = (char *) malloc ((s_tmp_len + 1) * sizeof (char)); |
---|
79 | int j; |
---|
80 | for (j = 0; j < s_tmp_len; j++) |
---|
81 | s_tmp[j] = zcfg_path[strlen (root_dir) + j]; |
---|
82 | s_tmp[s_tmp_len] = '\0'; |
---|
83 | char *save_ptr_strtok; |
---|
84 | char *token = strtok_r (s_tmp, "/", &save_ptr_strtok); |
---|
85 | strncat (identifier_tmp, token, strlen (token)); |
---|
86 | while (token != NULL) |
---|
87 | { |
---|
88 | token = strtok_r (NULL, "/", &save_ptr_strtok); |
---|
89 | if (token != NULL) |
---|
90 | { |
---|
91 | strncat (identifier_tmp, ".", strlen (".")); |
---|
92 | strncat (identifier_tmp, token, strlen (token)); |
---|
93 | } |
---|
94 | |
---|
95 | } |
---|
96 | *identifier = |
---|
97 | (char *) malloc ((strlen (identifier_tmp) + 1) * sizeof (char)); |
---|
98 | strncpy (*identifier, identifier_tmp, strlen (identifier_tmp) + 1); |
---|
99 | free (s_tmp); |
---|
100 | } |
---|
101 | free (identifier_tmp); |
---|
102 | return 1; |
---|
103 | } |
---|
104 | |
---|
105 | void |
---|
106 | init_services_conf (char *rootDir) |
---|
107 | { |
---|
108 | maps *m = (maps *) malloc (MAP_SIZE); |
---|
109 | GList *L = NULL; |
---|
110 | scanServiceCfg (rootDir, &L, 0); |
---|
111 | GList *l = NULL; |
---|
112 | for (l = L; l; l = l->next) |
---|
113 | { |
---|
114 | service *s1 = (service *) malloc (SERVICE_SIZE); |
---|
115 | get_identifier (rootDir, (char *) (l->data), &(s1->identifier)); |
---|
116 | s1->zcfg = (char *) (l->data); |
---|
117 | readServiceFile (m, (char *) l->data, &s1, s1->identifier); |
---|
118 | serviceCfgList = g_list_append (serviceCfgList, s1); |
---|
119 | } |
---|
120 | freeMap(&m); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | service * |
---|
125 | search_service (char *identifier) |
---|
126 | { |
---|
127 | GList *l; |
---|
128 | int i = 0; |
---|
129 | for (l = serviceCfgList; l; l = l->next) |
---|
130 | { |
---|
131 | #ifdef DEBUG |
---|
132 | fprintf (stderr, "%d ### %s ###\n", i, |
---|
133 | ((service *) (l->data))->identifier); |
---|
134 | i++; |
---|
135 | #endif |
---|
136 | if (strcasecmp (identifier, ((service *) (l->data))->identifier) == 0) |
---|
137 | return (service *) l->data; |
---|
138 | } |
---|
139 | return NULL; |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | void |
---|
144 | XML_CapabilitiesAllProcess (maps * m, xmlNodePtr nc) |
---|
145 | { |
---|
146 | GList *l; |
---|
147 | for (l = serviceCfgList; l; l = l->next) |
---|
148 | { |
---|
149 | |
---|
150 | service *serv = (service *) l->data; |
---|
151 | |
---|
152 | xmlNsPtr ns, ns_ows, ns_xlink; |
---|
153 | xmlNodePtr n = NULL, nc1, nc2; |
---|
154 | /** |
---|
155 | * Initialize or get existing namspaces |
---|
156 | */ |
---|
157 | int wpsId = |
---|
158 | zooXmlAddNs (NULL, "http://www.opengis.net/wps/1.0.0", "wps"); |
---|
159 | ns = usedNs[wpsId]; |
---|
160 | int owsId = zooXmlAddNs (NULL, "http://www.opengis.net/ows/1.1", "ows"); |
---|
161 | ns_ows = usedNs[owsId]; |
---|
162 | int xlinkId = zooXmlAddNs (n, "http://www.w3.org/1999/xlink", "xlink"); |
---|
163 | ns_xlink = usedNs[xlinkId]; |
---|
164 | |
---|
165 | map *tmp1; |
---|
166 | if (serv->content != NULL) |
---|
167 | { |
---|
168 | nc1 = xmlNewNode (ns, BAD_CAST "Process"); |
---|
169 | tmp1 = getMap (serv->content, "processVersion"); |
---|
170 | if (tmp1 != NULL) |
---|
171 | xmlNewNsProp (nc1, ns, BAD_CAST "processVersion", |
---|
172 | BAD_CAST tmp1->value); |
---|
173 | //map *tmp3 = getMapFromMaps (m, "lenv", "level"); |
---|
174 | //addPrefix (m, tmp3, serv); |
---|
175 | printDescription (nc1, ns_ows, serv->identifier, serv->content); |
---|
176 | tmp1 = serv->metadata; |
---|
177 | while (tmp1 != NULL) |
---|
178 | { |
---|
179 | nc2 = xmlNewNode (ns_ows, BAD_CAST "Metadata"); |
---|
180 | xmlNewNsProp (nc2, ns_xlink, BAD_CAST tmp1->name, |
---|
181 | BAD_CAST tmp1->value); |
---|
182 | xmlAddChild (nc1, nc2); |
---|
183 | tmp1 = tmp1->next; |
---|
184 | } |
---|
185 | xmlAddChild (nc, nc1); |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | void |
---|
191 | XML_Describe_Process (maps * m, xmlNodePtr nc, char *identifiers) |
---|
192 | { |
---|
193 | if (strcasecmp ("all", identifiers) == 0) |
---|
194 | { |
---|
195 | GList *l; |
---|
196 | for (l = serviceCfgList; l; l = l->next) |
---|
197 | { |
---|
198 | service *serv = (service *) l->data; |
---|
199 | printDescribeProcessForProcess (m, nc, serv); |
---|
200 | } |
---|
201 | } |
---|
202 | else |
---|
203 | { |
---|
204 | char *save_ptr_strtok; |
---|
205 | char *token = strtok_r (identifiers, ",", &save_ptr_strtok); |
---|
206 | while (token != NULL) |
---|
207 | { |
---|
208 | service *serv = search_service (token); |
---|
209 | if (serv != NULL) |
---|
210 | printDescribeProcessForProcess (m, nc, serv); |
---|
211 | token = strtok_r (NULL, ",", &save_ptr_strtok); |
---|
212 | } |
---|
213 | } |
---|
214 | } |
---|