[Phpxd-commits] CVS: phpXD/include/parser DTDParser.php,NONE,1.1 DOMParser.php,1.2,1.3
Status: Beta
Brought to you by:
growbal
From: Thomas D. <th...@us...> - 2002-01-26 13:20:06
|
Update of /cvsroot/phpxd/phpXD/include/parser In directory usw-pr-cvs1:/tmp/cvs-serv12482/include/parser Modified Files: DOMParser.php Added Files: DTDParser.php Log Message: Just started the implementation of a DTD parser. --- NEW FILE: DTDParser.php --- <?php // phpXD - a XML DOM Implementation // // This Software and all associated files are released unter the // GNU Public License (GPL), see LICENSE for details. // // $Id: DTDParser.php,v 1.1 2002/01/26 13:20:01 thomi Exp $ /** * Parse a dtd. EXPERIMENTAL! * * @package DTDParser * @author Thomas Dohmke <th...@do...> * @version $Revision: 1.1 $ */ class DTDParser { var $parseDTD = false; var $parameterEntities; function parse($str) { if (!empty($str)) { while (preg_match('=(.*)\%([a-z,A-Z,0-9,\.]*);(.*)$=sU', $str, $ent)) { $str = preg_replace('=<!--.*-->=sU', '', $str); while (preg_match('=(.*)<!ENTITY[ ,\n,\r,\t]*\%[ ,\n,\r,\t]*([a-z,A-Z,0-9,\.]*)[ ,\n,\r,\t]*PUBLIC[ ,\n,\r,\t]*"([a-z,A-Z,0-9,\-,/, ]*)"[ ,\n,\r,\t]*"([a-z,A-Z,0-9,\.,\-,_,/]*)"[ ,\n,\r,\t]*>(.*)$=sU', $str, $ent)) { if ($this->file_exists($this->currentDir."/".$ent[4])) { $this->parameterEntities[$ent[2]] = preg_replace('=<!--.*-->=sU', '', implode("", file($this->currentDir."/".$ent[4]))); } $str = $ent[1].$ent[5]; } while (preg_match('=(.*)<!ENTITY[ ,\n,\r,\t]*\%[ ,\n,\r,\t]*([a-z,A-Z,0-9,\.]*)[ ,\n,\r,\t]*"(.*)"[ ,\n,\r,\t]*>(.*)$=sU', $str, $ent)) { $this->parameterEntities[$ent[2]] = $ent[3]; $str = $ent[1].$ent[4]; } while (preg_match('=(.*)\%([a-z,A-Z,0-9,\.]*);(.*)$=sU', $str, $ent)) { $str = $ent[1].$this->parameterEntities[$ent[2]].$ent[3]; } } $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_set_object($parser, $this); // parse only the dtd xml_set_default_handler($parser, "handleDefault"); // xml_set_unparsed_entity_decl_handler($parser, // "handleUnparsedEntityDecl"); // xml_set_external_entity_ref_handler($parser, "handleExternalEntityRef"); if (!xml_parse($parser, $str, true)) { $this->displayError("XML error in line %d column %d: %s", xml_get_current_line_number($parser), xml_get_current_column_number($parser), xml_error_string(xml_get_error_code($parser))); } xml_parser_free($parser); } else { $this->displayError("The string is empty."); } } function parseFile($filename) { if ($this->file_exists($filename)) { $path = explode("/", $filename); if (count($path) > 0) { unset($path[count($path) - 1]); $this->currentDir = implode("/", $path); } $content = implode("", file($filename)); $content = "<!DOCTYPE null [".$content."]><null />"; return $this->parse($content); } else { $this->displayError("File %s could not be found or read.", $filename); } } function parseTokens($tokens) { // assert: tokens are all valid $this->parseDoctype($tokens); } function parseDoctype($tokens) { $token = $tokens[0]; if ($this->compareToken($token, "<!DOCTYPE")) { $this->parseDocumentElement(array_slice($tokens, 1)); } } function parseDocumentElement($tokens) { $token = $tokens[0]; $this->documentElement = $token; $token = $tokens[1]; if ($this->compareToken($token, "[")) { $this->parseInternalDTD(array_slice($tokens, 2)); } if ($this->compareToken($token, "SYSTEM")) { $this->parseExternalSystemDTD(array_slice($tokens, 2)); } if ($this->compareToken($token, "PUBLIC")) { $this->parseExternalPublicDTD(array_slice($tokens, 2)); } } function parseInternalDTD($tokens) { } function parseExternalSystemDTD($tokens) { } function parseExternalPublicDTD($tokens) { $tokenPublicID = $this->removeQuotes($tokens[0]); $tokenURI = $this->removeQuotes($tokens[1]); switch ($tokenPublicID) { case "-//W3C//DTD XHTML 1.0 Strict//EN": { $DTDParser = new DTDParser(); // $DTDParser->parseFile("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"); $DTDParser->parseFile("../xhtml1-strict.dtd"); break; } } // echo $tokenURI; } function compareToken($tok1, $tok2) { if ($tok1 == $tok2) { return true; } else { return false; } } function removeQuotes($str) { if (($str[0] == "\"") && ($str[strlen($str) - 1] == "\"")) { return substr($str, 1, strlen($str) - 2); } return $str; } function handleDefault($parser, $data) { $data = trim($data); // echo "<pre>".$data."</pre>"; if ($data == "<!DOCTYPE") { $this->DTDTokens[] = $data; $this->parseDTD = true; return true; } if ($this->parseDTD) { $this->DTDTokens[] = $data; if ($data == "[") { $this->parseInternalDTD = true; } if ($data == "]") { $this->parseInternalDTD = false; } if (($data == ">") && (!$this->parseInternalDTD)) { $this->parseDTD = false; $this->parseTokens($this->DTDTokens); } return true; } } function handleExternalEntityRef($parser, $openEntityNames, $base, $systemId, $publicId) { echo "hier"; return true; } function handleUnparsedEntityDecl($parser, $entityName, $base, $systemId, $publicId, $notationName) { echo "hier"; return true; } function file_exists($filename) { $file = @fopen($filename, "r"); if (!$file) { return false; } return true; } /** * Displays errors, which occur while parsing. * * @private * @returns void */ function displayError($message) { if (func_num_args() > 1) { $arguments = func_get_args(); $command = "\$message = sprintf(\$message, "; for ($i = 1; $i < sizeof($arguments); $i++ ) { $command .= "\$arguments[".$i."], "; } $command = eregi_replace(", $", ");", $command); eval($command); } echo "<strong>phpXD error:</strong> ".$message; exit; } } Index: DOMParser.php =================================================================== RCS file: /cvsroot/phpxd/phpXD/include/parser/DOMParser.php,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -r1.2 -r1.3 *** DOMParser.php 2002/01/25 22:29:26 1.2 --- DOMParser.php 2002/01/26 13:20:01 1.3 *************** *** 50,76 **** /** ! * The parsed DTD string. Only valid while parsing a XML file! * * @private ! * @type string */ ! var $parsedDTD = false; ! /** ! * The parsed DTD is an internal DTD. Only valid while parsing ! * a XML file! * * @private ! * @type boolean */ ! var $parsedDTDInternal = false; /** ! * A array with the defined namespaces. ! * ! * @private ! * @type array */ ! var $namespaces = false; /** --- 50,74 ---- /** ! * A array with the defined namespaces. * * @private ! * @type array */ ! var $namespaces = false; ! /** ! * The DTD is parsed. * * @private ! * @type string */ ! var $parseDTD = false; ! var $parseInternalDTD = false; /** ! * ****TODO**** */ ! var $DTDParser = null; ! var $DTDTokens = ""; /** *************** *** 97,123 **** if (!empty($content)) { ! $this->document = new Document(); ! ! $parser = xml_parser_create(); ! ! xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); ! ! xml_set_object($parser, $this); ! ! xml_set_element_handler($parser, "handleStartElement", ! "handleEndElement"); ! xml_set_character_data_handler($parser, "handleCharacterData"); ! xml_set_processing_instruction_handler($parser, ! "handleProcessingInstruction"); ! xml_set_default_handler($parser, "handleDefault"); ! ! if (!xml_parse($parser, $content, true)) { ! $this->displayError("XML error in file %s, line %d: %s", ! $file, xml_get_current_line_number($parser), ! xml_error_string(xml_get_error_code($parser))); ! } ! ! xml_parser_free($parser); ! return $this->document; } } --- 95,99 ---- if (!empty($content)) { ! return $this->parse($content); } } *************** *** 137,140 **** --- 113,117 ---- if (!empty($str)) { $this->document = new Document(); + $this->DTDParser = new DTDParser(); $parser = xml_parser_create(); *************** *** 150,156 **** "handleProcessingInstruction"); xml_set_default_handler($parser, "handleDefault"); if (!xml_parse($parser, $str, true)) { ! $this->displayError("XML error in XML-String, line %d: %s", xml_get_current_line_number($parser), xml_error_string(xml_get_error_code($parser))); --- 127,134 ---- "handleProcessingInstruction"); xml_set_default_handler($parser, "handleDefault"); + xml_set_external_entity_ref_handler($parser, "handleExternalEntityRef"); if (!xml_parse($parser, $str, true)) { ! $this->displayError("XML error in line %d: %s", xml_get_current_line_number($parser), xml_error_string(xml_get_error_code($parser))); *************** *** 194,198 **** if (!(strpos($name, ":") === false)) { $newChild =& $this->document->createElementNS($namespace, $name); ! if ($newChild == NAME_SPACE_ERR) { $this->displayError("XML error: namespace not valid in line %d.", --- 172,176 ---- if (!(strpos($name, ":") === false)) { $newChild =& $this->document->createElementNS($namespace, $name); ! if ($newChild == NAME_SPACE_ERR) { $this->displayError("XML error: namespace not valid in line %d.", *************** *** 320,375 **** /** ! * The DTD Handler. Expat has no DTD Handler callback, so this ! * is set by handleDefault. * * @private * @returns void */ ! function handleDTD($parser, $data) { $data = trim($data); ! ! if (!empty($data)) { ! $this->parsedDTD .= " ".$data; ! } ! if ($data == "[") { ! $this->parseDTDInternal = true; ! } ! if ($data == "]") { ! $this->parseDTDInternal = false; } - if ((substr($this->parsedDTD, strlen($this->parsedDTD)-1) == ">") && - !$this->parseDTDInternal) { - $this->document->doctype = new DocumentType(); - - // this is just a hack to differ between internal and external DTDs - $DTD = explode(" ", $this->parsedDTD); ! $this->document->doctype->name = $DTD[1]; ! if ($DTD[2] == "SYSTEM") { ! $this->document->doctype->systemId = $DTD[3]; ! } ! else { ! if ($DTD[2] == "PUBLIC") { ! $this->document->doctype->publicId = $DTD[3]; ! $this->document->doctype->systemId = $DTD[4]; ! } ! else { ! $this->document->doctype->internalSubset = $this->parsedDTD; ! } } ! xml_set_default_handler($parser, "handleDefault"); return true; } - } ! /** ! * The Default Handler for Expat. ! * ! * @private ! * @returns void ! */ ! function handleDefault($parser, $data) { ! $data = trim($data); if (!(strpos($data, "<!--") === false)) { --- 298,341 ---- /** ! * The Default Handler for Expat. * * @private * @returns void */ ! function handleDefault($parser, $data) { $data = trim($data); ! ! if ($data == "<!DOCTYPE") { ! $this->DTDTokens[] = $data; ! $this->parseDTD = true; ! return true; } ! if ($this->parseDTD) { ! if ($data != "") { ! $this->DTDTokens[] = $data; ! } ! if ($data == "[") { ! $this->parseInternalDTD = true; ! } ! if ($data == "]") { ! $this->parseInternalDTD = false; ! } ! if (($data == ">") && (!$this->parseInternalDTD)) { ! $this->parseDTD = false; ! // $this->DTDParser->parseTokens($this->DTDTokens); } + return true; + } ! if ($data == "<![CDATA[") { ! $this->parseCData = true; return true; } ! if ($data == "]]>" && $this->parseCData) { ! $this->parseCData = false; ! return true; ! } if (!(strpos($data, "<!--") === false)) { *************** *** 387,409 **** } - if ($data == "<![CDATA[") { - $this->parseCData = true; - return true; - } - - if ($data == "]]>" && $this->parseCData) { - $this->parseCData = false; - return true; - } - - if ($data == "<!DOCTYPE") { - $this->parsedDTD .= $data; - xml_set_default_handler($parser, "handleDTD"); - return true; - } - return false; } /** * Displays errors, which occur while parsing. --- 353,363 ---- } return false; } + function handleExternalEntityRef($parser, $openEntityNames, $base, $systemId, $publicId) { + return true; + } + /** * Displays errors, which occur while parsing. *************** *** 430,434 **** exit; } - } ?> --- 384,387 ---- |