VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/websrv-php.xsl@ 45674

Last change on this file since 45674 was 45483, checked in by vboxsync, 12 years ago

Main/glue+idl+webservice: move the type mapping XSLT include file to a central place, it is not webservice specific, and extend it slightly

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.4 KB
Line 
1<xsl:stylesheet version = '1.0'
2 xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
3 xmlns:vbox="http://www.virtualbox.org/">
4
5<!--
6
7 websrv-php.xsl:
8 XSLT stylesheet that generates vboxServiceWrappers.php from
9 VirtualBox.xidl. This PHP file represents our
10 web service API. Depends on WSDL file for actual SOAP bindings.
11
12 Contributed by James Lucas (mjlucas at eng.uts.edu.au).
13
14 Copyright (C) 2008-2013 Oracle Corporation
15
16 This file is part of VirtualBox Open Source Edition (OSE), as
17 available from http://www.virtualbox.org. This file is free software;
18 you can redistribute it and/or modify it under the terms of the GNU
19 General Public License (GPL) as published by the Free Software
20 Foundation, in version 2 as it comes in the "COPYING" file of the
21 VirtualBox OSE distribution. VirtualBox OSE is distributed in the
22 hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
23-->
24
25
26<xsl:output
27 method="text"
28 version="1.0"
29 encoding="utf-8"
30 indent="no"/>
31
32<xsl:include href="../idl/typemap-shared.inc.xsl" />
33
34<xsl:variable name="G_setSuppressedInterfaces"
35 select="//interface[@wsmap='suppress']" />
36
37<xsl:template name="emitOutParam">
38 <xsl:param name="type" />
39 <xsl:param name="value" />
40 <xsl:param name="safearray" />
41
42 <xsl:choose>
43 <xsl:when test="$type='wstring' or $type='uuid'">
44 <xsl:call-template name="emitPrimitive">
45 <xsl:with-param name="type">string</xsl:with-param>
46 <xsl:with-param name="value" select="$value" />
47 <xsl:with-param name="safearray" select="$safearray"/>
48 </xsl:call-template>
49 </xsl:when>
50 <xsl:when test="$type='boolean'">
51 <xsl:call-template name="emitPrimitive">
52 <xsl:with-param name="type">bool</xsl:with-param>
53 <xsl:with-param name="value" select="$value" />
54 <xsl:with-param name="safearray" select="$safearray"/>
55 </xsl:call-template>
56 </xsl:when>
57 <xsl:when test="$type='short' or $type='unsigned short' or $type='long' or $type='octet'">
58 <xsl:call-template name="emitPrimitive">
59 <xsl:with-param name="type">int</xsl:with-param>
60 <xsl:with-param name="value" select="$value" />
61 <xsl:with-param name="safearray" select="$safearray"/>
62 </xsl:call-template>
63 </xsl:when>
64 <xsl:when test="$type='double' or $type='float' or $type='unsigned long' or $type='long long' or $type='unsigned long long'">
65 <xsl:call-template name="emitPrimitive">
66 <xsl:with-param name="type">float</xsl:with-param>
67 <xsl:with-param name="value" select="$value" />
68 <xsl:with-param name="safearray" select="$safearray"/>
69 </xsl:call-template>
70 </xsl:when>
71 <xsl:when test="$type='$unknown'">
72 <xsl:call-template name="emitObject">
73 <xsl:with-param name="type">VBox_ManagedObject</xsl:with-param>
74 <xsl:with-param name="value" select="$value" />
75 <xsl:with-param name="safearray" select="$safearray"/>
76 </xsl:call-template>
77 </xsl:when>
78 <xsl:otherwise>
79 <xsl:call-template name="emitObject">
80 <xsl:with-param name="type" select="$type" />
81 <xsl:with-param name="value" select="$value" />
82 <xsl:with-param name="safearray" select="$safearray"/>
83 </xsl:call-template>
84 </xsl:otherwise>
85 </xsl:choose>
86</xsl:template>
87
88<xsl:template name="emitObject">
89 <xsl:param name="type" />
90 <xsl:param name="value" />
91 <xsl:param name="safearray" />
92 <xsl:choose>
93 <xsl:when test="$safearray='yes'">
94 <xsl:text>new </xsl:text><xsl:value-of select="$type" />Collection ($this->connection, (array)<xsl:value-of select="$value"/><xsl:text>)</xsl:text>
95 </xsl:when>
96 <xsl:otherwise>
97 <xsl:text>new </xsl:text><xsl:value-of select="$type" /> ($this->connection, <xsl:value-of select="$value"/><xsl:text>)</xsl:text>
98 </xsl:otherwise>
99 </xsl:choose>
100</xsl:template>
101
102<xsl:template name="emitPrimitive">
103 <xsl:param name="type" />
104 <xsl:param name="value" />
105 <xsl:param name="safearray" />
106 <xsl:choose>
107 <xsl:when test="$safearray='yes'">
108 <xsl:text>(array)</xsl:text><xsl:value-of select="$value"/>
109 </xsl:when>
110 <xsl:otherwise>
111 <xsl:text>(</xsl:text><xsl:value-of select="$type" /><xsl:text>)</xsl:text><xsl:value-of select="$value"/>
112 </xsl:otherwise>
113 </xsl:choose>
114</xsl:template>
115
116<xsl:template name="emitGetAttribute">
117 <xsl:param name="ifname" />
118 <xsl:param name="attrname" />
119 <xsl:param name="attrtype" />
120 <xsl:param name="attrsafearray" />
121 <xsl:variable name="fname"><xsl:call-template name="makeGetterName"><xsl:with-param name="attrname" select="$attrname"/></xsl:call-template> </xsl:variable>
122 public function <xsl:value-of select="$fname"/>()
123 {
124 $request = new stdClass();
125 $request->_this = $this->handle;
126 $response = $this->connection->__soapCall('<xsl:value-of select="$ifname"/>_<xsl:value-of select="$fname"/>', array((array)$request));
127 <xsl:text>return </xsl:text>
128 <xsl:call-template name="emitOutParam">
129 <xsl:with-param name="type" select="$attrtype" />
130 <xsl:with-param name="value" select="concat('$response->','returnval')" />
131 <xsl:with-param name="safearray" select="@safearray"/>
132 </xsl:call-template><xsl:text>;</xsl:text>
133 }
134</xsl:template>
135
136<xsl:template name="emitSetAttribute">
137 <xsl:param name="ifname" />
138 <xsl:param name="attrname" />
139 <xsl:param name="attrtype" />
140 <xsl:param name="attrsafearray" />
141 <xsl:variable name="fname"><xsl:call-template name="makeSetterName"><xsl:with-param name="attrname" select="$attrname"/></xsl:call-template></xsl:variable>
142 public function <xsl:value-of select="$fname"/>($value)
143 {
144 $request = new stdClass();
145 $request->_this = $this->handle;
146<xsl:choose>
147<xsl:when test="$attrsafearray='yes'"> if (is_array($value) || is_null($value) || is_scalar($value))</xsl:when>
148<xsl:otherwise> if (is_null($value) || is_scalar($value))</xsl:otherwise>
149</xsl:choose>
150 {
151 $request-><xsl:value-of select="$attrname"/> = $value;
152 }
153 else
154 {
155 $request-><xsl:value-of select="$attrname"/> = $value->handle;
156 }
157 $this->connection->__soapCall('<xsl:value-of select="$ifname"/>_<xsl:value-of select="$fname"/>', array((array)$request));
158 }
159</xsl:template>
160
161<xsl:template name="interface">
162 <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
163 <xsl:variable name="wsmap"><xsl:value-of select="@wsmap" /></xsl:variable>
164 <xsl:variable name="extends"><xsl:value-of select="@extends" /></xsl:variable>
165 <xsl:text>
166/**
167 * Generated VBoxWebService Interface Wrapper
168 */
169</xsl:text>
170 <xsl:choose>
171 <xsl:when test="($extends = '$unknown') or ($extends = '$dispatched') or ($extends = '$errorinfo')">
172 <xsl:value-of select="concat('class ', $ifname, ' extends VBox_ManagedObject&#10;{&#10;')" />
173 </xsl:when>
174 <xsl:when test="//interface[@name=$extends]">
175 <xsl:value-of select="concat('class ', $ifname, ' extends ', $extends, '&#10;{&#10;')" />
176 </xsl:when>
177 </xsl:choose>
178 <xsl:for-each select="method">
179 <xsl:if test="not((param[@type=($G_setSuppressedInterfaces/@name)])
180 or (param[@mod='ptr']))" >
181 <xsl:call-template name="method">
182 <xsl:with-param name="wsmap" select="$wsmap" />
183 </xsl:call-template>
184 </xsl:if>
185 </xsl:for-each>
186 <xsl:for-each select="attribute">
187 <xsl:variable name="attrname"><xsl:value-of select="@name" /></xsl:variable>
188 <xsl:variable name="attrtype"><xsl:value-of select="@type" /></xsl:variable>
189 <xsl:variable name="attrreadonly"><xsl:value-of select="@readonly" /></xsl:variable>
190 <xsl:variable name="attrsafearray"><xsl:value-of select="@safearray" /></xsl:variable>
191 <!-- skip this attribute if it has parameters of a type that has wsmap="suppress" -->
192 <xsl:choose>
193 <xsl:when test="( $attrtype=($G_setSuppressedInterfaces/@name) )">
194 <xsl:comment><xsl:value-of select="concat('skipping attribute ', $attrtype, ' for it is of a suppressed type')" /></xsl:comment>
195 </xsl:when>
196 <xsl:otherwise>
197 <xsl:choose>
198 <xsl:when test="@readonly='yes'">
199 <xsl:comment> readonly attribute <xsl:copy-of select="$ifname" />::<xsl:copy-of select="$attrname" /> </xsl:comment>
200 </xsl:when>
201 <xsl:otherwise>
202 <xsl:comment> read/write attribute <xsl:copy-of select="$ifname" />::<xsl:copy-of select="$attrname" /> </xsl:comment>
203 </xsl:otherwise>
204 </xsl:choose>
205 <!-- aa) get method: emit request and result -->
206 <xsl:call-template name="emitGetAttribute">
207 <xsl:with-param name="ifname" select="$ifname" />
208 <xsl:with-param name="attrname" select="$attrname" />
209 <xsl:with-param name="attrtype" select="$attrtype" />
210 <xsl:with-param name="attrsafearray" select="$attrsafearray" />
211 </xsl:call-template>
212 <!-- bb) emit a set method if the attribute is read/write -->
213 <xsl:if test="not($attrreadonly='yes')">
214 <xsl:call-template name="emitSetAttribute">
215 <xsl:with-param name="ifname" select="$ifname" />
216 <xsl:with-param name="attrname" select="$attrname" />
217 <xsl:with-param name="attrtype" select="$attrtype" />
218 <xsl:with-param name="attrsafearray" select="$attrsafearray" />
219 </xsl:call-template>
220 </xsl:if>
221 </xsl:otherwise>
222 </xsl:choose>
223 </xsl:for-each>
224 <xsl:text>}
225</xsl:text>
226</xsl:template>
227
228<xsl:template name="collection">
229 <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
230 <xsl:text>
231/**
232 * Generated VBoxWebService Managed Object Collection
233 */</xsl:text>
234class <xsl:value-of select="$ifname"/>Collection extends VBox_ManagedObjectCollection
235{
236 protected $_interfaceName = "<xsl:value-of select="$ifname"/>";
237}
238</xsl:template>
239
240<xsl:template name="interfacestruct">
241 <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
242 <xsl:text>
243/**
244 * Generated VBoxWebService Struct
245 */</xsl:text>
246class <xsl:value-of select="$ifname"/> extends VBox_Struct
247{
248<xsl:for-each select="attribute"> protected $<xsl:value-of select="@name"/>;
249</xsl:for-each>
250 public function __construct($connection, $values)
251 {
252 $this->connection = $connection;
253<xsl:for-each select="attribute"> $this-><xsl:value-of select="@name"/> = $values-><xsl:value-of select="@name"/>;
254</xsl:for-each> }
255
256<xsl:for-each select="attribute"> public function <xsl:call-template name="makeGetterName"><xsl:with-param name="attrname" select="@name"/></xsl:call-template>()
257 {
258 <xsl:text>return </xsl:text>
259 <xsl:call-template name="emitOutParam">
260 <xsl:with-param name="type" select="@type" />
261 <xsl:with-param name="value" select="concat('$this->',@name)" />
262 <xsl:with-param name="safearray" select="@safearray"/>
263 </xsl:call-template>;
264 }
265</xsl:for-each>}
266</xsl:template>
267
268<xsl:template name="structcollection">
269 <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
270 <xsl:text>
271/**
272 * Generated VBoxWebService Struct Collection
273 */</xsl:text>
274class <xsl:value-of select="$ifname"/>Collection extends VBox_StructCollection
275{
276 protected $_interfaceName = "<xsl:value-of select="$ifname"/>";
277}
278</xsl:template>
279
280<xsl:template name="genreq">
281 <xsl:param name="wsmap" />
282 <xsl:text> $request = new stdClass();
283</xsl:text>
284 <xsl:if test="$wsmap='managed'"> $request->_this = $this->handle;</xsl:if>
285 <xsl:for-each select="param[@dir='in']">
286 $request-><xsl:value-of select="@name" /> = $arg_<xsl:value-of select="@name" /><xsl:text>;</xsl:text>
287 </xsl:for-each>
288 $response = $this->connection->__soapCall('<xsl:value-of select="../@name"/>_<xsl:value-of select="@name"/>', array((array)$request));
289 return <xsl:if test="param[@dir='out']">
290 <xsl:text>array(</xsl:text>
291 </xsl:if>
292 <xsl:for-each select="param[@dir='return']">
293 <xsl:call-template name="emitOutParam">
294 <xsl:with-param name="type" select="@type" />
295 <xsl:with-param name="value" select="concat('$response->','returnval')" />
296 <xsl:with-param name="safearray" select="@safearray"/>
297 </xsl:call-template>
298 <xsl:if test="../param[@dir='out']">
299 <xsl:text>, </xsl:text>
300 </xsl:if>
301 </xsl:for-each>
302 <xsl:for-each select="param[@dir='out']">
303 <xsl:if test="not(position()=1)">
304 <xsl:text>, </xsl:text>
305 </xsl:if>
306 <xsl:call-template name="emitOutParam">
307 <xsl:with-param name="type" select="@type" />
308 <xsl:with-param name="value" select="concat('$response->',@name)" />
309 <xsl:with-param name="safearray" select="@safearray"/>
310 </xsl:call-template>
311 </xsl:for-each>
312 <xsl:if test="param[@dir='out']">
313 <xsl:text>)</xsl:text>
314 </xsl:if>
315 <xsl:text>;&#10;</xsl:text>
316</xsl:template>
317
318<xsl:template name="method" >
319 <xsl:param name="wsmap" />
320 public function <xsl:value-of select="@name"/><xsl:text>(</xsl:text>
321 <xsl:for-each select="param[@dir='in']">
322 <xsl:if test="not(position()=1)">
323 <xsl:text>, </xsl:text>
324 </xsl:if>
325 <xsl:value-of select="concat('$arg_',@name)"/>
326 </xsl:for-each> <xsl:text>)&#10; {&#10;</xsl:text>
327 <xsl:call-template name="genreq"><xsl:with-param name="wsmap" select="$wsmap" /></xsl:call-template>
328 <xsl:text> }&#10;</xsl:text>
329</xsl:template>
330
331<xsl:template name="enum">
332 <xsl:text>
333/**
334 * Generated VBoxWebService ENUM
335 */</xsl:text>
336class <xsl:value-of select="@name"/> extends VBox_Enum
337{
338 public $NameMap = array(<xsl:for-each select="const"><xsl:if test="not(@wsmap='suppress')"><xsl:value-of select="@value"/> => '<xsl:value-of select="@name"/>'<xsl:if test="not(position()=last())">, </xsl:if></xsl:if></xsl:for-each>);
339 public $ValueMap = array(<xsl:for-each select="const"><xsl:if test="not(@wsmap='suppress')">'<xsl:value-of select="@name"/>' => <xsl:value-of select="@value"/><xsl:if test="not(position()=last())">, </xsl:if></xsl:if></xsl:for-each>);
340}
341</xsl:template>
342
343<xsl:template name="enumcollection">
344 <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
345 <xsl:text>
346/**
347 * Generated VBoxWebService Enum Collection
348 */</xsl:text>
349class <xsl:value-of select="$ifname"/>Collection extends VBox_EnumCollection
350{
351 protected $_interfaceName = "<xsl:value-of select="$ifname"/>";
352}
353</xsl:template>
354
355<xsl:template name="comResultCodes">
356 const <xsl:value-of select="@name"/> = <xsl:value-of select="@value"/>;
357</xsl:template>
358
359<xsl:template match="/">
360<xsl:text>&lt;?php
361
362/*
363 * Copyright (C) 2008-2013 Oracle Corporation
364 *
365 * This file is part of a free software library; you can redistribute
366 * it and/or modify it under the terms of the GNU Lesser General
367 * Public License version 2.1 as published by the Free Software
368 * Foundation and shipped in the "COPYING.LIB" file with this library.
369 * The library is distributed in the hope that it will be useful,
370 * but WITHOUT ANY WARRANTY of any kind.
371 *
372 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if
373 * any license choice other than GPL or LGPL is available it will
374 * apply instead, Oracle elects to use only the Lesser General Public
375 * License version 2.1 (LGPLv2) at this time for any software where
376 * a choice of LGPL license versions is made available with the
377 * language indicating that LGPLv2 or any later version may be used,
378 * or where a choice of which version of the LGPL is applied is
379 * otherwise unspecified.
380 *
381 * This file is autogenerated from VirtualBox.xidl, DO NOT EDIT!
382 */
383
384class VBox_ManagedObject
385{
386 protected $connection;
387 protected $handle;
388
389 public function __construct($soap, $handle = null)
390 {
391 $this->connection = $soap;
392 $this->handle = $handle;
393 }
394
395 public function __toString()
396 {
397 return (string)$this->handle;
398 }
399
400 public function __set($attr, $value)
401 {
402 $methodName = "set" . $attr;
403 if (method_exists($this, $methodName))
404 $this->$methodName($value);
405 else
406 throw new Exception("Attribute does not exist");
407 }
408
409 public function __get($attr)
410 {
411 $methodName = "get" . $attr;
412 if (method_exists($this, $methodName))
413 return $this->$methodName();
414 else
415 throw new Exception("Attribute does not exist");
416 }
417
418 public function getHandle()
419 {
420 return $this->handle;
421 }
422
423 public function cast($class)
424 {
425 if (is_subclass_of($class, 'VBox_ManagedObject'))
426 {
427 return new $class($this->connection, $this->handle);
428 }
429 throw new Exception('Cannot cast VBox_ManagedObject to non-child class VBox_ManagedObject');
430 }
431
432 public function releaseRemote()
433 {
434 try
435 {
436 $request = new stdClass();
437 $request->_this = $this->handle;
438 $this->connection->__soapCall('IManagedObjectRef_release', array((array)$request));
439 }
440 catch (Exception $ex)
441 {
442 }
443 }
444}
445
446abstract class VBox_Collection implements ArrayAccess, Iterator, Countable
447{
448 protected $_connection;
449 protected $_values;
450 protected $_objects;
451 protected $_interfaceName;
452
453 public function __construct($soap, array $values = array())
454 {
455 $this->_connection = $soap;
456 $this->_values = $values;
457 $this->_soapToObject();
458 }
459
460 protected function _soapToObject()
461 {
462 $this->_objects = array();
463 foreach($this->_values as $value)
464 {
465 $this->_objects[] = new $this->_interfaceName($this->_connection, $value);
466 }
467 }
468
469 /** ArrayAccess Functions **/
470 public function offsetSet($offset, $value)
471 {
472 if ($value instanceof $this->_interfaceName)
473 {
474 if ($offset)
475 {
476 $this->_objects[$offset] = $value;
477 }
478 else
479 {
480 $this->_objects[] = $value;
481 }
482 }
483 else
484 {
485 throw new Exception("Value must be a instance of " . $this->_interfaceName);
486 }
487 }
488
489 public function offsetExists($offset)
490 {
491 return isset($this->_objects[$offset]);
492 }
493
494 public function offsetUnset($offset)
495 {
496 unset($this->_objects[$offset]);
497 }
498
499 public function offsetGet($offset)
500 {
501 return isset($this->_objects[$offset]) ? $this->_objects[$offset] : null;
502 }
503
504 /** Iterator Functions **/
505 public function rewind()
506 {
507 reset($this->_objects);
508 }
509
510 public function current()
511 {
512 return current($this->_objects);
513 }
514
515 public function key()
516 {
517 return key($this->_objects);
518 }
519
520 public function next()
521 {
522 return next($this->_objects);
523 }
524
525 public function valid()
526 {
527 return ($this->current() !== false);
528 }
529
530 /** Countable Functions **/
531 public function count()
532 {
533 return count($this->_objects);
534 }
535}
536
537class VBox_ManagedObjectCollection extends VBox_Collection
538{
539 protected $_interfaceName = 'VBox_ManagedObject';
540
541 // Result is undefined if this is called AFTER any call to VBox_Collection::offsetSet or VBox_Collection::offsetUnset
542 public function setInterfaceName($interface)
543 {
544 if (!is_subclass_of($interface, 'VBox_ManagedObject'))
545 {
546 throw new Exception('Cannot set collection interface to non-child class of VBox_ManagedObject');
547 }
548 $this->_interfaceName = $interface;
549 $this->_soapToObject();
550 }
551}
552
553abstract class VBox_Struct
554{
555 protected $connection;
556
557 public function __get($attr)
558 {
559 $methodName = "get" . $attr;
560 if (method_exists($this, $methodName))
561 return $this->$methodName();
562 else
563 throw new Exception("Attribute does not exist");
564 }
565}
566
567abstract class VBox_StructCollection extends VBox_Collection
568{
569
570 public function __construct($soap, array $values = array())
571 {
572 if (!(array_values($values) === $values))
573 {
574 $values = array((object)$values); //Fix for when struct return value only contains one list item (e.g. one medium attachment)
575 }
576 parent::__construct($soap, $values);
577 }
578}
579
580abstract class VBox_Enum
581{
582 protected $_handle;
583
584 public function __construct($connection, $handle)
585 {
586 if (is_string($handle))
587 $this->_handle = $this->ValueMap[$handle];
588 else
589 $this->_handle = $handle;
590 }
591
592 public function __toString()
593 {
594 return (string)$this->NameMap[$this->_handle];
595 }
596}
597
598abstract class VBox_EnumCollection extends VBox_Collection
599{
600}
601
602</xsl:text>
603
604<xsl:text>
605/**
606 * VirtualBox COM result codes
607 */
608class VirtualBox_COM_result_codes
609{
610</xsl:text>
611 <xsl:for-each select="/idl/library/result">
612 <xsl:call-template name="comResultCodes"/>
613 </xsl:for-each>
614<xsl:text>
615}
616</xsl:text>
617 <xsl:for-each select="//interface[@wsmap='managed' or @wsmap='global']">
618 <xsl:call-template name="interface"/>
619 <xsl:call-template name="collection"/>
620 </xsl:for-each>
621 <xsl:for-each select="//interface[@wsmap='struct']">
622 <xsl:call-template name="interfacestruct"/>
623 <xsl:call-template name="structcollection"/>
624 </xsl:for-each>
625 <xsl:for-each select="//enum">
626 <xsl:call-template name="enum"/>
627 <xsl:call-template name="enumcollection"/>
628 </xsl:for-each>
629
630</xsl:template>
631
632</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette