cvs: pear /XML_DTD DTD_Parser.php

From: Date: Sun, 24 Feb 2002 04:35:18 +0000
Subject: cvs: pear /XML_DTD DTD_Parser.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-2581@lists.php.net to get a copy of this message
cox Sat Feb 23 23:35:18 2002 EDT Modified files: /pear/XML_DTD DTD_Parser.php Log: - Better split of the different part of each DTD element - Added Lincense header Index: pear/XML_DTD/DTD_Parser.php diff -u pear/XML_DTD/DTD_Parser.php:1.1 pear/XML_DTD/DTD_Parser.php:1.2 --- pear/XML_DTD/DTD_Parser.php:1.1 Sat Feb 23 16:15:02 2002 +++ pear/XML_DTD/DTD_Parser.php Sat Feb 23 23:35:17 2002 @@ -1,4 +1,25 @@ <?php +// +// +----------------------------------------------------------------------+ +// | XML_DTD_Parser class | +// +----------------------------------------------------------------------+ +// | Copyright (c) 2002 Tomas Von Veschler Cox | +// +----------------------------------------------------------------------+ +// | This source file is subject to version 2.02 of the PHP license, | +// | that is bundled with this package in the file LICENSE, and is | +// | available at through the world-wide-web at | +// | http://www.php.net/license/2_02.txt. | +// | If you did not receive a copy of the PHP license and are unable to | +// | obtain it through the world-wide-web, please send a note to | +// | license@php.net so we can mail you a copy immediately. | +// +----------------------------------------------------------------------+ +// | Authors: Tomas V.V.Cox <cox@idecnet.com> | +// | | +// +----------------------------------------------------------------------+ +// +// $Id: DTD_Parser.php,v 1.2 2002/02/24 04:35:17 cox Exp $ +// + /* DTD tree format: @@ -15,7 +36,6 @@ ) ) ) - */ class XML_DTD_Parser { @@ -26,15 +46,39 @@ $cont = join('', file($file)); if (preg_match_all('|<!([^>]+)>|s', $cont, $m)) { foreach ($m[1] as $tag) { - // XXX Will fail with this format: <!ELEMENT p (a | ul | b )*> - // because of the spaces inside the allowed list - $t = preg_split('|\s+|s', trim($tag)); - switch ($t[0]) { + $fields = array(); + $split = true; + $buff = ''; + $tag = preg_replace('|\s+|s', ' ', $tag); + // Manual split the parts of the elements + for ($i = 0; $i < strlen($tag); $i++) { + if ($tag{$i} == ' ' && $split && $buff) { + $fields[] = $buff; + $buff = ''; + continue; + } + if ($tag{$i} == '(') { + $split = false; + } elseif ($tag{$i} == ')') { + $split = true; + } + $buff .= $tag{$i}; + } + if ($buff) { + $fields[] = $buff; + } + // Call the element handler + $elem = $fields[0]; + array_shift($fields); + switch ($elem) { case 'ELEMENT': - $this->ELEMENT($t); + $this->ELEMENT($fields); break; case 'ATTLIST': - $this->ATTLIST($t); + $this->ATTLIST($fields); + break; + case 'ENTITY': + trigger_error("$elem not implemented yet", E_USER_WARNING); break; } } @@ -44,7 +88,7 @@ function ELEMENT($data) { - $childs_str = $data[2]; + $childs_str = $data[1]; if (substr($childs_str, -1) != ')') { $list_prop = substr($childs_str, -1); $childs_str = substr($childs_str, 1, -2); @@ -66,29 +110,13 @@ } $c[$child] = $prop; } - $this->dtd['elements'][$data[1]]['children'] = $c; + $this->dtd['elements'][$data[0]]['children'] = $c; } - /* - Array - ( - [0] => ATTLIST - [1] => dep - [2] => type - [3] => (pkg|ext|php|prog|ldlib|ltlib|os|websrv|sapi) - [4] => #REQUIRED - [5] => rel - [6] => (has|eq|lt|le|gt|ge) - [7] => #IMPLIED - [8] => version - [9] => CDATA - [10] => #IMPLIED - ) - */ function ATTLIST($data) { - $elem = $data[1]; - array_shift($data); array_shift($data); // XXX Wish: array_shift(array, num_of_elems) + $elem = $data[0]; + array_shift($data); // XXX #FIXED not supported yet for ($i=0; $i < count($data) ; $i = $i + 3) { $att = $data[$i];

« previous php.pear.cvs (#2581) next »