PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ get_xlink_node()

static xmlNodePtr get_xlink_node ( xmlNodePtr  xnode)
static

Return a xmlNodePtr on a node referenced by a XLink or NULL otherwise.

Definition at line 259 of file lwgeom_in_gml.c.

260 {
261  char *id;
262  xmlNsPtr *ns, *n;
263  xmlXPathContext *ctx;
264  xmlXPathObject *xpath;
265  xmlNodePtr node, ret_node;
266  xmlChar *href, *p, *node_id;
267 
268  href = xmlGetNsProp(xnode, (xmlChar *)"href", (xmlChar *) XLINK_NS);
269  id = lwalloc((xmlStrlen(xnode->ns->prefix) * 2 + xmlStrlen(xnode->name)
270  + xmlStrlen(href) + sizeof("//:[@:id='']") + 1));
271  p = href;
272  p++; /* ignore '#' first char */
273 
274  /* XPath pattern look like: //gml:point[@gml:id='p1'] */
275  sprintf(id, "//%s:%s[@%s:id='%s']", (char *) xnode->ns->prefix,
276  (char *) xnode->name,
277  (char *) xnode->ns->prefix,
278  (char *) p);
279 
280  ctx = xmlXPathNewContext(xnode->doc);
281  if (ctx == NULL)
282  {
283  xmlFree(href);
284  lwfree(id);
285  return NULL;
286  }
287 
288  /* Handle namespaces */
289  ns = xmlGetNsList(xnode->doc, xnode);
290  for (n=ns ; *n; n++) xmlXPathRegisterNs(ctx, (*n)->prefix, (*n)->href);
291  xmlFree(ns);
292 
293  /* Execute XPath expression */
294  xpath = xmlXPathEvalExpression((xmlChar *) id, ctx);
295  lwfree(id);
296  if (xpath == NULL || xpath->nodesetval == NULL || xpath->nodesetval->nodeNr != 1)
297  {
298  xmlFree(href);
299  xmlXPathFreeObject(xpath);
300  xmlXPathFreeContext(ctx);
301  return NULL;
302  }
303  ret_node = xpath->nodesetval->nodeTab[0];
304  xmlXPathFreeObject(xpath);
305  xmlXPathFreeContext(ctx);
306 
307  /* Protection against circular calls */
308  for (node = xnode ; node != NULL ; node = node->parent)
309  {
310  if (node->type != XML_ELEMENT_NODE) continue;
311  node_id = gmlGetProp(node, "id");
312  if (node_id != NULL)
313  {
314  if (!xmlStrcmp(node_id, p))
315  gml_lwpgerror("invalid GML representation", 2);
316  xmlFree(node_id);
317  }
318  }
319 
320  xmlFree(href);
321  return ret_node;
322 }
void lwfree(void *mem)
Definition: lwutil.c:242
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define XLINK_NS
Definition: lwgeom_in_gml.c:76
static xmlChar * gmlGetProp(xmlNodePtr xnode, const char *charProp)
Retrieve a GML property from a node or NULL otherwise Respect namespaces if presents in the node elem...
static void gml_lwpgerror(char *msg, __attribute__((__unused__)) int error_code)
Definition: lwgeom_in_gml.c:82

References gml_lwpgerror(), gmlGetProp(), lwalloc(), lwfree(), and XLINK_NS.

Referenced by parse_gml_coll(), parse_gml_curve(), parse_gml_data(), parse_gml_line(), parse_gml_linearring(), parse_gml_mcurve(), parse_gml_mline(), parse_gml_mpoint(), parse_gml_mpoly(), parse_gml_msurface(), parse_gml_point(), parse_gml_polygon(), parse_gml_psurface(), parse_gml_surface(), parse_gml_tin(), and parse_gml_triangle().

Here is the call graph for this function:
Here is the caller graph for this function: