]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.c
- added methods to retrieve client IP address related info
[helm.git] / helm / DEVEL / gdome_xslt / C / gdome_xslt / gdome_xslt.c
1 /* This file implements a XSLT engine working on Gdome documents. In fact,
2  * it just maps Gdome documents to libxml documents back and forth, and
3  * applyes the transformation on libxml documents using libxlt.
4  * 
5  * The code is largely based on the code of T.J. Mather's XML::GDOME::XSLT
6  * Perl module (http://kobesearch.cpan.org/search?dist=XML-GDOME-XSLT)
7  *
8  * Copyright (C) 2002:
9  *      Claudio Sacerdoti Coen          <sacerdot@cs.unibo.it>
10  *      Stefano Zacchiroli              <zack@cs.unibo.it>
11  * 
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  * 
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  * 
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  *
26  * For more information, please send an email to {sacerdot,zack}@cs.unibo.it
27  */
28
29 #include <stdio.h>
30 #include <libgdome/gdome.h>
31 #include <libxslt/xsltconfig.h>
32 #include <libxslt/xslt.h>
33 #include <libxslt/xsltutils.h>
34 #include <libxslt/transform.h>
35 #include <libxslt/imports.h>
36 #include "gdome_xslt.h"
37
38 // Begin of Gdome internals exposed
39 typedef struct _Gdome_xml_Document Gdome_xml_Document;
40 struct _Gdome_xml_Document {
41         GdomeDocument super;
42         const GdomeDocumentVtab* vtab;
43         int refcnt;
44         xmlDocPtr n;
45         GdomeAccessType accessType;
46 };
47
48 GdomeNode* gdome_xml_n_mkref(xmlNode* n);
49 // End of Gdome internals exposed
50
51 // Begin of the abstraction of Gdome internals. Uses the Gdome internals exposed
52 xmlDocPtr libxml_of_gdome(GdomeDocument* doc)
53 {
54    return ((Gdome_xml_Document*)doc)->n;
55 }
56
57 GdomeDocument* gdome_of_libxml(xmlDocPtr n)
58 {
59    return (GdomeDocument*)gdome_xml_n_mkref((xmlNode*)n);
60 }
61 // End of the abstraction of Gdome internals. Uses the Gdome internals exposed.
62
63
64
65 // From now on no Gdome internal should be used directly.
66
67 xsltStylesheetPtr processStylesheet(GdomeDocument* style)
68 {
69    xmlDocPtr style_copy;
70    xmlDocPtr style_libxml;
71
72    if (style == NULL) {
73       return NULL;
74    }
75    style_libxml = libxml_of_gdome(style);
76    style_copy = xmlCopyDoc(style_libxml, 1);
77    style_copy->URL = xmlStrdup(style_libxml->URL);
78
79    xsltSetGenericDebugFunc(NULL, NULL);
80
81    return xsltParseStylesheetDoc(style_copy);
82 }
83
84 GdomeDocument* applyStylesheet(GdomeDocument* source, xsltStylesheetPtr style_libxslt, const char** params)
85 {
86    xmlDocPtr source_libxml;
87    xmlDocPtr output_libxml;
88
89    if (source == NULL) return NULL;
90    source_libxml = libxml_of_gdome(source);
91
92    xsltSetGenericDebugFunc(NULL, NULL);
93
94    output_libxml = xsltApplyStylesheet(style_libxslt,
95                                        source_libxml,
96                                        params);
97
98    if (output_libxml == NULL) return NULL;
99
100    return gdome_of_libxml(output_libxml);
101 }
102
103 /***************** serialization functions *****************/
104
105 int             saveResultToFilename    (const char* name,
106                                          GdomeDocument* result,
107                                          xsltStylesheetPtr style_libxslt,
108                                          int compression)
109 {
110         xmlDocPtr result_libxml;
111
112         if (result == NULL) return -1;
113         result_libxml = libxml_of_gdome(result);
114
115         xsltSetGenericDebugFunc(NULL, NULL);
116
117         return xsltSaveResultToFilename(name,
118                                         result_libxml,
119                                         style_libxslt,
120                                         compression);
121 }
122
123 int             saveResultToFile        (FILE* file,
124                                          GdomeDocument* result,
125                                          xsltStylesheetPtr style_libxslt)
126 {
127         xmlDocPtr result_libxml;
128
129         if (result == NULL) return -1;
130         result_libxml = libxml_of_gdome(result);
131
132         xsltSetGenericDebugFunc(NULL, NULL);
133
134         return xsltSaveResultToFile(file,
135                                     result_libxml,
136                                     style_libxslt);
137 }
138
139 int             saveResultToFd          (int fd,
140                                          GdomeDocument* result,
141                                          xsltStylesheetPtr style_libxslt)
142 {
143         xmlDocPtr result_libxml;
144
145         if (result == NULL) return -1;
146         result_libxml = libxml_of_gdome(result);
147
148         xsltSetGenericDebugFunc(NULL, NULL);
149
150         return xsltSaveResultToFd(fd,
151                                   result_libxml,
152                                   style_libxslt);
153 }
154