/* * call-seq: * create_internal_subset(name, external_id, system_id) * * Create the internal subset of a document. * * doc.create_internal_subset("chapter", "-//OASIS//DTD DocBook XML//EN", "chapter.dtd") * # => <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML//EN" "chapter.dtd"> * * doc.create_internal_subset("chapter", nil, "chapter.dtd") * # => <!DOCTYPE chapter SYSTEM "chapter.dtd"> */ static VALUE create_internal_subset(VALUE self, VALUE name, VALUE external_id, VALUE system_id) { xmlNodePtr node; xmlDocPtr doc; xmlDtdPtr dtd; Data_Get_Struct(self, xmlNode, node); doc = node->doc; if(xmlGetIntSubset(doc)) { rb_raise(rb_eRuntimeError, "Document already has an internal subset"); } dtd = xmlCreateIntSubset( doc, NIL_P(name) ? NULL : (const xmlChar *)StringValueCStr(name), NIL_P(external_id) ? NULL : (const xmlChar *)StringValueCStr(external_id), NIL_P(system_id) ? NULL : (const xmlChar *)StringValueCStr(system_id) ); if(!dtd) { return Qnil; } return Nokogiri_wrap_xml_node(Qnil, (xmlNodePtr)dtd); }
/* * call-seq: * document.xhtml? -> (true|false) * * Determine whether this is an XHTML document. */ static VALUE rxml_document_xhtml_q(VALUE self) { xmlDocPtr xdoc; xmlDtdPtr xdtd; Data_Get_Struct(self, xmlDoc, xdoc); xdtd = xmlGetIntSubset(xdoc); if (xdtd != NULL && xmlIsXHTML(xdtd->SystemID, xdtd->ExternalID) > 0) return (Qtrue); else return (Qfalse); }
/* * call-seq: * internal_subset * * Get the internal subset */ static VALUE internal_subset(VALUE self) { xmlNodePtr node; xmlDocPtr doc; Data_Get_Struct(self, xmlNode, node); if(!node->doc) return Qnil; doc = node->doc; xmlDtdPtr dtd = xmlGetIntSubset(doc); if(!dtd) return Qnil; return Nokogiri_wrap_xml_node((xmlNodePtr)dtd); }
static HRESULT WINAPI xmldoc_get_doctype(IXMLDocument *iface, BSTR *p) { xmldoc *This = impl_from_IXMLDocument(iface); xmlDtd *dtd; TRACE("(%p, %p)\n", This, p); if (!p) return E_INVALIDARG; dtd = xmlGetIntSubset(This->xmldoc); if (!dtd) return S_FALSE; *p = bstr_from_xmlChar(dtd->name); CharUpperBuffW(*p, SysStringLen(*p)); return S_OK; }
/* {{{ internalSubset string readonly=yes URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-Core-DocType-internalSubset Since: DOM Level 2 */ int dom_documenttype_internal_subset_read(dom_object *obj, zval *retval) { xmlDtdPtr dtdptr = (xmlDtdPtr) dom_object_get_node(obj); xmlDtdPtr intsubset; if (dtdptr == NULL) { php_dom_throw_error(INVALID_STATE_ERR, 0); return FAILURE; } if (dtdptr->doc != NULL && ((intsubset = xmlGetIntSubset(dtdptr->doc)) != NULL)) { smart_str ret_buf = {0}; xmlNodePtr cur = intsubset->children; while (cur != NULL) { xmlOutputBuffer *buff = xmlAllocOutputBuffer(NULL); if (buff != NULL) { xmlNodeDumpOutput (buff, NULL, cur, 0, 0, NULL); xmlOutputBufferFlush(buff); #ifdef LIBXML2_NEW_BUFFER smart_str_appendl(&ret_buf, (const char *) xmlOutputBufferGetContent(buff), xmlOutputBufferGetSize(buff)); #else smart_str_appendl(&ret_buf, (char *) buff->buffer->content, buff->buffer->use); #endif (void)xmlOutputBufferClose(buff); } cur = cur->next; } if (ret_buf.s) { smart_str_0(&ret_buf); ZVAL_NEW_STR(retval, ret_buf.s); return SUCCESS; } } ZVAL_NULL(retval); return SUCCESS; }
static int hwloc_libxml_look_init(struct hwloc_xml_backend_data_s *bdata, struct hwloc__xml_import_state_s *state) { hwloc__libxml_import_state_data_t lstate = (void*) state->data; xmlNode* root_node; xmlDtd *dtd; assert(sizeof(*lstate) <= sizeof(state->data)); dtd = xmlGetIntSubset((xmlDoc*) bdata->data); if (!dtd) { if (hwloc__xml_verbose()) fprintf(stderr, "Loading XML topology without DTD\n"); } else if (strcmp((char *) dtd->SystemID, "hwloc.dtd")) { if (hwloc__xml_verbose()) fprintf(stderr, "Loading XML topology with wrong DTD SystemID (%s instead of %s)\n", (char *) dtd->SystemID, "hwloc.dtd"); } root_node = xmlDocGetRootElement((xmlDoc*) bdata->data); if (strcmp((const char *) root_node->name, "topology") && strcmp((const char *) root_node->name, "root")) { /* root node should be in "topology" class (or "root" if importing from < 1.0) */ if (hwloc__xml_verbose()) fprintf(stderr, "ignoring object of class `%s' not at the top the xml hierarchy\n", (const char *) root_node->name); goto failed; } state->global->next_attr = hwloc__libxml_import_next_attr; state->global->find_child = hwloc__libxml_import_find_child; state->global->close_tag = hwloc__libxml_import_close_tag; state->global->close_child = hwloc__libxml_import_close_child; state->global->get_content = hwloc__libxml_import_get_content; state->global->close_content = hwloc__libxml_import_close_content; state->parent = NULL; lstate->node = root_node; lstate->child = root_node->children; lstate->attr = NULL; return 0; /* success */ failed: return -1; /* failed */ }
/* SOAP client calls this function to parse response from SOAP server */ bool parse_packet_soap(SoapClient *obj, const char *buffer, int buffer_size, std::shared_ptr<sdlFunction> fn, const char *fn_name, Variant &return_value, Array& soap_headers) { char* envelope_ns = nullptr; xmlNodePtr trav, env, head, body, resp, cur, fault; xmlAttrPtr attr; int param_count = 0; int soap_version = SOAP_1_1; sdlSoapBindingFunctionHeaderMap *hdrs = nullptr; assert(return_value.asTypedValue()->m_type == KindOfUninit); return_value.asTypedValue()->m_type = KindOfNull; /* Response for one-way opearation */ if (buffer_size == 0) { return true; } /* Parse XML packet */ xmlDocPtr response = soap_xmlParseMemory(buffer, buffer_size); if (!response) { add_soap_fault(obj, "Client", "looks like we got no XML document"); return false; } if (xmlGetIntSubset(response) != nullptr) { add_soap_fault(obj, "Client", "DTD are not supported by SOAP"); xmlFreeDoc(response); return false; } /* Get <Envelope> element */ env = nullptr; trav = response->children; while (trav != nullptr) { if (trav->type == XML_ELEMENT_NODE) { if (!env && node_is_equal_ex(trav,"Envelope", SOAP_1_1_ENV_NAMESPACE)) { env = trav; envelope_ns = SOAP_1_1_ENV_NAMESPACE; soap_version = SOAP_1_1; } else if (!env && node_is_equal_ex(trav, "Envelope", SOAP_1_2_ENV_NAMESPACE)) { env = trav; envelope_ns = SOAP_1_2_ENV_NAMESPACE; soap_version = SOAP_1_2; } else { add_soap_fault(obj, "VersionMismatch", "Wrong Version"); xmlFreeDoc(response); return false; } } trav = trav->next; } if (env == nullptr) { add_soap_fault(obj, "Client", "looks like we got XML without \"Envelope\" element"); xmlFreeDoc(response); return false; } attr = env->properties; while (attr != nullptr) { if (attr->ns == nullptr) { add_soap_fault(obj, "Client", "A SOAP Envelope element cannot have non Namespace " "qualified attributes"); xmlFreeDoc(response); return false; } if (attr_is_equal_ex(attr, "encodingStyle", SOAP_1_2_ENV_NAMESPACE)) { if (soap_version == SOAP_1_2) { add_soap_fault(obj, "Client", "encodingStyle cannot be specified on the Envelope"); xmlFreeDoc(response); return false; } if (strcmp((char*)attr->children->content, SOAP_1_1_ENC_NAMESPACE)) { add_soap_fault(obj, "Client", "Unknown data encoding style"); xmlFreeDoc(response); return false; } } attr = attr->next; } /* Get <Header> element */ head = nullptr; trav = env->children; while (trav != nullptr && trav->type != XML_ELEMENT_NODE) { trav = trav->next; } if (trav != nullptr && node_is_equal_ex(trav,"Header",envelope_ns)) { head = trav; trav = trav->next; } /* Get <Body> element */ body = nullptr; while (trav != nullptr && trav->type != XML_ELEMENT_NODE) { trav = trav->next; } if (trav != nullptr && node_is_equal_ex(trav,"Body",envelope_ns)) { body = trav; trav = trav->next; } while (trav != nullptr && trav->type != XML_ELEMENT_NODE) { trav = trav->next; } if (body == nullptr) { add_soap_fault(obj, "Client", "Body must be present in a SOAP envelope"); xmlFreeDoc(response); return false; } attr = body->properties; while (attr != nullptr) { if (attr->ns == nullptr) { if (soap_version == SOAP_1_2) { add_soap_fault(obj, "Client", "A SOAP Body element cannot have non Namespace " "qualified attributes"); xmlFreeDoc(response); return false; } } else if (attr_is_equal_ex(attr,"encodingStyle",SOAP_1_2_ENV_NAMESPACE)) { if (soap_version == SOAP_1_2) { add_soap_fault(obj, "Client", "encodingStyle cannot be specified on the Body"); xmlFreeDoc(response); return false; } if (strcmp((char*)attr->children->content, SOAP_1_1_ENC_NAMESPACE)) { add_soap_fault(obj, "Client", "Unknown data encoding style"); xmlFreeDoc(response); return false; } } attr = attr->next; } if (trav != nullptr && soap_version == SOAP_1_2) { add_soap_fault(obj, "Client", "A SOAP 1.2 envelope can contain only Header and Body"); xmlFreeDoc(response); return false; } if (head != nullptr) { attr = head->properties; while (attr != nullptr) { if (attr->ns == nullptr) { add_soap_fault(obj, "Client", "A SOAP Header element cannot have non Namespace " "qualified attributes"); xmlFreeDoc(response); return false; } if (attr_is_equal_ex(attr, "encodingStyle", SOAP_1_2_ENV_NAMESPACE)) { if (soap_version == SOAP_1_2) { add_soap_fault(obj, "Client", "encodingStyle cannot be specified on the Header"); xmlFreeDoc(response); return false; } if (strcmp((char*)attr->children->content, SOAP_1_1_ENC_NAMESPACE)) { add_soap_fault(obj, "Client", "Unknown data encoding style"); xmlFreeDoc(response); return false; } } attr = attr->next; } } /* Check if <Body> contains <Fault> element */ fault = get_node_ex(body->children,"Fault",envelope_ns); if (fault != nullptr) { char *faultcode = nullptr; String faultstring, faultactor; Variant details; xmlNodePtr tmp; if (soap_version == SOAP_1_1) { tmp = get_node(fault->children, "faultcode"); if (tmp != nullptr && tmp->children != nullptr) { faultcode = (char*)tmp->children->content; } tmp = get_node(fault->children, "faultstring"); if (tmp != nullptr && tmp->children != nullptr) { Variant zv = master_to_zval(get_conversion(KindOfString), tmp); faultstring = zv.toString(); } tmp = get_node(fault->children, "faultactor"); if (tmp != nullptr && tmp->children != nullptr) { Variant zv = master_to_zval(get_conversion(KindOfString), tmp); faultactor = zv.toString(); } tmp = get_node(fault->children, "detail"); if (tmp != nullptr) { details = master_to_zval(encodePtr(), tmp); } } else { tmp = get_node(fault->children, "Code"); if (tmp != nullptr && tmp->children != nullptr) { tmp = get_node(tmp->children, "Value"); if (tmp != nullptr && tmp->children != nullptr) { faultcode = (char*)tmp->children->content; } } tmp = get_node(fault->children,"Reason"); if (tmp != nullptr && tmp->children != nullptr) { /* TODO: lang attribute */ tmp = get_node(tmp->children,"Text"); if (tmp != nullptr && tmp->children != nullptr) { Variant zv = master_to_zval(get_conversion(KindOfString), tmp); faultstring = zv.toString(); } } tmp = get_node(fault->children,"Detail"); if (tmp != nullptr) { details = master_to_zval(encodePtr(), tmp); } } obj->m_soap_fault = SystemLib::AllocSoapFaultObject(String(faultcode, CopyString), faultstring, faultactor, details); xmlFreeDoc(response); return false; } /* Parse content of <Body> element */ return_value = Array::Create(); resp = body->children; while (resp != nullptr && resp->type != XML_ELEMENT_NODE) { resp = resp->next; } if (resp != nullptr) { if (fn && fn->binding && fn->binding->bindingType == BINDING_SOAP) { /* Function has WSDL description */ sdlParamPtr h_param, param; xmlNodePtr val = nullptr; const char *name, *ns = nullptr; Variant tmp(Variant::NullInit{}); sdlSoapBindingFunctionPtr fnb = (sdlSoapBindingFunctionPtr)fn->bindingAttributes; int res_count; hdrs = &fnb->output.headers; if (!fn->responseParameters.empty()) { res_count = fn->responseParameters.size(); for (unsigned int i = 0; i < fn->responseParameters.size(); i++) { h_param = fn->responseParameters[i]; param = h_param; if (fnb->style == SOAP_DOCUMENT) { if (param->element) { name = param->element->name.c_str(); ns = param->element->namens.c_str(); /* name = param->encode->details.type_str; ns = param->encode->details.ns; */ } else { name = param->paramName.c_str(); } } else { name = fn->responseName.c_str(); /* ns = ? */ } /* Get value of parameter */ cur = get_node_ex(resp, (char*)name, (char*)ns); if (!cur) { cur = get_node(resp, (char*)name); /* TODO: produce warning invalid ns */ } if (!cur && fnb->style == SOAP_RPC) { cur = resp; } if (cur) { if (fnb->style == SOAP_DOCUMENT) { val = cur; } else { val = get_node(cur->children, (char*)param->paramName.c_str()); if (res_count == 1) { if (val == nullptr) { val = get_node(cur->children, "return"); } if (val == nullptr) { val = get_node(cur->children, "result"); } if (val == nullptr && cur->children && !cur->children->next) { val = cur->children; } } } } if (!val) { /* TODO: may be "nil" is not OK? */ /* add_soap_fault(obj, "Client", "Can't find response data"); xmlFreeDoc(response); return false; */ } else { /* Decoding value of parameter */ if (param != nullptr) { tmp = master_to_zval(param->encode, val); } else { tmp = master_to_zval(encodePtr(), val); } } return_value.toArrRef().set(String(param->paramName), tmp); param_count++; } } } else { /* Function hasn't WSDL description */ xmlNodePtr val; val = resp->children; while (val != nullptr) { while (val && val->type != XML_ELEMENT_NODE) { val = val->next; } if (val != nullptr) { if (!node_is_equal_ex(val,"result",RPC_SOAP12_NAMESPACE)) { Variant tmp = master_to_zval(encodePtr(), val); if (val->name) { String key((char*)val->name, CopyString); if (return_value.toCArrRef().exists(key)) { auto& lval = return_value.toArrRef().lvalAt(key); if (!lval.isArray()) lval = lval.toArray(); lval.toArrRef().append(tmp); } else if (val->next && get_node(val->next, (char*)val->name)) { Array arr = Array::Create(); arr.append(tmp); return_value.toArrRef().set(key, arr); } else { return_value.toArrRef().set(key, tmp); } } else { return_value.toArrRef().append(tmp); } ++param_count; } val = val->next; } } } } if (return_value.isArray()) { if (param_count == 0) { return_value = init_null(); } else if (param_count == 1) { Array arr = return_value.toArray(); ArrayIter iter(arr); return_value = iter.second(); } } if (head) { trav = head->children; while (trav) { if (trav->type == XML_ELEMENT_NODE) { encodePtr enc; if (hdrs && !hdrs->empty()) { std::string key; if (trav->ns) { key += (char*)trav->ns->href; key += ':'; } key += (char*)trav->name; sdlSoapBindingFunctionHeaderMap::const_iterator iter = hdrs->find(key); if (iter != hdrs->end()) { enc = iter->second->encode; } } soap_headers.set(String((char*)trav->name, CopyString), master_to_zval(enc, trav)); } trav = trav->next; } } xmlFreeDoc(response); return true; }
int main(int argc, char **argv) { int ch; char *encoding = "utf8"; char *filename; int quiet = 0; int exitcode = EXIT_OK; xmlDocPtr doc = NULL; xmlDtdPtr dtd = NULL; xmlCharEncoding enc; xmlCharEncodingHandlerPtr enchandler; xmlOutputBufferPtr out; /* LibXML version checking */ LIBXML_TEST_VERSION; /* Who am I? */ if ((progname = rindex(argv[0], '/'))) progname++; else progname = argv[0]; /* Parsing CLI */ while ((ch = getopt(argc, argv, "hqe:")) != -1) switch (ch) { case 'q': quiet = 1; break; case 'e': encoding = optarg; break; case 'h': /* FALL THROUGH */ case '?': usage(EXIT_OK); break; default: usage(EXIT_USAGE); break; } argc -= optind; argv += optind; switch (argc) { case 1: filename = argv[0]; break; case 0: filename = "-"; break; default: usage(EXIT_USAGE); } /* Get user encoding */ if ((enc = xmlParseCharEncoding(encoding)) == XML_CHAR_ENCODING_ERROR) { fprintf(stderr, "Unknown encoding: %s\n", encoding); usage(EXIT_USAGE); } enchandler = xmlGetCharEncodingHandler(enc); /* Shut up */ xmlSetGenericErrorFunc(NULL, &noerror_handler); xmlGetWarningsDefaultValue = 0; xmlPedanticParserDefault(0); /* File exists? (race condition below) */ if (strcmp(filename, "-") && (open(filename, O_RDONLY) < 0)) { if (!quiet) fprintf(stderr, "ERROR: unable to open %s\n", filename); exitcode = EXIT_NOFILE; goto exit; } /* Parse document */ if ((doc = xmlParseFile(filename)) == NULL) { if (!quiet) fprintf(stderr, "ERROR: badly formed document\n"); exitcode = EXIT_BADXML; goto exit; } /* Extract DTD (if any) */ dtd = xmlGetIntSubset(doc); /* Create output buffer */ if ((out = xmlOutputBufferCreateFd(1, enchandler)) == NULL) { if (!quiet) fprintf(stderr, "ERROR: unable to open output channel\n"); exitcode = EXIT_INTERNAL; goto exit; } /* Dump information */ if (doc->version) { xmlOutputBufferWriteString(out, "Version : "); xmlOutputBufferWriteString(out, doc->version); xmlOutputBufferWriteString(out, "\n"); } if (doc->encoding) { xmlOutputBufferWriteString(out, "Encoding : "); xmlOutputBufferWriteString(out, doc->encoding); xmlOutputBufferWriteString(out, "\n"); } if (dtd && dtd->name) { xmlOutputBufferWriteString(out, "Name : "); xmlOutputBufferWriteString(out, dtd->name); xmlOutputBufferWriteString(out, "\n"); } if (dtd && dtd->ExternalID) { xmlOutputBufferWriteString(out, "Identifier: "); xmlOutputBufferWriteString(out, dtd->ExternalID); xmlOutputBufferWriteString(out, "\n"); } if (dtd && dtd->SystemID) { xmlOutputBufferWriteString(out, "URI : "); xmlOutputBufferWriteString(out, dtd->SystemID); xmlOutputBufferWriteString(out, "\n"); xmlOutputBufferFlush(out); } /* Free resources */ if (doc) xmlFreeDoc(doc); xmlCleanupParser(); /* OK */ exit: exit(exitcode); }
static int hwloc_libxml_import_diff(struct hwloc__xml_import_state_s *state, const char *xmlpath, const char *xmlbuffer, int xmlbuflen, hwloc_topology_diff_t *firstdiffp, char **refnamep) { hwloc__libxml_import_state_data_t lstate = (void*) state->data; char *refname = NULL; xmlDoc *doc = NULL; xmlNode* root_node; xmlDtd *dtd; int ret; assert(sizeof(*lstate) <= sizeof(state->data)); LIBXML_TEST_VERSION; hwloc_libxml2_init_once(); errno = 0; /* set to 0 so that we know if libxml2 changed it */ if (xmlpath) doc = xmlReadFile(xmlpath, NULL, 0); else if (xmlbuffer) doc = xmlReadMemory(xmlbuffer, xmlbuflen, "", NULL, 0); if (!doc) { if (!errno) /* libxml2 read the file fine, but it got an error during parsing */ errno = EINVAL; goto out; } dtd = xmlGetIntSubset(doc); if (!dtd) { if (hwloc__xml_verbose()) fprintf(stderr, "Loading XML topologydiff without DTD\n"); } else if (strcmp((char *) dtd->SystemID, "hwloc.dtd")) { if (hwloc__xml_verbose()) fprintf(stderr, "Loading XML topologydiff with wrong DTD SystemID (%s instead of %s)\n", (char *) dtd->SystemID, "hwloc.dtd"); } root_node = xmlDocGetRootElement(doc); if (strcmp((const char *) root_node->name, "topologydiff")) { /* root node should be in "topologydiff" class */ if (hwloc__xml_verbose()) fprintf(stderr, "ignoring object of class `%s' not at the top the xml hierarchy\n", (const char *) root_node->name); goto out_with_doc; } state->global->next_attr = hwloc__libxml_import_next_attr; state->global->find_child = hwloc__libxml_import_find_child; state->global->close_tag = hwloc__libxml_import_close_tag; state->global->close_child = hwloc__libxml_import_close_child; state->global->get_content = hwloc__libxml_import_get_content; state->global->close_content = hwloc__libxml_import_close_content; state->parent = NULL; lstate->node = root_node; lstate->child = root_node->children; lstate->attr = NULL; while (1) { char *attrname, *attrvalue; if (state->global->next_attr(state, &attrname, &attrvalue) < 0) break; if (!strcmp(attrname, "refname")) { free(refname); refname = strdup(attrvalue); } else goto out_with_doc; } ret = hwloc__xml_import_diff(state, firstdiffp); if (refnamep && !ret) *refnamep = refname; else free(refname); xmlFreeDoc(doc); hwloc_libxml2_cleanup(); return ret; out_with_doc: free(refname); xmlFreeDoc(doc); hwloc_libxml2_cleanup(); out: return -1; /* failed */ }
DTD * Document::InternalSubset() const { return Wrapped<DTD, _xmlDtd>(xmlGetIntSubset(const_cast<xmlDocPtr>(xml()))); }