-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastXSL.php
325 lines (297 loc) · 9.17 KB
/
FastXSL.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
define('FASTXSL_NOCACHE_RESULT', 0);
define('FASTXSL_PRMCACHE_RESULT', 1);
define('FASTXSL_SHMCACHE_RESULT', 2);
/**
* The base class for which documents are managed, the FastXSL_Cache and FastXSL_NoCache classes
* both inherit from this class.
*
* This class is not directly instiable, instead use either FastXSL_Cache or FastXSL_NoCache.
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*
*/
class FastXSL_Base {
/**
* Whether or not the files passed to FastXSL will be absolute paths. If this
* variable is set to false, then a realpath() will be done on every file to
* assure that it is being accessed through an absolute path.
*
* @access public
* @since FastXSL 1.0
*/
var $useAbsolutePath = false;
/**
* Whether or not to stat() the documents in the fastxsl cache. If this is turned off,
* then documents will not be checked for freshness, and you need to gracefully restart
* Apache in order for new documents to be recognized.
*
* @access public
* @since FastXSL 1.0
*/
var $noStat = false;
function FastXSL_Base() {
}
/**
* Resolves a relative path to an absolute path using the realpath() system call which not
* only expands paths, but properly handles symlinks and the like. This function respects
* the $useAbsolutePath property, and will avoid the expensive realpath() call if
* $useAbsolutePath is off.
*
* @param string The filename to resolve
* @param string The class and method name pair for error reporting
*
* @return boolean
*
* @access private
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function resolvePath(&$filename, $class_meth_name) {
if ($this->useAbsolutePath == false) {
$resolved_filename = realpath($filename);
if ($resolved_filename === false) {
trigger_error(E_WARNING, "Cannot resolve path of XML file $filename passed to $class_meth_name");
return false;
}
$filename = $resolved_filename;
}
return true;
}
/**
* Parses an XML file into a FastXSL XML resource for use with the transform() or profile() methods.
*
* @param string The name of the file you'd like to parse
*
* @return resource the FastXSL XML document
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function parseXMLFile($filename) {
$this->resolvePath($filename, "FastXSL::setParseXMLFile()");
return fastxsl_xml_parsefile($filename);
}
/**
* Parses XML data into a FastXSL XML resource for use with the transform() or profile() methods.
*
* @param string The XML data to compile
*
* @return resource The FastXSL XML Document
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function parseXMLData($data) {
return fastxsl_xml_parsestring($data);
}
/**
* Parses an XSL stylesheet into a FastXSL Stylesheet resource for use with the transformParsed() or profile()
* methods.
*
* @param string The stylesheet file to compile
*
* @return resource The FastXSL stylesheet document
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function parseStylesheetFile($stylesheet_file) {
$this->resolvePath($stylesheet_file, "FastXSL::parseStylesheetFile()");
return fastxsl_stylesheet_parsefile($stylesheet_file);
}
function &transformParsed($stylesheet, $xml, $parameters = null)
{
$res = fastxsl_nocache_transform($stylesheet, $xml, $parameters);
if (!$res) {
return false;
}
$resobj = &new FastXSL_Result(FASTXSL_NOCACHE_RESULT, $stylesheet, $res);
return $resobj;
}
/**
* Profile the transformation of an XML document against a XSL stylesheet.
*
* @param resource The Stylesheet Document to profile
* @param resource The XML Document to profile
* @param array Transformation resources
* @param string The directory to place the profile results
*
* @return mixed A FastXSL result object on success, false on failure
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function &profile($stylesheet, $xml, $parameters, $profiledir) {
$res = fastxsl_nocache_profile($stylesheet, $xml, $parameters, $filename);
if (!$res) {
return false;
}
$resobj = &new FastXSL_Result(FASTXSL_NOCACHE_RESULT, $stylesheet, $res);
return $resobj;
}
}
/**
* A container that holds the result of an XSL transformation and provides a set of
* common methods for acting on that container.
*
* This class is instantiated by the transformation methods and should not be directly
* instantiated.
*
* @access private
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
class FastXSL_Result {
var $type;
var $stylesheet;
var $result;
/**
* Constructor sets up the context for this object.
*
* @param mixed The stylesheet associated with the resulting XML document
* @param resource The resulting XML document from a FastXSL transformation
*
* @access private
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function FastXSL_Result($type, $stylesheet, $result) {
$this->type = $type;
$this->stylesheet = $stylesheet;
$this->result = $result;
}
/**
* Return the value of this result object as a string
*
* @return string The string value of this XML document object
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function toString() {
switch ($this->type) {
case FASTXSL_NOCACHE_RESULT:
return fastxsl_nocache_tostring($this->stylesheet, $this->result);
case FASTXSL_PRMCACHE_RESULT:
return fastxsl_prmcache_tostring($this->stylesheet, $this->result);
case FASTXSL_SHMCACHE_RESULT:
return fastxsl_shmcache_tostring($this->stylesheet, $this->result);
}
}
}
/**
* FastXSL_Cache extends FastXSL_Base to provide shared memory cached transformations
* to Apache processes. This class calls the underlying fastxsl api's to transparently
* markup the transformations.
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
class FastXSL_ShmCache extends FastXSL_Base {
/**
* Constructor - calls the FastXSL_Base constructor.
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function FastXSL_ShmCache() {
$this->FastXSL_Base();
}
/**
* Transform an XSL stylesheet from the shared memory cache against an
* XML document with the given parameters. If the passed stylesheet does not
* exist in shared memory, parse the stylesheet and place it in shared memory.
*
* @param string The filename of the sylesheet
* @param resource The XML document to parse
* @param array The parameters for the transformation
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function &transform($stylesheet, $xml, $parameters = null) {
$this->resolvePath($stylesheet, "FastXSL_Cache::transform()");
$res = fastxsl_shmcache_transform($stylesheet, $xml, $parameters);
if (!$res) {
return false;
}
$resobj = &new FastXSL_Result(FASTXSL_SHMCACHE_RESULT, $stylesheet, $res);
return $resobj;
}
}
/**
* FastXSL_PrmCache extends FastXSL_Base to provide memory cached transformations
* to Apache processes. This class calls the underlying fastxsl api's to transparently
* markup the transformations.
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
class FastXSL_PrmCache extends FastXSL_Base {
/**
* Constructor - calls the FastXSL_Base constructor.
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function FastXSL_PrmCache() {
$this->FastXSL_Base();
}
/**
* Transform an XSL stylesheet from the memory cache against an
* XML document with the given parameters. If the passed stylesheet does not
* exist in shared memory, parse the stylesheet and place it in shared memory.
*
* @param string The filename of the sylesheet
* @param resource The XML document to parse
* @param array The parameters for the transformation
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*/
function &transform($stylesheet, $xml, $parameters = null) {
$this->resolvePath($stylesheet, "FastXSL_Cache::transform()");
$res = fastxsl_prmcache_transform($stylesheet, $xml, $parameters);
if (!$res) {
return false;
}
$resobj = &new FastXSL_Result(FASTXSL_PRMCACHE_RESULT, $stylesheet, $res);
return $resobj;
}
}
/**
* This class implements a version of FastXSL that does not cache the documents in
* shared memory. This class can directly replace the FastXSL_Cache class, and is
* useful for isolating cases where the shared memory class FastXSL_Cache, may be causing
* problems.
*
* Method signatures are the same as those of the FastXSL_Cache class.
*
* @access public
* @author Sterling Hughes <[email protected]>
* @since FastXSL 1.0
*
*/
class FastXSL_NoCache extends FastXSL_Base {
function FastXSL() {
$this->FastXSL_Base();
}
function &transform($stylesheet_file, $xml, $parameters = null) {
return $this->transformParsed($this->parseStylesheetFile($stylesheet_file), $xml, $parameters);
}
}
?>