CPLXMLNode* GDALGMLJP2GenerateMetadata( const CPLString& osTemplateFile, const CPLString& osSourceFile ) { GByte* pabyStr = nullptr; if( !VSIIngestFile( nullptr, osTemplateFile, &pabyStr, nullptr, -1 ) ) return nullptr; CPLString osTemplate(reinterpret_cast<char *>(pabyStr)); CPLFree(pabyStr); if( !VSIIngestFile( nullptr, osSourceFile, &pabyStr, nullptr, -1 ) ) return nullptr; CPLString osSource(reinterpret_cast<char *>(pabyStr)); CPLFree(pabyStr); xmlDocPtr pDoc = xmlParseDoc( reinterpret_cast<const xmlChar *>(osSource.c_str())); if( pDoc == nullptr ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot parse %s", osSourceFile.c_str()); return nullptr; } xmlXPathContextPtr pXPathCtx = xmlXPathNewContext(pDoc); if( pXPathCtx == nullptr ) { xmlFreeDoc(pDoc); return nullptr; } xmlXPathRegisterFunc(pXPathCtx, reinterpret_cast<const xmlChar *>("if"), GDALGMLJP2XPathIf); xmlXPathRegisterFunc(pXPathCtx, reinterpret_cast<const xmlChar *>("uuid"), GDALGMLJP2XPathUUID); pXPathCtx->error = GDALGMLJP2XPathErrorHandler; GDALGMLJP2RegisterNamespaces(pXPathCtx, xmlDocGetRootElement(pDoc)); CPLString osXMLRes = GDALGMLJP2EvalExpr(osTemplate, pXPathCtx, pDoc); xmlXPathFreeContext(pXPathCtx); xmlFreeDoc(pDoc); return CPLParseXMLString(osXMLRes); }
/** * xsltRegisterAllFunctions: * @ctxt: the XPath context * * Registers all default XSLT functions in this context */ void xsltRegisterAllFunctions(xmlXPathContextPtr ctxt) { xmlXPathRegisterFunc(ctxt, (const xmlChar *) "current", xsltCurrentFunction); xmlXPathRegisterFunc(ctxt, (const xmlChar *) "document", xsltDocumentFunction); xmlXPathRegisterFunc(ctxt, (const xmlChar *) "key", xsltKeyFunction); xmlXPathRegisterFunc(ctxt, (const xmlChar *) "unparsed-entity-uri", xsltUnparsedEntityURIFunction); xmlXPathRegisterFunc(ctxt, (const xmlChar *) "format-number", xsltFormatNumberFunction); xmlXPathRegisterFunc(ctxt, (const xmlChar *) "generate-id", xsltGenerateIdFunction); xmlXPathRegisterFunc(ctxt, (const xmlChar *) "system-property", xsltSystemPropertyFunction); xmlXPathRegisterFunc(ctxt, (const xmlChar *) "element-available", xsltElementAvailableFunction); xmlXPathRegisterFunc(ctxt, (const xmlChar *) "function-available", xsltFunctionAvailableFunction); }
HRESULT create_selection(xmlNodePtr node, xmlChar* query, IXMLDOMNodeList **out) { domselection *This = heap_alloc(sizeof(domselection)); xmlXPathContextPtr ctxt = xmlXPathNewContext(node->doc); HRESULT hr; TRACE("(%p, %s, %p)\n", node, wine_dbgstr_a((char const*)query), out); *out = NULL; if (!This || !ctxt || !query) { xmlXPathFreeContext(ctxt); heap_free(This); return E_OUTOFMEMORY; } This->IXMLDOMSelection_iface.lpVtbl = &domselection_vtbl; This->ref = 1; This->resultPos = 0; This->node = node; This->enumvariant = NULL; init_dispex(&This->dispex, (IUnknown*)&This->IXMLDOMSelection_iface, &domselection_dispex); xmldoc_add_ref(This->node->doc); ctxt->error = query_serror; ctxt->node = node; registerNamespaces(ctxt); if (is_xpathmode(This->node->doc)) { xmlXPathRegisterAllFunctions(ctxt); This->result = xmlXPathEvalExpression(query, ctxt); } else { xmlChar* pattern_query = XSLPattern_to_XPath(ctxt, query); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"not", xmlXPathNotFunction); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"boolean", xmlXPathBooleanFunction); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"index", XSLPattern_index); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"end", XSLPattern_end); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"nodeType", XSLPattern_nodeType); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_IEq", XSLPattern_OP_IEq); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_INEq", XSLPattern_OP_INEq); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_ILt", XSLPattern_OP_ILt); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_ILEq", XSLPattern_OP_ILEq); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_IGt", XSLPattern_OP_IGt); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_IGEq", XSLPattern_OP_IGEq); This->result = xmlXPathEvalExpression(pattern_query, ctxt); xmlFree(pattern_query); } if (!This->result || This->result->type != XPATH_NODESET) { hr = E_FAIL; goto cleanup; } *out = (IXMLDOMNodeList*)&This->IXMLDOMSelection_iface; hr = S_OK; TRACE("found %d matches\n", xmlXPathNodeSetGetLength(This->result->nodesetval)); cleanup: if (This && FAILED(hr)) IXMLDOMSelection_Release( &This->IXMLDOMSelection_iface ); xmlXPathFreeContext(ctxt); return hr; }
HRESULT queryresult_create(xmlNodePtr node, xmlChar* szQuery, IXMLDOMNodeList **out) { queryresult *This = heap_alloc_zero(sizeof(queryresult)); xmlXPathContextPtr ctxt = xmlXPathNewContext(node->doc); HRESULT hr; TRACE("(%p, %s, %p)\n", node, wine_dbgstr_a((char const*)szQuery), out); *out = NULL; if (This == NULL || ctxt == NULL || szQuery == NULL) { hr = E_OUTOFMEMORY; goto cleanup; } This->lpVtbl = &queryresult_vtbl; This->ref = 1; This->resultPos = 0; This->node = node; xmldoc_add_ref(This->node->doc); ctxt->error = query_serror; ctxt->node = node; registerNamespaces(ctxt); if (is_xpathmode(This->node->doc)) { xmlXPathRegisterAllFunctions(ctxt); This->result = xmlXPathEvalExpression(szQuery, ctxt); } else { xmlChar* xslpQuery = XSLPattern_to_XPath(ctxt, szQuery); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"not", xmlXPathNotFunction); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"boolean", xmlXPathBooleanFunction); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"index", XSLPattern_index); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"end", XSLPattern_end); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"nodeType", XSLPattern_nodeType); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_IEq", XSLPattern_OP_IEq); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_INEq", XSLPattern_OP_INEq); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_ILt", XSLPattern_OP_ILt); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_ILEq", XSLPattern_OP_ILEq); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_IGt", XSLPattern_OP_IGt); xmlXPathRegisterFunc(ctxt, (xmlChar const*)"OP_IGEq", XSLPattern_OP_IGEq); This->result = xmlXPathEvalExpression(xslpQuery, ctxt); xmlFree(xslpQuery); } if (!This->result || This->result->type != XPATH_NODESET) { hr = E_FAIL; goto cleanup; } init_dispex(&This->dispex, (IUnknown*)&This->lpVtbl, &queryresult_dispex); *out = (IXMLDOMNodeList *) &This->lpVtbl; hr = S_OK; TRACE("found %d matches\n", xmlXPathNodeSetGetLength(This->result->nodesetval)); cleanup: if (This != NULL && FAILED(hr)) IXMLDOMNodeList_Release( (IXMLDOMNodeList*) &This->lpVtbl ); xmlXPathFreeContext(ctxt); return hr; }
static xmlSecNodeSetPtr xmlSecXPathDataExecute(xmlSecXPathDataPtr data, xmlDocPtr doc, xmlNodePtr hereNode) { xmlXPathObjectPtr xpathObj = NULL; xmlSecNodeSetPtr nodes; xmlSecAssert2(data != NULL, NULL); xmlSecAssert2(data->expr != NULL, NULL); xmlSecAssert2(data->ctx != NULL, NULL); xmlSecAssert2(doc != NULL, NULL); xmlSecAssert2(hereNode != NULL, NULL); /* do not forget to set the doc */ data->ctx->doc = doc; /* here function works only on the same document */ if(hereNode->doc == doc) { xmlXPathRegisterFunc(data->ctx, (xmlChar *)"here", xmlSecXPathHereFunction); data->ctx->here = hereNode; data->ctx->xptr = 1; } /* execute xpath or xpointer expression */ switch(data->type) { case xmlSecXPathDataTypeXPath: case xmlSecXPathDataTypeXPath2: xpathObj = xmlXPathEvalExpression(data->expr, data->ctx); if(xpathObj == NULL) { xmlSecError(XMLSEC_ERRORS_HERE, NULL, "xmlXPathEvalExpression", XMLSEC_ERRORS_R_XML_FAILED, "expr=%s", xmlSecErrorsSafeString(data->expr)); return(NULL); } break; case xmlSecXPathDataTypeXPointer: xpathObj = xmlXPtrEval(data->expr, data->ctx); if(xpathObj == NULL) { xmlSecError(XMLSEC_ERRORS_HERE, NULL, "xmlXPtrEval", XMLSEC_ERRORS_R_XML_FAILED, "expr=%s", xmlSecErrorsSafeString(data->expr)); return(NULL); } break; } /* sometime LibXML2 returns an empty nodeset or just NULL, we want to reserve NULL for our own purposes so we simply create an empty node set here */ if(xpathObj->nodesetval == NULL) { xpathObj->nodesetval = xmlXPathNodeSetCreate(NULL); if(xpathObj->nodesetval == NULL) { xmlXPathFreeObject(xpathObj); xmlSecError(XMLSEC_ERRORS_HERE, NULL, "xmlXPathNodeSetCreate", XMLSEC_ERRORS_R_XML_FAILED, "expr=%s", xmlSecErrorsSafeString(data->expr)); return(NULL); } } nodes = xmlSecNodeSetCreate(doc, xpathObj->nodesetval, data->nodeSetType); if(nodes == NULL) { xmlSecError(XMLSEC_ERRORS_HERE, NULL, "xmlSecNodeSetCreate", XMLSEC_ERRORS_R_XMLSEC_FAILED, "type=%d", data->nodeSetType); xmlXPathFreeObject(xpathObj); return(NULL); } xpathObj->nodesetval = NULL; xmlXPathFreeObject(xpathObj); return(nodes); }