source: trunk/third/libxslt/libxslt/documents.c @ 18543

Revision 18543, 6.4 KB checked in by ghudson, 21 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18542, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 * documents.c: Implementation of the documents handling
3 *
4 * See Copyright for the status of this software.
5 *
6 * daniel@veillard.com
7 */
8
9#define IN_LIBXSLT
10#include "libxslt.h"
11
12#include <string.h>
13
14#include <libxml/xmlmemory.h>
15#include <libxml/tree.h>
16#include <libxml/hash.h>
17#include "xslt.h"
18#include "xsltInternals.h"
19#include "xsltutils.h"
20#include "documents.h"
21#include "transform.h"
22#include "imports.h"
23#include "keys.h"
24#include "security.h"
25
26#ifdef LIBXML_XINCLUDE_ENABLED
27#include <libxml/xinclude.h>
28#endif
29
30#define WITH_XSLT_DEBUG_DOCUMENTS
31
32#ifdef WITH_XSLT_DEBUG
33#define WITH_XSLT_DEBUG_DOCUMENTS
34#endif
35
36/************************************************************************
37 *                                                                      *
38 *                      Module interfaces                               *
39 *                                                                      *
40 ************************************************************************/
41
42/**
43 * xsltNewDocument:
44 * @ctxt: an XSLT transformation context (or NULL)
45 * @doc:  a parsed XML document
46 *
47 * Register a new document, apply key computations
48 *
49 * Returns a handler to the document
50 */
51xsltDocumentPtr
52xsltNewDocument(xsltTransformContextPtr ctxt, xmlDocPtr doc) {
53    xsltDocumentPtr cur;
54
55    cur = (xsltDocumentPtr) xmlMalloc(sizeof(xsltDocument));
56    if (cur == NULL) {
57        xsltTransformError(ctxt, NULL, (xmlNodePtr) doc,
58                "xsltNewDocument : malloc failed\n");
59        return(NULL);
60    }
61    memset(cur, 0, sizeof(xsltDocument));
62    cur->doc = doc;
63    if (ctxt != NULL) {
64        cur->next = ctxt->docList;
65        ctxt->docList = cur;
66        xsltInitCtxtKeys(ctxt, cur);
67    }
68    return(cur);
69}
70
71/**
72 * xsltNewStyleDocument:
73 * @style: an XSLT style sheet
74 * @doc:  a parsed XML document
75 *
76 * Register a new document, apply key computations
77 *
78 * Returns a handler to the document
79 */
80xsltDocumentPtr
81xsltNewStyleDocument(xsltStylesheetPtr style, xmlDocPtr doc) {
82    xsltDocumentPtr cur;
83
84    cur = (xsltDocumentPtr) xmlMalloc(sizeof(xsltDocument));
85    if (cur == NULL) {
86        xsltTransformError(NULL, style, (xmlNodePtr) doc,
87                "xsltNewStyleDocument : malloc failed\n");
88        return(NULL);
89    }
90    memset(cur, 0, sizeof(xsltDocument));
91    cur->doc = doc;
92    if (style != NULL) {
93        cur->next = style->docList;
94        style->docList = cur;
95    }
96    return(cur);
97}
98
99/**
100 * xsltFreeStyleDocuments:
101 * @style: an XSLT style sheet
102 *
103 * Free up all the space used by the loaded documents
104 */
105void   
106xsltFreeStyleDocuments(xsltStylesheetPtr style) {
107    xsltDocumentPtr doc, cur;
108
109    cur = style->docList;
110    while (cur != NULL) {
111        doc = cur;
112        cur = cur->next;
113        xsltFreeDocumentKeys(doc);
114        if (!doc->main)
115            xmlFreeDoc(doc->doc);
116        xmlFree(doc);
117    }
118}
119
120/**
121 * xsltFreeDocuments:
122 * @ctxt: an XSLT transformation context
123 *
124 * Free up all the space used by the loaded documents
125 */
126void   
127xsltFreeDocuments(xsltTransformContextPtr ctxt) {
128    xsltDocumentPtr doc, cur;
129
130    cur = ctxt->docList;
131    while (cur != NULL) {
132        doc = cur;
133        cur = cur->next;
134        xsltFreeDocumentKeys(doc);
135        if (!doc->main)
136            xmlFreeDoc(doc->doc);
137        xmlFree(doc);
138    }
139    cur = ctxt->styleList;
140    while (cur != NULL) {
141        doc = cur;
142        cur = cur->next;
143        xsltFreeDocumentKeys(doc);
144        if (!doc->main)
145            xmlFreeDoc(doc->doc);
146        xmlFree(doc);
147    }
148}
149
150
151/**
152 * xsltLoadDocument:
153 * @ctxt: an XSLT transformation context
154 * @URI:  the computed URI of the document
155 *
156 * Try to load a document (not a stylesheet)
157 * within the XSLT transformation context
158 *
159 * Returns the new xsltDocumentPtr or NULL in case of error
160 */
161xsltDocumentPtr
162xsltLoadDocument(xsltTransformContextPtr ctxt, const xmlChar *URI) {
163    xsltDocumentPtr ret;
164    xmlDocPtr doc;
165
166    if ((ctxt == NULL) || (URI == NULL))
167        return(NULL);
168
169    /*
170     * Security framework check
171     */
172    if (ctxt->sec != NULL) {
173        int res;
174       
175        res = xsltCheckRead(ctxt->sec, ctxt, URI);
176        if (res == 0) {
177            xsltTransformError(ctxt, NULL, NULL,
178                 "xsltLoadDocument: read rights for %s denied\n",
179                             URI);
180            return(NULL);
181        }
182    }
183
184    /*
185     * Walk the context list to find the document if preparsed
186     */
187    ret = ctxt->docList;
188    while (ret != NULL) {
189        if ((ret->doc != NULL) && (ret->doc->URL != NULL) &&
190            (xmlStrEqual(ret->doc->URL, URI)))
191            return(ret);
192        ret = ret->next;
193    }
194
195    doc = xmlParseFile((const char *) URI);
196    if (doc == NULL)
197        return(NULL);
198
199    if (ctxt->xinclude != 0) {
200#ifdef LIBXML_XINCLUDE_ENABLED
201        xmlXIncludeProcess(doc);
202#else
203        xsltTransformError(ctxt, NULL, NULL,
204            "xsltLoadDocument(%s) : XInclude processing not compiled in\n",
205                         URI);
206#endif
207    }
208    /*
209     * Apply white-space stripping if asked for
210     */
211    if (xsltNeedElemSpaceHandling(ctxt))
212        xsltApplyStripSpaces(ctxt, xmlDocGetRootElement(doc));
213
214    ret = xsltNewDocument(ctxt, doc);
215    return(ret);
216}
217
218/**
219 * xsltLoadStyleDocument:
220 * @style: an XSLT style sheet
221 * @URI:  the computed URI of the document
222 *
223 * Try to load a stylesheet document within the XSLT transformation context
224 *
225 * Returns the new xsltDocumentPtr or NULL in case of error
226 */
227xsltDocumentPtr
228xsltLoadStyleDocument(xsltStylesheetPtr style, const xmlChar *URI) {
229    xsltDocumentPtr ret;
230    xmlDocPtr doc;
231    xsltSecurityPrefsPtr sec;
232
233    if ((style == NULL) || (URI == NULL))
234        return(NULL);
235
236    /*
237     * Security framework check
238     */
239    sec = xsltGetDefaultSecurityPrefs();
240    if (sec != NULL) {
241        int res;
242
243        res = xsltCheckRead(sec, NULL, URI);
244        if (res == 0) {
245            xsltTransformError(NULL, NULL, NULL,
246                 "xsltLoadStyleDocument: read rights for %s denied\n",
247                             URI);
248            return(NULL);
249        }
250    }
251
252    /*
253     * Walk the context list to find the document if preparsed
254     */
255    ret = style->docList;
256    while (ret != NULL) {
257        if ((ret->doc != NULL) && (ret->doc->URL != NULL) &&
258            (xmlStrEqual(ret->doc->URL, URI)))
259            return(ret);
260        ret = ret->next;
261    }
262
263    doc = xmlParseFile((const char *) URI);
264    if (doc == NULL)
265        return(NULL);
266
267    ret = xsltNewStyleDocument(style, doc);
268    return(ret);
269}
270
271/**
272 * xsltFindDocument:
273 * @ctxt: an XSLT transformation context
274 * @doc: a parsed XML document
275 *
276 * Try to find a document within the XSLT transformation context
277 *
278 * Returns the desired xsltDocumentPtr or NULL in case of error
279 */
280xsltDocumentPtr
281xsltFindDocument (xsltTransformContextPtr ctxt, xmlDocPtr doc) {
282    xsltDocumentPtr ret;
283
284    if ((ctxt == NULL) || (doc == NULL))
285        return(NULL);
286
287    /*
288     * Walk the context list to find the document
289     */
290    ret = ctxt->docList;
291    while (ret != NULL) {
292        if (ret->doc == doc)
293            return(ret);
294        ret = ret->next;
295    }
296    if (doc == ctxt->style->doc)
297        return(ctxt->document);
298    return(NULL);
299}
300
Note: See TracBrowser for help on using the repository browser.