class.xmlschema.php 35 KB

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