static int xmlSecAppAddIDAttr(xmlNodePtr node, const xmlChar* attrName, const xmlChar* nodeName, const xmlChar* nsHref) { xmlAttrPtr attr, tmpAttr; xmlNodePtr cur; xmlChar* id; if((node == NULL) || (attrName == NULL) || (nodeName == NULL)) { return(-1); } /* process children first because it does not matter much but does simplify code */ cur = xmlSecGetNextElementNode(node->children); while(cur != NULL) { if(xmlSecAppAddIDAttr(cur, attrName, nodeName, nsHref) < 0) { return(-1); } cur = xmlSecGetNextElementNode(cur->next); } /* node name must match */ if(!xmlStrEqual(node->name, nodeName)) { return(0); } /* if nsHref is set then it also should match */ if((nsHref != NULL) && (node->ns != NULL) && (!xmlStrEqual(nsHref, node->ns->href))) { return(0); } /* the attribute with name equal to attrName should exist */ for(attr = node->properties; attr != NULL; attr = attr->next) { if(xmlStrEqual(attr->name, attrName)) { break; } } if(attr == NULL) { return(0); } /* and this attr should have a value */ id = xmlNodeListGetString(node->doc, attr->children, 1); if(id == NULL) { return(0); } /* check that we don't have same ID already */ tmpAttr = xmlGetID(node->doc, id); if(tmpAttr == NULL) { xmlAddID(NULL, node->doc, id, attr); } else if(tmpAttr != attr) { fprintf(stderr, "XmlSecError: duplicate ID attribute \"%s\"\n", id); xmlFree(id); return(-1); } xmlFree(id); return(0); }
void XSLStyleSheet::loadChildSheets() { if (!document()) return; xmlNodePtr stylesheetRoot = document()->children; // Top level children may include other things such as DTD nodes, we ignore // those. while (stylesheetRoot && stylesheetRoot->type != XML_ELEMENT_NODE) stylesheetRoot = stylesheetRoot->next; if (m_embedded) { // We have to locate (by ID) the appropriate embedded stylesheet // element, so that we can walk the import/include list. xmlAttrPtr idNode = xmlGetID(document(), (const xmlChar*)(finalURL().string().utf8().data())); if (!idNode) return; stylesheetRoot = idNode->parent; } else { // FIXME: Need to handle an external URI with a # in it. This is a // pretty minor edge case, so we'll deal with it later. } if (stylesheetRoot) { // Walk the children of the root element and look for import/include // elements. Imports must occur first. xmlNodePtr curr = stylesheetRoot->children; while (curr) { if (curr->type != XML_ELEMENT_NODE) { curr = curr->next; continue; } if (IS_XSLT_ELEM(curr) && IS_XSLT_NAME(curr, "import")) { xmlChar* uriRef = xsltGetNsProp(curr, (const xmlChar*)"href", XSLT_NAMESPACE); loadChildSheet(String::fromUTF8((const char*)uriRef)); xmlFree(uriRef); } else { break; } curr = curr->next; } // Now handle includes. while (curr) { if (curr->type == XML_ELEMENT_NODE && IS_XSLT_ELEM(curr) && IS_XSLT_NAME(curr, "include")) { xmlChar* uriRef = xsltGetNsProp(curr, (const xmlChar*)"href", XSLT_NAMESPACE); loadChildSheet(String::fromUTF8((const char*)uriRef)); xmlFree(uriRef); } curr = curr->next; } } }
static int xmlSecTransformVisa3DHackExecute(xmlSecTransformPtr transform, int last, xmlSecTransformCtxPtr transformCtx) { xmlChar** idPtr; xmlDocPtr doc; xmlAttrPtr attr; xmlNodeSetPtr nodeSet; xmlSecAssert2(xmlSecTransformVisa3DHackCheckId(transform), -1); xmlSecAssert2(transform->outNodes == NULL, -1); xmlSecAssert2(last != 0, -1); xmlSecAssert2(transformCtx != NULL, -1); idPtr = xmlSecVisa3DHackTransformGetIDPtr(transform); xmlSecAssert2(idPtr != NULL, -1); xmlSecAssert2((*idPtr) != NULL, -1); doc = (transform->inNodes != NULL) ? transform->inNodes->doc : transform->hereNode->doc; xmlSecAssert2(doc != NULL, -1); attr = xmlGetID(doc, (*idPtr)); if((attr == NULL) || (attr->parent == NULL)) { xmlSecError(XMLSEC_ERRORS_HERE, xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), "xmlGetID", XMLSEC_ERRORS_R_XML_FAILED, "id=\"%s\"", xmlSecErrorsSafeString((*idPtr))); return(-1); } nodeSet = xmlXPathNodeSetCreate(attr->parent); if(nodeSet == NULL) { xmlSecError(XMLSEC_ERRORS_HERE, xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), "xmlXPathNodeSetCreate", XMLSEC_ERRORS_R_XML_FAILED, "id=\"%s\"", xmlSecErrorsSafeString((*idPtr))); return(-1); } transform->outNodes = xmlSecNodeSetCreate(doc, nodeSet, xmlSecNodeSetTreeWithoutComments); if(transform->outNodes == NULL) { xmlSecError(XMLSEC_ERRORS_HERE, xmlSecErrorsSafeString(xmlSecTransformGetName(transform)), "xmlSecNodeSetCreate", XMLSEC_ERRORS_R_XMLSEC_FAILED, XMLSEC_ERRORS_NO_MESSAGE); xmlXPathFreeNodeSet(nodeSet); return(-1); } return(0); }
int assign_id_attributes(xmlDocPtr doc) { // Assume the ID attribute is one of (ID | Id | id) and tell this to libxml xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { xmlFreeDoc(doc); rb_raise(rb_eRuntimeError,"Error: unable to create new XPath context\n"); return(-1); } xmlChar* xpathExpr = "//*[@ID | @Id | @id]"; xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); if(xpathObj == NULL) { xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); rb_raise(rb_eRuntimeError,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr); return(-1); } xmlNodeSetPtr nodes = xpathObj->nodesetval; int size = (nodes) ? nodes->nodeNr : 0; char* idNames[] = {"ID", "Id", "id"}; xmlAttrPtr attr, tmp; int i,j; for(i = 0; i < size; i++) { for(j=0; j<3;j++) { tmp = xmlHasProp(nodes->nodeTab[i], idNames[j]); if(tmp != NULL) attr = tmp; } if(attr == NULL) { xmlXPathFreeContext(xpathCtx); return(-1); } xmlChar* name = xmlNodeListGetString(doc, attr->children, 1); if(name == NULL) { xmlXPathFreeContext(xpathCtx); return(-1); } xmlAttrPtr tmp = xmlGetID(doc, name); if(tmp != NULL) { xmlFree(name); return 0; } xmlAddID(NULL, doc, name, attr); xmlFree(name); } xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); }