class.xmlschema.php 35 KB

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