📄 xinclude.c
字号:
doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
#ifdef LIBXML_XPTR_ENABLED
ctxt->parseFlags = saveFlags;
#endif
if (doc == NULL) {
xmlFree(URL);
if (fragment != NULL)
xmlFree(fragment);
return(-1);
}
ctxt->incTab[nr]->doc = doc;
/*
* It's possible that the requested URL has been mapped to a
* completely different location (e.g. through a catalog entry).
* To check for this, we compare the URL with that of the doc
* and change it if they disagree (bug 146988).
*/
if (!xmlStrEqual(URL, doc->URL)) {
xmlFree(URL);
URL = xmlStrdup(doc->URL);
}
for (i = nr + 1; i < ctxt->incNr; i++) {
if (xmlStrEqual(URL, ctxt->incTab[i]->URI)) {
ctxt->incTab[nr]->count++;
#ifdef DEBUG_XINCLUDE
printf("Increasing %s count since reused\n", URL);
#endif
break;
}
}
/*
* Make sure we have all entities fixed up
*/
xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
/*
* We don't need the DTD anymore, free up space
if (doc->intSubset != NULL) {
xmlUnlinkNode((xmlNodePtr) doc->intSubset);
xmlFreeNode((xmlNodePtr) doc->intSubset);
doc->intSubset = NULL;
}
if (doc->extSubset != NULL) {
xmlUnlinkNode((xmlNodePtr) doc->extSubset);
xmlFreeNode((xmlNodePtr) doc->extSubset);
doc->extSubset = NULL;
}
*/
xmlXIncludeRecurseDoc(ctxt, doc, URL);
loaded:
if (fragment == NULL) {
/*
* Add the top children list as the replacement copy.
*/
if (doc == NULL)
{
/* Hopefully a DTD declaration won't be copied from
* the same document */
ctxt->incTab[nr]->inc = xmlCopyNodeList(ctxt->doc->children);
} else {
ctxt->incTab[nr]->inc = xmlXIncludeCopyNodeList(ctxt, ctxt->doc,
doc, doc->children);
}
}
#ifdef LIBXML_XPTR_ENABLED
else {
/*
* Computes the XPointer expression and make a copy used
* as the replacement copy.
*/
xmlXPathObjectPtr xptr;
xmlXPathContextPtr xptrctxt;
xmlNodeSetPtr set;
if (doc == NULL) {
xptrctxt = xmlXPtrNewContext(ctxt->doc, ctxt->incTab[nr]->ref,
NULL);
} else {
xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
}
if (xptrctxt == NULL) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_XPTR_FAILED,
"could not create XPointer context\n", NULL);
xmlFree(URL);
xmlFree(fragment);
return(-1);
}
xptr = xmlXPtrEval(fragment, xptrctxt);
if (xptr == NULL) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_XPTR_FAILED,
"XPointer evaluation failed: #%s\n",
fragment);
xmlXPathFreeContext(xptrctxt);
xmlFree(URL);
xmlFree(fragment);
return(-1);
}
switch (xptr->type) {
case XPATH_UNDEFINED:
case XPATH_BOOLEAN:
case XPATH_NUMBER:
case XPATH_STRING:
case XPATH_POINT:
case XPATH_USERS:
case XPATH_XSLT_TREE:
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_XPTR_RESULT,
"XPointer is not a range: #%s\n",
fragment);
xmlXPathFreeContext(xptrctxt);
xmlFree(URL);
xmlFree(fragment);
return(-1);
case XPATH_NODESET:
if ((xptr->nodesetval == NULL) ||
(xptr->nodesetval->nodeNr <= 0)) {
xmlXPathFreeContext(xptrctxt);
xmlFree(URL);
xmlFree(fragment);
return(-1);
}
case XPATH_RANGE:
case XPATH_LOCATIONSET:
break;
}
set = xptr->nodesetval;
if (set != NULL) {
for (i = 0;i < set->nodeNr;i++) {
if (set->nodeTab[i] == NULL)
continue;
switch (set->nodeTab[i]->type) {
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_ENTITY_REF_NODE:
case XML_ENTITY_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
#ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE:
#endif
continue;
case XML_ELEMENT_NODE: {
xmlChar *nodeBase;
xmlNodePtr el = set->nodeTab[i];
nodeBase = xmlNodeGetBase(el->doc, el);
if (nodeBase != NULL) {
if (!xmlStrEqual(nodeBase, el->doc->URL))
xmlNodeSetBase(el, nodeBase);
xmlFree(nodeBase);
}
continue;
}
case XML_ATTRIBUTE_NODE:
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_XPTR_RESULT,
"XPointer selects an attribute: #%s\n",
fragment);
set->nodeTab[i] = NULL;
continue;
case XML_NAMESPACE_DECL:
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_XPTR_RESULT,
"XPointer selects a namespace: #%s\n",
fragment);
set->nodeTab[i] = NULL;
continue;
case XML_DOCUMENT_TYPE_NODE:
case XML_DOCUMENT_FRAG_NODE:
case XML_NOTATION_NODE:
case XML_DTD_NODE:
case XML_ELEMENT_DECL:
case XML_ATTRIBUTE_DECL:
case XML_ENTITY_DECL:
case XML_XINCLUDE_START:
case XML_XINCLUDE_END:
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_XPTR_RESULT,
"XPointer selects unexpected nodes: #%s\n",
fragment);
set->nodeTab[i] = NULL;
set->nodeTab[i] = NULL;
continue; /* for */
}
}
}
if (doc == NULL) {
ctxt->incTab[nr]->xptr = xptr;
ctxt->incTab[nr]->inc = NULL;
} else {
ctxt->incTab[nr]->inc =
xmlXIncludeCopyXPointer(ctxt, ctxt->doc, doc, xptr);
xmlXPathFreeObject(xptr);
}
xmlXPathFreeContext(xptrctxt);
xmlFree(fragment);
}
#endif
/*
* Do the xml:base fixup if needed
*/
if ((doc != NULL) && (URL != NULL) && (xmlStrchr(URL, (xmlChar) '/'))) {
xmlNodePtr node;
xmlChar *base;
xmlChar *curBase;
/*
* The base is only adjusted if "necessary", i.e. if the xinclude node
* has a base specified, or the URL is relative
*/
base = xmlGetNsProp(ctxt->incTab[nr]->ref, BAD_CAST "base",
XML_XML_NAMESPACE);
if (base == NULL) {
/*
* No xml:base on the xinclude node, so we check whether the
* URI base is different than (relative to) the context base
*/
curBase = xmlBuildRelativeURI(URL, ctxt->base);
if (curBase == NULL) { /* Error return */
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_HREF_URI,
"trying to build relative URI from %s\n", URL);
} else {
/* If the URI doesn't contain a slash, it's not relative */
if (!xmlStrchr(curBase, (xmlChar) '/'))
xmlFree(curBase);
else
base = curBase;
}
}
if (base != NULL) { /* Adjustment may be needed */
node = ctxt->incTab[nr]->inc;
while (node != NULL) {
/* Only work on element nodes */
if (node->type == XML_ELEMENT_NODE) {
curBase = xmlNodeGetBase(node->doc, node);
/* If no current base, set it */
if (curBase == NULL) {
xmlNodeSetBase(node, base);
} else {
/*
* If the current base is the same as the
* URL of the document, then reset it to be
* the specified xml:base or the relative URI
*/
if (xmlStrEqual(curBase, node->doc->URL)) {
xmlNodeSetBase(node, base);
} else {
/*
* If the element already has an xml:base
* set, then relativise it if necessary
*/
xmlChar *xmlBase;
xmlBase = xmlGetNsProp(node,
BAD_CAST "base",
XML_XML_NAMESPACE);
if (xmlBase != NULL) {
xmlChar *relBase;
relBase = xmlBuildURI(xmlBase, base);
if (relBase == NULL) { /* error */
xmlXIncludeErr(ctxt,
ctxt->incTab[nr]->ref,
XML_XINCLUDE_HREF_URI,
"trying to rebuild base from %s\n",
xmlBase);
} else {
xmlNodeSetBase(node, relBase);
xmlFree(relBase);
}
xmlFree(xmlBase);
}
}
xmlFree(curBase);
}
}
node = node->next;
}
xmlFree(base);
}
}
if ((nr < ctxt->incNr) && (ctxt->incTab[nr]->doc != NULL) &&
(ctxt->incTab[nr]->count <= 1)) {
#ifdef DEBUG_XINCLUDE
printf("freeing %s\n", ctxt->incTab[nr]->doc->URL);
#endif
xmlFreeDoc(ctxt->incTab[nr]->doc);
ctxt->incTab[nr]->doc = NULL;
}
xmlFree(URL);
return(0);
}
/**
* xmlXIncludeLoadTxt:
* @ctxt: the XInclude context
* @url: the associated URL
* @nr: the xinclude node number
*
* Load the content, and store the result in the XInclude context
*
* Returns 0 in case of success, -1 in case of failure
*/
static int
xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
xmlParserInputBufferPtr buf;
xmlNodePtr node;
xmlURIPtr uri;
xmlChar *URL;
int i;
xmlChar *encoding = NULL;
xmlCharEncoding enc = (xmlCharEncoding) 0;
/*
* Check the URL and remove any fragment identifier
*/
uri = xmlParseURI((const char *)url);
if (uri == NULL) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
"invalid value URI %s\n", url);
return(-1);
}
if (uri->fragment != NULL) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
"fragment identifier forbidden for text: %s\n",
(const xmlChar *) uri->fragment);
xmlFreeURI(uri);
return(-1);
}
URL = xmlSaveUri(uri);
xmlFreeURI(uri);
if (URL == NULL) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
"invalid value URI %s\n", url);
return(-1);
}
/*
* Handling of references to the local document are done
* directly through ctxt->doc.
*/
if (URL[0] == 0) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_TEXT_DOCUMENT,
"text serialization of document not available\n", NULL);
xmlFree(URL);
return(-1);
}
/*
* Prevent reloading twice the document.
*/
for (i = 0; i < ctxt->txtNr; i++) {
if (xmlStrEqual(URL, ctxt->txturlTab[i])) {
node = xmlCopyNode(ctxt->txtTab[i], 1);
goto loaded;
}
}
/*
* Try to get the encoding if available
*/
if ((ctxt->incTab[nr] != NULL) && (ctxt->incTab[nr]->ref != NULL)) {
encoding = xmlGetProp(ctxt->incTab[nr]->ref, XINCLUDE_PARSE_ENCODING);
}
if (encoding != NULL) {
/*
* TODO: we should not have to remap to the xmlCharEncoding
* predefined set, a better interface than
* xmlParserInputBufferCreateFilename should allow any
* encoding supported by iconv
*/
enc = xmlParseCharEncoding((const char *) encoding);
if (enc == XML_CHAR_ENCODING_ERROR) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_UNKNOWN_ENCODING,
"encoding %s not supported\n", encoding);
xmlFree(encoding);
xmlFree(URL);
return(-1);
}
xmlFree(encoding);
}
/*
* Load it.
*/
buf = xmlParserInputBufferCreateFilename((const char *)URL, enc);
if (buf == NULL) {
xmlFree(URL);
return(-1);
}
node = xmlNewText(NULL);
/*
* Scan all chars from the resource and add the to the node
*/
while (xmlParserInputBufferRead(buf, 128) > 0) {
int len;
const xmlChar *content;
content = xmlBufferContent(buf->buffer);
len = xmlBufferLength(buf->buffer);
for (i = 0;i < len;) {
int cur;
int l;
cur = xmlStringCurrentChar(NULL, &content[i], &l);
if (!IS_CHAR(cur)) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_INVALID_CHAR,
"%s contains invalid char\n", URL);
} else {
xmlNodeAddContentLen(node, &content[i], l);
}
i += l;
}
xmlBufferShrink(buf->buffer, len);
}
xmlFreeParserInputBuffer(buf);
xmlXIncludeAddTxt(ctxt, node, URL);
loaded:
/*
* Add the element as the replacement copy.
*/
ctxt->incTab[nr]->inc = node;
xmlFree(URL);
return(0);
}
/**
* xmlXIncludeLoadFallback:
* @ctxt: the XInclude context
* @fallback: the fallback node
* @nr: the xinclude node number
*
* Load the content of the fallback node, and store the result
* in the XInclude context
*
* Returns 0 in case of success, -1 in case of failure
*/
static int
xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback, int nr) {
xmlXIncludeCtxtPtr newctxt;
int ret = 0;
if ((fallback == NULL) || (ctxt == NULL))
return(-1);
if (fallback->children != NULL) {
/*
* It's possible that the fallback also has 'includes'
* (Bug 129969), so we re-process the fallback just in case
*/
newctxt = xmlXIncludeNewContext(ctxt->doc);
if (newctxt == NULL)
return (-1);
newctxt->base = xmlStrdup(ctxt->base); /* Inherit the base from the existing context */
xmlXIncludeSetFlags(newctxt, ctxt->parseFlags);
ret = xmlXIncludeDoProcess(newctxt, ctxt->doc, fallback->children);
if (ctxt->nbErrors > 0)
ret = -1;
else if (ret > 0)
ret = 0; /* xmlXIncludeDoProcess can return +ve number */
xmlXIncludeFreeContext(newctxt);
ctxt->incTab[nr]->inc = xmlDocCopyNodeList(ctxt->doc,
fallback->children);
} else {
ctxt->incTab[nr]->inc = NULL;
ctxt->incTab[nr]->emptyFb = 1; /* flag empty callback */
}
return(ret);
}
/************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -