2 Copyright (c) 2008 Instituto Nokia de Tecnologia
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13 * Neither the name of the INdT nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
31 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
32 * @date Mon Aug 25 15:50:20 2008
34 * @brief A XSLT helper module, converts a XML doc to another format
37 * Depends on libxml and libxslt.
40 * - doxygen comments about use
41 * - move code to a '.c' file or make functions static
42 * - make 'xslt_resources' an abstract type
49 #include <libxml/HTMLtree.h>
50 #include <libxml/catalog.h>
51 #include <libxml/debugXML.h>
52 #include <libxml/xinclude.h>
53 #include <libxml/xmlIO.h>
54 #include <libxml/xmlmemory.h>
55 #include <libxslt/transform.h>
56 #include <libxslt/xslt.h>
57 #include <libxslt/xsltInternals.h>
58 #include <libxslt/xsltutils.h>
61 struct xslt_resources
{
64 xsltStylesheetPtr cur
;
70 struct xslt_resources
*xslt_new(void)
72 struct xslt_resources
*result
;
73 result
= (struct xslt_resources
*)malloc(sizeof(struct xslt_resources
));
75 memset(result
, 0, sizeof(struct xslt_resources
));
80 int xslt_initialize(struct xslt_resources
*ctx
, const char *stylesheet_path
)
83 if (!stylesheet_path
|| !ctx
)
86 xmlSubstituteEntitiesDefault(1);
87 xmlLoadExtDtdDefaultValue
= 1;
90 xsltFreeStylesheet(ctx
->cur
);
92 ctx
->cur
= xsltParseStylesheetFile((const xmlChar
*)stylesheet_path
);
94 fprintf(stderr
, "Cannot create XSLT context!\n");
104 int xslt_transform(struct xslt_resources
*ctx
, const char *document
)
107 if (!ctx
|| !document
)
111 xmlFreeDoc(ctx
->doc
);
113 xmlFreeDoc(ctx
->output
);
116 ctx
->doc
= xmlReadMemory(document
, strlen(document
), "noname.xml",
119 fprintf(stderr
, "Cannot create document with "
124 ctx
->output
= xsltApplyStylesheet(ctx
->cur
, ctx
->doc
, NULL
);
126 fprintf(stderr
, "Cannot create document with "
132 xmlFree(ctx
->xml_str
);
135 xmlDocDumpMemory(ctx
->output
, &(ctx
->xml_str
), &(ctx
->length
));
141 xmlFreeDoc(ctx
->doc
);
145 xmlFreeDoc(ctx
->output
);
152 void xslt_delete(struct xslt_resources
*ctx
)
157 /* TODO: 'output' is pointing to a non null value (probably an
158 * overflow in some other place) even when not initialized.
159 * It happens just after all clients are disconnected.
160 * I must investigate it further, for while the flag is a work
161 * around to stop msync to crash.
163 /* fprintf(stderr, "\ndoc: %x\tout: %x\txml: %x\tcur: %x\n\n", */
164 /* ctx->doc, ctx->output, ctx->xml_str, ctx->cur); */
171 xmlFreeDoc(ctx
->doc
);
173 xmlFreeDoc(ctx
->output
);
175 xmlFree(ctx
->xml_str
);
177 xsltFreeStylesheet(ctx
->cur
);
178 xsltCleanupGlobals();