class.xmlschema.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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.49 2007/11/06 14:17:53 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(ereg("^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. $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. break;
  274. case 'complexType':
  275. array_push($this->complexTypeStack, $this->currentComplexType);
  276. if(isset($attrs['name'])){
  277. // TODO: what is the scope of named complexTypes that appear
  278. // nested within other c complexTypes?
  279. $this->xdebug('processing named complexType '.$attrs['name']);
  280. //$this->currentElement = false;
  281. $this->currentComplexType = $attrs['name'];
  282. $this->complexTypes[$this->currentComplexType] = $attrs;
  283. $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
  284. // This is for constructs like
  285. // <complexType name="ListOfString" base="soap:Array">
  286. // <sequence>
  287. // <element name="string" type="xsd:string"
  288. // minOccurs="0" maxOccurs="unbounded" />
  289. // </sequence>
  290. // </complexType>
  291. if(isset($attrs['base']) && ereg(':Array$',$attrs['base'])){
  292. $this->xdebug('complexType is unusual array');
  293. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  294. } else {
  295. $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
  296. }
  297. } else {
  298. $name = $this->CreateTypeName($this->currentElement);
  299. $this->xdebug('processing unnamed complexType for element ' . $this->currentElement . ' named ' . $name);
  300. $this->currentComplexType = $name;
  301. //$this->currentElement = false;
  302. $this->complexTypes[$this->currentComplexType] = $attrs;
  303. $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
  304. // This is for constructs like
  305. // <complexType name="ListOfString" base="soap:Array">
  306. // <sequence>
  307. // <element name="string" type="xsd:string"
  308. // minOccurs="0" maxOccurs="unbounded" />
  309. // </sequence>
  310. // </complexType>
  311. if(isset($attrs['base']) && ereg(':Array$',$attrs['base'])){
  312. $this->xdebug('complexType is unusual array');
  313. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  314. } else {
  315. $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
  316. }
  317. }
  318. break;
  319. case 'element':
  320. array_push($this->elementStack, $this->currentElement);
  321. if (!isset($attrs['form'])) {
  322. $attrs['form'] = $this->schemaInfo['elementFormDefault'];
  323. }
  324. if(isset($attrs['type'])){
  325. $this->xdebug("processing typed element ".$attrs['name']." of type ".$attrs['type']);
  326. if (! $this->getPrefix($attrs['type'])) {
  327. if ($this->defaultNamespace[$pos]) {
  328. $attrs['type'] = $this->defaultNamespace[$pos] . ':' . $attrs['type'];
  329. $this->xdebug('used default namespace to make type ' . $attrs['type']);
  330. }
  331. }
  332. // This is for constructs like
  333. // <complexType name="ListOfString" base="soap:Array">
  334. // <sequence>
  335. // <element name="string" type="xsd:string"
  336. // minOccurs="0" maxOccurs="unbounded" />
  337. // </sequence>
  338. // </complexType>
  339. if ($this->currentComplexType && $this->complexTypes[$this->currentComplexType]['phpType'] == 'array') {
  340. $this->xdebug('arrayType for unusual array is ' . $attrs['type']);
  341. $this->complexTypes[$this->currentComplexType]['arrayType'] = $attrs['type'];
  342. }
  343. $this->currentElement = $attrs['name'];
  344. $ename = $attrs['name'];
  345. } elseif(isset($attrs['ref'])){
  346. $this->xdebug("processing element as ref to ".$attrs['ref']);
  347. $this->currentElement = "ref to ".$attrs['ref'];
  348. $ename = $this->getLocalPart($attrs['ref']);
  349. } else {
  350. $type = $this->CreateTypeName($this->currentComplexType . '_' . $attrs['name']);
  351. $this->xdebug("processing untyped element " . $attrs['name'] . ' type ' . $type);
  352. $this->currentElement = $attrs['name'];
  353. $attrs['type'] = $this->schemaTargetNamespace . ':' . $type;
  354. $ename = $attrs['name'];
  355. }
  356. if (isset($ename) && $this->currentComplexType) {
  357. $this->xdebug("add element $ename to complexType $this->currentComplexType");
  358. $this->complexTypes[$this->currentComplexType]['elements'][$ename] = $attrs;
  359. } elseif (!isset($attrs['ref'])) {
  360. $this->xdebug("add element $ename to elements array");
  361. $this->elements[ $attrs['name'] ] = $attrs;
  362. $this->elements[ $attrs['name'] ]['typeClass'] = 'element';
  363. }
  364. break;
  365. case 'enumeration': // restriction value list member
  366. $this->xdebug('enumeration ' . $attrs['value']);
  367. if ($this->currentSimpleType) {
  368. $this->simpleTypes[$this->currentSimpleType]['enumeration'][] = $attrs['value'];
  369. } elseif ($this->currentComplexType) {
  370. $this->complexTypes[$this->currentComplexType]['enumeration'][] = $attrs['value'];
  371. }
  372. break;
  373. case 'extension': // simpleContent or complexContent type extension
  374. $this->xdebug('extension ' . $attrs['base']);
  375. if ($this->currentComplexType) {
  376. $this->complexTypes[$this->currentComplexType]['extensionBase'] = $attrs['base'];
  377. }
  378. break;
  379. case 'import':
  380. if (isset($attrs['schemaLocation'])) {
  381. //$this->xdebug('import namespace ' . $attrs['namespace'] . ' from ' . $attrs['schemaLocation']);
  382. $this->imports[$attrs['namespace']][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
  383. } else {
  384. //$this->xdebug('import namespace ' . $attrs['namespace']);
  385. $this->imports[$attrs['namespace']][] = array('location' => '', 'loaded' => true);
  386. if (! $this->getPrefixFromNamespace($attrs['namespace'])) {
  387. $this->namespaces['ns'.(count($this->namespaces)+1)] = $attrs['namespace'];
  388. }
  389. }
  390. break;
  391. case 'list': // simpleType value list
  392. break;
  393. case 'restriction': // simpleType, simpleContent or complexContent value restriction
  394. $this->xdebug('restriction ' . $attrs['base']);
  395. if($this->currentSimpleType){
  396. $this->simpleTypes[$this->currentSimpleType]['type'] = $attrs['base'];
  397. } elseif($this->currentComplexType){
  398. $this->complexTypes[$this->currentComplexType]['restrictionBase'] = $attrs['base'];
  399. if(strstr($attrs['base'],':') == ':Array'){
  400. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  401. }
  402. }
  403. break;
  404. case 'schema':
  405. $this->schemaInfo = $attrs;
  406. $this->schemaInfo['schemaVersion'] = $this->getNamespaceFromPrefix($prefix);
  407. if (isset($attrs['targetNamespace'])) {
  408. $this->schemaTargetNamespace = $attrs['targetNamespace'];
  409. }
  410. if (!isset($attrs['elementFormDefault'])) {
  411. $this->schemaInfo['elementFormDefault'] = 'unqualified';
  412. }
  413. if (!isset($attrs['attributeFormDefault'])) {
  414. $this->schemaInfo['attributeFormDefault'] = 'unqualified';
  415. }
  416. break;
  417. case 'simpleContent': // (optional) content for a complexType
  418. break;
  419. case 'simpleType':
  420. array_push($this->simpleTypeStack, $this->currentSimpleType);
  421. if(isset($attrs['name'])){
  422. $this->xdebug("processing simpleType for name " . $attrs['name']);
  423. $this->currentSimpleType = $attrs['name'];
  424. $this->simpleTypes[ $attrs['name'] ] = $attrs;
  425. $this->simpleTypes[ $attrs['name'] ]['typeClass'] = 'simpleType';
  426. $this->simpleTypes[ $attrs['name'] ]['phpType'] = 'scalar';
  427. } else {
  428. $name = $this->CreateTypeName($this->currentComplexType . '_' . $this->currentElement);
  429. $this->xdebug('processing unnamed simpleType for element ' . $this->currentElement . ' named ' . $name);
  430. $this->currentSimpleType = $name;
  431. //$this->currentElement = false;
  432. $this->simpleTypes[$this->currentSimpleType] = $attrs;
  433. $this->simpleTypes[$this->currentSimpleType]['phpType'] = 'scalar';
  434. }
  435. break;
  436. case 'union': // simpleType type list
  437. break;
  438. default:
  439. //$this->xdebug("do not have anything to do for element $name");
  440. }
  441. }
  442. /**
  443. * end-element handler
  444. *
  445. * @param string $parser XML parser object
  446. * @param string $name element name
  447. * @access private
  448. */
  449. function schemaEndElement($parser, $name) {
  450. // bring depth down a notch
  451. $this->depth--;
  452. // position of current element is equal to the last value left in depth_array for my depth
  453. if(isset($this->depth_array[$this->depth])){
  454. $pos = $this->depth_array[$this->depth];
  455. }
  456. // get element prefix
  457. if ($prefix = $this->getPrefix($name)){
  458. // get unqualified name
  459. $name = $this->getLocalPart($name);
  460. } else {
  461. $prefix = '';
  462. }
  463. // move on...
  464. if($name == 'complexType'){
  465. $this->xdebug('done processing complexType ' . ($this->currentComplexType ? $this->currentComplexType : '(unknown)'));
  466. $this->currentComplexType = array_pop($this->complexTypeStack);
  467. //$this->currentElement = false;
  468. }
  469. if($name == 'element'){
  470. $this->xdebug('done processing element ' . ($this->currentElement ? $this->currentElement : '(unknown)'));
  471. $this->currentElement = array_pop($this->elementStack);
  472. }
  473. if($name == 'simpleType'){
  474. $this->xdebug('done processing simpleType ' . ($this->currentSimpleType ? $this->currentSimpleType : '(unknown)'));
  475. $this->currentSimpleType = array_pop($this->simpleTypeStack);
  476. }
  477. }
  478. /**
  479. * element content handler
  480. *
  481. * @param string $parser XML parser object
  482. * @param string $data element content
  483. * @access private
  484. */
  485. function schemaCharacterData($parser, $data){
  486. $pos = $this->depth_array[$this->depth - 1];
  487. $this->message[$pos]['cdata'] .= $data;
  488. }
  489. /**
  490. * serialize the schema
  491. *
  492. * @access public
  493. */
  494. function serializeSchema(){
  495. $schemaPrefix = $this->getPrefixFromNamespace($this->XMLSchemaVersion);
  496. $xml = '';
  497. // imports
  498. if (sizeof($this->imports) > 0) {
  499. foreach($this->imports as $ns => $list) {
  500. foreach ($list as $ii) {
  501. if ($ii['location'] != '') {
  502. $xml .= " <$schemaPrefix:import location=\"" . $ii['location'] . '" namespace="' . $ns . "\" />\n";
  503. } else {
  504. $xml .= " <$schemaPrefix:import namespace=\"" . $ns . "\" />\n";
  505. }
  506. }
  507. }
  508. }
  509. // complex types
  510. foreach($this->complexTypes as $typeName => $attrs){
  511. $contentStr = '';
  512. // serialize child elements
  513. if(isset($attrs['elements']) && (count($attrs['elements']) > 0)){
  514. foreach($attrs['elements'] as $element => $eParts){
  515. if(isset($eParts['ref'])){
  516. $contentStr .= " <$schemaPrefix:element ref=\"$element\"/>\n";
  517. } else {
  518. $contentStr .= " <$schemaPrefix:element name=\"$element\" type=\"" . $this->contractQName($eParts['type']) . "\"";
  519. foreach ($eParts as $aName => $aValue) {
  520. // handle, e.g., abstract, default, form, minOccurs, maxOccurs, nillable
  521. if ($aName != 'name' && $aName != 'type') {
  522. $contentStr .= " $aName=\"$aValue\"";
  523. }
  524. }
  525. $contentStr .= "/>\n";
  526. }
  527. }
  528. // compositor wraps elements
  529. if (isset($attrs['compositor']) && ($attrs['compositor'] != '')) {
  530. $contentStr = " <$schemaPrefix:$attrs[compositor]>\n".$contentStr." </$schemaPrefix:$attrs[compositor]>\n";
  531. }
  532. }
  533. // attributes
  534. if(isset($attrs['attrs']) && (count($attrs['attrs']) >= 1)){
  535. foreach($attrs['attrs'] as $attr => $aParts){
  536. $contentStr .= " <$schemaPrefix:attribute";
  537. foreach ($aParts as $a => $v) {
  538. if ($a == 'ref' || $a == 'type') {
  539. $contentStr .= " $a=\"".$this->contractQName($v).'"';
  540. } elseif ($a == 'http://schemas.xmlsoap.org/wsdl/:arrayType') {
  541. $this->usedNamespaces['wsdl'] = $this->namespaces['wsdl'];
  542. $contentStr .= ' wsdl:arrayType="'.$this->contractQName($v).'"';
  543. } else {
  544. $contentStr .= " $a=\"$v\"";
  545. }
  546. }
  547. $contentStr .= "/>\n";
  548. }
  549. }
  550. // if restriction
  551. if (isset($attrs['restrictionBase']) && $attrs['restrictionBase'] != ''){
  552. $contentStr = " <$schemaPrefix:restriction base=\"".$this->contractQName($attrs['restrictionBase'])."\">\n".$contentStr." </$schemaPrefix:restriction>\n";
  553. // complex or simple content
  554. if ((isset($attrs['elements']) && count($attrs['elements']) > 0) || (isset($attrs['attrs']) && count($attrs['attrs']) > 0)){
  555. $contentStr = " <$schemaPrefix:complexContent>\n".$contentStr." </$schemaPrefix:complexContent>\n";
  556. }
  557. }
  558. // finalize complex type
  559. if($contentStr != ''){
  560. $contentStr = " <$schemaPrefix:complexType name=\"$typeName\">\n".$contentStr." </$schemaPrefix:complexType>\n";
  561. } else {
  562. $contentStr = " <$schemaPrefix:complexType name=\"$typeName\"/>\n";
  563. }
  564. $xml .= $contentStr;
  565. }
  566. // simple types
  567. if(isset($this->simpleTypes) && count($this->simpleTypes) > 0){
  568. foreach($this->simpleTypes as $typeName => $eParts){
  569. $xml .= " <$schemaPrefix:simpleType name=\"$typeName\">\n <$schemaPrefix:restriction base=\"".$this->contractQName($eParts['type'])."\">\n";
  570. if (isset($eParts['enumeration'])) {
  571. foreach ($eParts['enumeration'] as $e) {
  572. $xml .= " <$schemaPrefix:enumeration value=\"$e\"/>\n";
  573. }
  574. }
  575. $xml .= " </$schemaPrefix:restriction>\n </$schemaPrefix:simpleType>";
  576. }
  577. }
  578. // elements
  579. if(isset($this->elements) && count($this->elements) > 0){
  580. foreach($this->elements as $element => $eParts){
  581. $xml .= " <$schemaPrefix:element name=\"$element\" type=\"".$this->contractQName($eParts['type'])."\"/>\n";
  582. }
  583. }
  584. // attributes
  585. if(isset($this->attributes) && count($this->attributes) > 0){
  586. foreach($this->attributes as $attr => $aParts){
  587. $xml .= " <$schemaPrefix:attribute name=\"$attr\" type=\"".$this->contractQName($aParts['type'])."\"\n/>";
  588. }
  589. }
  590. // finish 'er up
  591. $attr = '';
  592. foreach ($this->schemaInfo as $k => $v) {
  593. if ($k == 'elementFormDefault' || $k == 'attributeFormDefault') {
  594. $attr .= " $k=\"$v\"";
  595. }
  596. }
  597. $el = "<$schemaPrefix:schema$attr targetNamespace=\"$this->schemaTargetNamespace\"\n";
  598. foreach (array_diff($this->usedNamespaces, $this->enclosingNamespaces) as $nsp => $ns) {
  599. $el .= " xmlns:$nsp=\"$ns\"";
  600. }
  601. $xml = $el . ">\n".$xml."</$schemaPrefix:schema>\n";
  602. return $xml;
  603. }
  604. /**
  605. * adds debug data to the clas level debug string
  606. *
  607. * @param string $string debug data
  608. * @access private
  609. */
  610. function xdebug($string){
  611. $this->debug('<' . $this->schemaTargetNamespace . '> '.$string);
  612. }
  613. /**
  614. * get the PHP type of a user defined type in the schema
  615. * PHP type is kind of a misnomer since it actually returns 'struct' for assoc. arrays
  616. * returns false if no type exists, or not w/ the given namespace
  617. * else returns a string that is either a native php type, or 'struct'
  618. *
  619. * @param string $type name of defined type
  620. * @param string $ns namespace of type
  621. * @return mixed
  622. * @access public
  623. * @deprecated
  624. */
  625. function getPHPType($type,$ns){
  626. if(isset($this->typemap[$ns][$type])){
  627. //print "found type '$type' and ns $ns in typemap<br>";
  628. return $this->typemap[$ns][$type];
  629. } elseif(isset($this->complexTypes[$type])){
  630. //print "getting type '$type' and ns $ns from complexTypes array<br>";
  631. return $this->complexTypes[$type]['phpType'];
  632. }
  633. return false;
  634. }
  635. /**
  636. * returns an associative array of information about a given type
  637. * returns false if no type exists by the given name
  638. *
  639. * For a complexType typeDef = array(
  640. * 'restrictionBase' => '',
  641. * 'phpType' => '',
  642. * 'compositor' => '(sequence|all)',
  643. * 'elements' => array(), // refs to elements array
  644. * 'attrs' => array() // refs to attributes array
  645. * ... and so on (see addComplexType)
  646. * )
  647. *
  648. * For simpleType or element, the array has different keys.
  649. *
  650. * @param string $type
  651. * @return mixed
  652. * @access public
  653. * @see addComplexType
  654. * @see addSimpleType
  655. * @see addElement
  656. */
  657. function getTypeDef($type){
  658. //$this->debug("in getTypeDef for type $type");
  659. if (substr($type, -1) == '^') {
  660. $is_element = 1;
  661. $type = substr($type, 0, -1);
  662. } else {
  663. $is_element = 0;
  664. }
  665. if((! $is_element) && isset($this->complexTypes[$type])){
  666. $this->xdebug("in getTypeDef, found complexType $type");
  667. return $this->complexTypes[$type];
  668. } elseif((! $is_element) && isset($this->simpleTypes[$type])){
  669. $this->xdebug("in getTypeDef, found simpleType $type");
  670. if (!isset($this->simpleTypes[$type]['phpType'])) {
  671. // get info for type to tack onto the simple type
  672. // TODO: can this ever really apply (i.e. what is a simpleType really?)
  673. $uqType = substr($this->simpleTypes[$type]['type'], strrpos($this->simpleTypes[$type]['type'], ':') + 1);
  674. $ns = substr($this->simpleTypes[$type]['type'], 0, strrpos($this->simpleTypes[$type]['type'], ':'));
  675. $etype = $this->getTypeDef($uqType);
  676. if ($etype) {
  677. $this->xdebug("in getTypeDef, found type for simpleType $type:");
  678. $this->xdebug($this->varDump($etype));
  679. if (isset($etype['phpType'])) {
  680. $this->simpleTypes[$type]['phpType'] = $etype['phpType'];
  681. }
  682. if (isset($etype['elements'])) {
  683. $this->simpleTypes[$type]['elements'] = $etype['elements'];
  684. }
  685. }
  686. }
  687. return $this->simpleTypes[$type];
  688. } elseif(isset($this->elements[$type])){
  689. $this->xdebug("in getTypeDef, found element $type");
  690. if (!isset($this->elements[$type]['phpType'])) {
  691. // get info for type to tack onto the element
  692. $uqType = substr($this->elements[$type]['type'], strrpos($this->elements[$type]['type'], ':') + 1);
  693. $ns = substr($this->elements[$type]['type'], 0, strrpos($this->elements[$type]['type'], ':'));
  694. $etype = $this->getTypeDef($uqType);
  695. if ($etype) {
  696. $this->xdebug("in getTypeDef, found type for element $type:");
  697. $this->xdebug($this->varDump($etype));
  698. if (isset($etype['phpType'])) {
  699. $this->elements[$type]['phpType'] = $etype['phpType'];
  700. }
  701. if (isset($etype['elements'])) {
  702. $this->elements[$type]['elements'] = $etype['elements'];
  703. }
  704. } elseif ($ns == 'http://www.w3.org/2001/XMLSchema') {
  705. $this->xdebug("in getTypeDef, element $type is an XSD type");
  706. $this->elements[$type]['phpType'] = 'scalar';
  707. }
  708. }
  709. return $this->elements[$type];
  710. } elseif(isset($this->attributes[$type])){
  711. $this->xdebug("in getTypeDef, found attribute $type");
  712. return $this->attributes[$type];
  713. } elseif (ereg('_ContainedType$', $type)) {
  714. $this->xdebug("in getTypeDef, have an untyped element $type");
  715. $typeDef['typeClass'] = 'simpleType';
  716. $typeDef['phpType'] = 'scalar';
  717. $typeDef['type'] = 'http://www.w3.org/2001/XMLSchema:string';
  718. return $typeDef;
  719. }
  720. $this->xdebug("in getTypeDef, did not find $type");
  721. return false;
  722. }
  723. /**
  724. * returns a sample serialization of a given type, or false if no type by the given name
  725. *
  726. * @param string $type name of type
  727. * @return mixed
  728. * @access public
  729. * @deprecated
  730. */
  731. function serializeTypeDef($type){
  732. //print "in sTD() for type $type<br>";
  733. if($typeDef = $this->getTypeDef($type)){
  734. $str .= '<'.$type;
  735. if(is_array($typeDef['attrs'])){
  736. foreach($typeDef['attrs'] as $attName => $data){
  737. $str .= " $attName=\"{type = ".$data['type']."}\"";
  738. }
  739. }
  740. $str .= " xmlns=\"".$this->schema['targetNamespace']."\"";
  741. if(count($typeDef['elements']) > 0){
  742. $str .= ">";
  743. foreach($typeDef['elements'] as $element => $eData){
  744. $str .= $this->serializeTypeDef($element);
  745. }
  746. $str .= "</$type>";
  747. } elseif($typeDef['typeClass'] == 'element') {
  748. $str .= "></$type>";
  749. } else {
  750. $str .= "/>";
  751. }
  752. return $str;
  753. }
  754. return false;
  755. }
  756. /**
  757. * returns HTML form elements that allow a user
  758. * to enter values for creating an instance of the given type.
  759. *
  760. * @param string $name name for type instance
  761. * @param string $type name of type
  762. * @return string
  763. * @access public
  764. * @deprecated
  765. */
  766. function typeToForm($name,$type){
  767. // get typedef
  768. if($typeDef = $this->getTypeDef($type)){
  769. // if struct
  770. if($typeDef['phpType'] == 'struct'){
  771. $buffer .= '<table>';
  772. foreach($typeDef['elements'] as $child => $childDef){
  773. $buffer .= "
  774. <tr><td align='right'>$childDef[name] (type: ".$this->getLocalPart($childDef['type'])."):</td>
  775. <td><input type='text' name='parameters[".$name."][$childDef[name]]'></td></tr>";
  776. }
  777. $buffer .= '</table>';
  778. // if array
  779. } elseif($typeDef['phpType'] == 'array'){
  780. $buffer .= '<table>';
  781. for($i=0;$i < 3; $i++){
  782. $buffer .= "
  783. <tr><td align='right'>array item (type: $typeDef[arrayType]):</td>
  784. <td><input type='text' name='parameters[".$name."][]'></td></tr>";
  785. }
  786. $buffer .= '</table>';
  787. // if scalar
  788. } else {
  789. $buffer .= "<input type='text' name='parameters[$name]'>";
  790. }
  791. } else {
  792. $buffer .= "<input type='text' name='parameters[$name]'>";
  793. }
  794. return $buffer;
  795. }
  796. /**
  797. * adds a complex type to the schema
  798. *
  799. * example: array
  800. *
  801. * addType(
  802. * 'ArrayOfstring',
  803. * 'complexType',
  804. * 'array',
  805. * '',
  806. * 'SOAP-ENC:Array',
  807. * array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'string[]'),
  808. * 'xsd:string'
  809. * );
  810. *
  811. * example: PHP associative array ( SOAP Struct )
  812. *
  813. * addType(
  814. * 'SOAPStruct',
  815. * 'complexType',
  816. * 'struct',
  817. * 'all',
  818. * array('myVar'=> array('name'=>'myVar','type'=>'string')
  819. * );
  820. *
  821. * @param name
  822. * @param typeClass (complexType|simpleType|attribute)
  823. * @param phpType: currently supported are array and struct (php assoc array)
  824. * @param compositor (all|sequence|choice)
  825. * @param restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  826. * @param elements = array ( name = array(name=>'',type=>'') )
  827. * @param attrs = array(
  828. * array(
  829. * 'ref' => "http://schemas.xmlsoap.org/soap/encoding/:arrayType",
  830. * "http://schemas.xmlsoap.org/wsdl/:arrayType" => "string[]"
  831. * )
  832. * )
  833. * @param arrayType: namespace:name (http://www.w3.org/2001/XMLSchema:string)
  834. * @access public
  835. * @see getTypeDef
  836. */
  837. function addComplexType($name,$typeClass='complexType',$phpType='array',$compositor='',$restrictionBase='',$elements=array(),$attrs=array(),$arrayType=''){
  838. $this->complexTypes[$name] = array(
  839. 'name' => $name,
  840. 'typeClass' => $typeClass,
  841. 'phpType' => $phpType,
  842. 'compositor'=> $compositor,
  843. 'restrictionBase' => $restrictionBase,
  844. 'elements' => $elements,
  845. 'attrs' => $attrs,
  846. 'arrayType' => $arrayType
  847. );
  848. $this->xdebug("addComplexType $name:");
  849. $this->appendDebug($this->varDump($this->complexTypes[$name]));
  850. }
  851. /**
  852. * adds a simple type to the schema
  853. *
  854. * @param string $name
  855. * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  856. * @param string $typeClass (should always be simpleType)
  857. * @param string $phpType (should always be scalar)
  858. * @param array $enumeration array of values
  859. * @access public
  860. * @see nusoap_xmlschema
  861. * @see getTypeDef
  862. */
  863. function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) {
  864. $this->simpleTypes[$name] = array(
  865. 'name' => $name,
  866. 'typeClass' => $typeClass,
  867. 'phpType' => $phpType,
  868. 'type' => $restrictionBase,
  869. 'enumeration' => $enumeration
  870. );
  871. $this->xdebug("addSimpleType $name:");
  872. $this->appendDebug($this->varDump($this->simpleTypes[$name]));
  873. }
  874. /**
  875. * adds an element to the schema
  876. *
  877. * @param array $attrs attributes that must include name and type
  878. * @see nusoap_xmlschema
  879. * @access public
  880. */
  881. function addElement($attrs) {
  882. if (! $this->getPrefix($attrs['type'])) {
  883. $attrs['type'] = $this->schemaTargetNamespace . ':' . $attrs['type'];
  884. }
  885. $this->elements[ $attrs['name'] ] = $attrs;
  886. $this->elements[ $attrs['name'] ]['typeClass'] = 'element';
  887. $this->xdebug("addElement " . $attrs['name']);
  888. $this->appendDebug($this->varDump($this->elements[ $attrs['name'] ]));
  889. }
  890. }
  891. /**
  892. * Backward compatibility
  893. */
  894. class XMLSchema extends nusoap_xmlschema {
  895. }
  896. ?>