| [ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // $Id$ 4 // 5 // Definition of eZSchema class 6 // 7 // Bård Farstad <bf@ez.no> 8 // Created on: <13-Feb-2002 09:15:42 bf> 9 // 10 // SOFTWARE NAME: eZ publish 11 // SOFTWARE RELEASE: 3.9.0 12 // BUILD VERSION: 17785 13 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 14 // SOFTWARE LICENSE: GNU General Public License v2.0 15 // NOTICE: > 16 // This program is free software; you can redistribute it and/or 17 // modify it under the terms of version 2.0 of the GNU General 18 // Public License as published by the Free Software Foundation. 19 // 20 // This program is distributed in the hope that it will be useful, 21 // but WITHOUT ANY WARRANTY; without even the implied warranty of 22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 // GNU General Public License for more details. 24 // 25 // You should have received a copy of version 2.0 of the GNU General 26 // Public License along with this program; if not, write to the Free 27 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 28 // MA 02110-1301, USA. 29 // 30 // 31 32 /*! 33 \class eZSchema ezschema.php 34 \ingroup eZXML 35 \brief eZSchema handles schema validation on dom documents 36 37 */ 38 39 include_once ( "lib/ezutils/classes/ezdebug.php" ); 40 41 include_once ( "lib/ezxml/classes/ezxml.php" ); 42 include_once ( "lib/ezxml/classes/ezschemaelement.php" ); 43 44 include_once ( "lib/ezxml/classes/ezsimpletype.php" ); 45 include_once ( "lib/ezxml/classes/ezcomplextype.php" ); 46 47 class eZSchema 48 { 49 /*! 50 Constructs a new schema element. 51 */ 52 function eZSchema( ) 53 { 54 } 55 56 /*! 57 Sets the schema from a text file or a URL. 58 */ 59 function setSchemaFromFile( $url ) 60 { 61 $fp = fopen( $url, "rb" ); 62 $doc = fread( $fp, filesize( $url ) ); 63 fclose( $fp ); 64 $this->setSchema( $doc ); 65 } 66 67 /*! 68 Reads the schema and builds the schema tree. 69 */ 70 function setSchema( &$schemaDocument ) 71 { 72 $xml = new eZXML(); 73 $schemaDom =& $xml->domTree( $schemaDocument ); 74 75 $schemaRoot = $schemaDom->root(); 76 77 $this->RootPrefix = $schemaRoot->prefix(); 78 print( "default namespace prefix. " . $schemaRoot->prefix() . "<br>" ); 79 80 $registeredTypes = array(); 81 $usedTypes = array(); 82 83 $i = 0; 84 // check that this is a schema definition 85 if ( $schemaRoot->name() == "schema" ) 86 { 87 foreach ( $schemaRoot->children() as $schemaNode ) 88 { 89 // register types 90 $element =& $this->parseElement( $schemaNode ); 91 92 if ( get_class( $element ) == "ezschemaelement" ) 93 { 94 if ( $i == 0 ) 95 { 96 print( "validation root found: "); 97 $this->ValidationRoot =& $element; 98 } 99 100 // $this->Elements[] =& $element; 101 $i++; 102 } 103 } 104 } 105 } 106 107 /*! 108 \private 109 Parses the given dom tree part and returns an element object. 110 */ 111 function &parseElement( &$schemaNode, $parentElement=false ) 112 { 113 $ret = false; 114 // if ( $schemaNode->name() == "element" ) 115 { 116 $element = new eZSchemaElement(); 117 $element->setParent( $parentElement ); 118 $element->setName( $schemaNode->attributeValue( "name" ) ); 119 120 // set the next reference in the parent element 121 if ( get_class( $parentElement ) == "ezelement" ) 122 $parentElement->setNext( $element ); 123 124 $minOccurs = $schemaNode->attributeValue( "minOccurs" ); 125 $element->setMinOccurs( $minOccurs ); 126 127 $maxOccurs = $schemaNode->attributeValue( "maxOccurs" ); 128 $element->setMaxOccurs( $maxOccurs ); 129 130 // get element type: 131 132 // check attributes for type def 133 $type = $schemaNode->attributeValue( "type" ); 134 $ref = $schemaNode->attributeValue( "ref" ); 135 136 if ( $schemaNode->name() == 'element' ) 137 { 138 // check for standard xml schema types 139 if ( strpos( $type, $this->RootPrefix . ":" ) === 0 ) 140 { 141 $typeName = substr( $type, strlen( $this->RootPrefix . ":" ), strlen( $type ) - strlen( $this->RootPrefix . ":" ) ); 142 print( "Standard type found: " . $typeName . "<br>" ); 143 $element->setDataType( $typeName ); 144 } 145 else 146 { 147 // complex type 148 $element->setDataType( $type ); 149 } 150 } 151 else if ( $schemaNode->name() == 'complexType' ) 152 { 153 // complex type definition 154 print( "complex def..<br/>" ); 155 } 156 else if ( $ref != false ) 157 { 158 // reference element 159 print( "reference element found: $ref<br>" ); 160 $element->setName( $ref ); 161 $element->setReference( $ref ); 162 } 163 else 164 { 165 print( "no attribute type def. found. Checking children...<br>" ); 166 // check for complex type 167 foreach ( $schemaNode->children() as $typeElement ) 168 { 169 if ( $typeElement->name() == "complexType" ) 170 { 171 print( $typeElement->name() . " found <br>" ); 172 173 foreach ( $typeElement->children() as $listElement ) 174 { 175 print( $listElement->name() . "<br>" ); 176 switch ( $listElement->name() ) 177 { 178 case "sequence" : 179 { 180 foreach ( $listElement->children() as $seqElement ) 181 { 182 $subElement =& $this->parseElement( $seqElement, $element ); 183 184 if ( get_clasS( $subElement ) == "ezschemaelement" ) 185 $element->Children[] = $subElement; 186 187 } 188 189 }break; 190 } 191 192 } 193 } 194 } 195 196 } 197 198 $ret = $element; 199 print( "new element found: ". $element->name() . "<br>" ); 200 201 $this->Elements[$element->name()] =& $element; 202 } 203 204 return $ret; 205 } 206 207 /*! 208 Validates the eZDOMDocument with the schema. 209 */ 210 function validate( $dom ) 211 { 212 $root =& $dom->root(); 213 214 $schemaRoot =& $this->ValidationRoot; 215 print( "Validating document: " . $root->name() . "<br>" ); 216 217 if ( $schemaRoot->name() == $root->name() ) 218 { 219 $this->validateNode( $root, $schemaRoot ); 220 } 221 else 222 { 223 print( "Error: document root mismatch." ); 224 } 225 } 226 227 /*! 228 \private 229 Validates a DOM node. 230 */ 231 function validateNode( &$domNode, &$schemaElement ) 232 { 233 print( "Validating: " . $domNode->name() . "<br>" ); 234 235 print( "Comparing: " . $domNode->name() . " against " . 236 $schemaElement->name() . " <br>" ); 237 238 if ( $domNode->name() == $schemaElement->name() ) 239 { 240 // find the next schema element 241 $refElement = false; 242 if ( $schemaElement->type() == "reference" ) 243 { 244 if ( isset( $this->Elements[$element->name()] ) ) 245 { 246 $refElement =& $this->Elements[$element->name()]; 247 } 248 else 249 print( "reference not found<br>" ); 250 } 251 252 foreach ( $domNode->children() as $subNode ) 253 { 254 $this->validateNode( $subNode, $schemaElement ); 255 // print( $subNode->name() ); 256 } 257 } 258 else 259 { 260 print( "Error validating document" . $domNode->name() ); 261 } 262 263 } 264 265 /*! 266 Debug function to print the document tree. 267 */ 268 function printTree( $dom ) 269 { 270 $level = 1; 271 print( "<br>Document structure for: ". $this->ValidationRoot->name() . "<br>" ); 272 $children = $this->ValidationRoot->children(); 273 $childrenKeys = array_keys( $children ); 274 foreach ( $childrenKeys as $key ) 275 { 276 $this->printElement( $children[$key], $dom->root(), $level ); 277 } 278 } 279 280 /*! 281 \private 282 Debug function. Prints the element information. 283 */ 284 function printElement( &$element, &$dom, $level ) 285 { 286 $spacer = str_repeat( " ", $level*4 ); 287 288 // fetch reference, if any 289 $refElement = false; 290 if ( $element->type() == "reference" ) 291 { 292 if ( isset( $this->Elements[$element->name()] ) ) 293 { 294 $refElement =& $this->Elements[$element->name()]; 295 } 296 else 297 print( "reference not found<br>" ); 298 } 299 300 if ( $refElement ) 301 $tmpElement =& $refElement; 302 else 303 $tmpElement =& $element; 304 305 print( "$spacer Looking for element: " . 306 $tmpElement->name() . 307 " " . $tmpElement->dataType() . 308 " " . $element->minOccurs() . 309 "->" . $element->maxOccurs() . 310 "<br>" ); 311 312 $currentElementName = $tmpElement->name(); 313 314 $counter = 0; 315 foreach ( $dom->children() as $domNode ) 316 { 317 if ( $domNode->name() == $currentElementName ) 318 { 319 // validate children 320 321 foreach ( $tmpElement->children() as $child ) 322 { 323 $this->printElement( $child, $domNode, $level + 1 ); 324 } 325 326 $counter++; 327 } 328 else 329 { 330 // print( "Unknown tag: " . $domNode->name() . " expecting $currentElementName <br> " ); 331 } 332 } 333 334 if ( $counter >= $element->minOccurs() ) 335 { 336 if ( ( $counter <= $element->maxOccurs() ) or 337 ( $element->maxOccurs() == "unbounded" ) 338 ) 339 { 340 print( "Found $counter elements of $currentElementName , ok ! <br>" ); 341 } 342 else 343 { 344 print( "<b>Error</b>: Element $currentElementName count NOT within range, too many elements: $counter <br>" ); 345 } 346 347 } 348 else 349 print( "<b>Error</b>: Element $currentElementName count NOT within range , to few items $counter <br>" ); 350 } 351 352 /*! 353 Debug function to print the schema tree. 354 */ 355 function printSchemaTree() 356 { 357 print( "<br/><b>Schema Tree</b><br/>" ); 358 $this->printSchemaNode( $this->ValidationRoot, 0 ); 359 print( "<br/>" ); 360 } 361 362 /*! 363 364 */ 365 function printSchemaNode( $node, $level ) 366 { 367 $spacer = str_repeat( " ", $level*4 ); 368 print( $spacer . $node->name() . " " . $node->type() . "<br>" ); 369 370 foreach ( $node->children() as $childNode ) 371 { 372 $this->printSchemaNode( $childNode, $level + 1 ); 373 } 374 } 375 376 /// Contains the validation root 377 var $ValidationRoot = false; 378 379 /// Contains the schema elements, elements are indexed by their name. 380 var $Elements = array(); 381 382 /// Description or the schema 383 var $Annotation = ""; 384 385 /// Contains the schema root namespace prefix 386 var $RootPrefix; 387 388 } 389 390 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |