예제 #1
0
 public function __construct($input, $maxDepth = 20)
 {
     if (!is_string($input)) {
         throw new XmlToArrayException('No valid input.');
     }
     $this->_maxDepth = $maxDepth;
     $XMLParser = xml_parser_create();
     xml_parser_set_option($XMLParser, XML_OPTION_SKIP_WHITE, false);
     xml_parser_set_option($XMLParser, XML_OPTION_CASE_FOLDING, false);
     xml_parser_set_option($XMLParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
     xml_set_character_data_handler($XMLParser, array($this, '_contents'));
     xml_set_default_handler($XMLParser, array($this, '_default'));
     xml_set_element_handler($XMLParser, array($this, '_start'), array($this, '_end'));
     xml_set_external_entity_ref_handler($XMLParser, array($this, '_externalEntity'));
     xml_set_notation_decl_handler($XMLParser, array($this, '_notationDecl'));
     xml_set_processing_instruction_handler($XMLParser, array($this, '_processingInstruction'));
     xml_set_unparsed_entity_decl_handler($XMLParser, array($this, '_unparsedEntityDecl'));
     if (!xml_parse($XMLParser, $input, true)) {
         $errorCode = xml_get_error_code($XMLParser);
         $message = sprintf('%s. line: %d, char: %d' . ($this->_tagStack ? ', tag: %s' : ''), xml_error_string($errorCode), xml_get_current_line_number($XMLParser), xml_get_current_column_number($XMLParser) + 1, implode('->', $this->_tagStack));
         xml_parser_free($XMLParser);
         throw new XmlToArrayException($message, $errorCode);
     }
     xml_parser_free($XMLParser);
 }
예제 #2
0
 function AbstractSAXParser()
 {
     // create a parser
     $this->parserResource = xml_parser_create();
     if (isset($this->parserResource)) {
         // allowing object instance to use the xml parser
         xml_set_object($this->parserResource, $this);
         // set tag event handler
         xml_set_element_handler($this->parserResource, "startTagElement", "endTagElement");
         // set CDATA event handler
         xml_set_character_data_handler($this->parserResource, "cdataElement");
         // set processing instruction handler
         xml_set_processing_instruction_handler($this->parserResource, "instructionElement");
         // set undeclare entity
         xml_set_unparsed_entity_decl_handler($this->parserResource, "undeclaredEntityElement");
         // set notation delcaration handler
         xml_set_notation_decl_handler($this->parserResource, "notationDeclarationElement");
         // set external entity handler
         xml_set_external_entity_ref_handler($this->parserResource, "externalEntityElement");
         // seat default parser option
         xml_parser_set_option($this->parserResource, XML_OPTION_SKIP_WHITE, 1);
         xml_parser_set_option($this->parserResource, XML_OPTION_CASE_FOLDING, 0);
         xml_parser_set_option($this->parserResource, XML_OPTION_TARGET_ENCODING, 'UTF-8');
     }
 }
 function parse($data)
 {
     $parser = xml_parser_create();
     xml_set_object($parser, $this);
     xml_set_processing_instruction_handler($parser, "PIHandler");
     xml_parse($parser, $data, true);
     xml_parser_free($parser);
 }
예제 #4
0
 function XMLParser()
 {
     $this->parser = xml_parser_create();
     xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, FALSE);
     xml_set_element_handler($this->parser, "startElementhandler", "endElementHandler");
     xml_set_character_data_handler($this->parser, "cdataHandler");
     xml_set_processing_instruction_handler($this->parser, "processingInstructionHandler");
     xml_set_default_handler($this->parser, "defaultHandler");
 }
예제 #5
0
function createParser($filename)
{
    $fh = fopen($filename, 'r');
    $parser = xml_parser_create();
    xml_set_element_handler($parser, "startElement", "endElement");
    xml_set_character_data_handler($parser, "characterData");
    xml_set_processing_instruction_handler($parser, "processingInstruction");
    xml_set_default_handler($parser, "default");
    return array($parser, $fh);
}
예제 #6
0
 protected function createXmlParser($encoding = 'utf-8')
 {
     $xp = xml_parser_create($encoding);
     xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, true);
     xml_parser_set_option($xp, XML_OPTION_SKIP_WHITE, true);
     xml_set_object($xp, $this);
     xml_set_element_handler($xp, 'handleElementStart', 'handleElementEnd');
     xml_set_character_data_handler($xp, 'handleCharacterData');
     xml_set_processing_instruction_handler($xp, 'handleProcessingInstruction');
     return $xp;
 }
예제 #7
0
 /**
  * Class constructor
  * 
  * @param   string  The path of the file to parse
  * @param   string  A prefix path to add to all 'src' and 'href' attributes, if relative
  * @param   string  The output charset (default is UTF-8)
  * @return  void
  */
 public function __construct($file, $charset = 'UTF-8')
 {
     // The charset must be uppercase
     $charset = strtoupper($charset);
     // Checks if the file exists
     if (!file_exists($file) || !is_file($file)) {
         // The file does not exist
         throw new Woops_Xml_Parser_Exception('The specified XML file (\'' . $file . '\') does not exist', Woops_Xml_Parser_Exception::EXCEPTION_NO_FILE);
     }
     // Checks if the file is readable
     if (!is_readable($file)) {
         // Cannot read the file
         throw new Woops_Xml_Parser_Exception('The specified XML file (\'' . $file . '\') is not readable', Woops_Xml_Parser_Exception::EXCEPTION_FILE_NOT_READABLE);
     }
     // Checks if the charset is supported
     if (!isset(self::$_charsets[$charset])) {
         // Unsupported charset
         throw new Woops_Xml_Parser_Exception('The specified charset (' . $charset . ') is not supported', Woops_Xml_Parser_Exception::EXCEPTION_INVALID_CHARSET);
     }
     // Creates an XML parser
     $this->_parser = xml_parser_create($charset);
     // Sets the current instance as the XML parser object
     xml_set_object($this->_parser, $this);
     // Disables case-folding
     xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
     // Sets the element handler methods
     xml_set_element_handler($this->_parser, '_startElementHandler', '_endElementHandler');
     // Sets the character data handler method
     xml_set_character_data_handler($this->_parser, '_characterDataHandler');
     // Sets the processing instruction handler method
     xml_set_processing_instruction_handler($this->_parser, '_processingInstructionHandler');
     // Sets the default data handler method
     xml_set_default_handler($this->_parser, '_defaultHandler');
     // Tries to open a file handler
     if ($fileHandler = fopen($file, 'r')) {
         // Reads data from the file
         while ($data = fread($fileHandler, 4096)) {
             // Tries to parse the data
             if (!xml_parse($this->_parser, $data, feof($fileHandler))) {
                 // Gets the error string and line number
                 $errorString = xml_error_string(xml_get_error_code($this->_parser));
                 $errorLine = xml_get_current_line_number($this->_parser);
                 // Throws an exception, as we have an XML error
                 throw new Woops_Xml_Parser_Exception('XML parser error: ' . $errorString . ' at line number ' . $errorLine, Woops_Xml_Parser_Exception::EXCEPTION_XML_PARSER_ERROR);
             }
         }
         // Closes the file handler
         fclose($fileHandler);
     }
     // Frees the parser
     xml_parser_free($this->_parser);
 }
예제 #8
0
 function parse($xml)
 {
     $this->node_list = array(0 => array('type' => 'start', 'children' => array()));
     $this->cur_nodeid = 0;
     $this->parser = xml_parser_create();
     xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
     xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
     xml_set_element_handler($this->parser, array(&$this, 'handle_element_open'), array(&$this, 'handle_element_close'));
     xml_set_character_data_handler($this->parser, array(&$this, 'handle_cdata'));
     xml_set_processing_instruction_handler($this->parser, array(&$this, 'handle_instruction'));
     xml_set_default_handler($this->parser, array(&$this, 'handle_default'));
     xml_parse($this->parser, $xml, true);
     xml_parser_free($this->parser);
     $this->parser = null;
     return $this->node_list;
 }
예제 #9
0
파일: Encoder.php 프로젝트: horde/horde
 /**
  * Take the input $xml and turn it into WBXML. This is _not_ the
  * intended way of using this class. It is derived from
  * Contenthandler and one should use it as a ContentHandler and
  * produce the XML-structure with startElement(), endElement(),
  * and characters().
  *
  * @throws Horde_Xml_Wbxml_Exception
  */
 public function encode($xml)
 {
     // Create the XML parser and set method references.
     $this->_parser = xml_parser_create_ns($this->_charset);
     xml_set_object($this->_parser, $this);
     xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
     xml_set_element_handler($this->_parser, '_startElement', '_endElement');
     xml_set_character_data_handler($this->_parser, '_characters');
     xml_set_processing_instruction_handler($this->_parser, '');
     xml_set_external_entity_ref_handler($this->_parser, '');
     if (!xml_parse($this->_parser, $xml)) {
         throw new Horde_Xml_Wbxml_Exception(sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($this->_parser)), xml_get_current_line_number($this->_parser)));
     }
     xml_parser_free($this->_parser);
     return $this->_output;
 }
예제 #10
0
 function SaxParser(&$input)
 {
     $this->level = 0;
     $this->parser = xml_parser_create('UTF-8');
     xml_set_object($this->parser, $this);
     $this->input =& $input;
     $this->setCaseFolding(false);
     $this->useUtfEncoding();
     xml_set_element_handler($this->parser, 'handleBeginElement', 'handleEndElement');
     xml_set_character_data_handler($this->parser, 'handleCharacterData');
     xml_set_processing_instruction_handler($this->parser, 'handleProcessingInstruction');
     xml_set_default_handler($this->parser, 'handleDefault');
     xml_set_unparsed_entity_decl_handler($this->parser, 'handleUnparsedEntityDecl');
     xml_set_notation_decl_handler($this->parser, 'handleNotationDecl');
     xml_set_external_entity_ref_handler($this->parser, 'handleExternalEntityRef');
 }
예제 #11
0
파일: Sax.php 프로젝트: kelsoncm/phpage
 protected function parse($xmlData)
 {
     $this->parser = xml_parser_create();
     xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
     xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
     xml_set_object($this->parser, $this);
     xml_set_element_handler($this->parser, 'onTagStart', 'onTagEnd');
     xml_set_character_data_handler($this->parser, 'onTagContent');
     xml_set_processing_instruction_handler($this->parser, "onPiHandler");
     xml_set_default_handler($this->parser, "onDefaultHandler");
     if (!xml_parse($this->parser, $xmlData)) {
         $xmlErroCode = xml_get_error_code($this->parser);
         $xmlErroString = xml_error_string($xmlErroCode);
         $xmlLine = xml_get_current_line_number($this->parser);
         die(sprintf("XML error: %s at line %d", $xmlErroString, $xmlLine));
     }
     xml_parser_free($this->parser);
     $this->parser = NULL;
 }
예제 #12
0
 protected final function init()
 {
     if ($this instanceof Features\IXmlNamespaceParser) {
         $this->parser = xml_parser_create_ns('UTF-8');
         // Set up start namespace declaration handler
         xml_set_start_namespace_decl_handler($this->parser, 'ns_start');
         // Set up end namespace declaration handler
         xml_set_end_namespace_decl_handler($this->parser, 'ns_end');
     } elseif ($this instanceof Features\IXmlBasicParser) {
         $this->parser = xml_parser_create('UTF-8');
     } else {
         throw new \BadMethodCallException('This class does not implements the XML Parser capabilities. Please implement either IXmlBasicParser or IXmlNamespaceParser.');
     }
     xml_set_object($this->parser, $this);
     foreach ($this->options as $option => $value) {
         xml_parser_set_option($this->parser, $option, $value);
     }
     if ($this instanceof Features\IXmlProcessorParser) {
         // Set up processing instruction (PI) handler
         xml_set_processing_instruction_handler($this->parser, 'pi_handler');
     }
     if ($this instanceof Features\IXmlEntityHandler) {
         // Set up external entity reference handler
         xml_set_external_entity_ref_handler($this->parser, 'entity_handler');
     }
     if ($this instanceof Features\IXmlNdataHandler) {
         // Set up notation declaration handler
         xml_set_notation_decl_handler($this->parser, 'notation_handler');
         // Set up unparsed entity declaration handler
         xml_set_unparsed_entity_decl_handler($this->parser, 'ndata_handler');
     }
     xml_set_element_handler($this->parser, "element_start", "element_end");
     xml_set_character_data_handler($this->parser, "cdata_handler");
     if ($this instanceof Features\IXmlDefaultHandler) {
         if (!defined('ACTIVATE_XML_PARSER_DEFAULT_HANDLER_I_KNOW_WHAT_AM_I_DOING')) {
             trigger_error('Active default handler interferes with many XML features like internal parsable entities.', E_USER_WARNING);
         }
         // Set up default (fallback) handler.
         // Warning: Interferes with INTERNAL ENTITY declarations like
         // <!ENTITY a 'b'>
         xml_set_default_handler($this->parser, "default_handler");
     }
 }
예제 #13
0
 function runtest($num, $i)
 {
     $this->readfile($num);
     echo "parsing xml data file {$this->currentFile} iteration {$i}\n";
     $xml_parser = xml_parser_create();
     xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
     xml_set_object($xml_parser, $this);
     xml_set_element_handler($xml_parser, 'startElement', 'endElement');
     xml_set_character_data_handler($xml_parser, 'cDataHandler');
     xml_set_external_entity_ref_handler($xml_parser, 'externalEntityRefHandler');
     xml_set_processing_instruction_handler($xml_parser, 'piHandler');
     xml_set_unparsed_entity_decl_handler($xml_parser, 'unparsedEntityDeclHandler');
     xml_set_notation_decl_handler($xml_parser, 'entityDeclHandler');
     xml_set_default_handler($xml_parser, 'defaultHandler');
     if (!xml_parse($xml_parser, $this->contentBuffer, true)) {
         $this->currentFile != '' ? $inFile = "in file {$this->currentFile}" : ($inFile = '');
         $this->fatalErrorPage(sprintf(get_class($this) . ": XML error: %s at line %d {$inFile}", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
     }
     xml_parser_free($xml_parser);
 }
예제 #14
0
 function __construct($xml = '', $params = self::XML_ENCLOSE)
 {
     $this->_params = $params;
     if ($xml) {
         if ($this->_params & self::XML_ARRAY2XML_FORMAT) {
             $domDocument = new CMS_DOMDocument();
             $domDocument->loadXML($xml, 0, false, false);
             $this->_arrOutput = $this->_xml2Array($domDocument->documentElement, $domDocument->encoding);
         } else {
             $parser = xml_parser_create(APPLICATION_DEFAULT_ENCODING);
             xml_set_object($parser, $this);
             xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
             xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
             xml_set_element_handler($parser, "_tagOpen", "_tagClosed");
             xml_set_character_data_handler($parser, "_charData");
             xml_set_processing_instruction_handler($parser, "_piData");
             xml_set_default_handler($parser, "_tagData");
             //enclose with html tag
             if ($this->_params & self::XML_ENCLOSE) {
                 $xml = '<html>' . $xml . '</html>';
             }
             //add encoding declaration
             if ($this->_params ^ self::XML_DONT_ADD_XMLDECL) {
                 $xml = '<?xml version="1.0" encoding="' . APPLICATION_DEFAULT_ENCODING . '"?>' . "\n" . $xml;
             }
             if ($this->_params & self::XML_PROTECT_ENTITIES) {
                 $xml = $this->_codeEntities($xml);
             }
             if (!xml_parse($parser, $xml)) {
                 $this->_parsingError = sprintf("Parse error %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser));
                 if ($this->_params & ~self::XML_DONT_THROW_ERROR) {
                     $this->raiseError($this->_parsingError . " :\n" . $xml, true);
                 }
             }
             xml_parser_free($parser);
             unset($parser);
         }
     }
 }
예제 #15
0
 /**
  * Initializes the parser.
  *
  * The constructor method instantiates and configures the underlying XML
  * parser and its handlers.
  *
  * \param $encoding The character encoding to use for this parser. This
  * parameter is optional (defaults to null) and can be safely omitted. See
  * the PHP manual for xml_parser_create for more information about
  * encodings.
  */
 public function __construct($encoding = null)
 {
     $this->finalized = false;
     $this->cdata_buffer = array();
     $this->tags = array();
     /* Create the parser instance */
     if (is_null($encoding)) {
         $this->parser = xml_parser_create();
     } else {
         assert('is_string($encoding)');
         $this->parser = xml_parser_create($encoding);
     }
     /* Set some options */
     xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
     xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
     /* Always use UTF-8 */
     xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
     /* Setup the handlers */
     xml_set_element_handler($this->parser, array(&$this, 'start_element_handler'), array(&$this, 'end_element_handler'));
     xml_set_character_data_handler($this->parser, array(&$this, '_handle_data'));
     xml_set_processing_instruction_handler($this->parser, array(&$this, 'handle_pi'));
     xml_set_default_handler($this->parser, array(&$this, '_default_handler'));
 }
예제 #16
0
 /**
  * Contructor.
  *
  * Initializes XML Parser object and pass it to $this->_parser.
  *
  * @access public
  */
 public function __construct()
 {
     $this->_parser = xml_parser_create();
     xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
     xml_set_object($this->_parser, $this);
     xml_set_element_handler($this->_parser, '_tagOpen', '_tagClose');
     xml_set_character_data_handler($this->_parser, '_cdata');
     xml_set_default_handler($this->_parser, '_hDefault');
     xml_set_processing_instruction_handler($this->_parser, '_PIHandler');
 }
예제 #17
0
 /**
  * Parses string.
  * 
  * This method parses string given in the passed parameter.
  * 
  * @param   string      XML data.
  * @param   boolean     end of file.
  * @access  public
  * @see     parseFile()
  */
 function parse($data, $eof = true)
 {
     if (!is_resource($this->_parser)) {
         $this->_parser = xml_parser_create();
         xml_set_object($this->_parser, &$this);
         xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, 0);
         xml_set_element_handler($this->_parser, '_startElementHandler', '_endElementHandler');
         xml_set_character_data_handler($this->_parser, '_characterDataHandler');
         xml_set_processing_instruction_handler($this->_parser, '_processingInstructionHandler');
         xml_set_default_handler($this->_parser, '_defaultHandler');
         //xml_set_external_entity_ref_handler($this->_parser, '_externalEntityRefHandler');
         //xml_set_start_namespace_decl_handler($this->_parser, '_namespaceDeclHandler');
     }
     if (!xml_parse($this->_parser, $data, $eof)) {
         return $this->raiseError(sprintf("%s at line %d position %d\n", xml_error_string(xml_get_error_code($this->_parser)), xml_get_current_line_number($this->_parser), xml_get_current_column_number($this->_parser)));
     }
     if ($eof == true) {
         xml_parser_free($this->_parser);
         unset($this->_parser);
     }
 }
 /**
  * Parses xml text using Expat
  * @param Object A reference to the DOM document that the xml is to be parsed into
  * @param string The text to be parsed
  * @param boolean True if CDATA Section nodes are not to be converted into Text nodes
  * @return boolean True if the parsing is successful
  */
 function parse(&$myXMLDoc, $xmlText, $preserveCDATA = true)
 {
     $this->xmlDoc =& $myXMLDoc;
     $this->lastChild =& $this->xmlDoc;
     $this->preserveCDATA = $preserveCDATA;
     //create instance of expat parser (should be included in php distro)
     if (version_compare(phpversion(), '5.0', '<=')) {
         if ($this->xmlDoc->isNamespaceAware) {
             $parser = xml_parser_create_ns('');
         } else {
             $parser = xml_parser_create('');
         }
     } else {
         if ($this->xmlDoc->isNamespaceAware) {
             $parser = xml_parser_create_ns();
         } else {
             $parser = xml_parser_create();
         }
     }
     //set handlers for SAX events
     xml_set_object($parser, $this);
     xml_set_character_data_handler($parser, 'dataElement');
     xml_set_default_handler($parser, 'defaultDataElement');
     xml_set_notation_decl_handler($parser, 'notationElement');
     xml_set_processing_instruction_handler($parser, 'processingInstructionElement');
     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
     if ($this->xmlDoc->isNamespaceAware) {
         xml_set_start_namespace_decl_handler($parser, 'startNamespaceDeclaration');
         xml_set_end_namespace_decl_handler($parser, 'endNamespaceDeclaration');
         xml_set_element_handler($parser, 'startElementNS', 'endElement');
         $this->namespaceURIMap[DOMIT_XML_NAMESPACE] = 'xml';
     } else {
         xml_set_element_handler($parser, 'startElement', 'endElement');
     }
     //parse out whitespace -  (XML_OPTION_SKIP_WHITE = 1 does not
     //seem to work consistently across versions of PHP and Expat
     $xmlText = eregi_replace('>' . "[[:space:]]+" . '<', '><', $xmlText);
     $success = xml_parse($parser, $xmlText);
     $this->xmlDoc->errorCode = xml_get_error_code($parser);
     $this->xmlDoc->errorString = xml_error_string($this->xmlDoc->errorCode);
     xml_parser_free($parser);
     return $success;
 }
예제 #19
0
function new_xml_parser($file)
{
    global $parser_file;
    $xml_parser = xml_parser_create();
    xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    xml_set_processing_instruction_handler($xml_parser, "PIHandler");
    xml_set_default_handler($xml_parser, "defaultHandler");
    xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");
    if (!($fp = @fopen($file, "r"))) {
        return false;
    }
    if (!is_array($parser_file)) {
        settype($parser_file, "array");
    }
    $parser_file[$xml_parser] = $file;
    return array($xml_parser, $fp);
}
 /**
  * Reads a string and parses the XML data.
  *
  * Parse the XML source and (upon success) store the information into an internal structure.
  * If a parent xpath is given this means that XML data is to be *appended* to that parent.
  *
  * ### If a function uses setLastError(), then say in the function header that getLastError() is useful.
  *
  * @param  $xmlString           (string) Name of the string to be read and parsed.
  * @param  $absoluteParentPath  (string) Node to append data too (see above)
  * @return                      (bool)   TRUE on success, FALSE on failure 
  *                                       (check getLastError())
  */
 function importFromString($xmlString, $absoluteParentPath = '')
 {
     $bDebugThisFunction = FALSE;
     if ($bDebugThisFunction) {
         $aStartTime = $this->_beginDebugFunction("importFromString");
         echo "Importing from string of length " . strlen($xmlString) . " to node '{$absoluteParentPath}'\n<br>";
         echo "Parser options:\n<br>";
         print_r($this->parseOptions);
     }
     $status = FALSE;
     $errStr = '';
     do {
         // try-block
         // If we already have content, then complain.
         if (!empty($this->nodeRoot) and empty($absoluteParentPath)) {
             $errStr = 'Called when this object already contains xml data. Use reset() or pass the parent Xpath as 2ed param to where tie data will append.';
             break;
             // try-block
         }
         // Check whether content has been read.
         if (empty($xmlString)) {
             // Nothing to do!!
             $status = TRUE;
             // If we were importing to root, build a blank root.
             if (empty($absoluteParentPath)) {
                 $this->_createSuperRoot();
             }
             $this->reindexNodeTree();
             //        $errStr = 'This xml document (string) was empty';
             break;
             // try-block
         } else {
             $xmlString = $this->_translateAmpersand($xmlString);
         }
         // Restart our node index with a root entry.
         $nodeStack = array();
         $this->parseStackIndex = 0;
         // If a parent xpath is given this means that XML data is to be *appended* to that parent.
         if (!empty($absoluteParentPath)) {
             // Check if parent exists
             if (!isset($nodeIndex[$absoluteParentPath])) {
                 $errStr = "You tried to append XML data to a parent '{$absoluteParentPath}' that does not exist.";
                 break;
                 // try-block
             }
             // Add it as the starting point in our array.
             $this->nodeStack[0] =& $nodeIndex[$absoluteParentPath];
         } else {
             // Build a 'super-root'
             $this->_createSuperRoot();
             // Put it in as the start of our node stack.
             $this->nodeStack[0] =& $this->nodeRoot;
         }
         // Point our text buffer reference at the next text part of the root
         $this->parsedTextLocation =& $this->nodeStack[0]['textParts'][];
         $this->parsInCData = 0;
         // We cache this now.
         $this->parseSkipWhiteCache = isset($this->parseOptions[XML_OPTION_SKIP_WHITE]) ? $this->parseOptions[XML_OPTION_SKIP_WHITE] : FALSE;
         // Create an XML parser.
         $parser = xml_parser_create();
         // Set default XML parser options.
         if (is_array($this->parseOptions)) {
             foreach ($this->parseOptions as $key => $val) {
                 xml_parser_set_option($parser, $key, $val);
             }
         }
         // Set the object and the element handlers for the XML parser.
         xml_set_object($parser, $this);
         xml_set_element_handler($parser, '_handleStartElement', '_handleEndElement');
         xml_set_character_data_handler($parser, '_handleCharacterData');
         xml_set_default_handler($parser, '_handleDefaultData');
         xml_set_processing_instruction_handler($parser, '_handlePI');
         if ($bDebugThisFunction) {
             $this->_profileFunction($aStartTime, "Setup for parse");
         }
         // Parse the XML source and on error generate an error message.
         if (!xml_parse($parser, $xmlString, TRUE)) {
             $source = empty($this->properties['xmlFile']) ? 'string' : 'file ' . basename($this->properties['xmlFile']) . "'";
             $errStr = "XML error in given {$source} on line " . xml_get_current_line_number($parser) . '  column ' . xml_get_current_column_number($parser) . '. Reason:' . xml_error_string(xml_get_error_code($parser));
             break;
             // try-block
         }
         // Free the parser.
         @xml_parser_free($parser);
         // And we don't need this any more.
         $this->nodeStack = array();
         if ($bDebugThisFunction) {
             $this->_profileFunction($aStartTime, "Parse Object");
         }
         $this->reindexNodeTree();
         if ($bDebugThisFunction) {
             print_r(array_keys($this->nodeIndex));
         }
         if ($bDebugThisFunction) {
             $this->_profileFunction($aStartTime, "Reindex Object");
         }
         $status = TRUE;
     } while (FALSE);
     if (!$status) {
         $this->_displayError('In importFromString(): ' . $errStr, __LINE__, __FILE__, FALSE);
         $bResult = FALSE;
     } else {
         $bResult = TRUE;
     }
     ////////////////////////////////////////////
     if ($bDebugThisFunction) {
         $this->_closeDebugFunction($aStartTime, $bResult);
     }
     return $bResult;
 }
<?php

/* Prototype  : proto int xml_set_processing_instruction_handler(resource parser, string hdl)
 * Description: Set up processing instruction (PI) handler 
 * Source code: ext/xml/xml.c
 * Alias to functions: 
 */
echo "*** Testing xml_set_processing_instruction_handler() : error conditions ***\n";
//Test xml_set_processing_instruction_handler with one more than the expected number of arguments
echo "\n-- Testing xml_set_processing_instruction_handler() function with more than expected no. of arguments --\n";
$hdl = 'string_val';
$extra_arg = 10;
var_dump(xml_set_processing_instruction_handler(null, $hdl, $extra_arg));
// Testing xml_set_processing_instruction_handler with one less than the expected number of arguments
echo "\n-- Testing xml_set_processing_instruction_handler() function with less than expected no. of arguments --\n";
var_dump(xml_set_processing_instruction_handler(null));
echo "Done";
예제 #22
0
 /**
  * @internal
  * Init the XML parser
  * @param $ns
  * @param $encoding
  * @param $separator
  * @private
  */
 function _initParser($ns = false, $encoding = 'UTF-8', $separator = null)
 {
     $this->_p = $ns ? xml_parser_create_ns($encoding, $separator) : xml_parser_create($encoding);
     xml_set_object($this->_p, $this);
     xml_set_default_handler($this->_p, '_default');
     xml_set_element_handler($this->_p, '_tagOpen', '_tagClose');
     xml_set_character_data_handler($this->_p, '_cdata');
     xml_set_start_namespace_decl_handler($this->_p, '_nsStart');
     xml_set_end_namespace_decl_handler($this->_p, '_nsEnd');
     xml_set_external_entity_ref_handler($this->_p, '_entityRef');
     xml_set_processing_instruction_handler($this->_p, '_pi');
     xml_set_notation_decl_handler($this->_p, '_notation');
     xml_set_unparsed_entity_decl_handler($this->_p, '_unparsedEntity');
     $this->setOption(XML_OPTION_CASE_FOLDING, false);
 }
예제 #23
0
function Aspis_xml_set_processing_instruction_handler($parser, $handler)
{
    global $Aspis_xml_objects;
    if (!empty($Aspis_xml_objects)) {
        $c = count($Aspis_xml_objects);
        for ($i = $c - 1; $i >= 0; $i--) {
            $e = $Aspis_xml_objects[$i];
            if ($e[0] === $parser[0]) {
                $handler[0] = AspisInternalCallback(array(array(array($e[1], false), array($handler[0], false)), false));
                break;
            }
        }
    }
    return array(xml_set_processing_instruction_handler($parser[0], $handler[0]), false);
}
예제 #24
0
 /**
  * Initialize the XML parser.
  */
 function init()
 {
     // Check that the charset is supported by the XML parser.
     $allowed_charsets = array('us-ascii', 'iso-8859-1', 'utf-8', 'utf-16');
     if (!in_array($this->charset, $allowed_charsets)) {
         $this->charset = 'utf-8';
     }
     // Create the XML parser.
     $this->parser = xml_parser_create($this->charset);
     xml_set_object($this->parser, $this);
     xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
     xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
     xml_set_element_handler($this->parser, 'startElement', 'endElement');
     xml_set_character_data_handler($this->parser, 'characterData');
     xml_set_default_handler($this->parser, 'defaultHandler');
     // Disable processing instructions and external entities.
     xml_set_processing_instruction_handler($this->parser, '');
     xml_set_external_entity_ref_handler($this->parser, '');
 }
예제 #25
0
 /**
  * Here's were all the processing takes place: gets the SyncML request
  * data and returns a SyncML response. The only thing that needs to be in
  * place before invoking this public function is a working backend.
  *
  * @param string $request      The raw request string.
  * @param string $contentType  The MIME content type of the request. Should
  *                             be either application/vnd.syncml or
  *                             application/vnd.syncml+wbxml.
  * @param string $respURI      The url of the server endpoint. Will be
  *                             returned in the RespURI element.
  */
 public function process($request, $contentType, $respURI = null)
 {
     $isWBXML = $contentType == 'application/vnd.syncml+wbxml';
     $this->_respURI = $respURI;
     /* Catch any errors/warnings/notices that may get thrown while
      * processing. Don't want to let anything go to the client that's not
      * part of the valid response. */
     ob_start();
     $GLOBALS['backend']->logFile(Horde_SyncMl_Backend::LOGFILE_CLIENTMESSAGE, $request, $isWBXML);
     if (!$isWBXML) {
         /* XML code. */
         /* try to extract charset from XML text */
         if (preg_match('/^\\s*<\\?xml[^>]*encoding\\s*=\\s*"([^"]*)"/i', $request, $m)) {
             $charset = $m[1];
         } else {
             $charset = 'UTF-8';
         }
         $GLOBALS['backend']->setCharset($charset);
         /* Init output handler. */
         $this->_xmlWriter =& Horde_SyncMl_XmlOutput::singleton();
         /* Horde_Xml_Wbxml_ContentHandler Is a class that produces plain XML
          * output. */
         $this->_xmlWriter->init(new Horde_Xml_Wbxml_ContentHandler());
         /* Create the XML parser and set method references. */
         $parser = xml_parser_create_ns($charset);
         xml_set_object($parser, $this);
         xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
         xml_set_element_handler($parser, '_startElement', '_endElement');
         xml_set_character_data_handler($parser, '_characters');
         xml_set_processing_instruction_handler($parser, '');
         xml_set_external_entity_ref_handler($parser, '');
         /* Here we go: fire off events: */
         if (!xml_parse($parser, $request)) {
             $s = sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser));
             $GLOBALS['backend']->logMessage($s, 'ERR');
             xml_parser_free($parser);
             return new PEAR_Error($s);
         }
         xml_parser_free($parser);
     } else {
         /* The decoder works like the parser in the XML code above: It
          * parses the input and calls the callback functions of $this. */
         $this->_wbxmlparser = new Horde_Xml_Wbxml_Decoder();
         $this->_wbxmlparser->setContentHandler($this);
         /* Init output handler. */
         $this->_xmlWriter =& Horde_SyncMl_XmlOutput::singleton();
         $this->_xmlWriter->init(new Horde_Xml_Wbxml_Encoder());
         /* Here we go: fire off events: */
         /* @todo catch exceptions */
         $this->_wbxmlparser->decode($request);
     }
     $id = @session_id();
     $sessionclose = empty($id);
     $output = $this->getOutput();
     if (!$isWBXML) {
         $output = '<?xml version="1.0" encoding="' . $charset . '"?>' . $output;
     }
     $GLOBALS['backend']->logFile(Horde_SyncMl_Backend::LOGFILE_SERVERMESSAGE, $output, $isWBXML, $sessionclose);
     /* Clear the output buffer that we started above, and log anything
      * that came up for later debugging. */
     $errorLogging = ob_get_clean();
     if (!empty($errorLogging)) {
         $GLOBALS['backend']->logMessage('Caught output: ' . $errorLogging, 'WARN');
     }
     return $output;
 }
예제 #26
0
 /**
  *
  * @access protected
  */
 function register()
 {
     xml_set_character_data_handler($this->parser, 'character_data');
     xml_set_default_handler($this->parser, 'default_data');
     xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
     xml_set_end_namespace_decl_handler($this->parser, 'end_namespace_decl');
     xml_set_external_entity_ref_handler($this->parser, 'external_entity_ref');
     xml_set_notation_decl_handler($this->parser, 'notation_decl');
     xml_set_processing_instruction_handler($this->parser, 'processing_instruction');
     xml_set_start_namespace_decl_handler($this->parser, 'start_namespace_decl');
     xml_set_unparsed_entity_decl_handler($this->parser, 'unparsed_entity');
     return $this->registered = 1;
 }
예제 #27
0
 /**
  * Get an XML parser with the root element handler.
  * @see XmlTypeCheck::rootElementOpen()
  * @return resource a resource handle for the XML parser
  */
 private function getParser()
 {
     $parser = xml_parser_create_ns('UTF-8');
     // case folding violates XML standard, turn it off
     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
     xml_set_element_handler($parser, array($this, 'rootElementOpen'), false);
     if ($this->parserOptions['processing_instruction_handler']) {
         xml_set_processing_instruction_handler($parser, array($this, 'processingInstructionHandler'));
     }
     return $parser;
 }
예제 #28
0
 function init_mapper()
 {
     $this->document['pi'] = array();
     $this->document['children'] = array(0);
     $this->curr_tag =& $this->document;
     $this->xml_parser = xml_parser_create();
     xml_set_object($this->xml_parser, $this);
     xml_set_element_handler($this->xml_parser, "start_element_handler", "end_element_handler");
     xml_set_character_data_handler($this->xml_parser, "cdata_handler");
     xml_set_processing_instruction_handler($this->xml_parser, "pi_handler");
     xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, 0);
     $this->parse();
 }
예제 #29
0
파일: sax.php 프로젝트: nursit/SPIP
function xml_sax_dist($page, $apply = false, $phraseur = NULL, $doctype = '', $charset = null)
{
    if (is_null($charset)) {
        $charset = $GLOBALS['meta']['charset'];
    }
    if ($apply) {
        ob_start();
        if (is_array($apply)) {
            $r = call_user_func_array($page, $apply);
        } else {
            $r = $page();
        }
        $page = ob_get_contents();
        ob_end_clean();
        // fonction sans aucun "echo", ca doit etre le resultat
        if (!$page) {
            $page = $r;
        }
    }
    if (!$page) {
        return '';
    }
    // charger la DTD et transcoder les entites,
    // et escamoter le doctype que sax mange en php5 mais pas en  php4
    if (!$doctype) {
        if (!($r = analyser_doctype($page))) {
            $page = _MESSAGE_DOCTYPE . _DOCTYPE_ECRIRE . preg_replace(_REGEXP_DOCTYPE, '', $page);
            $r = analyser_doctype($page);
        }
        list($entete, $avail, $grammaire, $rotlvl) = array_pad($r, 4, null);
        $page = substr($page, strlen($entete));
    } else {
        $avail = 'SYSTEM';
        $grammaire = $doctype;
        $rotlvl = basename($grammaire);
    }
    include_spip('xml/analyser_dtd');
    $dtc = charger_dtd($grammaire, $avail, $rotlvl);
    $page = sax_bug($page, $dtc, $charset);
    // compatibilite Tidy espace public
    if (!$phraseur) {
        $indenter_xml = charger_fonction('indenter', 'xml');
        return $indenter_xml($page, $apply);
    }
    $xml_parser = xml_parser_create($charset);
    xml_set_element_handler($xml_parser, array($phraseur, "debutElement"), array($phraseur, "finElement"));
    xml_set_character_data_handler($xml_parser, array($phraseur, "textElement"));
    xml_set_processing_instruction_handler($xml_parser, array($phraseur, 'piElement'));
    xml_set_default_handler($xml_parser, array($phraseur, "defaultElement"));
    xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
    $phraseur->sax = $xml_parser;
    if (isset($entete)) {
        $phraseur->entete = $entete;
    }
    $phraseur->page = $page;
    $phraseur->dtc = $dtc;
    $phraseur->phraserTout($xml_parser, $page);
    xml_parser_free($xml_parser);
    $phraseur->sax = '';
    return $phraseur;
}
예제 #30
0
 /**
  *  Register this class with a parser
  *
  *  @param string $namespace (optional)
  *  @param string $encoding (optional) default into to autodetect based on env
  *  @param string $separator (optional) the seperator to use for namespace
  *  @return resource the parser
  */
 public function register($encoding = '', $namespace = false, $separator = null)
 {
     if ($this->parser === null) {
         if ($namespace !== null) {
             $this->parser = xml_parser_create_ns($encoding, $separator);
         } else {
             $this->parser = xml_parser_create($encoding);
         }
         if (!$this->parser) {
             throw new RegisterParserFailure('call to xml_parser_create() failed');
         }
         # set element handler
         xml_set_element_handler($this->parser, array($this, "xmlStartTag"), array($this, "xmlEndTag"));
         # set CDATA hander
         xml_set_character_data_handler($this->parser, array($this, "xmlTagContent"));
         # set processing instructions handler
         xml_set_processing_instruction_handler($this->parser, array($this, "xmlPI"));
         # set the unparsed entity declaration handler
         xml_set_unparsed_entity_decl_handler($this->parser, array($this, "xmlUnparsedEntity"));
         # set the notation declaration handler function
         xml_set_notation_decl_handler($this->parser, array($this, "xmlNotation"));
         # set the external entity reference handler function
         xml_set_external_entity_ref_handler($this->parser, array($this, "xmlEentityRef"));
         # Sets the default handler function
         xml_set_default_handler($this->parser, array($this, "xmlDefault"));
         # Set a handler to be called when a namespace is declared.
         xml_set_start_namespace_decl_handler($this->parser, array($this, "xmlNSStart"));
         # Set a handler to be called when leaving the scope of a namespace declaration
         xml_set_end_namespace_decl_handler($this->parser, array($this, "xmlNSEnd"));
         # turn off case folding to stop element names from being uppercased;
         $this->setOption(XML_OPTION_CASE_FOLDING, false);
         //$this->setOption(XML_OPTION_SKIP_WHITE, true);
     } else {
         throw new ParserException('Parser already registered call XML::unregister() first');
     }
     return $this->parser;
 }