PostGIS  2.5.7dev-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 234 of file lwgeom_in_gml.c.

235 {
236  char *id;
237  xmlNsPtr *ns, *n;
238  xmlXPathContext *ctx;
239  xmlXPathObject *xpath;
240  xmlNodePtr node, ret_node;
241  xmlChar *href, *p, *node_id;
242 
243  href = xmlGetNsProp(xnode, (xmlChar *)"href", (xmlChar *) XLINK_NS);
244  id = lwalloc((xmlStrlen(xnode->ns->prefix) * 2 + xmlStrlen(xnode->name)
245  + xmlStrlen(href) + sizeof("//:[@:id='']") + 1));
246  p = href;
247  p++; /* ignore '#' first char */
248 
249  /* XPath pattern look like: //gml:point[@gml:id='p1'] */
250  sprintf(id, "//%s:%s[@%s:id='%s']", (char *) xnode->ns->prefix,
251  (char *) xnode->name,
252  (char *) xnode->ns->prefix,
253  (char *) p);
254 
255  ctx = xmlXPathNewContext(xnode->doc);
256  if (ctx == NULL)
257  {
258  xmlFree(href);
259  lwfree(id);
260  return NULL;
261  }
262 
263  /* Handle namespaces */
264  ns = xmlGetNsList(xnode->doc, xnode);
265  for (n=ns ; *n; n++) xmlXPathRegisterNs(ctx, (*n)->prefix, (*n)->href);
266  xmlFree(ns);
267 
268  /* Execute XPath expression */
269  xpath = xmlXPathEvalExpression((xmlChar *) id, ctx);
270  lwfree(id);
271  if (xpath == NULL || xpath->nodesetval == NULL || xpath->nodesetval->nodeNr != 1)
272  {
273  xmlFree(href);
274  xmlXPathFreeObject(xpath);
275  xmlXPathFreeContext(ctx);
276  return NULL;
277  }
278  ret_node = xpath->nodesetval->nodeTab[0];
279  xmlXPathFreeObject(xpath);
280  xmlXPathFreeContext(ctx);
281 
282  /* Protection against circular calls */
283  for (node = xnode ; node != NULL ; node = node->parent)
284  {
285  if (node->type != XML_ELEMENT_NODE) continue;
286  node_id = gmlGetProp(node, (xmlChar *) "id");
287  if (node_id != NULL)
288  {
289  if (!xmlStrcmp(node_id, p))
290  gml_lwpgerror("invalid GML representation", 2);
291  xmlFree(node_id);
292  }
293  }
294 
295  xmlFree(href);
296  return ret_node;
297 }
void lwfree(void *mem)
Definition: lwutil.c:244
void * lwalloc(size_t size)
Definition: lwutil.c:229
#define XLINK_NS
Definition: lwgeom_in_gml.c:76
static xmlChar * gmlGetProp(xmlNodePtr xnode, xmlChar *prop)
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: