[Phpxd-commits] CVS: phpXD/include/parser DOMParser.php,1.4,1.5 DTDParser.php,1.4,1.5
Status: Beta
Brought to you by:
growbal
|
From: Thomas D. <th...@us...> - 2002-02-06 23:10:50
|
Update of /cvsroot/phpxd/phpXD/include/parser
In directory usw-pr-cvs1:/tmp/cvs-serv1109/include/parser
Modified Files:
DOMParser.php DTDParser.php
Log Message:
Added support for DTDs. DOMParser uses DTDParser to parse internal and
external DTDs to make DOM interfaces Entity, EntityReference and Notation
work.
Index: DOMParser.php
===================================================================
RCS file: /cvsroot/phpxd/phpXD/include/parser/DOMParser.php,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** DOMParser.php 2002/01/29 20:57:01 1.4
--- DOMParser.php 2002/02/06 23:10:48 1.5
***************
*** 8,17 ****
/**
! * Parse a document.
! *
! * @package DOMParser
! * @author Thomas Dohmke <th...@do...>
! * @version $Revision$
! */
class DOMParser {
/**
--- 8,17 ----
/**
! * Parse a document.
! *
! * @package phpXD
! * @author Thomas Dohmke <th...@do...>
! * @version $Revision$
! */
class DOMParser {
/**
***************
*** 58,74 ****
/**
! * The DTD is parsed.
*
* @private
! * @type string
*/
var $parseDTD = false;
! var $parseInternalDTD = false;
!
/**
! * ****TODO****
*/
var $DTDParser = null;
var $DTDTokens;
var $DTDString = "";
--- 58,106 ----
/**
! * If true, the DTD will be parsed.
*
* @private
! * @type boolean
*/
var $parseDTD = false;
!
/**
! * Flag for handleDefault, that tokens are inside the dtd.
! *
! * @private
! * @type boolean
*/
+ var $DTDDecl = false;
+
+ /**
+ * Flag for handleDefault, that a internal dtd is parsed.
+ *
+ * @private
+ * @type boolean
+ */
+ var $DTDInternal = false;
+
+ /**
+ * Reference to a DTDParser.
+ *
+ * @private
+ * @type DTDParser
+ */
var $DTDParser = null;
+
+ /**
+ * An array with all tokens from dtd.
+ *
+ * @private
+ * @type array
+ */
var $DTDTokens;
+
+ /**
+ * Same as $DTDTokens but as flat string.
+ *
+ * @private
+ * @type string
+ */
var $DTDString = "";
***************
*** 89,100 ****
* @public
* @param $file <code>string</code>
! * @returns phpXD
*/
! function parseFile($file) {
! if (file_exists($file)) {
$content = implode("", file($file));
if (!empty($content)) {
! return $this->parse($content);
}
}
--- 121,133 ----
* @public
* @param $file <code>string</code>
! * @param $parseDTD <code>boolean</code>
! * @returns Document
*/
! function &parseFile($file, $parseDTD = false) {
! if ($this->file_exists($file)) {
$content = implode("", file($file));
if (!empty($content)) {
! return $this->parse($content, $parseDTD);
}
}
***************
*** 109,118 ****
* @public
* @param $str <code>string</code>
! * @returns phpXD
*/
! function parse($str) {
if (!empty($str)) {
$this->document = new Document();
! $this->DTDParser = new DTDParser();
$parser = xml_parser_create();
--- 142,160 ----
* @public
* @param $str <code>string</code>
! * @param $parseDTD <code>boolean</code>
! * @returns Document
*/
! function &parse($str, $parseDTD = false) {
if (!empty($str)) {
+ // Just a hack, cause it seems that expat doesn't support IGNORE or
+ // INCLUDE.
+ $str = preg_replace('=<!\[IGNORE\[.*\]\]>=sU', '', $str);
+ $str = preg_replace('=<!\[INCLUDE\[(.*)\]\]>=sU', '\\1', $str);
+
$this->document = new Document();
! if ($parseDTD) {
! $this->DTDParser = new DTDParser();
! $this->parseDTD = true;
! }
$parser = xml_parser_create();
***************
*** 128,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),
--- 170,175 ----
"handleProcessingInstruction");
xml_set_default_handler($parser, "handleDefault");
! if (!xml_parse($parser, $str)) {
$this->displayError("XML error in line %d: %s",
xml_get_current_line_number($parser),
***************
*** 137,140 ****
--- 178,182 ----
xml_parser_free($parser);
+ $this->document->normalize();
return $this->document;
}
***************
*** 145,148 ****
--- 187,223 ----
/**
+ * Internal method to load a file of a parsed external entity.
+ *
+ * @private
+ * @param $file <code>string</code>
+ * @returns Document
+ */
+ function &parseExternalEntity($file) {
+ if ($this->file_exists($file)) {
+ $content = implode("", file($file));
+
+ if (!empty($content)) {
+ $content = preg_replace('=<\?xml.*\?>\n=sU', '', $content);
+ $content = preg_replace('=<\?xml.*\?>=sU', '', $content);
+ return $this->parseEntity($content);
+ }
+ }
+ else {
+ $this->displayError("File %s could not be found or read.", $file);
+ }
+ }
+
+ /**
+ * Internal method to parse an entity body.
+ *
+ * @private
+ * @param $str <code>string</code>
+ * @returns Document
+ */
+ function &parseEntity($str) {
+ return $this->parse("<entity>".$str."</entity>");
+ }
+
+ /**
* The Element Start Handler for Expat.
*
***************
*** 174,178 ****
$newChild =& $this->document->createElementNS($namespace, $name);
! if ($newChild == NAME_SPACE_ERR) {
$this->displayError("XML error: namespace not valid in line %d.",
xml_get_current_line_number($parser));
--- 249,253 ----
$newChild =& $this->document->createElementNS($namespace, $name);
! if ($newChild == NAMESPACE_ERR) {
$this->displayError("XML error: namespace not valid in line %d.",
xml_get_current_line_number($parser));
***************
*** 185,188 ****
--- 260,264 ----
if (!isset($this->document->documentElement)) {
$this->document->documentElement =& $newChild;
+ $this->document->appendChild($newChild);
$this->currentNode =& $this->document->documentElement;
}
***************
*** 196,200 ****
if (!(strpos($name, ":") === false)) {
$prefix = substr($name, 0, strpos($name, ":"));
! $localName = substr($name, strpos($this->nodeName, ":")+1);
if ($prefix == "xmlns") {
$this->namespaces[$localName] = $value;
--- 272,276 ----
if (!(strpos($name, ":") === false)) {
$prefix = substr($name, 0, strpos($name, ":"));
! $localName = substr($name, strpos($name, ":")+1);
if ($prefix == "xmlns") {
$this->namespaces[$localName] = $value;
***************
*** 221,238 ****
$result = $this->currentNode->setAttributeNS($namespace, $name,
$value);
! if ($result == NAME_SPACE_ERR) {
$this->displayError("XML error: namespace not valid in line %d.",
xml_get_current_line_number($parser));
}
-
- $attr =& $this->currentNode->getAttributeNode($name);
- $attr->specified = true;
}
else {
$this->currentNode->setAttribute($name, $value);
! $attr =& $this->currentNode->getAttributeNode($name);
! $attr->specified = true;
}
}
}
--- 297,332 ----
$result = $this->currentNode->setAttributeNS($namespace, $name,
$value);
+ $attribute =& $this->currentNode->getAttributeNodeNS($namespace,
+ $localName);
! if ($result == NAMESPACE_ERR) {
$this->displayError("XML error: namespace not valid in line %d.",
xml_get_current_line_number($parser));
}
}
else {
$this->currentNode->setAttribute($name, $value);
! $attribute =& $this->currentNode->getAttributeNode($name);
}
+
+ if (isset($this->document->doctype)) {
+ $dtdElement =&
+ $this->document->doctype->elements->
+ getNamedItem($this->currentNode->nodeName);
+ if (isset($dtdElement->attributes)) {
+ $dtdAttribute =& $dtdElement->attributes->getNamedItem($name);
+ if ($dtdAttribute->default == "#FIXED") {
+ // Because the xml parser do not differ between fixed and
+ // specified attributes, all fixed attributes are not
+ // specified, also if there are specified.
+ $attribute->specified = false;
+ }
+ else {
+ if ($attribute->value == $dtdAttribute->defaultValue) {
+ $attribute->specified = false;
+ }
+ }
+ }
+ }
}
}
***************
*** 269,276 ****
}
else {
! $text = str_replace("\r", "", $text);
$this->lastNode =& $this->currentNode->appendChild(
! $this->document->createTextNode($text));
}
}
--- 363,370 ----
}
else {
! // $text = str_replace("\r", "", $text);
$this->lastNode =& $this->currentNode->appendChild(
! $this->document->createTextNode($text));
}
}
***************
*** 295,298 ****
--- 389,412 ----
/**
+ * The EntityReference Handler for Expat.
+ *
+ * @private
+ * @returns void
+ */
+ function handleEntityReference($parser, $data) {
+ $entityRef =& $this->document->createEntityReference($data);
+ $this->lastNode =& $this->currentNode->appendChild($entityRef);
+ $entity = $this->document->doctype->entities->getNamedItem($data);
+ if (($entity != null) &&
+ ($entity->hasChildNodes())) {
+ if ($entity != null) {
+ for ($i = 0; $i < $entity->childNodes->length; ++$i) {
+ $entityRef->appendChild($entity->childNodes->item($i));
+ }
+ }
+ }
+ }
+
+ /**
* The Default Handler for Expat.
*
***************
*** 303,314 ****
$data2 = trim($data);
if ($data2 == "<!DOCTYPE") {
$this->DTDTokens[] = $data2;
$this->DTDString .= $data;
! $this->parseDTD = true;
return true;
}
! if ($this->parseDTD) {
if ($data2 != "") {
$this->DTDTokens[] = $data;
--- 417,432 ----
$data2 = trim($data);
+ if (preg_match('=&([a-z,A-Z,0-9]*);=sU', $data2, $ent)) {
+ $this->handleEntityReference($parser, $ent[1]);
+ }
+
if ($data2 == "<!DOCTYPE") {
$this->DTDTokens[] = $data2;
$this->DTDString .= $data;
! $this->DTDDecl = true;
return true;
}
! if ($this->DTDDecl) {
if ($data2 != "") {
$this->DTDTokens[] = $data;
***************
*** 316,326 ****
$this->DTDString .= $data;
if ($data2 == "[") {
! $this->parseInternalDTD = true;
}
if ($data2 == "]") {
! $this->parseInternalDTD = false;
}
! if (($data2 == ">") && (!$this->parseInternalDTD)) {
! $this->parseDTD = false;
$doctype = new DocumentType();
--- 434,444 ----
$this->DTDString .= $data;
if ($data2 == "[") {
! $this->DTDInternal = true;
}
if ($data2 == "]") {
! $this->DTDInternal = false;
}
! if (($data2 == ">") && (!$this->DTDInternal)) {
! $this->DTDDecl = false;
$doctype = new DocumentType();
***************
*** 346,349 ****
--- 464,494 ----
}
$this->document->doctype =& $doctype;
+ if ($this->parseDTD) {
+ $this->DTDParser->parseTokens($this->DTDTokens);
+ if ((isset($this->DTDParser->elements)) &&
+ ($this->DTDParser->elements->getLength() > 0)) {
+ $doctype->elements =& $this->DTDParser->elements;
+ }
+ if ((isset($this->DTDParser->entities)) &&
+ ($this->DTDParser->entities->getLength() > 0)) {
+ $doctype->entities = new NamedNodeMap();
+ for ($i = 0; $i < $this->DTDParser->entities->length; ++$i) {
+ $node =& $this->DTDParser->entities->item($i);
+ $doctype->entities->setNamedItem($this->document->
+ importNode($node,
+ true));
+ }
+ }
+ if ((isset($this->DTDParser->notations)) &&
+ ($this->DTDParser->notations->getLength() > 0)) {
+ $doctype->notations = new NamedNodeMap();
+ for ($i = 0; $i < $this->DTDParser->notations->length; ++$i) {
+ $node =& $this->DTDParser->notations->item($i);
+ $doctype->notations->setNamedItem($this->document->
+ importNode($node,
+ true));
+ }
+ }
+ }
}
return true;
***************
*** 377,381 ****
}
! function handleExternalEntityRef($parser, $openEntityNames, $base, $systemId, $publicId) {
return true;
}
--- 522,536 ----
}
! /**
! * Check whether a file or url exists.
! *
! * @private
! * @returns void
! */
! function file_exists($filename) {
! $file = @fopen($filename, "r");
! if (!$file) {
! return false;
! }
return true;
}
***************
*** 402,406 ****
}
! echo "<strong>phpXD error:</strong> ".$message;
exit;
}
--- 557,561 ----
}
! echo "<strong>DOMParser error:</strong> ".$message;
exit;
}
Index: DTDParser.php
===================================================================
RCS file: /cvsroot/phpxd/phpXD/include/parser/DTDParser.php,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** DTDParser.php 2002/01/29 20:57:01 1.4
--- DTDParser.php 2002/02/06 23:10:48 1.5
***************
*** 8,21 ****
/**
! * Parse a dtd. EXPERIMENTAL!
! *
! * @package DTDParser
! * @author Thomas Dohmke <th...@do...>
! * @version $Revision$
! */
class DTDParser {
! // public
var $elements;
- var $attLists;
var $entities;
var $notations;
--- 8,20 ----
/**
! * Parse a dtd. EXPERIMENTAL!
! *
! * @package phpXD
! * @author Thomas Dohmke <th...@do...>
! * @version $Revision$
! */
class DTDParser {
! // public -- NamedNodeMaps
var $elements;
var $entities;
var $notations;
***************
*** 28,36 ****
var $parameterEntitiesDefined;
! // private
var $currentElement;
var $currentAttList;
- var $currentEntity;
function parse($str, $dtdonly = true) {
if (!empty($str)) {
--- 27,40 ----
var $parameterEntitiesDefined;
! // private -- only valid while parsing
! var $document;
var $currentElement;
+ var $attLists;
var $currentAttList;
+ function DTDParser() {
+ $this->document = new Document();
+ }
+
function parse($str, $dtdonly = true) {
if (!empty($str)) {
***************
*** 42,46 ****
--- 46,55 ----
// search and replace parameter entities
while (preg_match('=(.*)\%([a-z,A-Z,0-9,\.]*);(.*)$=sU', $str, $ent)) {
+ // Just a hack, cause it seems that expat doesn't support IGNORE or
+ // INCLUDE.
$str = preg_replace('=<!--.*-->=sU', '', $str);
+ $str = preg_replace('=<!\[IGNORE\[.*\]\]>=sU', '', $str);
+ $str = preg_replace('=<!\[INCLUDE\[(.*)\]\]>=sU', '\\1', $str);
+
// <!ENTITY % name PUBLIC publicId systemid>
while (preg_match('=(.*)<!ENTITY[ ,\n,\r,\t]*\%[ ,\n,\r,\t]*'.
***************
*** 73,77 ****
'[ ,\n,\r,\t]*"([a-z,A-Z,0-9,\.,\-,_,/]*)"'.
'[ ,\n,\r,\t]*>(.*)$=sU', $str, $ent)) {
! if ($this->file_exists($this->currentDir."/".$ent[4])) {
if (!isset($this->parameterEntitiesDefined[$ent[2]]) ||
($this->parameterEntitiesDefined[$ent[2]] < 2)) {
--- 82,86 ----
'[ ,\n,\r,\t]*"([a-z,A-Z,0-9,\.,\-,_,/]*)"'.
'[ ,\n,\r,\t]*>(.*)$=sU', $str, $ent)) {
! if ($this->file_exists($ent[4])) {
if (!isset($this->parameterEntitiesDefined[$ent[2]]) ||
($this->parameterEntitiesDefined[$ent[2]] < 2)) {
***************
*** 85,90 ****
$this->parameterEntities[$ent[2]] =
preg_replace('=<!--.*-->=sU', '',
! implode("", file($this->currentDir."/".
! $ent[3])));
$this->parameterEntitiesDefined[$ent[2]] = 1;
}
--- 94,98 ----
$this->parameterEntities[$ent[2]] =
preg_replace('=<!--.*-->=sU', '',
! implode("", file($ent[3])));
$this->parameterEntitiesDefined[$ent[2]] = 1;
}
***************
*** 113,116 ****
--- 121,130 ----
$str = str_replace("%".$entref.";", $body, $str);
}
+
+ // Just a hack, cause it seems that expat doesn't support IGNORE or
+ // INCLUDE.
+ $str = preg_replace('=<!--.*-->=sU', '', $str);
+ $str = preg_replace('=<!\[IGNORE\[.*\]\]>=sU', '', $str);
+ $str = preg_replace('=<!\[INCLUDE\[(.*)\]\]>=sU', '\\1', $str);
}
***************
*** 157,160 ****
--- 171,186 ----
// assert: tokens are all valid
$this->parseDoctype($tokens);
+
+ if (isset($this->elements)) {
+ for ($i = 0; $i < $this->elements->getLength(); ++$i) {
+ $element =& $this->elements->item($i);
+ if (isset($this->attLists[$element->nodeName])) {
+ $element->attributes =& $this->attLists[$element->nodeName];
+ }
+ }
+ }
+ else {
+ $this->elements = new NamedNodeMap();
+ }
}
***************
*** 190,196 ****
$this->parseElement($tokens, $offset + 1);
// copy element
! $this->elements[$this->currentElement->tagName] = $this->currentElement;
! // $this->elements[$this->currentElement->tagName]->children =&
! // $this->currentElement->children;
$offset = $this->skipToGt($tokens, $offset + 1);
break;
--- 216,226 ----
$this->parseElement($tokens, $offset + 1);
// copy element
! if (!isset($this->elements)) {
! $this->elements = new NamedNodeMap();
! }
! $currentElement = $this->currentElement;
! $currentElement->childNodes =& $this->currentElement->childNodes;
! $this->currentElement = null;
! $this->elements->setNamedItem($currentElement);
$offset = $this->skipToGt($tokens, $offset + 1);
break;
***************
*** 199,204 ****
$this->parseAttList($tokens, $offset + 1);
$offset = $this->skipToGt($tokens, $offset + 1);
! $this->attLists[$this->currentAttList->element] =
! $this->currentAttList;
break;
}
--- 229,234 ----
$this->parseAttList($tokens, $offset + 1);
$offset = $this->skipToGt($tokens, $offset + 1);
! $this->attLists[$this->currentAttList->element] =&
! $this->currentAttList->attributes;
break;
}
***************
*** 229,233 ****
--- 259,276 ----
$token = $this->removeQuotes($tokens[$offset]);
$DTDParser = new DTDParser();
+ $DTDParser->parameterEntities =& $this->parameterEntities;
+ if ((isset($this->parameterEntitiesDefined)) &&
+ (is_array($this->parameterEntitiesDefined))) {
+ foreach ($this->parameterEntitiesDefined as $key => $value) {
+ // internal parameter entities overwrite external
+ $DTDParser->parameterEntitiesDefined[$key] = 2;
+ }
+ }
+ $DTDParser->attLists =& $this->attLists;
+ $DTDParser->entities =& $this->entities;
+ $DTDParser->elements =& $this->elements;
+ $DTDParser->notations =& $this->notations;
$DTDParser->parseFile($token, true);
+
// merge definitions from $DTDParser with this
$token = $tokens[$offset + 1];
***************
*** 243,247 ****
$DTDParser = new DTDParser();
$DTDParser->parameterEntities =& $this->parameterEntities;
! if (is_array($this->parameterEntitiesDefined)) {
foreach ($this->parameterEntitiesDefined as $key => $value) {
// internal parameter entities overwrite external
--- 286,291 ----
$DTDParser = new DTDParser();
$DTDParser->parameterEntities =& $this->parameterEntities;
! if ((isset($this->parameterEntitiesDefined)) &&
! (is_array($this->parameterEntitiesDefined))) {
foreach ($this->parameterEntitiesDefined as $key => $value) {
// internal parameter entities overwrite external
***************
*** 254,258 ****
$DTDParser->notations =& $this->notations;
$DTDParser->parseFile($filename, true);
-
// merge definitions from $DTDParser with this
--- 298,301 ----
***************
*** 293,317 ****
}
if ($nestlevel == 1) {
! if ($this->currentElement->children == null) {
! $this->currentElement->children = new DTDElementSequence();
}
else {
$newChild = new DTDElementSequence();
! $newChild->parent =& $this->currentElement->children;
! $newChild->parent->appendChild($newChild);
! unset($this->currentElement->children);
! $this->currentElement->children =& $newChild;
}
}
else {
! if ($this->currentElement->children == null) {
! $this->currentElement->children = new DTDElementChoice();
}
else {
$newChild = new DTDElementChoice();
! $newChild->parent =& $this->currentElement->children;
! $newChild->parent->appendChild($newChild);
! unset($this->currentElement->children);
! $this->currentElement->children =& $newChild;
}
}
--- 336,360 ----
}
if ($nestlevel == 1) {
! if ($this->currentElement->childNodes == null) {
! $this->currentElement->childNodes = new DTDElementSequence();
}
else {
$newChild = new DTDElementSequence();
! $newChild->parent =& $this->currentElement->childNodes;
! $newChild->parent->insertNode($newChild);
! unset($this->currentElement->childNodes);
! $this->currentElement->childNodes =& $newChild;
}
}
else {
! if ($this->currentElement->childNodes == null) {
! $this->currentElement->childNodes = new DTDElementChoice();
}
else {
$newChild = new DTDElementChoice();
! $newChild->parent =& $this->currentElement->childNodes;
! $newChild->parent->insertNode($newChild);
! unset($this->currentElement->childNodes);
! $this->currentElement->childNodes =& $newChild;
}
}
***************
*** 323,331 ****
($token[strlen($token) - 1] == "+") ||
($token[strlen($token) - 1] == "*")) {
! $this->currentElement->children->setNumber($token[strlen($token) - 1]);
}
! if ($this->currentElement->children->parent != null) {
! $parent =& $this->currentElement->children->parent;
! $this->currentElement->children =& $parent;
$this->parseElementChilds($tokens, $offset + 1);
return;
--- 366,375 ----
($token[strlen($token) - 1] == "+") ||
($token[strlen($token) - 1] == "*")) {
! $this->currentElement->childNodes->setNumber($token[strlen($token) -
! 1]);
}
! if ($this->currentElement->childNodes->parent != null) {
! $parent =& $this->currentElement->childNodes->parent;
! $this->currentElement->childNodes =& $parent;
$this->parseElementChilds($tokens, $offset + 1);
return;
***************
*** 351,362 ****
$token = $tokens[$offset];
if ($token == "#PCDATA") {
! if ($this->currentElement->children == null) {
! $this->currentElement->children =
new DTDElementChild("", "", false, true);
return;
}
else {
! $this->currentElement->children->
! appendChild(new DTDElementChild("", "", false, true));
return;
}
--- 395,406 ----
$token = $tokens[$offset];
if ($token == "#PCDATA") {
! if ($this->currentElement->childNodes == null) {
! $this->currentElement->childNodes =
new DTDElementChild("", "", false, true);
return;
}
else {
! $this->currentElement->childNodes->
! insertNode(new DTDElementChild("", "", false, true));
return;
}
***************
*** 372,381 ****
}
$child = new DTDElementChild($token, $number);
! if ($this->currentElement->children == null) {
! $this->currentElement->children =& $child;
return;
}
else {
! $this->currentElement->children->appendChild($child);
return;
}
--- 416,425 ----
}
$child = new DTDElementChild($token, $number);
! if ($this->currentElement->childNodes == null) {
! $this->currentElement->childNodes =& $child;
return;
}
else {
! $this->currentElement->childNodes->insertNode($child);
return;
}
***************
*** 389,392 ****
--- 433,437 ----
function parseAttributes($tokens, $offset) {
+ $count = 0;
while ($tokens[$offset] != ">") {
$name = $tokens[$offset];
***************
*** 398,407 ****
$type = $tokens[$offset + 1];
if ($type == "(") {
! $attribute = new DTDAttribute($name, "ENUMERATION");
// Enumeration
$offset += 2;
while ($tokens[$offset] != ")") {
if ($tokens[$offset] != "|") {
! $attribute->value[] = $tokens[$offset];
}
$offset++;
--- 443,452 ----
$type = $tokens[$offset + 1];
if ($type == "(") {
! $attribute[$count] = new DTDAttribute($name, "ENUMERATION");
// Enumeration
$offset += 2;
while ($tokens[$offset] != ")") {
if ($tokens[$offset] != "|") {
! $attribute[$count]->value[] = $tokens[$offset];
}
$offset++;
***************
*** 409,481 ****
}
else {
! $attribute = new DTDAttribute($name, $type);
$offset++;
}
$default = $tokens[$offset + 1];
! $attribute->default = $default;
if (($default != "#IMPLIED") &&
($default != "#REQUIRED")) {
if ($default == "#FIXED") {
! $attribute->defaultValue = $this->removeQuotes($tokens[$offset + 2]);
$offset++;
}
else {
! $attribute->defaultValue = $this->removeQuotes($tokens[$offset + 1]);
}
}
$offset += 2;
! $this->currentAttList->attributes[$attribute->name] = $attribute;
}
}
function parseEntity($tokens, $offset) {
! $name = $tokens[$offset];
! $entity = new DTDEntity($name);
! $entity->ndata = false;
! $body = $tokens[$offset + 1];
! if (($body == "SYSTEM") || ($body == "PUBLIC")) {
! if ($body == "SYSTEM") {
! $entity->systemId = $tokens[$offset + 2];
$offset = $offset + 3;
}
else {
! $entity->publicId = $tokens[$offset + 2];
! $entity->systemId = $tokens[$offset + 3];
$offset = $offset + 4;
}
if ($tokens[$offset] == "NDATA") {
! $entity->ndata = true;
! $entity->notation = $tokens[$offset + 1];
}
}
else {
! $entity->body = $this->removeQuotes($body);
}
! $this->entities[$name] =& $entity;
}
function parseNotation($tokens, $offset) {
! $name = $tokens[$offset];
! $notation = new DTDNotation($name);
! $entity->publicId = "";
! $entity->systemId = "";
! $body = $tokens[$offset + 1];
! if ($body == "SYSTEM") {
! $notation->systemId = $tokens[$offset + 2];
}
! if ($body == "PUBLIC") {
! $notation->publicId = $tokens[$offset + 2];
if ($tokens[$offset + 3] != ">") {
! $notation->systemId = $tokens[$offset + 3];
}
}
! $this->notations[$name] =& $notation;
}
function publicId2Filename($publicId, $systemId) {
- // TODO: add all known publicId and systemIds
- if ($publicId == "-//W3C//DTD XHTML 1.0 Strict//EN") {
- return "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
- }
return $systemId;
}
--- 454,552 ----
}
else {
! $attribute[$count] = new DTDAttribute($name, $type);
$offset++;
}
$default = $tokens[$offset + 1];
! $attribute[$count]->default = $default;
if (($default != "#IMPLIED") &&
($default != "#REQUIRED")) {
if ($default == "#FIXED") {
! $attribute[$count]->defaultValue =
! $this->removeQuotes($tokens[$offset + 2]);
$offset++;
}
else {
! $attribute[$count]->defaultValue =
! $this->removeQuotes($tokens[$offset + 1]);
}
}
$offset += 2;
! if (!isset($this->currentAttList->attributes)) {
! $this->currentAttList->attributes = new NamedNodeMap();
! }
! $this->currentAttList->attributes->setNamedItem($attribute[$count]);
! $attribute = null;
! $count++;
}
}
function parseEntity($tokens, $offset) {
! $entity = new Entity();
! $entity->ownerDocument =& $this->document;
! $entity->nodeName = $tokens[$offset];
! $value = $tokens[$offset + 1];
! $entityParser = new DOMParser();
! if (($value == "SYSTEM") || ($value == "PUBLIC")) {
! if ($value == "SYSTEM") {
! $entity->systemId = $this->removeQuotes($tokens[$offset + 2]);
$offset = $offset + 3;
+
+ $entityDocument =&
+ $entityParser->parseExternalEntity($entity->systemId);
}
else {
! $entity->publicId = $this->removeQuotes($tokens[$offset + 2]);
! $entity->systemId = $this->removeQuotes($tokens[$offset + 3]);
$offset = $offset + 4;
+
+ $entityDocument =&
+ $entityParser->parseExternalEntity(
+ $this->publicId2Filename($entity->publicId,
+ $entity->systemId));
}
if ($tokens[$offset] == "NDATA") {
! $entity->notationName = $tokens[$offset + 1];
}
}
else {
! $entityDocument =&
! $entityParser->parseEntity($this->removeQuotes($value));
! }
! // parsed entity?
! if ($entity->notationName == null) {
! $entityNodes =& $entityDocument->documentElement->childNodes;
! for ($i = 0; $i < $entityNodes->length; ++$i) {
! $node =& $entityNodes->item($i);
! $entity->appendChild($this->document->importNode($node,
! true));
! }
}
! if (!isset($this->entities)) {
! $this->entities = new NamedNodeMap();
! }
! $this->entities->setNamedItem($entity);
}
function parseNotation($tokens, $offset) {
! $notation = new Notation();
! $notation->nodeName = $tokens[$offset];
! $notation->ownerDocument =& $this->document;
! $value = $tokens[$offset + 1];
! if ($value == "SYSTEM") {
! $notation->systemId = $this->removeQuotes($tokens[$offset + 2]);
}
! if ($value == "PUBLIC") {
! $notation->publicId = $this->removeQuotes($tokens[$offset + 2]);
if ($tokens[$offset + 3] != ">") {
! $notation->systemId = $this->removeQuotes($tokens[$offset + 3]);
}
+ }
+ if (!isset($this->notations)) {
+ $this->notations = new NamedNodeMap();
}
! $this->notations->setNamedItem($notation);
}
function publicId2Filename($publicId, $systemId) {
return $systemId;
}
***************
*** 546,706 ****
}
! echo "<strong>phpXD error:</strong> ".$message;
exit;
}
}
- class DTDNode {
- // abstract class for all DTD classes
- }
-
- class DTDAttList extends DTDNode {
- var $attributes;
- var $element;
-
- function DTDAttList($element) {
- $this->element = $element;
- }
- }
-
- class DTDAttribute extends DTDNode {
- var $name;
- var $type;
- var $value;
- var $default;
- var $defaultValue;
-
- function DTDAttribute($name, $type) {
- $this->name = $name;
- $this->type = $type;
- }
- }
-
- class DTDEntity extends DTDNode {
- var $name;
- var $body;
- var $publicId;
- var $systemId;
- var $ndata;
- var $notation;
-
- function DTDEntity($name) {
- $this->name = $name;
- }
- }
-
- class DTDNotation extends DTDNode {
- var $name;
- var $publicId;
- var $systemId;
-
- function DTDNotation($name) {
- $this->name = $name;
- }
- }
-
- class DTDElement extends DTDNode {
- var $tagName;
- var $children;
-
- function DTDElement($tagName) {
- $this->tagName = $tagName;
- $this->children = null;
- }
- }
-
- class DTDElementChild extends DTDNode {
- var $number = "";
-
- var $any = false;
- var $pcdata = false;
- var $tagName;
- var $id;
-
- function DTDElementChild($tagName, $number, $any = false, $pcdata = false) {
- if ($any) {
- $this->any = true;
- return;
- }
-
- if ($pcdata) {
- $this->pcdata = true;
- return;
- }
-
- $this->tagName = $tagName;
- $this->setNumber($number);
- }
-
-
- function setNumber($number) {
- $this->number = $number;
- }
-
- function toString() {
- if ($this->any) {
- return "ANY";
- }
- if ($this->pcdata) {
- return "#PCDATA";
- }
- return $this->tagName.$this->number;
- }
- }
-
- class DTDElementSequence extends DTDElementChild {
- var $sequence;
- var $length = 0;
- var $parent = null;
-
- function DTDElementSequence() {
- }
-
- function appendChild(&$child) {
- $this->sequence[$this->length++] =& $child;
- }
-
- function toString() {
- $str = "(";
- $count = 0;
- for ($count = 0; $count < $this->length; ++$count) {
- $child =& $this->sequence[$count];
- if ($count != 0) {
- $str .= " , ";
- }
- $str .= $child->toString();
- }
- $str .= ")".$this->number;
- return $str;
- }
- }
-
- class DTDElementChoice extends DTDElementChild {
- var $choices;
- var $length = 0;
- var $parent = null;
-
- function DTDElementChoice() {
- }
-
- function DTDElementChoice() {
- }
-
- function appendChild(&$child) {
- $this->choices[$this->length++] =& $child;
- }
-
- function toString() {
- $str = "(";
- $count = 0;
- for ($count = 0; $count < $this->length; ++$count) {
- $child =& $this->choices[$count];
- if ($count != 0) {
- $str .= " | ";
- }
- $str .= $child->toString();
- }
- $str .= ")".$this->number;
- return $str;
- }
- }
\ No newline at end of file
--- 617,623 ----
}
! echo "<strong>DTDParser error:</strong> ".$message;
exit;
}
}
|