VirtualBox

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

Last change on this file since 32928 was 29835, checked in by vboxsync, 14 years ago

Webservice: change license of generated files to LGPL

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.6 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) 2009-2010 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="websrv-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 $request = new stdClass();
124 $request->_this = $this->handle;
125 $response = $this->connection->__soapCall('<xsl:value-of select="$ifname"/>_<xsl:value-of select="$fname"/>', array((array)$request));
126 <xsl:text>return </xsl:text>
127 <xsl:call-template name="emitOutParam">
128 <xsl:with-param name="type" select="$attrtype" />
129 <xsl:with-param name="value" select="concat('$response->','returnval')" />
130 <xsl:with-param name="safearray" select="@safearray"/>
131 </xsl:call-template><xsl:text>;</xsl:text>
132 }
133</xsl:template>
134
135<xsl:template name="emitSetAttribute">
136 <xsl:param name="ifname" />
137 <xsl:param name="attrname" />
138 <xsl:param name="attrtype" />
139 <xsl:param name="attrsafearray" />
140 <xsl:variable name="fname"><xsl:call-template name="makeSetterName"><xsl:with-param name="attrname" select="$attrname"/></xsl:call-template></xsl:variable>
141 public function <xsl:value-of select="$fname"/>($value) {
142 $request = new stdClass();
143 $request->_this = $this->handle;
144 if (is_int($value) || is_string($value) || is_bool($value)) {
145 $request-><xsl:value-of select="$attrname"/> = $value;
146 }
147 else
148 {
149 $request-><xsl:value-of select="$attrname"/> = $value->handle;
150 }
151 $this->connection->__soapCall('<xsl:value-of select="$ifname"/>_<xsl:value-of select="$fname"/>', array((array)$request));
152 }
153</xsl:template>
154
155<xsl:template name="interface">
156 <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
157 <xsl:variable name="wsmap"><xsl:value-of select="@wsmap" /></xsl:variable>
158 <xsl:variable name="extends"><xsl:value-of select="@extends" /></xsl:variable>
159 <xsl:text>
160/**
161* Generated VBoxWebService Interface Wrapper
162*/
163</xsl:text>
164 <xsl:choose>
165 <xsl:when test="($extends = '$unknown') or ($extends = '$dispatched') or ($extends = '$errorinfo')">
166 <xsl:value-of select="concat('class ', $ifname, ' extends VBox_ManagedObject {&#10;')" />
167 </xsl:when>
168 <xsl:when test="//interface[@name=$extends]">
169 <xsl:value-of select="concat('class ', $ifname, ' extends ', $extends, ' {&#10;')" />
170 </xsl:when>
171 </xsl:choose>
172 <xsl:for-each select="method">
173 <xsl:if test="not((param[@type=($G_setSuppressedInterfaces/@name)])
174 or (param[@mod='ptr']))" >
175 <xsl:call-template name="method">
176 <xsl:with-param name="wsmap" select="$wsmap" />
177 </xsl:call-template>
178 </xsl:if>
179 </xsl:for-each>
180 <xsl:for-each select="attribute">
181 <xsl:variable name="attrname"><xsl:value-of select="@name" /></xsl:variable>
182 <xsl:variable name="attrtype"><xsl:value-of select="@type" /></xsl:variable>
183 <xsl:variable name="attrreadonly"><xsl:value-of select="@readonly" /></xsl:variable>
184 <!-- skip this attribute if it has parameters of a type that has wsmap="suppress" -->
185 <xsl:choose>
186 <xsl:when test="( $attrtype=($G_setSuppressedInterfaces/@name) )">
187 <xsl:comment><xsl:value-of select="concat('skipping attribute ', $attrtype, ' for it is of a suppressed type')" /></xsl:comment>
188 </xsl:when>
189 <xsl:otherwise>
190 <xsl:choose>
191 <xsl:when test="@readonly='yes'">
192 <xsl:comment> readonly attribute <xsl:copy-of select="$ifname" />::<xsl:copy-of select="$attrname" /> </xsl:comment>
193 </xsl:when>
194 <xsl:otherwise>
195 <xsl:comment> read/write attribute <xsl:copy-of select="$ifname" />::<xsl:copy-of select="$attrname" /> </xsl:comment>
196 </xsl:otherwise>
197 </xsl:choose>
198 <!-- aa) get method: emit request and result -->
199 <xsl:call-template name="emitGetAttribute">
200 <xsl:with-param name="ifname" select="$ifname" />
201 <xsl:with-param name="attrname" select="$attrname" />
202 <xsl:with-param name="attrtype" select="$attrtype" />
203 </xsl:call-template>
204 <!-- bb) emit a set method if the attribute is read/write -->
205 <xsl:if test="not($attrreadonly='yes')">
206 <xsl:call-template name="emitSetAttribute">
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:call-template>
211 </xsl:if>
212 </xsl:otherwise>
213 </xsl:choose>
214 </xsl:for-each>
215 <xsl:text>}
216 </xsl:text>
217</xsl:template>
218
219<xsl:template name="collection">
220 <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
221 <xsl:text>
222/**
223* Generated VBoxWebService Managed Object Collection
224*/</xsl:text>
225class <xsl:value-of select="$ifname"/>Collection extends VBox_ManagedObjectCollection {
226 protected $_interfaceName = "<xsl:value-of select="$ifname"/>";
227}
228</xsl:template>
229
230<xsl:template name="interfacestruct">
231 <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
232 <xsl:text>
233/**
234* Generated VBoxWebService Struct
235*/</xsl:text>
236class <xsl:value-of select="$ifname"/> extends VBox_Struct {
237 <xsl:for-each select="attribute">
238 protected $<xsl:value-of select="@name"/>;
239 </xsl:for-each>
240 public function __construct($connection, $values) {
241 $this->connection = $connection;
242 <xsl:for-each select="attribute">
243 $this-><xsl:value-of select="@name"/> = $values-><xsl:value-of select="@name"/><xsl:text>;</xsl:text>
244 </xsl:for-each>
245 }
246
247 <xsl:for-each select="attribute">
248 public function <xsl:call-template name="makeGetterName"><xsl:with-param name="attrname" select="@name"/></xsl:call-template>() {
249 <xsl:text>return </xsl:text>
250 <xsl:call-template name="emitOutParam">
251 <xsl:with-param name="type" select="@type" />
252 <xsl:with-param name="value" select="concat('$this->',@name)" />
253 <xsl:with-param name="safearray" select="@safearray"/>
254 </xsl:call-template>;
255 }
256 </xsl:for-each>
257
258}
259</xsl:template>
260
261<xsl:template name="structcollection">
262 <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
263 <xsl:text>
264/**
265* Generated VBoxWebService Struct Collection
266*/</xsl:text>
267class <xsl:value-of select="$ifname"/>Collection extends VBox_StructCollection {
268 protected $_interfaceName = "<xsl:value-of select="$ifname"/>";
269}
270</xsl:template>
271
272<xsl:template name="genreq">
273 <xsl:param name="wsmap" />
274 <xsl:text>$request = new stdClass()</xsl:text>;
275 <xsl:if test="$wsmap='managed'">
276 $request->_this = $this->handle;
277 </xsl:if>
278 <xsl:for-each select="param[@dir='in']">
279 $request-><xsl:value-of select="@name" /> = $arg_<xsl:value-of select="@name" /><xsl:text>;</xsl:text>
280 </xsl:for-each>
281 $response = $this->connection->__soapCall('<xsl:value-of select="../@name"/>_<xsl:value-of select="@name"/>', array((array)$request));
282 <!-- return needs to be the first one -->
283 return <xsl:if test="param[@dir='out']">
284 <xsl:text>array(</xsl:text>
285 </xsl:if>
286 <xsl:for-each select="param[@dir='return']">
287 <xsl:call-template name="emitOutParam">
288 <xsl:with-param name="type" select="@type" />
289 <xsl:with-param name="value" select="concat('$response->','returnval')" />
290 <xsl:with-param name="safearray" select="@safearray"/>
291 </xsl:call-template>
292 <xsl:if test="../param[@dir='out']">
293 <xsl:text>, </xsl:text>
294 </xsl:if>
295 </xsl:for-each>
296 <xsl:for-each select="param[@dir='out']">
297 <xsl:if test="not(position()=1)">
298 <xsl:text>, </xsl:text>
299 </xsl:if>
300 <xsl:call-template name="emitOutParam">
301 <xsl:with-param name="type" select="@type" />
302 <xsl:with-param name="value" select="concat('$response->',@name)" />
303 <xsl:with-param name="safearray" select="@safearray"/>
304 </xsl:call-template>
305 </xsl:for-each>
306 <xsl:if test="param[@dir='out']">
307 <xsl:text>)</xsl:text>
308 </xsl:if>
309 <xsl:text>;&#10;</xsl:text>
310</xsl:template>
311
312<xsl:template name="method" >
313 <xsl:param name="wsmap" />
314 public function <xsl:value-of select="@name"/><xsl:text>(</xsl:text>
315 <xsl:for-each select="param[@dir='in']">
316 <xsl:if test="not(position()=1)">
317 <xsl:text>, </xsl:text>
318 </xsl:if>
319 <xsl:value-of select="concat('$arg_',@name)"/>
320 </xsl:for-each><xsl:text>) { &#10; </xsl:text>
321 <xsl:call-template name="genreq"><xsl:with-param name="wsmap" select="$wsmap" /></xsl:call-template>
322 <xsl:text> }&#10;</xsl:text>
323</xsl:template>
324
325<xsl:template name="enum">
326 <xsl:text>
327/**
328* Generated VBoxWebService ENUM
329*/</xsl:text>
330class <xsl:value-of select="@name"/> extends VBox_Enum {
331 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>);
332 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>);
333}
334</xsl:template>
335
336<xsl:template name="enumcollection">
337 <xsl:variable name="ifname"><xsl:value-of select="@name" /></xsl:variable>
338 <xsl:text>
339/**
340* Generated VBoxWebService Enum Collection
341*/</xsl:text>
342class <xsl:value-of select="$ifname"/>Collection extends VBox_EnumCollection {
343 protected $_interfaceName = "<xsl:value-of select="$ifname"/>";
344}
345</xsl:template>
346
347<xsl:template match="/">
348<xsl:text>&lt;?php
349
350/*
351 * Copyright (C) 2008-2010 Oracle Corporation
352 *
353 * This file is part of a free software library; you can redistribute
354 * it and/or modify it under the terms of the GNU Lesser General
355 * Public License version 2.1 as published by the Free Software
356 * Foundation and shipped in the "COPYING.LIB" file with this library.
357 * The library is distributed in the hope that it will be useful,
358 * but WITHOUT ANY WARRANTY of any kind.
359 *
360 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if
361 * any license choice other than GPL or LGPL is available it will
362 * apply instead, Oracle elects to use only the Lesser General Public
363 * License version 2.1 (LGPLv2) at this time for any software where
364 * a choice of LGPL license versions is made available with the
365 * language indicating that LGPLv2 or any later version may be used,
366 * or where a choice of which version of the LGPL is applied is
367 * otherwise unspecified.
368 *
369 * This file is autogenerated from VirtualBox.xidl, DO NOT EDIT!
370 */
371
372class VBox_ManagedObject
373{
374 protected $connection;
375 protected $handle;
376
377 public function __construct($soap, $handle = null)
378 {
379 $this->connection = $soap;
380 $this->handle = $handle;
381 }
382
383 public function __toString()
384 {
385 return (string)$this->handle;
386 }
387
388 public function __set($attr, $value)
389 {
390 $methodName = "set" . $attr;
391 if (method_exists($this, $methodName))
392 $this->$methodName($value);
393 else
394 throw new Exception("Attribute does not exist");
395 }
396
397 public function __get($attr)
398 {
399 $methodName = "get" . $attr;
400 if (method_exists($this, $methodName))
401 return $this->$methodName();
402 else
403 throw new Exception("Attribute does not exist");
404 }
405
406 public function getHandle()
407 {
408 return $this->handle;
409 }
410
411 public function cast($class)
412 {
413 if (is_subclass_of($class, 'VBox_ManagedObject'))
414 {
415 return new $class($this->connection, $this->handle);
416 }
417 throw new Exception('Cannot cast VBox_ManagedObject to non-child class VBox_ManagedObject');
418 }
419
420 public function releaseRemote()
421 {
422 try
423 {
424 $request = new stdClass();
425 $request->_this = $this->handle;
426 $this->connection->__soapCall('IManagedObjectRef_release', array((array)$request));
427 } catch (Exception $ex) {}
428 }
429}
430
431abstract class VBox_Collection implements ArrayAccess, Iterator, Countable {
432 protected $_connection;
433 protected $_values;
434 protected $_objects;
435 protected $_interfaceName;
436
437 public function __construct($soap, array $values = array()) {
438 $this->_connection = $soap;
439 $this->_values = $values;
440 $this->_soapToObject();
441 }
442
443 protected function _soapToObject() {
444 $this->_objects = array();
445 foreach($this->_values as $value)
446 {
447 $this->_objects[] = new $this->_interfaceName($this->_connection, $value);
448 }
449 }
450
451 /** ArrayAccess Functions **/
452 public function offsetSet($offset, $value) {
453 if ($value instanceof $this->_interfaceName)
454 {
455 if ($offset)
456 {
457 $this->_objects[$offset] = $value;
458 }
459 else
460 {
461 $this->_objects[] = $value;
462 }
463 }
464 else
465 {
466 throw new Exception("Value must be a instance of " . $this->_interfaceName);
467 }
468 }
469
470 public function offsetExists($offset) {
471 return isset($this->_objects[$offset]);
472 }
473
474 public function offsetUnset($offset) {
475 unset($this->_objects[$offset]);
476 }
477
478 public function offsetGet($offset) {
479 return isset($this->_objects[$offset]) ? $this->_objects[$offset] : null;
480 }
481
482 /** Iterator Functions **/
483 public function rewind() {
484 reset($this->_objects);
485 }
486
487 public function current() {
488 return current($this->_objects);
489 }
490
491 public function key() {
492 return key($this->_objects);
493 }
494
495 public function next() {
496 return next($this->_objects);
497 }
498
499 public function valid() {
500 return ($this->current() !== false);
501 }
502
503 /** Countable Functions **/
504 public function count() {
505 return count($this->_objects);
506 }
507}
508
509class VBox_ManagedObjectCollection extends VBox_Collection {
510 protected $_interfaceName = 'VBox_ManagedObject';
511
512 // Result is undefined if this is called AFTER any call to VBox_Collection::offsetSet or VBox_Collection::offsetUnset
513 public function setInterfaceName($interface) {
514 if (!is_subclass_of($interface, 'VBox_ManagedObject'))
515 {
516 throw new Exception('Cannot set collection interface to non-child class of VBox_ManagedObject');
517 }
518 $this->_interfaceName = $interface;
519 $this->_soapToObject();
520 }
521}
522
523abstract class VBox_Struct {
524 protected $connection;
525
526 public function __get($attr)
527 {
528 $methodName = "get" . $attr;
529 if (method_exists($this, $methodName))
530 return $this->$methodName();
531 else
532 throw new Exception("Attribute does not exist");
533 }
534}
535
536abstract class VBox_StructCollection extends VBox_Collection {
537
538 public function __construct($soap, array $values = array())
539 {
540 if (!(array_values($values) === $values))
541 {
542 $values = array((object)$values); //Fix for when struct return value only contains one list item (e.g. one medium attachment)
543 }
544 parent::__construct($soap, $values);
545 }
546}
547
548abstract class VBox_Enum {
549 protected $_handle;
550
551 public function __construct($connection, $handle)
552 {
553 if (is_string($handle))
554 $this->_handle = $this->ValueMap[$handle];
555 else
556 $this->_handle = $handle;
557 }
558
559 public function __toString()
560 {
561 return (string)$this->NameMap[$this->_handle];
562 }
563}
564
565abstract class VBox_EnumCollection extends VBox_Collection {
566}
567
568</xsl:text>
569 <xsl:for-each select="//interface[@wsmap='managed' or @wsmap='global']">
570 <xsl:call-template name="interface"/>
571 <xsl:call-template name="collection"/>
572 </xsl:for-each>
573 <xsl:for-each select="//interface[@wsmap='struct']">
574 <xsl:call-template name="interfacestruct"/>
575 <xsl:call-template name="structcollection"/>
576 </xsl:for-each>
577 <xsl:for-each select="//enum">
578 <xsl:call-template name="enum"/>
579 <xsl:call-template name="enumcollection"/>
580 </xsl:for-each>
581
582</xsl:template>
583
584</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