1
/* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
* $Id: libxslt.pike,v 1.2 2006/07/06 19:57:10 astra Exp $
20
//! This is the libxslt module - the run() function is used to
21
//! transform given xml code with a xsl stylesheet.
24
constant cvs_version="$Id: libxslt.pike,v 1.2 2006/07/06 19:57:10 astra Exp $";
26
inherit "/kernel/module";
31
private static object parser = xslt.Parser();
32
private static Thread.Mutex xsltMutex = Thread.Mutex();
36
* callback function to find a stylesheet.
38
* @param string uri - the uri to locate the stylesheet
39
* @return the stylesheet content or zero.
40
* @author <a href="mailto:astra@upb.de">Thomas Bopp</a>)
42
static int match_stylesheet(string uri)
44
if ( search(uri, "steam:") == 0 )
49
static object open_stylesheet(string uri)
51
sscanf(uri, "steam:/%s", uri);
52
return _FILEPATH->path_to_object(uri);
56
read_stylesheet(object obj, string language, int position)
59
LOG("Stylesheet content found !");
60
string contstr = obj->get_content(language);
61
LOG("length="+strlen(contstr) + " of " + obj->get_object_id());
64
LOG("No Stylesheet given for reading");
69
close_stylesheet(object obj)
74
* Run the conversion and return the html code or whatever.
76
* @param string xml - the xml code.
77
* @param string|object xsl - the xsl stylesheet for transformation.
78
* @param mapping vars - the variables passed to the stylesheet as params.
79
* @return the transformed xml code.
80
* @author Thomas Bopp (astra@upb.de)
82
string run(string xml, object|string xsl, mapping params)
87
mapping vars = copy_value(params);
89
if ( !stringp(xml) || strlen(xml) == 0 )
90
steam_error("Failed to transform xml - xml is empty.");
93
MESSAGE("No Stylesheet param !");
95
object lock = xsltMutex->lock();
97
mapping cfgs = _Server->get_configs();
98
foreach ( indices(cfgs), string cfg) {
101
if ( intp(cfgs[cfg]) )
102
cfgs[cfg] = sprintf("%d", cfgs[cfg]);
103
else if ( !stringp(cfgs[cfg]) )
105
vars[replace(cfg, ":", "_")] = (string)cfgs[cfg];
109
foreach( indices(vars), string index) {
110
if ( (stringp(vars[index]) && search(vars[index], "\0") >= 0 ) ||
111
!stringp(vars[index]) && !intp(vars[index]) )
113
else if ( intp(vars[index]) )
114
vars[index] = (string)vars[index];
116
vars[index] = replace(vars[index],
117
({ "�", "�", "�", "�", "�", "�", "�", "\""}),
118
({ "%e4", "%f6", "%fc", "%c4", "%d6", "%dc",
123
parser->set_variables(vars);
126
if ( !stringp(vars["language"]) ) {
127
//werror("No language defined - setting english !\n");
128
vars["language"] = "english";
130
if ( objectp(xsl) ) {
131
string lang = vars["language"];
132
stylesheet = xsl->get_stylesheet(lang);
134
else if ( stringp(xsl) ) {
135
stylesheet = xslt.Stylesheet();
137
stylesheet->set_language(vars["language"]);
138
stylesheet->set_include_callbacks(match_stylesheet,
142
stylesheet->set_content(xsl);
145
error("xslt: Invalid run argument for XSL-Stylesheet !");
147
parser->set_xml_data(xml);
148
html = parser->run(stylesheet);
151
if ( arrayp(err) || objectp(err) ) {
152
FATAL("Error while processing xml !\n"+PRINT_BT(err));
154
THROW("LibXSLT (version="+parser->get_version()+
155
") xsl: Error while processing xsl ("
156
+xsl->get_identifier()+" #"+xsl->get_object_id()+" ):\n" +
157
err[0] + "\n", E_ERROR);
160
FATAL("Error running xslt: %O", err);
164
string get_identifier() { return "libxslt"; }
165
string get_version() { return parser->get_version(); }