~ubuntu-branches/ubuntu/wily/steam/wily

« back to all changes in this revision

Viewing changes to server/modules/libxslt.pike

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (1.1.4) (0.1.4 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2004  Thomas Bopp, Thorsten Hampel, Ludger Merkens
2
 
 *
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.
7
 
 *
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.
12
 
 *
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
16
 
 * 
17
 
 * $Id: libxslt.pike,v 1.2 2006/07/06 19:57:10 astra Exp $
18
 
 */
19
 
 
20
 
//! This is the libxslt module - the run() function is used to
21
 
//! transform given xml code with a xsl stylesheet.
22
 
//! sleece was here!
23
 
 
24
 
constant cvs_version="$Id: libxslt.pike,v 1.2 2006/07/06 19:57:10 astra Exp $";
25
 
 
26
 
inherit "/kernel/module";
27
 
 
28
 
#include <macros.h>
29
 
#include <database.h>
30
 
 
31
 
private static object parser = xslt.Parser();
32
 
private static Thread.Mutex xsltMutex = Thread.Mutex();
33
 
 
34
 
 
35
 
/**
36
 
 * callback function to find a stylesheet.
37
 
 *  
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>) 
41
 
 */
42
 
static int match_stylesheet(string uri)
43
 
{
44
 
    if ( search(uri, "steam:") == 0 )
45
 
        return 1;
46
 
    return 0;
47
 
}
48
 
 
49
 
static object open_stylesheet(string uri)
50
 
{
51
 
    sscanf(uri, "steam:/%s", uri);
52
 
    return _FILEPATH->path_to_object(uri);
53
 
}
54
 
 
55
 
static string|int
56
 
read_stylesheet(object obj, string language, int position)
57
 
{
58
 
    if ( objectp(obj) ) {
59
 
        LOG("Stylesheet content found !");
60
 
        string contstr = obj->get_content(language);
61
 
        LOG("length="+strlen(contstr) + " of " + obj->get_object_id());
62
 
        return contstr;
63
 
    }
64
 
    LOG("No Stylesheet given for reading");
65
 
    return 0;
66
 
}
67
 
 
68
 
static void
69
 
close_stylesheet(object obj)
70
 
{
71
 
}
72
 
 
73
 
/**
74
 
 * Run the conversion and return the html code or whatever.
75
 
 *  
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) 
81
 
 */
82
 
string run(string xml, object|string xsl, mapping params)
83
 
{
84
 
    string content;
85
 
    float        t;
86
 
    string    html;
87
 
    mapping vars = copy_value(params);
88
 
 
89
 
    if ( !stringp(xml) || strlen(xml) == 0 )
90
 
        steam_error("Failed to transform xml - xml is empty.");
91
 
    
92
 
    if ( !objectp(xsl) )
93
 
      MESSAGE("No Stylesheet param !");
94
 
 
95
 
    object lock = xsltMutex->lock();
96
 
    mixed err = catch {
97
 
        mapping cfgs = _Server->get_configs();
98
 
        foreach ( indices(cfgs), string cfg) {
99
 
          if(stringp(cfg))
100
 
            {
101
 
              if ( intp(cfgs[cfg]) ) 
102
 
                cfgs[cfg] = sprintf("%d", cfgs[cfg]);
103
 
              else if ( !stringp(cfgs[cfg]) )
104
 
                continue;
105
 
              vars[replace(cfg, ":", "_")] = (string)cfgs[cfg];
106
 
              m_delete(cfgs, cfg);
107
 
            }
108
 
        }
109
 
        foreach( indices(vars), string index) {
110
 
          if ( (stringp(vars[index]) && search(vars[index], "\0") >= 0 ) ||
111
 
               !stringp(vars[index]) && !intp(vars[index]) )
112
 
            vars[index] = 0;
113
 
          else if ( intp(vars[index]) )
114
 
            vars[index] = (string)vars[index];
115
 
          else {
116
 
            vars[index] = replace(vars[index], 
117
 
                                  ({ "�", "�", "�", "�", "�", "�", "�", "\""}),
118
 
                                  ({ "%e4", "%f6", "%fc", "%c4", "%d6", "%dc",
119
 
                                     "%df", "\\\"" }));
120
 
          }
121
 
        }
122
 
        
123
 
        parser->set_variables(vars);
124
 
        
125
 
        object stylesheet;
126
 
        if ( !stringp(vars["language"]) ) {
127
 
          //werror("No language defined - setting english !\n");
128
 
          vars["language"] = "english";
129
 
        }
130
 
        if ( objectp(xsl) ) {
131
 
          string lang = vars["language"];
132
 
          stylesheet = xsl->get_stylesheet(lang);
133
 
        }
134
 
        else if ( stringp(xsl) ) {
135
 
          stylesheet = xslt.Stylesheet();
136
 
          
137
 
          stylesheet->set_language(vars["language"]);
138
 
          stylesheet->set_include_callbacks(match_stylesheet,
139
 
                                            open_stylesheet,
140
 
                                            read_stylesheet,
141
 
                                            close_stylesheet);
142
 
          stylesheet->set_content(xsl);
143
 
        }
144
 
        else 
145
 
          error("xslt: Invalid run argument for XSL-Stylesheet !");
146
 
        
147
 
        parser->set_xml_data(xml);
148
 
        html = parser->run(stylesheet);
149
 
      };
150
 
    destruct(lock);
151
 
    if ( arrayp(err) || objectp(err) ) {
152
 
        FATAL("Error while processing xml !\n"+PRINT_BT(err));
153
 
        
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);
158
 
    }
159
 
    else if ( err )
160
 
      FATAL("Error running xslt: %O", err);
161
 
    return html;
162
 
}
163
 
 
164
 
string get_identifier() { return "libxslt"; }
165
 
string get_version() { return parser->get_version(); }
166