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.3 2009/05/18 20:25:24 astra Exp $
    19 inherit "/kernel/module";
    22 //! This is the libxslt module - the run() function is used to
    23 //! transform given xml code with a xsl stylesheet.
    25 class libxslt : public module{
    33 private  object parser = xslt.Parser();
    34 private  Thread.Mutex xsltMutex = Thread.Mutex();
    38  * callback function to find a stylesheet.
    40  * @param string uri - the uri to locate the stylesheet
    41  * @return the stylesheet content or zero.
    44  int match_stylesheet(string uri)
    46     if ( search(uri, "steam:") == 0 )
    54  object open_stylesheet(string uri)
    56     sscanf(uri, "steam:/%s", uri);
    57     return _FILEPATH->path_to_object(uri);
    64 read_stylesheet(object obj, string language, int position)
    67    LOG("Stylesheet content found !");
    68    string contstr = obj->get_content(language);
    69    LOG("length="+strlen(contstr) + " of " + obj->get_object_id());
    72     LOG("No Stylesheet given for reading");
    80 close_stylesheet(object obj)
    87  * Run the conversion and return the html code or whatever.
    89  * @param string xml - the xml code.
    90  * @param string|object xsl - the xsl stylesheet for transformation.
    91  * @param mapping vars - the variables passed to the stylesheet as params.
    92  * @return the transformed xml code.
    94 string run(string xml, object|string xsl, mapping params)
    97     mapping vars = copy_value(params);
    99     if ( !stringp(xml) || strlen(xml) == 0 )
   100    steam_error("Failed to transform xml - xml is empty.");
   103       MESSAGE("No Stylesheet param !");
   105     object lock = xsltMutex->lock();
   107    mapping cfgs = _Server->get_configs();
   108    foreach ( indices(cfgs), string cfg) {
   111          if ( intp(cfgs[cfg]) ) 
   112            cfgs[cfg] = sprintf("%d", cfgs[cfg]);
   113          else if ( !stringp(cfgs[cfg]) )
   115          vars[replace(cfg, ":", "_")] = (string)cfgs[cfg];
   119    foreach( indices(vars), string index) {
   120      if ( (stringp(vars[index]) && search(vars[index], "\0") >= 0 ) ||
   121           !stringp(vars[index]) && !intp(vars[index]) )
   123      else if ( intp(vars[index]) )
   124        vars[index] = (string)vars[index];
   126        vars[index] = replace(vars[index], 
   127                              ({ "ä", "ö", "ü", "Ä", "Ö", "Ü", "ß", "\""}),
   128                              ({ "%e4", "%f6", "%fc", "%c4", "%d6", "%dc",
   133    parser->set_variables(vars);
   136    if ( !stringp(vars["language"]) ) {
   137      //werror("No language defined - setting english !\n");
   138      vars["language"] = "english";
   140    if ( objectp(xsl) ) {
   141      string lang = vars["language"];
   142      stylesheet = xsl->get_stylesheet(lang);
   144    else if ( stringp(xsl) ) {
   145      stylesheet = xslt.Stylesheet();
   147      stylesheet->set_language(vars["language"]);
   148      stylesheet->set_include_callbacks(match_stylesheet,
   152      stylesheet->set_content(xsl);
   155      error("xslt: Invalid run argument for XSL-Stylesheet !");
   157    parser->set_xml_data(xml);
   158    html = parser->run(stylesheet);
   161     if ( arrayp(err) || objectp(err) ) {
   162    FATAL("Error while processing xml !\n"+PRINT_BT(err));
   164    THROW("LibXSLT (version="+parser->get_version()+
   165          ") xsl: Error while processing xsl ("
   166               +(objectp(xsl)?xsl->get_identifier()+" #"+xsl->get_object_id():
   167            "no stylesheet")+" ):\n" + 
   168          err[0] + "\n", E_ERROR);
   171       FATAL("Error running xslt: %O", err);
   175 string get_identifier() { return "libxslt"; }
   176 string get_version() { return parser->get_version(); }