summaryrefslogtreecommitdiffstats
path: root/1.4.0/dom/codeGen/1.4/tpl/template-engine.php
blob: d0160c17ae14032d0b2d8b8dc4cf782312f2809c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
/*
* Copyright 2006 Sony Computer Entertainment Inc.
*
* Licensed under the MIT Open Source License, for details please see license.txt or the website
* http://www.opensource.org/licenses/mit-license.php
*
*/ 

$_globals = array();
// FLAG: Full Code - set true to output verbose mode, minimal otherwise (does not include//                   inline elements in minimal)
$_globals['full_code'] = true;
$_globals['copyright'] = false;
$_globals['copyright_text'] = "/*\n" .
" * Copyright 2006 Sony Computer Entertainment Inc.\n" .
" *\n" .
" * Licensed under the SCEA Shared Source License, Version 1.0 (the \"License\"); you may not use this\n" .
" * file except in compliance with the License. You may obtain a copy of the License at:\n" .
" * http://research.scea.com/scea_shared_source_license.html\n" .
" *\n" .
" * Unless required by applicable law or agreed to in writing, software distributed under the License\n" .
" * is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n" .
" * implied. See the License for the specific language governing permissions and limitations under the\n" .
" * License.\n" .
" */\n\n";

$_globals['depth'] = 0;
$_globals['meta_prefix'] = 'dae';
$_globals['prefix'] = 'dom';
$_globals['language'] = 'en';
$_globals['dom_dir'] = 'gen/dom/';
$_globals['tmp_dir'] = 'tmp/';
$_globals['log_file'] = 'gen/gen.log';

$_globals['register_list'] = array();
$_globals['include_list'] = array();
$_globals['complex_types'] = array();
$_globals['groups'] = array();
$_globals['constStrings'] = array(); //used to store all the constant strings needed to be put in the file.
$_globals['elementTypes'] = array();
$_globals['elementNames'] = array();
$_globals['elementNames'][] = "COLLADA"; //needed because no elements have this as a child.
$_globals['typeID'] = 0;

$_globals['target_dir'] = '';

$_globals['global_elements'] = array();

$_globals['templates'] = array(
  'DOXYGEN' => 'tpl/tpl-doxygen.php',
  'TYPES_HEADER_FILE' => 'tpl/tpl-types-header-file.php',
  'TYPES_HEADER' => 'tpl/tpl-types-header.php',
  'TYPES_CPP_FILE' => 'tpl/tpl-types-cpp-file.php',
  'TYPES_CPP' => 'tpl/tpl-types-cpp.php',
  'INCLUDES' => 'tpl/tpl-includes.php',
  'HEADER' => 'tpl/tpl-dot-h.php',
  'HEADER_FILE' => 'tpl/tpl-header.php',
  'INCLUDE_LIST' => 'tpl/tpl-include-list.php',
  'CPP_FILE' => 'tpl/tpl-cpp.php',
  'CPP' => 'tpl/tpl-cpp-body.php',
  'CPP_STATIC' => 'tpl/tpl-cpp-static.php',
  'CPP_METHODS' => 'tpl/tpl-cpp-methods.php',
  'CLASS' => 'tpl/tpl-class-def.php',
  'ELEMENTS_FILE' => 'tpl/tpl-elements-file.php',
  'ELEMENTS' => 'tpl/tpl-elements.php',
  'CONSTANTS_FILE' => 'tpl/tpl-constants-file.php',
  'CONSTANTS' => 'tpl/tpl-constants.php',
  'CONSTANTS_CPP_FILE' => 'tpl/tpl-constants-cpp-file.php',
  'CONSTANTS_CPP' => 'tpl/tpl-constants-cpp.php'
);

function applyTemplate( $template, & $bag )
{
  global $_globals;
  
  $_result = '';
  if ( array_key_exists( $template, $_globals['templates'] ) )
  {
    ob_start();
    include( $_globals['templates'][ $template ] );

    $_result = ob_get_contents();
    ob_end_clean();
  }
  return $_result;
}

function initGen( $file )
{
  global $_globals;

  // A few defns
  $_globals['gen_start_time'] = date( "M d Y H:i:s" );
  $_globals['file_name'] = $file;

  // Verify target dirs exist, create if not
  makeGenDir( getcwd() . "/gen" );
  makeGenDir( getcwd() . "/" . $_globals['dom_dir'] );
  makeGenDir( getcwd() . "/" . $_globals['dom_dir'] . 'include/' );
  makeGenDir( getcwd() . "/" . $_globals['dom_dir'] . 'src/' );

  // Start buffering output
  ob_start();
}

function makeGenDir( $dir )
{
  if ( !is_dir( $dir ) )
  {
    if ( !mkdir( $dir ) )
    {
      die( "Could not create directory $dir\n" );
    }
  }
}

function cleanupGen()
{
  global $_globals;

  // Get output buffer
  $_result = ob_get_contents();
  ob_end_clean();
  // Assemble report
  ob_start();
  print "========================================\n\n";
  print "           Code Generation\n\n";
  print "----------------------------------------\n";
  print "COLLADA File: " . $_globals['file_name'] . "\n";
  print "Start time:   " . $_globals['gen_start_time'] . "\n";
  print "End time:     " . date( "M d Y H:i:s" ) . "\n";
  print "----------------------------------------\n\n";
  print $_result;
  print "\r\n\r\nend\r\n";
  file_put_contents( $_globals['log_file'], ob_get_contents() );
  ob_end_clean();
  print "Generation complete\n";
}

function saveTemplate( $file, $template, & $bag )
{
  $bytes = file_put_contents( $file, applyTemplate( $template, $bag ) );
}

function printAllSubChildren( & $elem, $prefix, $suffix ) {
	//print "subchild test count = ". count( $elem['elements'] ) ."\n";
	//print "subchild test name = ". $elem['element_name'] ."\n";
	global $_globals;
	global $meta;
	for ( $i = 0; $i < count( $elem['elements'] ); $i++ ) {
		if ( isset( $meta[$elem['elements'][$i]] ) ) {
			if ( $meta[$elem['elements'][$i]]['isAGroup'] ) {
				
				printAllSubChildren( $meta[$elem['elements'][$i]], $prefix, $suffix );
			}
			else if ( !$meta[$elem['elements'][$i]]['abstract'] ) {
				print $prefix ."_Meta->children.append(\"". $elem['elements'][$i] ."\");". $suffix;
				print ");\n";
			}
			for( $c = 0; $c < count( $meta[$elem['elements'][$i]]['substitutableWith']); $c++ ) {
				$subwith = $meta[$elem['elements'][$i]]['substitutableWith'][$c];
				print $prefix ."_Meta->children.append(\"". $subwith ."\");". $suffix;
				print ");\n";
			}
		}
		else {
			print $prefix . $elem['elements'][$i] . $suffix;
			if ( isset($meta[$elem['element_attrs'][ $elem['elements'][$i] ]['type']]) &&
					$meta[$elem['element_attrs'][ $elem['elements'][$i] ]['type']]['isAComplexType'] ) {
				print ", \"". $elem['element_attrs'][ $elem['elements'][$i] ]['type'] ."\"";
			}
			print ");\n";
		}
	}
}

function getInheritanceStatement($baseClasses) {
	if (count($baseClasses) == 0)
		return "";
	$statement = " : public " . $baseClasses[0];
	for ($i = 1; $i < count($baseClasses); $i++)
		$statement .= ", public " . $baseClasses[$i];
	return $statement;
}

function beginConstructorInitializer(& $initializerListStarted) {
	print $initializerListStarted ? ", " : " : ";
	$initializerListStarted = true;
}

function printBaseClassInitializers($elemName, $baseClasses, & $initializerListStarted) {
	$elt = strpos($elemName, "_complexType") === false ? "this" : "elt";
	for ($i = 0; $i < count($baseClasses); $i++) {
		beginConstructorInitializer($initializerListStarted);
		print $baseClasses[$i] .
		      (strpos($baseClasses[$i], "_complexType") !== false ? "(dae, " . $elt . ")" : "(dae)");
	}
}

function printConstructors( $elemName, & $bag, $baseClasses, $indent ) {
	//print the protected ctor and copy stuff
	print $indent ."protected:\n";
	print $indent ."\t/**\n". $indent ."\t * Constructor\n". $indent ."\t */\n";
	print $indent ."\t". $elemName ."(DAE& dae";
	if ($bag['isAComplexType'])
		print ", daeElement* elt";
	print ")";
	$initializerListStarted = false;
	$eltVar = $bag['isAComplexType'] ? "*elt" : "*this";

	printBaseClassInitializers($elemName, $baseClasses, $initializerListStarted);

	if ($bag['useXMLNS']) {
		beginConstructorInitializer($initializerListStarted);
		print "attrXmlns(dae, " . $eltVar . ")";
	}
	
	// Constructor initialization of attributes
	if (count($bag['attributes']) > 0) {
		foreach( $bag['attributes'] as $attr_name => & $a_list ) {
			beginConstructorInitializer($initializerListStarted);

			$attr_name = ucfirst($attr_name);
			$type = $a_list['type'];
			print "attr" . $attr_name . "(";
			if ($type == 'xs:anyURI' || $type == 'URIFragmentType')
				print "dae, " . $eltVar;
			else if ($type == 'xs:IDREF')
				print $eltVar;
			else if ($type == 'xs:IDREFS')
				print "new xsIDREF(" . $eltVar . ")";
			print ")";
		}
	}
	
	// Constructor initialization of elements
	for( $i=0; $i<count( $bag['elements'] ); $i++ ) {
		$maxOccurs = $bag['element_attrs'][ $bag['elements'][$i] ]['maxOccurs'];
		$maxOccurs = ($maxOccurs == 'unbounded' || $maxOccurs > 1);
		beginConstructorInitializer($initializerListStarted);
		print "elem" . ucfirst($bag['elements'][$i]) . ($maxOccurs ? "_array" : "") . "()";
	}

	if ( ($bag['content_type'] != '' || $bag['mixed']) && !$bag['abstract'] ) {
		beginConstructorInitializer($initializerListStarted);
		if ($bag['content_type'] == 'xs:anyURI' || $bag['content_type'] == 'URIFragmentType')
			print "_value(dae, " . $eltVar . ")";
		else if ($bag['content_type'] == 'xs:IDREF')
			print "_value(" . $eltVar . ")";
		else if ($bag['content_type'] == 'xs:IDREFS')
			print "_value(new xsIDREF(" . $eltVar . "))";
		else
			print "_value()";
	}	
	print " {}\n";
	
	print $indent ."\t/**\n". $indent ."\t * Destructor\n". $indent ."\t */\n";
	print $indent ."\tvirtual ~". $elemName ."() {";
	if ( $bag['hasChoice'] ) {
		print " daeElement::deleteCMDataArray(_CMData); ";
	}
	print "}\n";

	print $indent ."\t/**\n". $indent ."\t * Overloaded assignment operator\n". $indent ."\t */\n";
	print $indent ."\tvirtual ".$elemName ." &operator=( const ".$elemName ." &cpy ) { (void)cpy; return *this; }\n";
}

function printAttributes( & $bag, & $typemeta, & $indent, $vaa ) {
	global $_globals;

	$attrCnt = 0;
	if ( $bag['useXMLNS'] ) {
		//comment
		print $indent ."\t/**\n". $indent ."\t * Gets the xmlns attribute.\n";
		print $indent ."\t * @return Returns a xsAnyURI reference of the xmlns attribute.\n";
		print $indent ."\t */\n";
		//code
		print $indent ."\txsAnyURI &getXmlns() { return attrXmlns; }\n";
		//comment
		print $indent ."\t/**\n". $indent ."\t * Gets the xmlns attribute.\n";
		print $indent ."\t * @return Returns a constant xsAnyURI reference of the xmlns attribute.\n";
		print $indent ."\t */\n";
		//code
		print $indent ."\tconst xsAnyURI &getXmlns() const { return attrXmlns; }\n";
		//comment
		print $indent ."\t/**\n". $indent ."\t * Sets the xmlns attribute.\n";
		print $indent ."\t * @param xmlns The new value for the xmlns attribute.\n";
		print $indent ."\t */\n";	
		//code
		print $indent ."\tvoid setXmlns( const xsAnyURI &xmlns ) { attrXmlns = xmlns;";
		if ( $vaa ) {
			print $indent ."\n\t _validAttributeArray[". $attrCnt ."] = true;";
		}
		print " }\n\n";
		
		$attrCnt++;
	}
	
	foreach( $bag['attributes'] as $attr_name => & $a_list ) {
		$type = $a_list['type'];
		if ( preg_match( "/xs\:/", $type ) ) { 
			$type = substr( $type, 3 );
			$pre = "xs";
		}
		else {
			$pre = $_globals['prefix'];
		}
		if ( $type == '' )
		{
			$type = "String";
		}
		$baseStringTypes = "xsDateTime xsID xsNCName xsNMTOKEN xsName xsToken xsString";
		$baseType = $pre . ucfirst( $type );
		if ( isset( $typemeta[$type] ) ) {
			$typeInfo = $typemeta[$type];
			while ( $typeInfo['base'] != '' && isset( $typemeta[$typeInfo['base']] ) ) {
				$typeInfo = $typemeta[$typeInfo['base']];
				if ( preg_match( "/xs\:/", $typeInfo['type'] ) ) { 
					$baseType = "xs" . ucfirst( substr( $typeInfo['type'], 3 ) );
				}
				else {
					$baseType = $_globals['prefix'] . ucfirst( $typeInfo['type'] );
				}
			}
		}
		
		if ( (isset( $typemeta[$type] ) && $typemeta[$type]['isArray']) || $type == 'IDREFS' ) {
			//comment
			print $indent ."\t/**\n". $indent ."\t * Gets the ". $attr_name ." array attribute.\n";
			print $indent ."\t * @return Returns a ". $pre . ucfirst( $type ) ." reference of the ". $attr_name ." array attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\t" . $pre . ucfirst( $type ) . " &get" . ucfirst($attr_name) ."() { ";
			print "return attr". ucfirst($attr_name) ."; }\n";
			//comment
			print $indent ."\t/**\n". $indent ."\t * Gets the ". $attr_name ." array attribute.\n";
			print $indent ."\t * @return Returns a constant ". $pre . ucfirst( $type ) ." reference of the ". $attr_name ." array attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\tconst " . $pre . ucfirst( $type ) . " &get" . ucfirst($attr_name) ."() const { ";
			print "return attr". ucfirst($attr_name) ."; }\n";
			//comment
			print $indent ."\t/**\n". $indent ."\t * Sets the ". $attr_name ." array attribute.\n";
			print $indent ."\t * @param at". ucfirst($attr_name)." The new value for the ". $attr_name ." array attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\tvoid set". ucfirst( $attr_name ) ."( const ". $pre . ucfirst( $type ) ." &at";
			print ucfirst($attr_name) ." ) { attr". ucfirst($attr_name) ." = at". ucfirst($attr_name) .";";
			if ( $vaa ) {
				print " _validAttributeArray[". $attrCnt ."] = true;";
			}
			print " }\n\n";
		}
		else if ( ucfirst($type) == 'AnyURI' || ucfirst($type) == 'URIFragmentType' ) {
			//comment
			print $indent ."\t/**\n". $indent ."\t * Gets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @return Returns a ". $pre . ucfirst( $type ) ." reference of the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\t" . $pre . ucfirst( $type ) . " &get" . ucfirst($attr_name) ."() { ";
			print "return attr". ucfirst($attr_name) ."; }\n";
			//comment
			print $indent ."\t/**\n". $indent ."\t * Gets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @return Returns a constant ". $pre . ucfirst( $type ) ." reference of the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\tconst " . $pre . ucfirst( $type ) . " &get" . ucfirst($attr_name) ."() const { ";
			print "return attr". ucfirst($attr_name) ."; }\n";
			//comment
			print $indent ."\t/**\n". $indent ."\t * Sets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @param at". ucfirst($attr_name)." The new value for the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\tvoid set". ucfirst( $attr_name ) ."( const ". $pre . ucfirst( $type ) ." &at";
			print ucfirst($attr_name) ." ) { attr". ucfirst($attr_name) ." = at". ucfirst($attr_name) .";";
			if ( $vaa ) {
				print " _validAttributeArray[". $attrCnt ."] = true;";
			}
			print " }\n";
			// We add a setter that takes a plain string to help with backward compatibility
			//comment
			print $indent ."\t/**\n". $indent ."\t * Sets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @param at". ucfirst($attr_name)." The new value for the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\tvoid set". ucfirst( $attr_name ) ."( xsString at";
			print ucfirst($attr_name) ." ) { attr". ucfirst($attr_name) ." = at" . ucfirst($attr_name) . ";";
			if ( $vaa ) {
				print " _validAttributeArray[". $attrCnt ."] = true;";
			}
			print " }\n\n";
		}
		else if( ucfirst($type) == 'IDREF' ) {
			//comment
			print $indent ."\t/**\n". $indent ."\t * Gets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @return Returns a ". $pre . ucfirst( $type ) ." reference of the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\t" . $pre . ucfirst( $type ) . " &get" . ucfirst($attr_name) ."() { ";
			print "return attr". ucfirst($attr_name) ."; }\n";
			//comment
			print $indent ."\t/**\n". $indent ."\t * Gets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @return Returns a constant ". $pre . ucfirst( $type ) ." reference of the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\tconst " . $pre . ucfirst( $type ) . " &get" . ucfirst($attr_name) ."() const{ ";
			print "return attr". ucfirst($attr_name) ."; }\n";
			//comment
			print $indent ."\t/**\n". $indent ."\t * Sets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @param at". ucfirst($attr_name)." The new value for the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\tvoid set". ucfirst( $attr_name ) ."( const ". $pre . ucfirst( $type ) ." &at";
			print ucfirst($attr_name) ." ) { attr". ucfirst($attr_name) ." = at". ucfirst($attr_name) .";";
			if ( $vaa ) {
				print " _validAttributeArray[". $attrCnt ."] = true;";
			}
			print " }\n\n";
		}
		else if ( strstr( $baseStringTypes, $baseType ) !== FALSE && count( $a_list['enum'] ) == 0 ) {
			//comment
			print $indent ."\t/**\n". $indent ."\t * Gets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @return Returns a ". $pre . ucfirst( $type ) ." of the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\t" . $pre . ucfirst( $type ) . " get" . ucfirst($attr_name) ."() const { ";
			print "return attr". ucfirst($attr_name) ."; }\n";
			//comment
			print $indent ."\t/**\n". $indent ."\t * Sets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @param at". ucfirst($attr_name)." The new value for the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\tvoid set". ucfirst( $attr_name ) ."( ". $pre . ucfirst( $type ) ." at";
			print ucfirst($attr_name) ." ) { *(daeStringRef*)&attr". ucfirst($attr_name) ." = at". ucfirst($attr_name) .";";
			if ( $vaa ) {
				print " _validAttributeArray[". $attrCnt ."] = true; ";
			}
			if ( $attr_name == "id" )
			{
				print "\n". $indent ."\t\tif( _document != NULL ) _document->changeElementID( this, attrId );\n". $indent ."\t";
			}
			
			print "}\n\n";
		}
		else {
			//comment
			print $indent ."\t/**\n". $indent ."\t * Gets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @return Returns a ". $pre . ucfirst( $type ) ." of the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\t" . $pre . ucfirst( $type ) . " get" . ucfirst($attr_name) ."() const { ";
			print "return attr". ucfirst($attr_name) ."; }\n";
			//comment
			print $indent ."\t/**\n". $indent ."\t * Sets the ". $attr_name ." attribute.\n";
			print $indent ."\t * @param at". ucfirst($attr_name)." The new value for the ". $attr_name ." attribute.\n";
			print $indent ."\t */\n";
			//code
			print $indent ."\tvoid set". ucfirst( $attr_name ) ."( ". $pre . ucfirst( $type ) ." at";
			print ucfirst($attr_name) ." ) { attr". ucfirst($attr_name) ." = at". ucfirst($attr_name) .";";
			if ( $vaa ) {
				print " _validAttributeArray[". $attrCnt ."] = true;";
			}
			print " }\n\n";
		}
		$attrCnt++;
	}
}

?>