libxslt._pike
Go to the documentation of this file.
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.3 2009/05/18 20:25:24 astra Exp $
18  */
19 inherit "/kernel/module";
20 #include <macros.h>
21 #include <database.h>
22 //! This is the libxslt module - the run() function is used to
23 //! transform given xml code with a xsl stylesheet.
24 //! sleece was here!
25 class libxslt : public module{
26 public:
27 
28 
29 
30 
31 
32 
33 private object parser = xslt.Parser();
34 private Thread.Mutex xsltMutex = Thread.Mutex();
35 
36 
37 /**
38  * callback function to find a stylesheet.
39  *
40  * @param string uri - the uri to locate the stylesheet
41  * @return the stylesheet content or zero.
42  */
43 protected:
44  int match_stylesheet(string uri)
45 {
46  if ( search(uri, "steam:") == 0 )
47  return 1;
48  return 0;
49 }
50 
51 public:
52 
53 protected:
54  object open_stylesheet(string uri)
55 {
56  sscanf(uri, "steam:/%s", uri);
57  return _FILEPATH->path_to_object(uri);
58 }
59 
60 public:
61 
62 protected:
63  string|int
64 read_stylesheet(object obj, string language, int position)
65 {
66  if ( objectp(obj) ) {
67  LOG("Stylesheet content found !");
68  string contstr = obj->get_content(language);
69  LOG("length="+strlen(contstr) + " of " + obj->get_object_id());
70  return contstr;
71  }
72  LOG("No Stylesheet given for reading");
73  return 0;
74 }
75 
76 public:
77 
78 protected:
79  void
80 close_stylesheet(object obj)
81 {
82 }
83 
84 public:
85 
86 /**
87  * Run the conversion and return the html code or whatever.
88  *
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.
93  */
94 string run(string xml, object|string xsl, mapping params)
95 {
96  string html;
97  mapping vars = copy_value(params);
98 
99  if ( !stringp(xml) || strlen(xml) == 0 )
100  steam_error("Failed to transform xml - xml is empty.");
101 
102  if ( !objectp(xsl) )
103  MESSAGE("No Stylesheet param !");
104 
105  object lock = xsltMutex->lock();
106  mixed err = catch {
107  mapping cfgs = _Server->get_configs();
108  foreach ( indices(cfgs), string cfg) {
109  if(stringp(cfg))
110  {
111  if ( intp(cfgs[cfg]) )
112  cfgs[cfg] = sprintf("%d", cfgs[cfg]);
113  else if ( !stringp(cfgs[cfg]) )
114  continue;
115  vars[replace(cfg, ":", "_")] = (string)cfgs[cfg];
116  m_delete(cfgs, cfg);
117  }
118  }
119  foreach( indices(vars), string index) {
120  if ( (stringp(vars[index]) && search(vars[index], "\0") >= 0 ) ||
121  !stringp(vars[index]) && !intp(vars[index]) )
122  vars[index] = 0;
123  else if ( intp(vars[index]) )
124  vars[index] = (string)vars[index];
125  else {
126  vars[index] = replace(vars[index],
127  ({ "ä", "ö", "ü", "Ä", "Ö", "Ü", "ß", "\""}),
128  ({ "%e4", "%f6", "%fc", "%c4", "%d6", "%dc",
129  "%df", "\\\"" }));
130  }
131  }
132 
133  parser->set_variables(vars);
134 
135  object stylesheet;
136  if ( !stringp(vars["language"]) ) {
137  //werror("No language defined - setting english !\n");
138  vars["language"] = "english";
139  }
140  if ( objectp(xsl) ) {
141  string lang = vars["language"];
142  stylesheet = xsl->get_stylesheet(lang);
143  }
144  else if ( stringp(xsl) ) {
145  stylesheet = xslt.Stylesheet();
146 
147  stylesheet->set_language(vars["language"]);
148  stylesheet->set_include_callbacks(match_stylesheet,
149  open_stylesheet,
150  read_stylesheet,
151  close_stylesheet);
152  stylesheet->set_content(xsl);
153  }
154  else
155  error("xslt: Invalid run argument for XSL-Stylesheet !");
156 
157  parser->set_xml_data(xml);
158  html = parser->run(stylesheet);
159  };
160  destruct(lock);
161  if ( arrayp(err) || objectp(err) ) {
162  FATAL("Error while processing xml !\n"+PRINT_BT(err));
163 
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);
169  }
170  else if ( err )
171  FATAL("Error running xslt: %O", err);
172  return html;
173 }
174 
175 string get_identifier() { return "libxslt"; }
176 string get_version() { return parser->get_version(); }
177 
178 
179 
180 };