]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.c
reindented and recommented kindly
[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         /******************************/
68         /* XSLT stylesheet Processing */
69         /******************************/
70
71 xsltStylesheetPtr processStylesheet(GdomeDocument* style)
72 {
73    xmlDocPtr style_copy;
74    xmlDocPtr style_libxml;
75
76    if (style == NULL) {
77       return NULL;
78    }
79    style_libxml = libxml_of_gdome(style);
80    style_copy = xmlCopyDoc(style_libxml, 1);
81    style_copy->URL = xmlStrdup(style_libxml->URL);
82
83    xsltSetGenericDebugFunc(NULL, NULL);
84
85    return xsltParseStylesheetDoc(style_copy);
86 }
87
88         /*******************************/
89         /* XSLT stylesheet Application */
90         /*******************************/
91
92 GdomeDocument* applyStylesheet(GdomeDocument* source, xsltStylesheetPtr
93                 style_libxslt, const char** params)
94 {
95    xmlDocPtr source_libxml;
96    xmlDocPtr output_libxml;
97
98    if (source == NULL) return NULL;
99    source_libxml = libxml_of_gdome(source);
100
101    xsltSetGenericDebugFunc(NULL, NULL);
102
103    output_libxml = xsltApplyStylesheet(style_libxslt, source_libxml,
104                    params);
105
106    if (output_libxml == NULL) return NULL;
107
108    return gdome_of_libxml(output_libxml);
109 }
110
111         /******************/
112         /* Results Output */
113         /******************/
114
115 int saveResultToFilename (const char* name, GdomeDocument* result,
116                 xsltStylesheetPtr style_libxslt, int compression)
117 {
118         xmlDocPtr result_libxml;
119
120         if (result == NULL) return -1;
121         result_libxml = libxml_of_gdome(result);
122
123         xsltSetGenericDebugFunc(NULL, NULL);
124
125         return xsltSaveResultToFilename(name, result_libxml,
126                         style_libxslt, compression);
127 }
128
129 int saveResultToFile (FILE* file, GdomeDocument* result,
130                 xsltStylesheetPtr style_libxslt)
131 {
132         xmlDocPtr result_libxml;
133
134         if (result == NULL) return -1;
135         result_libxml = libxml_of_gdome(result);
136
137         xsltSetGenericDebugFunc(NULL, NULL);
138
139         return xsltSaveResultToFile(file, result_libxml, style_libxslt);
140 }
141
142 int saveResultToFd (int fd, GdomeDocument* result, xsltStylesheetPtr
143                 style_libxslt)
144 {
145         xmlDocPtr result_libxml;
146
147         if (result == NULL) return -1;
148         result_libxml = libxml_of_gdome(result);
149
150         xsltSetGenericDebugFunc(NULL, NULL);
151
152         return xsltSaveResultToFd(fd, result_libxml, style_libxslt);
153 }
154