class.xmlschema.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. <?php
  2. /**
  3. * parses an XML Schema, allows access to it's data, other utility methods.
  4. * imperfect, no validation... yet, but quite functional.
  5. *
  6. * @author Dietrich Ayala <dietrich@ganx4.com>
  7. * @author Scott Nichol <snichol@users.sourceforge.net>
  8. * @access public
  9. */
  10. class nusoap_xmlschema extends nusoap_base {
  11. // files
  12. var $schema = '';
  13. var $xml = '';
  14. // namespaces
  15. var $enclosingNamespaces;
  16. // schema info
  17. var $schemaInfo = array();
  18. var $schemaTargetNamespace = '';
  19. // types, elements, attributes defined by the schema
  20. var $attributes = array();
  21. var $complexTypes = array();
  22. var $complexTypeStack = array();
  23. var $currentComplexType = null;
  24. var $elements = array();
  25. var $elementStack = array();
  26. var $currentElement = null;
  27. var $simpleTypes = array();
  28. var $simpleTypeStack = array();
  29. var $currentSimpleType = null;
  30. // imports
  31. var $imports = array();
  32. // parser vars
  33. var $parser;
  34. var $position = 0;
  35. var $depth = 0;
  36. var $depth_array = array();
  37. var $message = array();
  38. var $defaultNamespace = array();
  39. /**
  40. * constructor
  41. *
  42. * @param string $schema schema document URI
  43. * @param string $xml xml document URI
  44. * @param string $namespaces namespaces defined in enclosing XML
  45. * @access public
  46. */
  47. function __construct($schema='',$xml='',$namespaces=array()){
  48. parent::__construct();
  49. $this->debug('nusoap_xmlschema class instantiated, inside constructor');
  50. // files
  51. $this->schema = $schema;
  52. $this->xml = $xml;
  53. // namespaces
  54. $this->enclosingNamespaces = $namespaces;
  55. $this->namespaces = array_merge($this->namespaces, $namespaces);
  56. // parse schema file
  57. if($schema != ''){
  58. $this->debug('initial schema file: '.$schema);
  59. $this->parseFile($schema, 'schema');
  60. }
  61. // parse xml file
  62. if($xml != ''){
  63. $this->debug('initial xml file: '.$xml);
  64. $this->parseFile($xml, 'xml');
  65. }
  66. }
  67. /**
  68. * parse an XML file
  69. *
  70. * @param string $xml path/URL to XML file
  71. * @param string $type (schema | xml)
  72. * @return boolean
  73. * @access public
  74. */
  75. function parseFile($xml,$type){
  76. // parse xml file
  77. if($xml != ""){
  78. $xmlStr = @join("",@file($xml));
  79. if($xmlStr == ""){
  80. $msg = 'Error reading XML from '.$xml;
  81. $this->setError($msg);
  82. $this->debug($msg);
  83. return false;
  84. } else {
  85. $this->debug("parsing $xml");
  86. $this->parseString($xmlStr,$type);
  87. $this->debug("done parsing $xml");
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. * parse an XML string
  95. *
  96. * @param string $xml path or URL
  97. * @param string $type (schema|xml)
  98. * @access private
  99. */
  100. function parseString($xml,$type){
  101. // parse xml string
  102. if($xml != ""){
  103. // Create an XML parser.
  104. $this->parser = xml_parser_create();
  105. // Set the options for parsing the XML data.
  106. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
  107. // Set the object for the parser.
  108. xml_set_object($this->parser, $this);
  109. // Set the element handlers for the parser.
  110. if($type == "schema"){
  111. xml_set_element_handler($this->parser, 'schemaStartElement','schemaEndElement');
  112. xml_set_character_data_handler($this->parser,'schemaCharacterData');
  113. } elseif($type == "xml"){
  114. xml_set_element_handler($this->parser, 'xmlStartElement','xmlEndElement');
  115. xml_set_character_data_handler($this->parser,'xmlCharacterData');
  116. }
  117. // Parse the XML file.
  118. if(!xml_parse($this->parser,$xml,true)){
  119. // Display an error message.
  120. $errstr = sprintf('XML error parsing XML schema on line %d: %s',
  121. xml_get_current_line_number($this->parser),
  122. xml_error_string(xml_get_error_code($this->parser))
  123. );
  124. $this->debug($errstr);
  125. $this->debug("XML payload:\n" . $xml);
  126. $this->setError($errstr);
  127. }
  128. xml_parser_free($this->parser);
  129. } else{
  130. $this->debug('no xml passed to parseString()!!');
  131. $this->setError('no xml passed to parseString()!!');
  132. }
  133. }
  134. /**
  135. * gets a type name for an unnamed type
  136. *
  137. * @param string Element name
  138. * @return string A type name for an unnamed type
  139. * @access private
  140. */
  141. function CreateTypeName($ename) {
  142. $scope = '';
  143. for ($i = 0; $i < count($this->complexTypeStack); $i++) {
  144. $scope .= $this->complexTypeStack[$i] . '_';
  145. }
  146. return $scope . $ename . '_ContainedType';
  147. }
  148. /**
  149. * start-element handler
  150. *
  151. * @param string $parser XML parser object
  152. * @param string $name element name
  153. * @param string $attrs associative array of attributes
  154. * @access private
  155. */
  156. function schemaStartElement($parser, $name, $attrs) {
  157. // position in the total number of elements, starting from 0
  158. $pos = $this->position++;
  159. $depth = $this->depth++;
  160. // set self as current value for this depth
  161. $this->depth_array[$depth] = $pos;
  162. $this->message[$pos] = array('cdata' => '');
  163. if ($depth > 0) {
  164. $this->defaultNamespace[$pos] = $this->defaultNamespace[$this->depth_array[$depth - 1]];
  165. } else {
  166. $this->defaultNamespace[$pos] = false;
  167. }
  168. // get element prefix
  169. if($prefix = $this->getPrefix($name)){
  170. // get unqualified name
  171. $name = $this->getLocalPart($name);
  172. } else {
  173. $prefix = '';
  174. }
  175. // loop thru attributes, expanding, and registering namespace declarations
  176. if(count($attrs) > 0){
  177. foreach($attrs as $k => $v){
  178. // if ns declarations, add to class level array of valid namespaces
  179. if(preg_match('/^xmlns/',$k)){
  180. //$this->xdebug("$k: $v");
  181. //$this->xdebug('ns_prefix: '.$this->getPrefix($k));
  182. if($ns_prefix = substr(strrchr($k,':'),1)){
  183. //$this->xdebug("Add namespace[$ns_prefix] = $v");
  184. $this->namespaces[$ns_prefix] = $v;
  185. } else {
  186. $this->defaultNamespace[$pos] = $v;
  187. if (! $this->getPrefixFromNamespace($v)) {
  188. $this->namespaces['ns'.(count($this->namespaces)+1)] = $v;
  189. }
  190. }
  191. if($v == 'http://www.w3.org/2001/XMLSchema' || $v == 'http://www.w3.org/1999/XMLSchema' || $v == 'http://www.w3.org/2000/10/XMLSchema'){
  192. $this->XMLSchemaVersion = $v;
  193. $this->namespaces['xsi'] = $v.'-instance';
  194. }
  195. }
  196. }
  197. foreach($attrs as $k => $v){
  198. // expand each attribute
  199. $k = strpos($k,':') ? $this->expandQname($k) : $k;
  200. $v = strpos($v,':') ? $this->expandQname($v) : $v;
  201. $eAttrs[$k] = $v;
  202. }
  203. $attrs = $eAttrs;
  204. } else {
  205. $attrs = array();
  206. }
  207. // find status, register data
  208. switch($name){
  209. case 'all': // (optional) compositor content for a complexType
  210. case 'choice':
  211. case 'group':
  212. case 'sequence':
  213. //$this->xdebug("compositor $name for currentComplexType: $this->currentComplexType and currentElement: $this->currentElement");
  214. $this->complexTypes[$this->currentComplexType]['compositor'] = $name;
  215. //if($name == 'all' || $name == 'sequence'){
  216. // $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
  217. //}
  218. break;
  219. case 'attribute': // complexType attribute
  220. //$this->xdebug("parsing attribute $attrs[name] $attrs[ref] of value: ".$attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']);
  221. $this->xdebug("parsing attribute:");
  222. $this->appendDebug($this->varDump($attrs));
  223. if (!isset($attrs['form'])) {
  224. // TODO: handle globals
  225. $attrs['form'] = $this->schemaInfo['attributeFormDefault'];
  226. }
  227. if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
  228. $v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
  229. if (!strpos($v, ':')) {
  230. // no namespace in arrayType attribute value...
  231. if ($this->defaultNamespace[$pos]) {
  232. // ...so use the default
  233. $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'] = $this->defaultNamespace[$pos] . ':' . $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
  234. }
  235. }
  236. }
  237. if(isset($attrs['name'])){
  238. $this->attributes[$attrs['name']] = $attrs;
  239. $aname = $attrs['name'];
  240. } elseif(isset($attrs['ref']) && $attrs['ref'] == 'http://schemas.xmlsoap.org/soap/encoding/:arrayType'){
  241. if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
  242. $aname = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
  243. } else {
  244. $aname = '';
  245. }
  246. } elseif(isset($attrs['ref'])){
  247. $aname = $attrs['ref'];
  248. $this->attributes[$attrs['ref']] = $attrs;
  249. }
  250. if($this->currentComplexType){ // This should *always* be
  251. $this->complexTypes[$this->currentComplexType]['attrs'][$aname] = $attrs;
  252. }
  253. // arrayType attribute
  254. if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']) || $this->getLocalPart($aname) == 'arrayType'){
  255. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  256. $prefix = $this->getPrefix($aname);
  257. if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])){
  258. $v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
  259. } else {
  260. $v = '';
  261. }
  262. if(strpos($v,'[,]')){
  263. $this->complexTypes[$this->currentComplexType]['multidimensional'] = true;
  264. }
  265. $v = substr($v,0,strpos($v,'[')); // clip the []
  266. if(!strpos($v,':') && isset($this->typemap[$this->XMLSchemaVersion][$v])){
  267. $v = $this->XMLSchemaVersion.':'.$v;
  268. }
  269. $this->complexTypes[$this->currentComplexType]['arrayType'] = $v;
  270. }
  271. break;
  272. case 'complexContent': // (optional) content for a complexType
  273. $this->xdebug("do nothing for element $name");
  274. break;
  275. case 'complexType':
  276. array_push($this->complexTypeStack, $this->currentComplexType);
  277. if(isset($attrs['name'])){
  278. // TODO: what is the scope of named complexTypes that appear
  279. // nested within other c complexTypes?
  280. $this->xdebug('processing named complexType '.$attrs['name']);
  281. //$this->currentElement = false;
  282. $this->currentComplexType = $attrs['name'];
  283. $this->complexTypes[$this->currentComplexType] = $attrs;
  284. $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
  285. // This is for constructs like
  286. // <complexType name="ListOfString" base="soap:Array">
  287. // <sequence>
  288. // <element name="string" type="xsd:string"
  289. // minOccurs="0" maxOccurs="unbounded" />
  290. // </sequence>
  291. // </complexType>
  292. if(isset($attrs['base']) && preg_match('/:Array$/',$attrs['base'])){
  293. $this->xdebug('complexType is unusual array');
  294. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  295. } else {
  296. $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
  297. }
  298. } else {
  299. $name = $this->CreateTypeName($this->currentElement);
  300. $this->xdebug('processing unnamed complexType for element ' . $this->currentElement . ' named ' . $name);
  301. $this->currentComplexType = $name;
  302. //$this->currentElement = false;
  303. $this->complexTypes[$this->currentComplexType] = $attrs;
  304. $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
  305. // This is for constructs like
  306. // <complexType name="ListOfString" base="soap:Array">
  307. // <sequence>
  308. // <element name="string" type="xsd:string"
  309. // minOccurs="0" maxOccurs="unbounded" />
  310. // </sequence>
  311. // </complexType>
  312. if(isset($attrs['base']) && preg_match('/:Array$/',$attrs['base'])){
  313. $this->xdebug('complexType is unusual array');
  314. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  315. } else {
  316. $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
  317. }
  318. }
  319. $this->complexTypes[$this->currentComplexType]['simpleContent'] = 'false';
  320. break;
  321. case 'element':
  322. array_push($this->elementStack, $this->currentElement);
  323. if (!isset($attrs['form'])) {
  324. if ($this->currentComplexType) {
  325. $attrs['form'] = $this->schemaInfo['elementFormDefault'];
  326. } else {
  327. // global
  328. $attrs['form'] = 'qualified';
  329. }
  330. }
  331. if(isset($attrs['type'])){
  332. $this->xdebug("processing typed element ".$attrs['name']." of type ".$attrs['type']);
  333. if (! $this->getPrefix($attrs['type'])) {
  334. if ($this->defaultNamespace[$pos]) {
  335. $attrs['type'] = $this->defaultNamespace[$pos] . ':' . $attrs['type'];
  336. $this->xdebug('used default namespace to make type ' . $attrs['type']);
  337. }
  338. }
  339. // This is for constructs like
  340. // <complexType name="ListOfString" base="soap:Array">
  341. // <sequence>
  342. // <element name="string" type="xsd:string"
  343. // minOccurs="0" maxOccurs="unbounded" />
  344. // </sequence>
  345. // </complexType>
  346. if ($this->currentComplexType && $this->complexTypes[$this->currentComplexType]['phpType'] == 'array') {
  347. $this->xdebug('arrayType for unusual array is ' . $attrs['type']);
  348. $this->complexTypes[$this->currentComplexType]['arrayType'] = $attrs['type'];
  349. }
  350. $this->currentElement = $attrs['name'];
  351. $ename = $attrs['name'];
  352. } elseif(isset($attrs['ref'])){
  353. $this->xdebug("processing element as ref to ".$attrs['ref']);
  354. $this->currentElement = "ref to ".$attrs['ref'];
  355. $ename = $this->getLocalPart($attrs['ref']);
  356. } else {
  357. $type = $this->CreateTypeName($this->currentComplexType . '_' . $attrs['name']);
  358. $this->xdebug("processing untyped element " . $attrs['name'] . ' type ' . $type);
  359. $this->currentElement = $attrs['name'];
  360. $attrs['type'] = $this->schemaTargetNamespace . ':' . $type;
  361. $ename = $attrs['name'];
  362. }
  363. if (isset($ename) && $this->currentComplexType) {
  364. $this->xdebug("add element $ename to complexType $this->currentComplexType");
  365. $this->complexTypes[$this->currentComplexType]['elements'][$ename] = $attrs;
  366. } elseif (!isset($attrs['ref'])) {
  367. $this->xdebug("add element $ename to elements array");
  368. $this->elements[ $attrs['name'] ] = $attrs;
  369. $this->elements[ $attrs['name'] ]['typeClass'] = 'element';
  370. }
  371. break;
  372. case 'enumeration': // restriction value list member
  373. $this->xdebug('enumeration ' . $attrs['value']);
  374. if ($this->currentSimpleType) {
  375. $this->simpleTypes[$this->currentSimpleType]['enumeration'][] = $attrs['value'];
  376. } elseif ($this->currentComplexType) {
  377. $this->complexTypes[$this->currentComplexType]['enumeration'][] = $attrs['value'];
  378. }
  379. break;
  380. case 'extension': // simpleContent or complexContent type extension
  381. $this->xdebug('extension ' . $attrs['base']);
  382. if ($this->currentComplexType) {
  383. $ns = $this->getPrefix($attrs['base']);
  384. if ($ns == '') {
  385. $this->complexTypes[$this->currentComplexType]['extensionBase'] = $this->schemaTargetNamespace . ':' . $attrs['base'];
  386. } else {
  387. $this->complexTypes[$this->currentComplexType]['extensionBase'] = $attrs['base'];
  388. }
  389. } else {
  390. $this->xdebug('no current complexType to set extensionBase');
  391. }
  392. break;
  393. case 'import':
  394. if (isset($attrs['schemaLocation'])) {
  395. $this->xdebug('import namespace ' . $attrs['namespace'] . ' from ' . $attrs['schemaLocation']);
  396. $this->imports[$attrs['namespace']][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
  397. } else {
  398. $this->xdebug('import namespace ' . $attrs['namespace']);
  399. $this->imports[$attrs['namespace']][] = array('location' => '', 'loaded' => true);
  400. if (! $this->getPrefixFromNamespace($attrs['namespace'])) {
  401. $this->namespaces['ns'.(count($this->namespaces)+1)] = $attrs['namespace'];
  402. }
  403. }
  404. break;
  405. case 'include':
  406. if (isset($attrs['schemaLocation'])) {
  407. $this->xdebug('include into namespace ' . $this->schemaTargetNamespace . ' from ' . $attrs['schemaLocation']);
  408. $this->imports[$this->schemaTargetNamespace][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
  409. } else {
  410. $this->xdebug('ignoring invalid XML Schema construct: include without schemaLocation attribute');
  411. }
  412. break;
  413. case 'list': // simpleType value list
  414. $this->xdebug("do nothing for element $name");
  415. break;
  416. case 'restriction': // simpleType, simpleContent or complexContent value restriction
  417. $this->xdebug('restriction ' . $attrs['base']);
  418. if($this->currentSimpleType){
  419. $this->simpleTypes[$this->currentSimpleType]['type'] = $attrs['base'];
  420. } elseif($this->currentComplexType){
  421. $this->complexTypes[$this->currentComplexType]['restrictionBase'] = $attrs['base'];
  422. if(strstr($attrs['base'],':') == ':Array'){
  423. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  424. }
  425. }
  426. break;
  427. case 'schema':
  428. $this->schemaInfo = $attrs;
  429. $this->schemaInfo['schemaVersion'] = $this->getNamespaceFromPrefix($prefix);
  430. if (isset($attrs['targetNamespace'])) {
  431. $this->schemaTargetNamespace = $attrs['targetNamespace'];
  432. }
  433. if (!isset($attrs['elementFormDefault'])) {
  434. $this->schemaInfo['elementFormDefault'] = 'unqualified';
  435. }
  436. if (!isset($attrs['attributeFormDefault'])) {
  437. $this->schemaInfo['attributeFormDefault'] = 'unqualified';
  438. }
  439. break;
  440. case 'simpleContent': // (optional) content for a complexType
  441. if ($this->currentComplexType) { // This should *always* be
  442. $this->complexTypes[$this->currentComplexType]['simpleContent'] = 'true';
  443. } else {
  444. $this->xdebug("do nothing for element $name because there is no current complexType");
  445. }
  446. break;
  447. case 'simpleType':
  448. array_push($this->simpleTypeStack, $this->currentSimpleType);
  449. if(isset($attrs['name'])){
  450. $this->xdebug("processing simpleType for name " . $attrs['name']);
  451. $this->currentSimpleType = $attrs['name'];
  452. $this->simpleTypes[ $attrs['name'] ] = $attrs;
  453. $this->simpleTypes[ $attrs['name'] ]['typeClass'] = 'simpleType';
  454. $this->simpleTypes[ $attrs['name'] ]['phpType'] = 'scalar';
  455. } else {
  456. $name = $this->CreateTypeName($this->currentComplexType . '_' . $this->currentElement);
  457. $this->xdebug('processing unnamed simpleType for element ' . $this->currentElement . ' named ' . $name);
  458. $this->currentSimpleType = $name;
  459. //$this->currentElement = false;
  460. $this->simpleTypes[$this->currentSimpleType] = $attrs;
  461. $this->simpleTypes[$this->currentSimpleType]['phpType'] = 'scalar';
  462. }
  463. break;
  464. case 'union': // simpleType type list
  465. $this->xdebug("do nothing for element $name");
  466. break;
  467. default:
  468. $this->xdebug("do not have any logic to process element $name");
  469. }
  470. }
  471. /**
  472. * end-element handler
  473. *
  474. * @param string $parser XML parser object
  475. * @param string $name element name
  476. * @access private
  477. */
  478. function schemaEndElement($parser, $name) {
  479. // bring depth down a notch
  480. $this->depth--;
  481. // position of current element is equal to the last value left in depth_array for my depth
  482. if(isset($this->depth_array[$this->depth])){
  483. $pos = $this->depth_array[$this->depth];
  484. }
  485. // get element prefix
  486. if ($prefix = $this->getPrefix($name)){
  487. // get unqualified name
  488. $name = $this->getLocalPart($name);
  489. } else {
  490. $prefix = '';
  491. }
  492. // move on...
  493. if($name == 'complexType'){
  494. $this->xdebug('done processing complexType ' . ($this->currentComplexType ? $this->currentComplexType : '(unknown)'));
  495. $this->xdebug($this->varDump($this->complexTypes[$this->currentComplexType]));
  496. $this->currentComplexType = array_pop($this->complexTypeStack);
  497. //$this->currentElement = false;
  498. }
  499. if($name == 'element'){
  500. $this->xdebug('done processing element ' . ($this->currentElement ? $this->currentElement : '(unknown)'));
  501. $this->currentElement = array_pop($this->elementStack);
  502. }
  503. if($name == 'simpleType'){
  504. $this->xdebug('done processing simpleType ' . ($this->currentSimpleType ? $this->currentSimpleType : '(unknown)'));
  505. $this->xdebug($this->varDump($this->simpleTypes[$this->currentSimpleType]));
  506. $this->currentSimpleType = array_pop($this->simpleTypeStack);
  507. }
  508. }
  509. /**
  510. * element content handler
  511. *
  512. * @param string $parser XML parser object
  513. * @param string $data element content
  514. * @access private
  515. */
  516. function schemaCharacterData($parser, $data){
  517. $pos = $this->depth_array[$this->depth - 1];
  518. $this->message[$pos]['cdata'] .= $data;
  519. }
  520. /**
  521. * serialize the schema
  522. *
  523. * @access public
  524. */
  525. function serializeSchema(){
  526. $schemaPrefix = $this->getPrefixFromNamespace($this->XMLSchemaVersion);
  527. $xml = '';
  528. // imports
  529. if (sizeof($this->imports) > 0) {
  530. foreach($this->imports as $ns => $list) {
  531. foreach ($list as $ii) {
  532. if ($ii['location'] != '') {
  533. $xml .= " <$schemaPrefix:import location=\"" . $ii['location'] . '" namespace="' . $ns . "\" />\n";
  534. } else {
  535. $xml .= " <$schemaPrefix:import namespace=\"" . $ns . "\" />\n";
  536. }
  537. }
  538. }
  539. }
  540. // complex types
  541. foreach($this->complexTypes as $typeName => $attrs){
  542. $contentStr = '';
  543. // serialize child elements
  544. if(isset($attrs['elements']) && (count($attrs['elements']) > 0)){
  545. foreach($attrs['elements'] as $element => $eParts){
  546. if(isset($eParts['ref'])){
  547. $contentStr .= " <$schemaPrefix:element ref=\"$element\"/>\n";
  548. } else {
  549. $contentStr .= " <$schemaPrefix:element name=\"$element\" type=\"" . $this->contractQName($eParts['type']) . "\"";
  550. foreach ($eParts as $aName => $aValue) {
  551. // handle, e.g., abstract, default, form, minOccurs, maxOccurs, nillable
  552. if ($aName != 'name' && $aName != 'type') {
  553. $contentStr .= " $aName=\"$aValue\"";
  554. }
  555. }
  556. $contentStr .= "/>\n";
  557. }
  558. }
  559. // compositor wraps elements
  560. if (isset($attrs['compositor']) && ($attrs['compositor'] != '')) {
  561. $contentStr = " <$schemaPrefix:$attrs[compositor]>\n".$contentStr." </$schemaPrefix:$attrs[compositor]>\n";
  562. }
  563. }
  564. // attributes
  565. if(isset($attrs['attrs']) && (count($attrs['attrs']) >= 1)){
  566. foreach($attrs['attrs'] as $attr => $aParts){
  567. $contentStr .= " <$schemaPrefix:attribute";
  568. foreach ($aParts as $a => $v) {
  569. if ($a == 'ref' || $a == 'type') {
  570. $contentStr .= " $a=\"".$this->contractQName($v).'"';
  571. } elseif ($a == 'http://schemas.xmlsoap.org/wsdl/:arrayType') {
  572. $this->usedNamespaces['wsdl'] = $this->namespaces['wsdl'];
  573. $contentStr .= ' wsdl:arrayType="'.$this->contractQName($v).'"';
  574. } else {
  575. $contentStr .= " $a=\"$v\"";
  576. }
  577. }
  578. $contentStr .= "/>\n";
  579. }
  580. }
  581. // if restriction
  582. if (isset($attrs['restrictionBase']) && $attrs['restrictionBase'] != ''){
  583. $contentStr = " <$schemaPrefix:restriction base=\"".$this->contractQName($attrs['restrictionBase'])."\">\n".$contentStr." </$schemaPrefix:restriction>\n";
  584. // complex or simple content
  585. if ((isset($attrs['elements']) && count($attrs['elements']) > 0) || (isset($attrs['attrs']) && count($attrs['attrs']) > 0)){
  586. $contentStr = " <$schemaPrefix:complexContent>\n".$contentStr." </$schemaPrefix:complexContent>\n";
  587. }
  588. }
  589. // finalize complex type
  590. if($contentStr != ''){
  591. $contentStr = " <$schemaPrefix:complexType name=\"$typeName\">\n".$contentStr." </$schemaPrefix:complexType>\n";
  592. } else {
  593. $contentStr = " <$schemaPrefix:complexType name=\"$typeName\"/>\n";
  594. }
  595. $xml .= $contentStr;
  596. }
  597. // simple types
  598. if(isset($this->simpleTypes) && count($this->simpleTypes) > 0){
  599. foreach($this->simpleTypes as $typeName => $eParts){
  600. $xml .= " <$schemaPrefix:simpleType name=\"$typeName\">\n <$schemaPrefix:restriction base=\"".$this->contractQName($eParts['type'])."\">\n";
  601. if (isset($eParts['enumeration'])) {
  602. foreach ($eParts['enumeration'] as $e) {
  603. $xml .= " <$schemaPrefix:enumeration value=\"$e\"/>\n";
  604. }
  605. }
  606. $xml .= " </$schemaPrefix:restriction>\n </$schemaPrefix:simpleType>";
  607. }
  608. }
  609. // elements
  610. if(isset($this->elements) && count($this->elements) > 0){
  611. foreach($this->elements as $element => $eParts){
  612. $xml .= " <$schemaPrefix:element name=\"$element\" type=\"".$this->contractQName($eParts['type'])."\"/>\n";
  613. }
  614. }
  615. // attributes
  616. if(isset($this->attributes) && count($this->attributes) > 0){
  617. foreach($this->attributes as $attr => $aParts){
  618. $xml .= " <$schemaPrefix:attribute name=\"$attr\" type=\"".$this->contractQName($aParts['type'])."\"\n/>";
  619. }
  620. }
  621. // finish 'er up
  622. $attr = '';
  623. foreach ($this->schemaInfo as $k => $v) {
  624. if ($k == 'elementFormDefault' || $k == 'attributeFormDefault') {
  625. $attr .= " $k=\"$v\"";
  626. }
  627. }
  628. $el = "<$schemaPrefix:schema$attr targetNamespace=\"$this->schemaTargetNamespace\"\n";
  629. foreach (array_diff($this->usedNamespaces, $this->enclosingNamespaces) as $nsp => $ns) {
  630. $el .= " xmlns:$nsp=\"$ns\"";
  631. }
  632. $xml = $el . ">\n".$xml."</$schemaPrefix:schema>\n";
  633. return $xml;
  634. }
  635. /**
  636. * adds debug data to the clas level debug string
  637. *
  638. * @param string $string debug data
  639. * @access private
  640. */
  641. function xdebug($string){
  642. $this->debug('<' . $this->schemaTargetNamespace . '> '.$string);
  643. }
  644. /**
  645. * get the PHP type of a user defined type in the schema
  646. * PHP type is kind of a misnomer since it actually returns 'struct' for assoc. arrays
  647. * returns false if no type exists, or not w/ the given namespace
  648. * else returns a string that is either a native php type, or 'struct'
  649. *
  650. * @param string $type name of defined type
  651. * @param string $ns namespace of type
  652. * @return mixed
  653. * @access public
  654. * @deprecated
  655. */
  656. function getPHPType($type,$ns){
  657. if(isset($this->typemap[$ns][$type])){
  658. //print "found type '$type' and ns $ns in typemap<br>";
  659. return $this->typemap[$ns][$type];
  660. } elseif(isset($this->complexTypes[$type])){
  661. //print "getting type '$type' and ns $ns from complexTypes array<br>";
  662. return $this->complexTypes[$type]['phpType'];
  663. }
  664. return false;
  665. }
  666. /**
  667. * returns an associative array of information about a given type
  668. * returns false if no type exists by the given name
  669. *
  670. * For a complexType typeDef = array(
  671. * 'restrictionBase' => '',
  672. * 'phpType' => '',
  673. * 'compositor' => '(sequence|all)',
  674. * 'elements' => array(), // refs to elements array
  675. * 'attrs' => array() // refs to attributes array
  676. * ... and so on (see addComplexType)
  677. * )
  678. *
  679. * For simpleType or element, the array has different keys.
  680. *
  681. * @param string $type
  682. * @return mixed
  683. * @access public
  684. * @see addComplexType
  685. * @see addSimpleType
  686. * @see addElement
  687. */
  688. function getTypeDef($type){
  689. //$this->debug("in getTypeDef for type $type");
  690. if (substr($type, -1) == '^') {
  691. $is_element = 1;
  692. $type = substr($type, 0, -1);
  693. } else {
  694. $is_element = 0;
  695. }
  696. if((! $is_element) && isset($this->complexTypes[$type])){
  697. $this->xdebug("in getTypeDef, found complexType $type");
  698. return $this->complexTypes[$type];
  699. } elseif((! $is_element) && isset($this->simpleTypes[$type])){
  700. $this->xdebug("in getTypeDef, found simpleType $type");
  701. if (!isset($this->simpleTypes[$type]['phpType'])) {
  702. // get info for type to tack onto the simple type
  703. // TODO: can this ever really apply (i.e. what is a simpleType really?)
  704. $uqType = substr($this->simpleTypes[$type]['type'], strrpos($this->simpleTypes[$type]['type'], ':') + 1);
  705. $ns = substr($this->simpleTypes[$type]['type'], 0, strrpos($this->simpleTypes[$type]['type'], ':'));
  706. $etype = $this->getTypeDef($uqType);
  707. if ($etype) {
  708. $this->xdebug("in getTypeDef, found type for simpleType $type:");
  709. $this->xdebug($this->varDump($etype));
  710. if (isset($etype['phpType'])) {
  711. $this->simpleTypes[$type]['phpType'] = $etype['phpType'];
  712. }
  713. if (isset($etype['elements'])) {
  714. $this->simpleTypes[$type]['elements'] = $etype['elements'];
  715. }
  716. }
  717. }
  718. return $this->simpleTypes[$type];
  719. } elseif(isset($this->elements[$type])){
  720. $this->xdebug("in getTypeDef, found element $type");
  721. if (!isset($this->elements[$type]['phpType'])) {
  722. // get info for type to tack onto the element
  723. $uqType = substr($this->elements[$type]['type'], strrpos($this->elements[$type]['type'], ':') + 1);
  724. $ns = substr($this->elements[$type]['type'], 0, strrpos($this->elements[$type]['type'], ':'));
  725. $etype = $this->getTypeDef($uqType);
  726. if ($etype) {
  727. $this->xdebug("in getTypeDef, found type for element $type:");
  728. $this->xdebug($this->varDump($etype));
  729. if (isset($etype['phpType'])) {
  730. $this->elements[$type]['phpType'] = $etype['phpType'];
  731. }
  732. if (isset($etype['elements'])) {
  733. $this->elements[$type]['elements'] = $etype['elements'];
  734. }
  735. if (isset($etype['extensionBase'])) {
  736. $this->elements[$type]['extensionBase'] = $etype['extensionBase'];
  737. }
  738. } elseif ($ns == 'http://www.w3.org/2001/XMLSchema') {
  739. $this->xdebug("in getTypeDef, element $type is an XSD type");
  740. $this->elements[$type]['phpType'] = 'scalar';
  741. }
  742. }
  743. return $this->elements[$type];
  744. } elseif(isset($this->attributes[$type])){
  745. $this->xdebug("in getTypeDef, found attribute $type");
  746. return $this->attributes[$type];
  747. } elseif (preg_match('/_ContainedType$/', $type)) {
  748. $this->xdebug("in getTypeDef, have an untyped element $type");
  749. $typeDef['typeClass'] = 'simpleType';
  750. $typeDef['phpType'] = 'scalar';
  751. $typeDef['type'] = 'http://www.w3.org/2001/XMLSchema:string';
  752. return $typeDef;
  753. }
  754. $this->xdebug("in getTypeDef, did not find $type");
  755. return false;
  756. }
  757. /**
  758. * returns a sample serialization of a given type, or false if no type by the given name
  759. *
  760. * @param string $type name of type
  761. * @return mixed
  762. * @access public
  763. * @deprecated
  764. */
  765. function serializeTypeDef($type){
  766. //print "in sTD() for type $type<br>";
  767. if($typeDef = $this->getTypeDef($type)){
  768. $str .= '<'.$type;
  769. if(is_array($typeDef['attrs'])){
  770. foreach($typeDef['attrs'] as $attName => $data){
  771. $str .= " $attName=\"{type = ".$data['type']."}\"";
  772. }
  773. }
  774. $str .= " xmlns=\"".$this->schema['targetNamespace']."\"";
  775. if(count($typeDef['elements']) > 0){
  776. $str .= ">";
  777. foreach($typeDef['elements'] as $element => $eData){
  778. $str .= $this->serializeTypeDef($element);
  779. }
  780. $str .= "</$type>";
  781. } elseif($typeDef['typeClass'] == 'element') {
  782. $str .= "></$type>";
  783. } else {
  784. $str .= "/>";
  785. }
  786. return $str;
  787. }
  788. return false;
  789. }
  790. /**
  791. * returns HTML form elements that allow a user
  792. * to enter values for creating an instance of the given type.
  793. *
  794. * @param string $name name for type instance
  795. * @param string $type name of type
  796. * @return string
  797. * @access public
  798. * @deprecated
  799. */
  800. function typeToForm($name,$type){
  801. // get typedef
  802. if($typeDef = $this->getTypeDef($type)){
  803. // if struct
  804. if($typeDef['phpType'] == 'struct'){
  805. $buffer .= '<table>';
  806. foreach($typeDef['elements'] as $child => $childDef){
  807. $buffer .= "
  808. <tr><td align='right'>$childDef[name] (type: ".$this->getLocalPart($childDef['type'])."):</td>
  809. <td><input type='text' name='parameters[".$name."][$childDef[name]]'></td></tr>";
  810. }
  811. $buffer .= '</table>';
  812. // if array
  813. } elseif($typeDef['phpType'] == 'array'){
  814. $buffer .= '<table>';
  815. for($i=0;$i < 3; $i++){
  816. $buffer .= "
  817. <tr><td align='right'>array item (type: $typeDef[arrayType]):</td>
  818. <td><input type='text' name='parameters[".$name."][]'></td></tr>";
  819. }
  820. $buffer .= '</table>';
  821. // if scalar
  822. } else {
  823. $buffer .= "<input type='text' name='parameters[$name]'>";
  824. }
  825. } else {
  826. $buffer .= "<input type='text' name='parameters[$name]'>";
  827. }
  828. return $buffer;
  829. }
  830. /**
  831. * adds a complex type to the schema
  832. *
  833. * example: array
  834. *
  835. * addType(
  836. * 'ArrayOfstring',
  837. * 'complexType',
  838. * 'array',
  839. * '',
  840. * 'SOAP-ENC:Array',
  841. * array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'string[]'),
  842. * 'xsd:string'
  843. * );
  844. *
  845. * example: PHP associative array ( SOAP Struct )
  846. *
  847. * addType(
  848. * 'SOAPStruct',
  849. * 'complexType',
  850. * 'struct',
  851. * 'all',
  852. * array('myVar'=> array('name'=>'myVar','type'=>'string')
  853. * );
  854. *
  855. * @param name
  856. * @param typeClass (complexType|simpleType|attribute)
  857. * @param phpType: currently supported are array and struct (php assoc array)
  858. * @param compositor (all|sequence|choice)
  859. * @param restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  860. * @param elements = array ( name = array(name=>'',type=>'') )
  861. * @param attrs = array(
  862. * array(
  863. * 'ref' => "http://schemas.xmlsoap.org/soap/encoding/:arrayType",
  864. * "http://schemas.xmlsoap.org/wsdl/:arrayType" => "string[]"
  865. * )
  866. * )
  867. * @param arrayType: namespace:name (http://www.w3.org/2001/XMLSchema:string)
  868. * @access public
  869. * @see getTypeDef
  870. */
  871. function addComplexType($name,$typeClass='complexType',$phpType='array',$compositor='',$restrictionBase='',$elements=array(),$attrs=array(),$arrayType=''){
  872. $this->complexTypes[$name] = array(
  873. 'name' => $name,
  874. 'typeClass' => $typeClass,
  875. 'phpType' => $phpType,
  876. 'compositor'=> $compositor,
  877. 'restrictionBase' => $restrictionBase,
  878. 'elements' => $elements,
  879. 'attrs' => $attrs,
  880. 'arrayType' => $arrayType
  881. );
  882. $this->xdebug("addComplexType $name:");
  883. $this->appendDebug($this->varDump($this->complexTypes[$name]));
  884. }
  885. /**
  886. * adds a simple type to the schema
  887. *
  888. * @param string $name
  889. * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  890. * @param string $typeClass (should always be simpleType)
  891. * @param string $phpType (should always be scalar)
  892. * @param array $enumeration array of values
  893. * @access public
  894. * @see nusoap_xmlschema
  895. * @see getTypeDef
  896. */
  897. function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) {
  898. $this->simpleTypes[$name] = array(
  899. 'name' => $name,
  900. 'typeClass' => $typeClass,
  901. 'phpType' => $phpType,
  902. 'type' => $restrictionBase,
  903. 'enumeration' => $enumeration
  904. );
  905. $this->xdebug("addSimpleType $name:");
  906. $this->appendDebug($this->varDump($this->simpleTypes[$name]));
  907. }
  908. /**
  909. * adds an element to the schema
  910. *
  911. * @param array $attrs attributes that must include name and type
  912. * @see nusoap_xmlschema
  913. * @access public
  914. */
  915. function addElement($attrs) {
  916. if (! $this->getPrefix($attrs['type'])) {
  917. $attrs['type'] = $this->schemaTargetNamespace . ':' . $attrs['type'];
  918. }
  919. $this->elements[ $attrs['name'] ] = $attrs;
  920. $this->elements[ $attrs['name'] ]['typeClass'] = 'element';
  921. $this->xdebug("addElement " . $attrs['name']);
  922. $this->appendDebug($this->varDump($this->elements[ $attrs['name'] ]));
  923. }
  924. }
  925. /**
  926. * Backward compatibility
  927. */
  928. class XMLSchema extends nusoap_xmlschema {
  929. }
  930. ?>