PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/dom/codeGen/1.4/om/xsElement.php

http://github.com/sbarthelemy/collada-dom
PHP | 368 lines | 283 code | 32 blank | 53 comment | 76 complexity | c9ebe94d963e44fef60ad398d447b36c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright 2006 Sony Computer Entertainment Inc.
  4. *
  5. * Licensed under the MIT Open Source License, for details please see license.txt or the website
  6. * http://www.opensource.org/licenses/mit-license.php
  7. *
  8. */
  9. require_once( 'src/ElementMeta.php' );
  10. require_once( 'src/TypeMeta.php' );
  11. class xsElement extends _elementSet
  12. {
  13. function xsElement()
  14. {
  15. $this->_addElement( 'xsAnnotation', array( 'minOccurs' => '0', 'maxOccurs' => 'unbounded' ) );
  16. $this->_addElement( 'xsComplexType', array( 'minOccurs' => '0', 'maxOccurs' => '1' ) );
  17. $this->_addElement( 'xsSimpleType', array( 'minOccurs' => '0', 'maxOccurs' => '1' ) );
  18. $this->_addAttribute( 'ref', array( 'type' => 'xs:string' ) );
  19. $this->_addAttribute( 'name', array( 'type' => 'xs:string' ) );
  20. $this->_addAttribute( 'type', array( 'type' => 'xs:string' ) );
  21. $this->_addAttribute( 'abstract', array( 'type' => 'xs:bool' ) );
  22. $this->_addAttribute( 'substitutionGroup', array( 'type' => 'xs:string' ) );
  23. // $this->_addAttribute( 'maxOccurs', array( 'type' => 'xs:integer' ) );
  24. // $this->_addAttribute( 'minOccurs', array( 'type' => 'xs:integer' ) );
  25. $this->type[] = 'xsElement';
  26. parent::_elementSet();
  27. }
  28. function & generate( $element_context, & $global_elements )
  29. {
  30. $element_context[] = $this->getAttribute( "name" );
  31. print implode( ",", $element_context ) . "\n";
  32. // Get new factory
  33. $generator = new ElementMeta( $global_elements );
  34. // Load the class name and a context pre-fix (in case we're inside another element)
  35. $generator->setName( $this->getAttribute( 'name' ) );
  36. $generator->setContext( $element_context );
  37. $subGroup = $this->getAttribute( 'substitutionGroup' );
  38. if ( $subGroup != '' ) {
  39. //print "found a subGroup ". $subGroup ."!\n";
  40. $generator->setSubstitutionGroup( $subGroup );
  41. $generator->bag['ref_elements'][] = $subGroup;
  42. }
  43. $abstract = $this->getAttribute( 'abstract' );
  44. if ( $abstract != '' ) {
  45. $generator->setAbstract( $abstract );
  46. }
  47. // Extract any documentation for this node
  48. $a = $this->getElementsByType( 'xsAnnotation' );
  49. if ( count( $a ) > 0 )
  50. {
  51. $d = $a[0]->getElementsByType( 'xsDocumentation' );
  52. if ( count( $d ) > 0 )
  53. {
  54. $generator->setDocumentation( $d[0]->get() );
  55. }
  56. $ap = $a[0]->getElementsByType( 'xsAppinfo' );
  57. if ( count( $ap ) > 0 )
  58. {
  59. $generator->setAppInfo( $ap[0]->get() );
  60. }
  61. }
  62. //******************************************************************************************/
  63. //$generator->setContentType( $this->getAttribute( 'type' ) );
  64. $type = $this->getAttribute( 'type' );
  65. $generator->bag['base_type'] = $type;
  66. //check if this type equals a complex type
  67. //print "element ". $this->getAttribute( 'name' ) ." is of type ". $type ."!\n";
  68. if ( $type != "" ) {
  69. //print "complex types: " . count($GLOBALS['_globals']['complex_types']) . "\n";
  70. for ( $i = 0; $i < count( $GLOBALS['_globals']['complex_types'] ); $i++ ) {
  71. if ( !strcmp($type, $GLOBALS['_globals']['complex_types'][$i]->getAttribute('name') ) ) {
  72. //print "found a match for ". $type ."\n";
  73. $generator->setComplexType( true );
  74. $generator->bag['ref_elements'][] = $type;
  75. break;
  76. }
  77. }
  78. if ( !$generator->bag['complex_type'] ) {
  79. //wasn't a complex type that means it needs a content type
  80. $generator->setContentType( $type );
  81. }
  82. }
  83. //*******************************************************************************************/
  84. // Inspect the semantic structure of this node and extract the elements/attributes
  85. $temp = $this->getElementsByType( 'xsComplexType' );
  86. if ( count( $temp ) > 0 )
  87. {
  88. if ( $temp[0]->getAttribute( 'mixed' ) == 'true' )
  89. {
  90. $generator->setMixed( true );
  91. $generator->setContentType( 'ListOfInts' );
  92. }
  93. $content = $temp[0]; // Should only be one
  94. $this->generateComplexType( $content, $generator, $element_context );
  95. if ( count( $generator->bag['elements'] ) == 0 ) {
  96. $generator->setIsEmptyContent( true );
  97. }
  98. }
  99. else if ( count( $this->getElementsByType( 'xsSimpleType' ) ) > 0 ) {
  100. //inline simple type definition. right now handle as string but needs to be fixed
  101. $generator->bag['simple_type'] = new TypeMeta();
  102. $temp = $this->getElementsByType( 'xsSimpleType' );
  103. $this->generateSimpleType( $temp[0], $generator->bag['simple_type'] );
  104. if ( count( $generator->bag['simple_type']->bag['enum'] ) >0 ) {
  105. $generator->setContentType( $this->getAttribute( 'name' ) ."_type" );
  106. }
  107. else {
  108. $generator->setContentType( $generator->bag['simple_type']->bag['base'] );
  109. }
  110. }
  111. $meta = & $generator->getMeta();
  112. if ( count( $element_context ) == 1 )
  113. {
  114. $global_elements[ $element_context[0] ] = & $meta;
  115. }
  116. return $meta;
  117. }
  118. // Flatten choice/all/sequence groups into a single list of contained elements
  119. function flatten( & $element, & $generator, & $context, $maxOccurs )
  120. {
  121. //print "in flatten ";
  122. $e_list = $element->getElements();
  123. for( $i=0; $i<count( $e_list ); $i++ )
  124. {
  125. switch( $e_list[$i]->getType() )
  126. {
  127. case 'xsChoice':
  128. $generator->setHasChoice( true );
  129. $generator->addContentModel( 1, $e_list[$i]->getAttribute( 'minOccurs' ), $e_list[$i]->getAttribute( 'maxOccurs' ) );
  130. // Propagate the maxOccurs down through choice hierarchy (while flattening)
  131. $local_max = $e_list[$i]->getAttribute( 'maxOccurs' );
  132. if ( $maxOccurs == 'unbounded' || (is_int( $local_max ) && ($maxOccurs > $local_max)) )
  133. {
  134. $this->flatten( $e_list[$i], $generator, $context, $maxOccurs );
  135. } else
  136. {
  137. $this->flatten( $e_list[$i], $generator, $context, $local_max );
  138. }
  139. break;
  140. case 'xsSequence':
  141. $generator->addContentModel( 0, $e_list[$i]->getAttribute( 'minOccurs' ), $e_list[$i]->getAttribute( 'maxOccurs' ) );
  142. // Propagate the maxOccurs down through choice hierarchy (while flattening)
  143. $local_max = $e_list[$i]->getAttribute( 'maxOccurs' );
  144. if ( $maxOccurs == 'unbounded' || (is_int( $local_max ) && ($maxOccurs > $local_max)) )
  145. {
  146. $this->flatten( $e_list[$i], $generator, $context, $maxOccurs );
  147. } else
  148. {
  149. $this->flatten( $e_list[$i], $generator, $context, $local_max );
  150. }
  151. break;
  152. case 'xsAll':
  153. $generator->addContentModel( 3, $e_list[$i]->getAttribute( 'minOccurs' ), $e_list[$i]->getAttribute( 'maxOccurs' ) );
  154. // Propagate the maxOccurs down through choice hierarchy (while flattening)
  155. $local_max = $e_list[$i]->getAttribute( 'maxOccurs' );
  156. if ( $maxOccurs == 'unbounded' || (is_int( $local_max ) && ($maxOccurs > $local_max)) )
  157. {
  158. $this->flatten( $e_list[$i], $generator, $context, $maxOccurs );
  159. } else
  160. {
  161. $this->flatten( $e_list[$i], $generator, $context, $local_max );
  162. }
  163. break;
  164. case 'xsGroup':
  165. $generator->addContentModel( 2, $e_list[$i]->getAttribute( 'minOccurs' ), $e_list[$i]->getAttribute( 'maxOccurs' ) );
  166. $generator->addGroup( $e_list[$i] );
  167. case 'xsElement':
  168. $nm = $e_list[$i]->getAttribute( 'name' );
  169. if ( $nm == '' ) { $nm = $e_list[$i]->getAttribute( 'ref' ); }
  170. $generator->addContentModel( $nm, $e_list[$i]->getAttribute( 'minOccurs' ), $e_list[$i]->getAttribute( 'maxOccurs' ) );
  171. //print "found element!\n";
  172. // If a containing element/group has a maxOccurs > 1, then inherit it (will flag as array in code gen)
  173. if ( $maxOccurs == 'unbounded' || $maxOccurs > 1 )
  174. {
  175. $e_list[$i]->setAttribute( 'maxOccurs', $maxOccurs );
  176. }
  177. $generator->addElement( $e_list[$i], $context );
  178. break;
  179. case 'xsAttribute':
  180. //print "found attribute!\n";
  181. $generator->addAttribute( $e_list[$i] );
  182. break;
  183. case 'xsAny':
  184. print "found an any\n";
  185. $generator->addContentModel( 4, $e_list[$i]->getAttribute( 'minOccurs' ), $e_list[$i]->getAttribute( 'maxOccurs' ) );
  186. $generator->bag['has_any'] = true;
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. $generator->addContentModel( 5, 0, 0 ); //END content model - There will be one extra on every element
  193. }
  194. //function that reads complex types. will recurse complex type derived heirarchies.
  195. function generateComplexType( $content, & $generator, & $context ) {
  196. //print "in generatecomplextype\n";
  197. if ( count( $content->getElementsByType( 'xsSimpleContent' ) ) > 0 ) {
  198. //print "found simpleContent!\n";
  199. $temp = $content->getElementsByType( 'xsSimpleContent' );
  200. $content = $temp[0]; // Should only be one - now we now find out element's parent class
  201. $temp = & $content->getElements();
  202. $content = $temp[0]; // Should either be an xsExtension or xsRestriction
  203. $type = $content->getAttribute( 'base' );
  204. //print "setting extends to ". $type ."\n";
  205. $generator->setContentType( $type );
  206. $temp = & $content->getElementsByType( 'xsAttribute' );
  207. for( $i=0; $i<count( $temp ); $i++ ) {
  208. $generator->addAttribute( $temp[$i] );
  209. }
  210. } else if ( count( $content->getElementsByType( 'xsComplexContent' ) ) > 0 ) {
  211. //print "found complexContent!\n";
  212. //ComplexContent specified means type is derived
  213. $temp = $content->getElementsByType( 'xsComplexContent' );
  214. $content = $temp[0]; // Should only be one - now we now find out element's parent class
  215. $temp = & $content->getElements();
  216. $content = $temp[0]; // Should either be an xsExtension or xsRestriction
  217. if ( $content->getType() == 'xsExtension' ) {
  218. $generator->bag['isExtension'] = true;
  219. }
  220. if ( $content->getType() == 'xsRestriction' ) {
  221. $generator->bag['isRestriction'] = true;
  222. }
  223. $type = $content->getAttribute( 'base' );
  224. //print "setting extends to ". $type ."\n";
  225. $generator->bag['base_type'] = $type;
  226. //Generate the complex type this is derived from
  227. for ( $i = 0; $i < count( $GLOBALS['_globals']['complex_types'] ); $i++ ) {
  228. if ( $type == $GLOBALS['_globals']['complex_types'][$i]->getAttribute('name') ) {
  229. $generator->setComplexType( true );
  230. $generator->bag['ref_elements'][] = $type;
  231. //$this->generateComplexType( $GLOBALS['_globals']['complex_types'][$i], $generator, $context );
  232. break;
  233. }
  234. }
  235. // Parse element context
  236. $this->flatten( $content, $generator, $element_context, $content->getAttribute( 'maxOccurs' ) );
  237. } else {
  238. //print "found nothing so doing complex content flatten\n";
  239. // The alternative to xsSimpleContent is xsComplexContent - if it is not specified, it is implied
  240. // Parse element context
  241. $this->flatten( $content, $generator, $element_context, $content->getAttribute( 'maxOccurs' ) );
  242. }
  243. }
  244. //function that generates the inline simpleType
  245. function generateSimpleType( $content, & $generator ) {
  246. $e = $content->getElements();
  247. $generator->setType( $this->getAttribute( 'name' ) );
  248. //print $this->getAttribute( 'name' ) ." has a simpletype\n";
  249. $a = $content->getElementsByType( 'xsAnnotation' );
  250. if ( count( $a ) > 0 ) {
  251. //print "found annotation for ". $this->getAttribute( 'name' ) ."!\n";
  252. $d = $a[0]->getElementsByType( 'xsDocumentation' );
  253. if ( count( $d ) > 0 )
  254. {
  255. //print "found documentation for ". $this->getAttribute( 'name' ) ."!\n";
  256. $generator->setDocumentation( $d[0]->get() );
  257. }
  258. $ap = $a[0]->getElementsByType( 'xsAppinfo' );
  259. if ( count( $ap ) > 0 )
  260. {
  261. $generator->setAppInfo( $ap[0]->get() );
  262. }
  263. }
  264. $idx = 0;
  265. if ( $e[$idx]->getType() == 'xsAnnotation' ) {
  266. $idx = 1;
  267. }
  268. if ( $e[$idx]->getType() == 'xsRestriction' || $e[$idx]->getType() == 'xsExtension' )
  269. {
  270. $generator->setIsExtension( $e[$idx]->getType() == 'xsExtension' );
  271. // Set base class
  272. $generator->setBase( $e[$idx]->getAttribute( 'base' ) );
  273. // Look for enums
  274. $enums = $e[$idx]->getElementsByType( 'xsEnumeration' );
  275. for( $i=0; $i<count( $enums ); $i++ )
  276. {
  277. $generator->addEnum( $enums[$i]->getAttribute( 'value' ) );
  278. //print $enums[$i]->getAttribute( 'value' );
  279. $an = $enums[$i]->getElementsByType('xsAnnotation');
  280. if ( count( $an ) > 0 ) {
  281. $doc = $an[0]->getElementsByType( 'xsDocumentation' );
  282. if ( count( $doc ) > 0 ) {
  283. $generator->addEnumDoc( $i, $doc[0]->get() );
  284. }
  285. $ap = $an[0]->getElementsByType( 'xsAppinfo' );
  286. if ( count( $ap ) > 0 )
  287. {
  288. $generator->addEnumAppInfo( $i, $ap[0]->get() );
  289. }
  290. }
  291. }
  292. // Look for max/mins
  293. $array_limits = array();
  294. $min = $e[$idx]->getElementsByType( 'xsMinLength' );
  295. $max = $e[$idx]->getElementsByType( 'xsMaxLength' );
  296. $minIn = $e[$idx]->getElementsByType( 'xsMinInclusive' );
  297. $maxIn = $e[$idx]->getElementsByType( 'xsMaxInclusive' );
  298. $minEx = $e[$idx]->getElementsByType( 'xsMinExclusive' );
  299. $maxEx = $e[$idx]->getElementsByType( 'xsMaxExclusive' );
  300. if ( count( $min ) > 0 )
  301. {
  302. $generator->setRestriction( 'minLength', $min[0]->getAttribute( 'value' ) );
  303. }
  304. if ( count( $max ) > 0 )
  305. {
  306. $generator->setRestriction( 'maxLength', $max[0]->getAttribute( 'value' ) );
  307. }
  308. if ( count( $minIn ) > 0 )
  309. {
  310. $generator->setRestriction( 'minInclusive', $minIn[0]->getAttribute( 'value' ) );
  311. }
  312. if ( count( $maxIn ) > 0 )
  313. {
  314. $generator->setRestriction( 'maxInclusive', $maxIn[0]->getAttribute( 'value' ) );
  315. }
  316. if ( count( $minEx ) > 0 )
  317. {
  318. $generator->setRestriction( 'minExclusive', $minEx[0]->getAttribute( 'value' ) );
  319. }
  320. if ( count( $maxEx ) > 0 )
  321. {
  322. $generator->setRestriction( 'maxExclusive', $maxEx[0]->getAttribute( 'value' ) );
  323. }
  324. } else if ( $e[$idx]->getType() == 'xsList' )
  325. {
  326. //$extends = "xsList";
  327. $itemType = $e[$idx]->getAttribute( 'itemType' );
  328. $generator->setListType( $itemType );
  329. $generator->bag['isArray'] = true;
  330. } else
  331. {
  332. $this->log( "WARN: unexpected element in xsSimpleType code generation" );
  333. }
  334. }
  335. }
  336. ?>