PyObject *xmlsec_NodeSetWalk(PyObject *self, PyObject *args) { PyObject *nset_obj, *walkFunc_obj, *data_obj; xmlSecNodeSetPtr nset; int ret; if (CheckArgs(args, "OC?:nodeSetWalk")) { if (!PyArg_ParseTuple(args, "OOO:nodeSetWalk", &nset_obj, &walkFunc_obj, &data_obj)) return NULL; } else return NULL; nset = xmlSecNodeSetPtr_get(nset_obj); if (NodeSetWalkCallbacks == NULL && walkFunc_obj != Py_None) NodeSetWalkCallbacks = xmlHashCreate(HASH_TABLE_SIZE); if (walkFunc_obj != Py_None) xmlHashAddEntry2(NodeSetWalkCallbacks, (const xmlChar *)nset->doc->name, nset->doc->URL, walkFunc_obj); Py_XINCREF(walkFunc_obj); ret = xmlSecNodeSetWalk(nset, xmlsec_NodeSetWalkCallback, PyCObject_AsVoidPtr(data_obj)); return (wrap_int(ret)); }
/* * exsltFuncRegisterImportFunc * @data: the exsltFuncFunctionData for the function * @ch: structure containing context and hash table * @URI: the function namespace URI * @name: the function name * * Checks if imported function is already registered in top-level * stylesheet. If not, copies function data and registers function */ static void exsltFuncRegisterImportFunc (exsltFuncFunctionData *data, exsltFuncImportRegData *ch, const xmlChar *URI, const xmlChar *name, ATTRIBUTE_UNUSED const xmlChar *ignored) { exsltFuncFunctionData *func=NULL; if ((data == NULL) || (ch == NULL) || (URI == NULL) || (name == NULL)) return; if (ch->ctxt == NULL || ch->hash == NULL) return; /* Check if already present */ func = (exsltFuncFunctionData*)xmlHashLookup2(ch->hash, URI, name); if (func == NULL) { /* Not yet present - copy it in */ func = exsltFuncNewFunctionData(); memcpy(func, data, sizeof(exsltFuncFunctionData)); if (xmlHashAddEntry2(ch->hash, URI, name, func) < 0) { xsltGenericError(xsltGenericErrorContext, "Failed to register function {%s}%s\n", URI, name); } else { /* Do the registration */ xsltGenericDebug(xsltGenericDebugContext, "exsltFuncRegisterImportFunc: register {%s}%s\n", URI, name); xsltRegisterExtFunction(ch->ctxt, name, URI, exsltFuncFunctionFunction); } } }
/** * xsltMergeSASCallback,: * @style: the XSLT stylesheet * * Merge an attribute set from an imported stylesheet. */ static void xsltMergeSASCallback(xsltAttrElemPtr values, xsltStylesheetPtr style, const xmlChar *name, const xmlChar *ns, ATTRIBUTE_UNUSED const xmlChar *ignored) { int ret; xsltAttrElemPtr topSet; ret = xmlHashAddEntry2(style->attributeSets, name, ns, values); if (ret < 0) { /* * Add failed, this attribute set can be removed. */ #ifdef WITH_XSLT_DEBUG_ATTRIBUTES xsltGenericDebug(xsltGenericDebugContext, "attribute set %s present already in top stylesheet" " - merging\n", name); #endif topSet = xmlHashLookup2(style->attributeSets, name, ns); if (topSet==NULL) { xsltGenericError(xsltGenericErrorContext, "xsl:attribute-set : logic error merging from imports for" " attribute-set %s\n", name); } else { topSet = xsltMergeAttrElemList(style, topSet, values); xmlHashUpdateEntry2(style->attributeSets, name, ns, topSet, NULL); } xsltFreeAttrElemList(values); #ifdef WITH_XSLT_DEBUG_ATTRIBUTES } else { xsltGenericDebug(xsltGenericDebugContext, "attribute set %s moved to top stylesheet\n", name); #endif } }
PyObject *xmlSecTransformCtx_setattr(PyObject *self, PyObject *args) { PyObject *transformCtx_obj, *value_obj; xmlSecTransformCtxPtr transformCtx; const char *name; if (CheckArgs(args, "OS?:transformCtxSetAttr")) { if (!PyArg_ParseTuple(args, "OsO:transformCtxSetAttr", &transformCtx_obj, &name, &value_obj)) return NULL; } else return NULL; transformCtx = xmlSecTransformCtxPtr_get(transformCtx_obj); if (!strcmp(name, "flags")) transformCtx->flags = PyInt_AsLong(value_obj); else if (!strcmp(name, "flags2")) transformCtx->flags2 = PyInt_AsLong(value_obj); else if (!strcmp(name, "enabledUris")) transformCtx->enabledUris = PyInt_AsLong(value_obj); else if (!strcmp(name, "enabledTransforms")) transformCtx->enabledTransforms = *(xmlSecPtrListPtr_get(value_obj)); else if (!strcmp(name, "preExecCallback")) if (value_obj != Py_None) { if (TransformCtxPreExecuteCallbacks == NULL) TransformCtxPreExecuteCallbacks = xmlHashCreate(HASH_TABLE_SIZE); xmlHashAddEntry2(TransformCtxPreExecuteCallbacks, transformCtx->uri, transformCtx->xptrExpr, value_obj); Py_XINCREF(value_obj); transformCtx->preExecCallback = xmlsec_TransformCtxPreExecuteCallback; } else transformCtx->preExecCallback = NULL; else if (!strcmp(name, "result")) transformCtx->result = xmlSecBufferPtr_get(value_obj); else if (!strcmp(name, "status")) transformCtx->status = PyInt_AsLong(value_obj); else if (!strcmp(name, "uri")) transformCtx->uri = (xmlChar *)PyString_AsString(value_obj); else if (!strcmp(name, "xptrExpr")) transformCtx->xptrExpr = (xmlChar *)PyString_AsString(value_obj); else if (!strcmp(name, "first")) transformCtx->first = xmlSecTransformPtr_get(value_obj); else if (!strcmp(name, "last")) transformCtx->last = xmlSecTransformPtr_get(value_obj); Py_INCREF(Py_None); return (Py_None); }
/** * xsltResolveSASCallback: * @set: the attribute set * @asctx: the context for attribute set resolution * @name: the local name of the attirbute set * @ns: the namespace of the attribute set * * resolve the references in an attribute set. */ static void xsltResolveSASCallback(xsltAttrSetPtr set, xsltAttrSetContextPtr asctx, const xmlChar *name, const xmlChar *ns, ATTRIBUTE_UNUSED const xmlChar *ignored) { xsltStylesheetPtr topStyle = asctx->topStyle; xsltStylesheetPtr style = asctx->style; xsltResolveAttrSet(set, topStyle, style, name, ns, 1); /* Move attribute sets to top stylesheet. */ if (style != topStyle) { /* * This imported stylesheet won't be visited anymore. Don't bother * removing the hash entry. */ if (xmlHashAddEntry2(topStyle->attributeSets, name, ns, set) < 0) { xsltGenericError(xsltGenericErrorContext, "xsl:attribute-set : internal error, can't move imported " " attribute set %s\n", name); } } }
void xsltParseStylesheetAttributeSet(xsltStylesheetPtr style, xmlNodePtr cur) { const xmlChar *ncname; const xmlChar *prefix; const xmlChar *nsUri = NULL; xmlChar *value; xmlNodePtr child; xsltAttrSetPtr set; if ((cur == NULL) || (style == NULL) || (cur->type != XML_ELEMENT_NODE)) return; value = xmlGetNsProp(cur, (const xmlChar *)"name", NULL); if ((value == NULL) || (*value == 0)) { xsltGenericError(xsltGenericErrorContext, "xsl:attribute-set : name is missing\n"); if (value) xmlFree(value); return; } if (xmlValidateQName(value, 0)) { xsltTransformError(NULL, style, cur, "xsl:attribute-set : The name '%s' is not a valid QName.\n", value); style->errors++; xmlFree(value); return; } ncname = xsltSplitQName(style->dict, value, &prefix); xmlFree(value); value = NULL; if (prefix != NULL) { xmlNsPtr ns = xmlSearchNs(style->doc, cur, prefix); if (ns == NULL) { xsltTransformError(NULL, style, cur, "xsl:attribute-set : No namespace found for QName '%s:%s'\n", prefix, ncname); style->errors++; return; } nsUri = ns->href; } if (style->attributeSets == NULL) { #ifdef WITH_XSLT_DEBUG_ATTRIBUTES xsltGenericDebug(xsltGenericDebugContext, "creating attribute set table\n"); #endif style->attributeSets = xmlHashCreate(10); } if (style->attributeSets == NULL) return; set = xmlHashLookup2(style->attributeSets, ncname, nsUri); if (set == NULL) { set = xsltNewAttrSet(); if (set == NULL) return; xmlHashAddEntry2(style->attributeSets, ncname, nsUri, set); } /* * Parse the content. Only xsl:attribute elements are allowed. */ child = cur->children; while (child != NULL) { /* * Report invalid nodes. */ if ((child->type != XML_ELEMENT_NODE) || (child->ns == NULL) || (! IS_XSLT_ELEM(child))) { if (child->type == XML_ELEMENT_NODE) xsltTransformError(NULL, style, child, "xsl:attribute-set : unexpected child %s\n", child->name); else xsltTransformError(NULL, style, child, "xsl:attribute-set : child of unexpected type\n"); } else if (!IS_XSLT_NAME(child, "attribute")) { xsltTransformError(NULL, style, child, "xsl:attribute-set : unexpected child xsl:%s\n", child->name); } else { #ifdef WITH_XSLT_DEBUG_ATTRIBUTES xsltGenericDebug(xsltGenericDebugContext, "add attribute to list %s\n", ncname); #endif if (child->psvi == NULL) { xsltTransformError(NULL, style, child, "xsl:attribute-set : internal error, attribute %s not " "compiled\n", child->name); } else { set->attrs = xsltAddAttrElemList(set->attrs, child); } } child = child->next; } /* * Process attribute "use-attribute-sets". */ value = xmlGetNsProp(cur, BAD_CAST "use-attribute-sets", NULL); if (value != NULL) { const xmlChar *curval, *endval; curval = value; while (*curval != 0) { while (IS_BLANK(*curval)) curval++; if (*curval == 0) break; endval = curval; while ((*endval != 0) && (!IS_BLANK(*endval))) endval++; curval = xmlDictLookup(style->dict, curval, endval - curval); if (curval) { const xmlChar *ncname2 = NULL; const xmlChar *prefix2 = NULL; const xmlChar *nsUri2 = NULL; #ifdef WITH_XSLT_DEBUG_ATTRIBUTES xsltGenericDebug(xsltGenericDebugContext, "xsl:attribute-set : %s adds use %s\n", ncname, curval); #endif if (xmlValidateQName(curval, 0)) { xsltTransformError(NULL, style, cur, "xsl:attribute-set : The name '%s' in " "use-attribute-sets is not a valid QName.\n", curval); style->errors++; xmlFree(value); return; } ncname2 = xsltSplitQName(style->dict, curval, &prefix2); if (prefix2 != NULL) { xmlNsPtr ns2 = xmlSearchNs(style->doc, cur, prefix2); if (ns2 == NULL) { xsltTransformError(NULL, style, cur, "xsl:attribute-set : No namespace found for QName " "'%s:%s' in use-attribute-sets\n", prefix2, ncname2); style->errors++; xmlFree(value); return; } nsUri2 = ns2->href; } set->useAttrSets = xsltAddUseAttrSetList(set->useAttrSets, ncname2, nsUri2); } curval = endval; } xmlFree(value); value = NULL; } #ifdef WITH_XSLT_DEBUG_ATTRIBUTES xsltGenericDebug(xsltGenericDebugContext, "updated attribute list %s\n", ncname); #endif }
int nominatim_import(const char *conninfo, const char *partionTagsFilename, const char *filename) { xmlTextReaderPtr reader; int ret = 0; PGresult * res; FILE * partionTagsFile; char * partionQueryName; char partionQuerySQL[1024]; conn = PQconnectdb(conninfo); if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } partionTableTagsHash = xmlHashCreate(200); partionTableTagsHashDelete = xmlHashCreate(200); partionTagsFile = fopen(partionTagsFilename, "rt"); if (!partionTagsFile) { fprintf(stderr, "Unable to read partition tags file: %s\n", partionTagsFilename); exit(EXIT_FAILURE); } char buffer[1024], osmkey[256], osmvalue[256]; int fields; while (fgets(buffer, sizeof(buffer), partionTagsFile) != NULL) { fields = sscanf( buffer, "%23s %63s", osmkey, osmvalue ); if ( fields <= 0 ) continue; if ( fields != 2 ) { fprintf( stderr, "Error partition file\n"); exit_nicely(); } partionQueryName = malloc(strlen("partition_insert_")+strlen(osmkey)+strlen(osmvalue)+2); strcpy(partionQueryName, "partition_insert_"); strcat(partionQueryName, osmkey); strcat(partionQueryName, "_"); strcat(partionQueryName, osmvalue); strcpy(partionQuerySQL, "insert into place_classtype_"); strcat(partionQuerySQL, osmkey); strcat(partionQuerySQL, "_"); strcat(partionQuerySQL, osmvalue); strcat(partionQuerySQL, " (place_id, centroid) values ($1, ST_Centroid(st_setsrid($2, 4326)))"); res = PQprepare(conn, partionQueryName, partionQuerySQL, 2, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare %s: %s\n", partionQueryName, PQerrorMessage(conn)); exit(EXIT_FAILURE); } xmlHashAddEntry2(partionTableTagsHash, BAD_CAST osmkey, BAD_CAST osmvalue, BAD_CAST partionQueryName); partionQueryName = malloc(strlen("partition_delete_")+strlen(osmkey)+strlen(osmvalue)+2); strcpy(partionQueryName, "partition_delete_"); strcat(partionQueryName, osmkey); strcat(partionQueryName, "_"); strcat(partionQueryName, osmvalue); strcpy(partionQuerySQL, "delete from place_classtype_"); strcat(partionQuerySQL, osmkey); strcat(partionQuerySQL, "_"); strcat(partionQuerySQL, osmvalue); strcat(partionQuerySQL, " where place_id = $1::integer"); res = PQprepare(conn, partionQueryName, partionQuerySQL, 1, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare %s: %s\n", partionQueryName, PQerrorMessage(conn)); exit(EXIT_FAILURE); } xmlHashAddEntry2(partionTableTagsHashDelete, BAD_CAST osmkey, BAD_CAST osmvalue, BAD_CAST partionQueryName); } res = PQprepare(conn, "get_new_place_id", "select nextval('seq_place')", 0, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare get_new_place_id: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "get_place_id", "select place_id from placex where osm_type = $1 and osm_id = $2 and class = $3 and type = $4", 4, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare get_place_id: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "placex_insert", "insert into placex (place_id,osm_type,osm_id,class,type,name,country_code,extratags,parent_place_id,admin_level,housenumber,rank_address,rank_search,geometry) " "values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, st_setsrid($14, 4326))", 12, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare placex_insert: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "search_name_insert", "insert into search_name (place_id, search_rank, address_rank, country_code, name_vector, nameaddress_vector, centroid) " "select place_id, rank_search, rank_address, country_code, make_keywords(name), " "(select uniq(sort(array_agg(parent_search_name.name_vector))) from search_name as parent_search_name where place_id in " "(select distinct address_place_id from place_addressline where place_addressline.place_id = $1 limit 1000)" "), st_centroid(geometry) from placex " "where place_id = $1", 1, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare search_name_insert: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "search_name_from_parent_insert", "insert into search_name (place_id, search_rank, address_rank, country_code, name_vector, nameaddress_vector, centroid) " "select place_id, rank_search, rank_address, country_code, make_keywords(name), " "(select uniq(sort(name_vector+nameaddress_vector)) from search_name as parent_search_name " "where parent_search_name.place_id = $2 ), st_centroid(geometry) from placex " "where place_id = $1", 2, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare search_name_insert: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "place_addressline_insert", "insert into place_addressline (place_id, address_place_id, fromarea, isaddress, distance, cached_rank_address) " "select $1, place_id, false, $7, $2, rank_address from placex where osm_type = $3 and osm_id = $4 and class = $5 and type = $6", 7, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare place_addressline_insert: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "placex_delete", "delete from placex where place_id = $1", 1, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare placex_delete: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "search_name_delete", "delete from search_name where place_id = $1", 1, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare search_name_delete: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "place_addressline_delete", "delete from place_addressline where place_id = $1", 1, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare place_addressline_delete: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } featureCount = 0; reader = inputUTF8(filename); if (reader == NULL) { fprintf(stderr, "Unable to open %s\n", filename); return 1; } ret = xmlTextReaderRead(reader); while (ret == 1) { processNode(reader); ret = xmlTextReaderRead(reader); } if (ret != 0) { fprintf(stderr, "%s : failed to parse\n", filename); return ret; } xmlFreeTextReader(reader); xmlHashFree(partionTableTagsHash, NULL); xmlHashFree(partionTableTagsHashDelete, NULL); return 0; }