📄 xml_saxy_parser.php
字号:
else if ($openBraceCount > 1) { $textNodeText .= $currentChar; } else if ($currentChar == '[') { //this won't be reached after the first open brace is found $openBraceCount ++; } } if ($this->cDataSectionHandler == null) { $this->fireCharacterDataEvent($textNodeText); } else { $this->fireCDataSectionEvent($textNodeText); } } else if (strpos($upperCaseTagText, SAXY_SEARCH_NOTATION) !== false) { //NOTATION node, discard return; } /* else if (substr($tagText, 0, 2) == '!-') { //comment node if ($this->commentHandler != null) { $this->fireCommentEvent(substr($tagText, 3, (strlen($tagText) - 5))); } } */ break; case '?': //Processing Instruction node $this->parseProcessingInstruction($tagText); break; default: if ((strpos($tagText, '"') !== false) || (strpos($tagText, "'") !== false)) { $total = strlen($tagText); $tagName = ''; for ($i = 0; $i < $total; $i++) {// $currentChar = $tagText{$i}; $currentChar = substr($tagText, $i, 1); if (($currentChar == ' ') || ($currentChar == "\t") || ($currentChar == "\n") || ($currentChar == "\r") || ($currentChar == "\x0B")) { $myAttributes = $this->parseAttributes(substr($tagText, $i)); break; } else { $tagName .= $currentChar; } } if (strrpos($tagText, '/') == (strlen($tagText) - 1)) { //check $tagText, but send $tagName $this->_fireStartElementEvent($tagName, $myAttributes); $this->_fireEndElementEvent($tagName); } else { $this->_fireStartElementEvent($tagName, $myAttributes); } } else { if (strpos($tagText, '/') !== false) { $tagText = trim(substr($tagText, 0, (strrchr($tagText, '/') - 1))); $this->_fireStartElementEvent($tagText, $myAttributes); $this->_fireEndElementEvent($tagText); } else { $this->_fireStartElementEvent($tagText, $myAttributes); } } } } //parseTag /** * Fires a start element event and pushes the element name onto the elementName stack * @param string The start element tag name * @param Array The start element attributes */ function _fireStartElementEvent($tagName, &$myAttributes) { $this->elementNameStack[] = $tagName; if ($this->isNamespaceAware) { $this->detectStartNamespaceDeclaration($myAttributes); $tagName = $this->expandNamespacePrefix($tagName); $this->expandAttributePrefixes($myAttributes); } $this->fireStartElementEvent($tagName, $myAttributes); } //_fireStartElementEvent /** * Expands attribute prefixes to full namespace uri * @param Array The start element attributes */ function expandAttributePrefixes(&$myAttributes) { $arTransform = array(); foreach ($myAttributes as $key => $value) { if (strpos($key, 'xmlns') === false) { if (strpos($key, ':') !== false) { $expandedTag = $this->expandNamespacePrefix($key); $arTransform[$key] = $expandedTag; } } } foreach ($arTransform as $key => $value) { $myAttributes[$value] = $myAttributes[$key]; unset($myAttributes[$key]); } } //expandAttributePrefixes /** * Expands the namespace prefix (if one exists) to the full namespace uri * @param string The tagName with the namespace prefix * @return string The tagName, with the prefix expanded to the namespace uri */ function expandNamespacePrefix($tagName) { $stackLen = count($this->defaultNamespaceStack); $defaultNamespace = $this->defaultNamespaceStack[($stackLen - 1)]; $colonIndex = strpos($tagName, ':'); if ($colonIndex !== false) { $prefix = substr($tagName, 0, $colonIndex); if ($prefix != 'xml') { $tagName = $this->getNamespaceURI($prefix) . substr($tagName, $colonIndex); } else { $tagName = SAXY_XML_NAMESPACE . substr($tagName, $colonIndex); } } else if ($defaultNamespace != '') { $tagName = $defaultNamespace . ':' . $tagName; } return $tagName; } //expandNamespacePrefix /** * Searches the namespaceMap for the specified prefix, and returns the full namespace URI * @param string The namespace prefix * @return string The namespace uri */ function getNamespaceURI($prefix) { $total = count($this->namespaceMap); $uri = $prefix; //in case uri can't be found, just send back prefix //should really generate an error, but worry about this later //reset($this->namespaceMap); for ($i = ($total - 1); $i >= 0; $i--) { $currMap =& $this->namespaceMap[$i]; if (isset($currMap[$prefix])) { $uri = $currMap[$prefix]; break; } } return $uri; } //getNamespaceURI /** * Searches the attributes array for an xmlns declaration and fires an event if found * @param Array The start element attributes */ function detectStartNamespaceDeclaration(&$myAttributes) { $namespaceExists = false; $namespaceMapUpper = 0; $userDefinedDefaultNamespace = false; $total = count($myAttributes); foreach ($myAttributes as $key => $value) { if (strpos($key, 'xmlns') !== false) { //add an array to store all namespaces for the current element if (!$namespaceExists) { $this->namespaceMap[] = array(); $namespaceMapUpper = count($this->namespaceMap) - 1; } //check for default namespace override, i.e. xmlns='...' if (strpos($key, ':') !== false) { $prefix = $namespaceMapKey = substr($key, 6); $this->namespaceMap[$namespaceMapUpper][$namespaceMapKey] = $value; } else { $prefix = ''; $userDefinedDefaultNamespace = true; //if default namespace '', store in map using key ':' $this->namespaceMap[$namespaceMapUpper][':'] = $value; $this->defaultNamespaceStack[] = $value; } $this->fireStartNamespaceDeclarationEvent($prefix, $value); $namespaceExists = true; unset($myAttributes[$key]); } } //store the default namespace (inherited from the parent elements so grab last one) if (!$userDefinedDefaultNamespace) { $stackLen = count($this->defaultNamespaceStack); if ($stackLen == 0) { $this->defaultNamespaceStack[] = ''; } else { $this->defaultNamespaceStack[] = $this->defaultNamespaceStack[($stackLen - 1)]; } } $this->namespaceStack[] = $namespaceExists; } //detectStartNamespaceDeclaration /** * Fires an end element event and pops the element name from the elementName stack * @param string The end element tag name */ function _fireEndElementEvent($tagName) { $lastTagName = array_pop($this->elementNameStack); //check for mismatched tag error if ($lastTagName != $tagName) { $this->errorCode = SAXY_XML_ERROR_TAG_MISMATCH; } if ($this->isNamespaceAware) { $tagName = $this->expandNamespacePrefix($tagName); $this->fireEndElementEvent($tagName); $this->detectEndNamespaceDeclaration(); $defaultNamespace = array_pop($this->defaultNamespaceStack); } else { $this->fireEndElementEvent($tagName); } } //_fireEndElementEvent /** * Determines whether an end namespace declaration event should be fired */ function detectEndNamespaceDeclaration() { $isNamespaceEnded = array_pop($this->namespaceStack); if ($isNamespaceEnded) { $map = array_pop($this->namespaceMap); foreach ($map as $key => $value) { if ($key == ':') { $key = ''; } $this->fireEndNamespaceDeclarationEvent($key); } } } //detectEndNamespaceDeclaration /** * Parses a processing instruction * @param string The interior text of the processing instruction */ function parseProcessingInstruction($data) { $endTarget = 0; $total = strlen($data); for ($x = 2; $x < $total; $x++) {// if (trim($data{$x}) == '') { if (trim(substr($data, $x, 1)) == '') { $endTarget = $x; break; } } $target = substr($data, 1, ($endTarget - 1)); $data = substr($data, ($endTarget + 1), ($total - $endTarget - 2)); if ($this->processingInstructionHandler != null) { $this->fireProcessingInstructionEvent($target, $data); } } //parseProcessingInstruction /** * Parses a comment * @param string The interior text of the comment */ function parseComment($data) { if ($this->commentHandler != null) { $this->fireCommentEvent($data); } } //parseComment /** * Fires a doctype event * @param string The doctype data */ function fireDTDEvent($data) { call_user_func($this->DTDHandler, $this, $data); } //fireDTDEvent /** * Fires a comment event * @param string The text of the comment */ function fireCommentEvent($data) { call_user_func($this->commentHandler, $this, $data); } //fireCommentEvent /** * Fires a processing instruction event * @param string The processing instruction data */ function fireProcessingInstructionEvent($target, $data) { call_user_func($this->processingInstructionHandler, $this, $target, $data); } //fireProcessingInstructionEvent /** * Fires a start namespace declaration event * @param string The namespace prefix * @param string The namespace uri */ function fireStartNamespaceDeclarationEvent($prefix, $uri) { call_user_func($this->startNamespaceDeclarationHandler, $this, $prefix, $uri); } //fireStartNamespaceDeclarationEvent /** * Fires an end namespace declaration event * @param string The namespace prefix */ function fireEndNamespaceDeclarationEvent($prefix) { call_user_func($this->endNamespaceDeclarationHandler, $this, $prefix); } //fireEndNamespaceDeclarationEvent /** * Returns the current error code * @return int The current error code */ function xml_get_error_code() { return $this->errorCode; } //xml_get_error_code /** * Returns a textual description of the error code * @param int The error code * @return string The error message */ function xml_error_string($code) { switch ($code) { case SAXY_XML_ERROR_NONE: return "No error"; break; case SAXY_XML_ERROR_NO_MEMORY: return "Out of memory"; break; case SAXY_XML_ERROR_SYNTAX: return "Syntax error"; break; case SAXY_XML_ERROR_NO_ELEMENTS: return "No elements in document"; break; case SAXY_XML_ERROR_INVALID_TOKEN: return "Invalid token"; break; case SAXY_XML_ERROR_UNCLOSED_TOKEN: return "Unclosed token"; break; case SAXY_XML_ERROR_PARTIAL_CHAR: return "Partial character"; break; case SAXY_XML_ERROR_TAG_MISMATCH: return "Tag mismatch"; break; case SAXY_XML_ERROR_DUPLICATE_ATTRIBUTE: return "Duplicate attribute"; break; case SAXY_XML_ERROR_JUNK_AFTER_DOC_ELEMENT: return "Junk encountered after document element"; break; case SAXY_XML_ERROR_PARAM_ENTITY_REF: return "Parameter entity reference error"; break; case SAXY_XML_ERROR_UNDEFINED_ENTITY: return "Undefined entity"; break; case SAXY_XML_ERROR_RECURSIVE_ENTITY_REF: return "Recursive entity reference"; break; case SAXY_XML_ERROR_ASYNC_ENTITY: return "Asynchronous internal entity found in external entity"; break; case SAXY_XML_ERROR_BAD_CHAR_REF: return "Bad character reference"; break; case SAXY_XML_ERROR_BINARY_ENTITY_REF: return "Binary entity reference"; break; case SAXY_XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF: return "Attribute external entity reference"; break; case SAXY_XML_ERROR_MISPLACED_XML_PI: return "Misplaced processing instruction"; break; case SAXY_XML_ERROR_UNKNOWN_ENCODING: return "Unknown encoding"; break; case SAXY_XML_ERROR_INCORRECT_ENCODING: return "Incorrect encoding"; break; case SAXY_XML_ERROR_UNCLOSED_CDATA_SECTION: return "Unclosed CDATA Section"; break; case SAXY_XML_ERROR_EXTERNAL_ENTITY_HANDLING: return "Problem in external entity handling"; break; default: return "No definition for error code " . $code; break; } } //xml_error_string} //SAXY_Parser?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -