2 * documents.c: Implementation of the documents handling
4 * See Copyright for the status of this software.
13 #include <libxml/xmlmemory.h>
14 #include <libxml/tree.h>
15 #include <libxml/hash.h>
17 #include "xsltInternals.h"
18 #include "xsltutils.h"
19 #include "documents.h"
20 #include "transform.h"
24 #ifdef LIBXML_XINCLUDE_ENABLED
25 #include <libxml/xinclude.h>
28 #ifdef WITH_XSLT_DEBUG
29 #define WITH_XSLT_DEBUG_DOCUMENTS
32 /************************************************************************
36 ************************************************************************/
40 * @ctxt: an XSLT transformation context (or NULL)
41 * @doc: a parsed XML document
43 * Register a new document, apply key computations
46 xsltNewDocument(xsltTransformContextPtr ctxt, xmlDocPtr doc) {
49 cur = (xsltDocumentPtr) xmlMalloc(sizeof(xsltDocument));
51 xsltPrintErrorContext(ctxt, NULL, (xmlNodePtr) doc);
52 xsltGenericError(xsltGenericErrorContext,
53 "xsltNewDocument : malloc failed\n");
56 memset(cur, 0, sizeof(xsltDocument));
59 cur->next = ctxt->docList;
61 xsltInitCtxtKeys(ctxt, cur);
67 * xsltNewStyleDocument:
68 * @style: an XSLT style sheet
69 * @doc: a parsed XML document
71 * Register a new document, apply key computations
74 xsltNewStyleDocument(xsltStylesheetPtr style, xmlDocPtr doc) {
77 cur = (xsltDocumentPtr) xmlMalloc(sizeof(xsltDocument));
79 xsltPrintErrorContext(NULL, style, (xmlNodePtr) doc);
80 xsltGenericError(xsltGenericErrorContext,
81 "xsltNewStyleDocument : malloc failed\n");
84 memset(cur, 0, sizeof(xsltDocument));
87 cur->next = style->docList;
94 * xsltFreeStyleDocuments:
95 * @style: an XSLT style sheet
97 * Free up all the space used by the loaded documents
100 xsltFreeStyleDocuments(xsltStylesheetPtr style) {
101 xsltDocumentPtr doc, cur;
103 cur = style->docList;
104 while (cur != NULL) {
107 xsltFreeDocumentKeys(doc);
109 xmlFreeDoc(doc->doc);
116 * @ctxt: an XSLT transformation context
118 * Free up all the space used by the loaded documents
121 xsltFreeDocuments(xsltTransformContextPtr ctxt) {
122 xsltDocumentPtr doc, cur;
125 while (cur != NULL) {
128 xsltFreeDocumentKeys(doc);
130 xmlFreeDoc(doc->doc);
138 * @ctxt: an XSLT transformation context
139 * @URI: the computed URI of the document
141 * Try to load a document within the XSLT transformation context
143 * Returns the new xsltDocumentPtr or NULL in case of error
146 xsltLoadDocument(xsltTransformContextPtr ctxt, const xmlChar *URI) {
150 if ((ctxt == NULL) || (URI == NULL))
154 * Walk the context list to find the document if preparsed
157 while (ret != NULL) {
158 if ((ret->doc != NULL) && (ret->doc->URL != NULL) &&
159 (xmlStrEqual(ret->doc->URL, URI)))
164 doc = xmlParseFile((const char *) URI);
168 if (ctxt->xinclude != 0) {
169 #ifdef LIBXML_XINCLUDE_ENABLED
170 xmlXIncludeProcess(doc);
172 xsltPrintErrorContext(ctxt, NULL, NULL);
173 xsltGenericError(xsltGenericErrorContext,
174 "xsltLoadDocument(%s) : XInclude processing not compiled in\n",
179 * Apply white-space stripping if asked for
181 if (xsltNeedElemSpaceHandling(ctxt))
182 xsltApplyStripSpaces(ctxt, xmlDocGetRootElement(doc));
184 ret = xsltNewDocument(ctxt, doc);
189 * xsltLoadStyleDocument:
190 * @style: an XSLT style sheet
191 * @URI: the computed URI of the document
193 * Try to load a document within the XSLT transformation context
195 * Returns the new xsltDocumentPtr or NULL in case of error
198 xsltLoadStyleDocument(xsltStylesheetPtr style, const xmlChar *URI) {
202 if ((style == NULL) || (URI == NULL))
206 * Walk the context list to find the document if preparsed
208 ret = style->docList;
209 while (ret != NULL) {
210 if ((ret->doc != NULL) && (ret->doc->URL != NULL) &&
211 (xmlStrEqual(ret->doc->URL, URI)))
216 doc = xmlParseFile((const char *) URI);
220 ret = xsltNewStyleDocument(style, doc);
226 * @ctxt: an XSLT transformation context
227 * @@doc: a parsed XML document
229 * Try to find a document within the XSLT transformation context
231 * Returns the desired xsltDocumentPtr or NULL in case of error
234 xsltFindDocument (xsltTransformContextPtr ctxt, xmlDocPtr doc) {
237 if ((ctxt == NULL) || (doc == NULL))
241 * Walk the context list to find the document
244 while (ret != NULL) {
249 if (doc == ctxt->style->doc)
250 return(ctxt->document);