function visitElementNode(PiBX_ParseTree_Tree $tree) { $this->plowTypesForLevel($tree->getLevel()); $logMessage = $tree->getLevel() . str_pad(" ", $tree->getLevel()) . " element"; if ($tree->isAnonym()) { $logMessage .= " <anonym>"; } else { $logMessage .= " (" . $tree->getName() . ")"; } if ($tree->getLevel() > 0) { if ($this->currentType() instanceof PiBX_AST_Type) { $ta = new PiBX_AST_TypeAttribute($tree->getName()); $ta->setType($tree->getType()); $schemaType = $tree->getType(); $sf = new PiBX_CodeGen_ASTStackFrame($tree->getLevel(), $ta); array_push($this->stack, $sf); // elements with maxOccurs "unbounded" are collections/lists // with no parent collection element. if ($tree->getMaxOccurs() == 'unbounded') { $ci = new PiBX_AST_CollectionItem($tree->getName()); $ci->setType($tree->getType()); $sf = new PiBX_CodeGen_ASTStackFrame($tree->getLevel(), $ci); array_push($this->stack, $sf); } } elseif ($this->currentType() instanceof PiBX_AST_Collection) { $ci = new PiBX_AST_CollectionItem($tree->getName()); $ci->setType($tree->getType()); $sf = new PiBX_CodeGen_ASTStackFrame($tree->getLevel(), $ci); array_push($this->stack, $sf); } elseif ($this->currentType() instanceof PiBX_AST_Structure) { $se = new PiBX_AST_StructureElement($tree->getName(), $tree->getType()); $sf = new PiBX_CodeGen_ASTStackFrame($tree->getLevel(), $se); array_push($this->stack, $sf); } } elseif ($tree->getLevel() == 0) { $logMessage .= " - global"; $t = new PiBX_AST_Type($tree->getName()); if ($this->countTypes() == 0) { // the first type is the XSD-root. $t->setAsRoot(); $t->setTargetNamespace($tree->getParent()->getTargetNamespace()); $t->setNamespaces($tree->getNamespaces()); } $sf = new PiBX_CodeGen_ASTStackFrame(-1, $t); array_push($this->stack, $sf); /* * Yes, add it two times. * It's a limitation that exist in the current implementation * of the TypeUsage-class. * The concrete class (which is the root element/type of a schema) * will not be referenced more than one time (at the definition). * But it must not be removed in an AST optimization afterwards, * so we just increase the usage-counter by one. */ $this->typeUsage->addType($tree->getName()); $this->typeUsage->addType($tree->getName()); } else { throw new RuntimeException('invalid element state'); } $this->lastLevel = $tree->getLevel(); $this->log($logMessage); }
public function testBooksExampleXSD() { $data = file_get_contents(dirname(__FILE__) . '/../_files/Books/books.xsd'); $expectedTypeList = array(); $expectedType1 = new PiBX_AST_Type('Collection'); $expectedType1->setAsRoot(); $expectedType1->setAttributeCount(1); $expectedType1->setNamespaces(array('xs' => 'http://www.w3.org/2001/XMLSchema')); $ta = new PiBX_AST_TypeAttribute('books'); $c = new PiBX_AST_Collection(); $ci = new PiBX_AST_CollectionItem('book'); $ci->setType('bookType'); $expectedType1->add($ta); $ta->add($c); $c->add($ci); $expectedTypeList[] = $expectedType1; $expectedType2 = new PiBX_AST_Type('bookType'); $expectedType2->setAttributeCount(8); $expectedType2->add(new PiBX_AST_TypeAttribute('name', 'string')); $expectedType2->add(new PiBX_AST_TypeAttribute('ISBN', 'long')); $expectedType2->add(new PiBX_AST_TypeAttribute('price', 'string')); $ta = new PiBX_AST_TypeAttribute('authors'); $c = new PiBX_AST_Collection(); $ci = new PiBX_AST_CollectionItem('authorName', 'string'); $c->add($ci); $ta->add($c); $expectedType2->add($ta); $expectedType2->add(new PiBX_AST_TypeAttribute('description', 'string')); $ta = new PiBX_AST_TypeAttribute('promotion'); $s = new PiBX_AST_Structure(); $s->setStructureType(PiBX_AST_StructureType::CHOICE()); $s->add(new PiBX_AST_StructureElement('Discount', 'string')); $s->add(new PiBX_AST_StructureElement('None', 'string')); $ta->add($s); $expectedType2->add($ta); $expectedType2->add(new PiBX_AST_TypeAttribute('publicationDate', 'date')); $ta = new PiBX_AST_TypeAttribute('bookCategory'); $e = new PiBX_AST_Enumeration(); $e->add(new PiBX_AST_EnumerationValue('magazine', 'string')); $e->add($ev = new PiBX_AST_EnumerationValue('novel', 'string')); $e->add($ev = new PiBX_AST_EnumerationValue('fiction', 'string')); $e->add($ev = new PiBX_AST_EnumerationValue('other', 'string')); $ta->add($e); $expectedType2->add($ta); $ta = new PiBX_AST_TypeAttribute('itemId', 'string'); $ta->setStyle('attribute'); $expectedType2->add($ta); $expectedTypeList[] = $expectedType2; $enumeration = new PiBX_AST_Enumeration(); $enumeration->add(new PiBX_AST_EnumerationValue('magazine', 'string')); $enumeration->add(new PiBX_AST_EnumerationValue('novel', 'string')); $enumeration->add(new PiBX_AST_EnumerationValue('fiction', 'string')); $enumeration->add(new PiBX_AST_EnumerationValue('other', 'string')); $expectedType3 = new PiBX_AST_Type('bookCategoryType'); $expectedType3->add($enumeration); $expectedTypeList[] = $expectedType3; $schema = simplexml_load_string($data); $parser = new PiBX_CodeGen_SchemaParser(); $parser->setSchema($schema); $tree = $parser->parse(); $creator = new PiBX_CodeGen_ASTCreator(new PiBX_CodeGen_TypeUsage()); $tree->accept($creator); $typeList = $creator->getTypeList(); $this->assertEquals(3, count($typeList)); $this->assertEquals($expectedType1, $typeList[0]); $this->assertEquals($expectedType2, $typeList[1]); $this->assertEquals($expectedType3, $typeList[2]); }