Example #1
1
 public function parse($xml_or_arr)
 {
     if (is_array($xml_or_arr)) {
         $output = array();
         foreach ($xml_or_arr as $val) {
             $to = $this->parse($val);
             $output = array_merge($output, $to);
         }
         return $output;
     }
     //		echo '<h1>xml in parser:</h1><pre>'; print_r($xml); echo '</pre>';
     // if we don't start with a processing instruction,
     // we add an outer node just to ensure it's a valid xml document
     // (i.e. only a single root node)
     if (substr($xml_or_arr, 0, 2) != '<?') {
         $xml_or_arr = "<{$this->insertedNode}>" . $xml_or_arr . "</{$this->insertedNode}>";
     }
     $this->parser = xml_parser_create();
     xml_set_object($this->parser, $this);
     xml_set_element_handler($this->parser, "tagOpen", "tagClosed");
     xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
     xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
     xml_set_character_data_handler($this->parser, "tagData");
     $this->xmldata = xml_parse($this->parser, $xml_or_arr);
     if (!$this->xmldata) {
         die(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);
     $this->parseCompleted();
     return $this->output;
 }
 public function runTest()
 {
     $testResult = true;
     $this->logger->trace('Entering ' . __METHOD__);
     $this->logger->info('  Testing the file [' . $this->fileName . '] for well-formedness');
     $xmlParser = xml_parser_create();
     $file = join(DIRECTORY_SEPARATOR, array($this->directory, $this->fileName));
     if (!($fp = fopen($file, "r"))) {
         $this->logger->error('  Could not open XML-file for input [' . $file . ']');
         throw new Exception('Could not open XML input : ' . $file);
     }
     $errorFound = false;
     while (($data = fread($fp, Constants::XML_PARSE_BUFFER_SIZE)) && $errorFound == false) {
         if (!xml_parse($xmlParser, $data, feof($fp))) {
             $testResult = false;
             $errorFound = true;
             $this->logger->error(xml_get_current_line_number($xmlParser) . ' ' . xml_error_string(xml_get_error_code($xmlParser)));
         }
     }
     xml_parser_free($xmlParser);
     if ($testResult == true) {
         $this->testProperty->addTestResult(true);
         $this->logger->info(' RESULT The file ' . $this->fileName . ' is well-formed.');
         $this->testProperty->addTestResultDescription('The file ' . $this->fileName . ' is well-formed.');
         $this->testProperty->addTestResultReportDescription('Filen ' . $this->fileName . ' er korrektstrukturert.');
     } else {
         $this->testProperty->addTestResult(false);
         $this->testProperty->addTestResultDescription('The file ' . $this->fileName . ' is not well-formed.');
         $this->logger->error(' RESULT The file ' . $this->fileName . ' is not well-formed.');
         $this->testProperty->addTestResultReportDescription('Filen ' . $this->fileName . ' er ikke korrektstrukturert.');
     }
 }
Example #3
0
 /**
  * Load translation data (TBX file reader)
  *
  * @param  string  $filename  TBX file to add, full path must be given for access
  * @param  string  $locale    Locale has no effect for TBX because TBX defines all languages within
  *                            the source file
  * @param  array   $option    OPTIONAL Options to use
  * @throws Zend_Translation_Exception
  * @return array
  */
 protected function _loadTranslationData($filename, $locale, array $options = array())
 {
     $this->_data = array();
     if (!is_readable($filename)) {
         require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
     }
     $encoding = $this->_findEncoding($filename);
     $this->_file = xml_parser_create($encoding);
     xml_set_object($this->_file, $this);
     xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
     xml_set_element_handler($this->_file, "_startElement", "_endElement");
     xml_set_character_data_handler($this->_file, "_contentElement");
     try {
         Zend_Xml_Security::scanFile($filename);
     } catch (Zend_Xml_Exception $e) {
         require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception($e->getMessage());
     }
     if (!xml_parse($this->_file, file_get_contents($filename))) {
         $ex = sprintf('XML error: %s at line %d of file %s', xml_error_string(xml_get_error_code($this->_file)), xml_get_current_line_number($this->_file), $filename);
         xml_parser_free($this->_file);
         require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception($ex);
     }
     return $this->_data;
 }
 function MagpieRSS($source)
 {
     # if PHP xml isn't compiled in, die
     #
     if (!function_exists('xml_parser_create')) {
         trigger_error("Failed to load PHP's XML Extension. http://www.php.net/manual/en/ref.xml.php");
     }
     $parser = @xml_parser_create();
     if (!is_resource($parser)) {
         trigger_error("Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php");
     }
     $this->parser = $parser;
     # pass in parser, and a reference to this object
     # set up handlers
     #
     xml_set_object($this->parser, $this);
     xml_set_element_handler($this->parser, 'feed_start_element', 'feed_end_element');
     xml_set_character_data_handler($this->parser, 'feed_cdata');
     $status = xml_parse($this->parser, $source);
     if (!$status) {
         $errorcode = xml_get_error_code($this->parser);
         if ($errorcode != XML_ERROR_NONE) {
             $xml_error = xml_error_string($errorcode);
             $error_line = xml_get_current_line_number($this->parser);
             $error_col = xml_get_current_column_number($this->parser);
             $errormsg = "{$xml_error} at line {$error_line}, column {$error_col}";
             $this->error($errormsg);
         }
     }
     xml_parser_free($this->parser);
     $this->normalize();
 }
Example #5
0
 public function __construct($dataDirectory)
 {
     //
     // Default to English for lack of anything better
     //
     $this->clientLang = 'en-us';
     //
     // See if we can find anything better
     //
     $matches = array();
     if (array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
         setlocale(LC_ALL, 'en-US');
         if (preg_match('/(en-US|fr-FR|de-DE|it-IT|es-ES|sv-SV|sv-SE|nl-NL)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches)) {
             $this->clientLang = strtolower($matches[1]);
         }
     }
     //
     // Pull in all of the strings we need
     //
     $lang2file = array("es-es" => "es-ES.xml", "en-us" => "en-US.xml", "de-de" => "de-DE.xml", "fr-fr" => "fr-FR.xml", "it-it" => "it-IT.xml", "nl-nl" => "nl-NL.xml", "sv-sv" => "sv-SE.xml", "sv-se" => "sv-SE.xml");
     $xml_parser = xml_parser_create();
     $l10nValues = array();
     $l10nIndexes = array();
     $l10nContents = file_get_contents("{$dataDirectory}/" . $lang2file[$this->getClientLang()]);
     if (!xml_parse_into_struct($xml_parser, $l10nContents, $l10nValues, $l10nIndexes)) {
         die(sprintf("l10n: XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
     } else {
         foreach ($l10nIndexes["STRING"] as $l10nStringIndex) {
             $this->l10n[$l10nValues[$l10nStringIndex]["attributes"]["ID"]] = $l10nValues[$l10nStringIndex]["value"];
         }
     }
     xml_parser_free($xml_parser);
 }
Example #6
0
 function load($xml_file)
 {
     $this->_path = dirname($xml_file);
     if (!file_exists($xml_file)) {
         return;
     }
     $fp = @fopen($xml_file, "r");
     if ($fp) {
         $data = '';
         while (!feof($fp)) {
             $data .= fread($fp, 8192);
         }
         fclose($fp);
         if (ini_get("magic_quotes_gpc")) {
             $data = stripslashes($data);
         }
     }
     $this->_parser = xml_parser_create('UTF-8');
     xml_set_object($this->_parser, $this);
     xml_set_element_handler($this->_parser, "_saxStartElement", "_saxEndElement");
     xml_parser_set_option($this->_parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
     if (!xml_parse($this->_parser, $data, true)) {
         trigger_error(sprintf("Language pack loading failed, XML error: %s at line %d.", xml_error_string(xml_get_error_code($this->_parser)), xml_get_current_line_number($this->_parser)), E_USER_ERROR);
     }
     xml_parser_free($this->_parser);
 }
 public function fetch()
 {
     $this->query = $this->url . '?pid=' . $this->pid . '&noredirect=true&format=unixref&id=doi%3A' . $this->doi;
     $request_options = array('method' => 'POST');
     $result = drupal_http_request($this->query, $request_options);
     if ($result->code != 200) {
         drupal_set_message(t('HTTP error: !error when trying to contact crossref.org for XML input', array('!error' => $result->code)), 'error');
         return;
     }
     if (empty($result->data)) {
         drupal_set_message(t('Did not get any data from crossref.org'), 'error');
         return;
     }
     $sxml = @simplexml_load_string($result->data);
     if ($error = (string) $sxml->doi_record->crossref->error) {
         drupal_set_message($error, 'error');
         return;
     }
     $this->nodes = array();
     $this->parser = drupal_xml_parser_create($result->data);
     // use case-folding so we are sure to find the tag in
     xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, FALSE);
     xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, TRUE);
     xml_set_object($this->parser, $this);
     xml_set_element_handler($this->parser, 'unixref_startElement', 'unixref_endElement');
     xml_set_character_data_handler($this->parser, 'unixref_characterData');
     if (!xml_parse($this->parser, $result->data)) {
         drupal_set_message(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser)), 'error');
     }
     xml_parser_free($this->parser);
     return $this->node;
 }
 public function parse($html)
 {
     // replace entities
     $html = preg_replace('/&([a-z0-9#]{2,5});/i', '+$1;', $html);
     //before sending to xml parser make sure we have valid xml by tidying it up
     $html = Kwf_Util_Tidy::repairHtml($html);
     $this->_stack = array();
     $this->_ret = '';
     $this->_parser = xml_parser_create();
     xml_set_object($this->_parser, $this);
     xml_set_element_handler($this->_parser, 'startElement', 'endElement');
     xml_set_character_data_handler($this->_parser, 'characterData');
     xml_set_default_handler($this->_parser, 'characterData');
     $result = xml_parse($this->_parser, '<body>' . $html . '</body>', true);
     if (!$result) {
         // wenn man ein nicht geschlossenes <br> rein gibt, schreit er hier,
         // macht aber normal weiter. wenns zu oft vorkommt, evtl. exception
         // entfernen und ignorieren, oder was andres überlegen :-)
         $errorCode = xml_get_error_code($this->_parser);
         $ex = new Kwf_Exception("HtmlExport UrlParser XML Error {$errorCode}: " . xml_error_string($errorCode) . "in line " . xml_get_current_line_number($this->_parser) . " parsed html: " . $html);
         $ex->logOrThrow();
     }
     // re-replace entities
     $this->_ret = preg_replace('/\\+([a-z0-9#]{2,5});/i', '&$1;', $this->_ret);
     return $this->_ret;
 }
Example #9
0
 function _parseXML()
 {
     $this->items = array();
     $this->item = array();
     $this->is_item = FALSE;
     $this->tag = '';
     $this->level_base = 0;
     $buf = $this->src;
     // Detect encoding
     $matches = array();
     if (preg_match('/<\\?xml [^>]*\\bencoding="([a-z0-9-_]+)"/i', $buf, $matches)) {
         $this->encoding = $matches[1];
     } else {
         $this->encoding = mb_detect_encoding($buf, $this->detect_order);
     }
     // Normalize to UTF-8 / ASCII
     if (!in_array(strtolower($this->encoding), array('us-ascii', 'iso-8859-1', 'utf-8'))) {
         $buf = mb_convert_encoding($buf, 'utf-8', $this->encoding);
         $this->encoding = 'utf-8';
     }
     // Parsing
     $xml_parser = xml_parser_create($this->encoding);
     xml_set_element_handler($xml_parser, array(&$this, 'start_element'), array(&$this, 'end_element'));
     xml_set_character_data_handler($xml_parser, array(&$this, 'character_data'));
     if (!xml_parse($xml_parser, $buf, 1)) {
         return sprintf('XML error: %s at line %d in %s', xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser), $buf);
     }
     xml_parser_free($xml_parser);
     //return $this->items;
 }
 /**
  * Method parses a mirror.xml file.
  *
  * @param string $file GZIP stream resource
  * @return void
  * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException in case of XML parser errors
  */
 public function parseXml($file)
 {
     $this->createParser();
     if (!is_resource($this->objXml)) {
         throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342641009);
     }
     // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
     $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
     // keep original character case of XML document
     xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, false);
     xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, false);
     xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
     xml_set_element_handler($this->objXml, 'startElement', 'endElement');
     xml_set_character_data_handler($this->objXml, 'characterData');
     if (!($fp = fopen($file, 'r'))) {
         throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342641010);
     }
     while ($data = fread($fp, 4096)) {
         if (!xml_parse($this->objXml, $data, feof($fp))) {
             throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('XML error %s in line %u of file resource %s.', xml_error_string(xml_get_error_code($this->objXml)), xml_get_current_line_number($this->objXml), $file), 1342641011);
         }
     }
     libxml_disable_entity_loader($previousValueOfEntityLoader);
     xml_parser_free($this->objXml);
 }
Example #11
0
 function parse()
 {
     set_error_handler(array(&$this, 'error_handler'));
     array_unshift($this->ns_contexts, array());
     $parser = xml_parser_create_ns();
     xml_set_object($parser, $this);
     xml_set_element_handler($parser, "start_element", "end_element");
     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
     xml_set_character_data_handler($parser, "cdata");
     xml_set_default_handler($parser, "_default");
     xml_set_start_namespace_decl_handler($parser, "start_ns");
     xml_set_end_namespace_decl_handler($parser, "end_ns");
     $this->content = '';
     $ret = true;
     $fp = fopen($this->FILE, "r");
     while ($data = fread($fp, 4096)) {
         if ($this->debug) {
             $this->content .= $data;
         }
         if (!xml_parse($parser, $data, feof($fp))) {
             trigger_error(sprintf(__('XML error: %s at line %d') . "\n", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
             $ret = false;
             break;
         }
     }
     fclose($fp);
     xml_parser_free($parser);
     restore_error_handler();
     return $ret;
 }
 /**
  * PHP5 constructor.
  */
 function __construct($source)
 {
     # Check if PHP xml isn't compiled
     #
     if (!function_exists('xml_parser_create')) {
         return trigger_error("PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension.");
     }
     $parser = xml_parser_create();
     $this->parser = $parser;
     # pass in parser, and a reference to this object
     # set up handlers
     #
     xml_set_object($this->parser, $this);
     xml_set_element_handler($this->parser, 'feed_start_element', 'feed_end_element');
     xml_set_character_data_handler($this->parser, 'feed_cdata');
     $status = xml_parse($this->parser, $source);
     if (!$status) {
         $errorcode = xml_get_error_code($this->parser);
         if ($errorcode != XML_ERROR_NONE) {
             $xml_error = xml_error_string($errorcode);
             $error_line = xml_get_current_line_number($this->parser);
             $error_col = xml_get_current_column_number($this->parser);
             $errormsg = "{$xml_error} at line {$error_line}, column {$error_col}";
             $this->error($errormsg);
         }
     }
     xml_parser_free($this->parser);
     $this->normalize();
 }
Example #13
0
/**
 * Create an array structure from an XML string.
 *
 * Usage:<br>
 * <code>
 * $xml = xmlize($array);
 * </code>
 * See the function {@link traverse_xmlize()} for information about the
 * structure of the array, it's much easier to explain by showing you.
 * Be aware that the array is somewhat tricky.  I use xmlize all the time,
 * but still need to use {@link traverse_xmlize()} quite often to show me the structure!
 *
 * THIS IS A PHP 5 VERSION:
 *
 * This modified version basically has a new optional parameter
 * to specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
 * I recommend you to read this PHP bug. There you can see how PHP4, PHP5.0.0
 * and PHP5.0.2 will handle this.
 * {@link http://bugs.php.net/bug.php?id=29711}
 * Ciao, Eloy :-)
 *
 * @param string $data The XML source to parse.
 * @param int $whitespace  If set to 1 allows the parser to skip "space" characters in xml document. Default is 1
 * @param string $encoding Specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
 * @param bool $reporterrors if set to true, then a {@link xml_format_exception}
 *      exception will be thrown if the XML is not well-formed. Otherwise errors are ignored.
 * @return array representation of the parsed XML.
 */
function xmlize($data, $whitespace = 1, $encoding = 'UTF-8', $reporterrors = false)
{
    $data = trim($data);
    $vals = array();
    $parser = xml_parser_create($encoding);
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $whitespace);
    xml_parse_into_struct($parser, $data, $vals);
    // Error handling when the xml file is not well-formed
    if ($reporterrors) {
        $errorcode = xml_get_error_code($parser);
        if ($errorcode) {
            $exception = new xml_format_exception(xml_error_string($errorcode), xml_get_current_line_number($parser), xml_get_current_column_number($parser));
            xml_parser_free($parser);
            throw $exception;
        }
    }
    xml_parser_free($parser);
    $i = 0;
    if (empty($vals)) {
        // XML file is invalid or empty, return false
        return false;
    }
    $array = array();
    $tagname = $vals[$i]['tag'];
    if (isset($vals[$i]['attributes'])) {
        $array[$tagname]['@'] = $vals[$i]['attributes'];
    } else {
        $array[$tagname]['@'] = array();
    }
    $array[$tagname]["#"] = xml_depth($vals, $i);
    return $array;
}
Example #14
0
 public function execute()
 {
     global $CFG;
     require_once $CFG->dirroot . '/course/lib.php';
     $options = $this->expandedOptions;
     $arguments = $this->arguments;
     $this->checkFileArg($arguments[0]);
     $this->categorystack = array();
     if ($options['parent']) {
         $this->categorystack[] = $options['parent'];
     } else {
         $this->categorystack[] = 0;
     }
     $this->coursesmap = array();
     $this->parser = xml_parser_create();
     xml_set_element_handler($this->parser, array(&$this, "start_element"), array(&$this, "end_element"));
     if (!($fp = fopen($arguments[0], "r"))) {
         die("could not open XML input");
     }
     while ($data = fread($fp, 4096)) {
         if (!xml_parse($this->parser, $data, feof($fp))) {
             die(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);
     fix_course_sortorder();
 }
 /** 
  * Pour tester la validité d'un document XML, on peut utiliser un analyseur syntaxique XML : http://fr3.php.net/manual/fr/book.xml.php
  * Voir en particulier l'exemple http://fr3.php.net/manual/fr/example.xml-structure.php
  * 
  * Mais ceci ne permet pas de vérifier la conformité d'un XML avec une DTD.
  * DOMDocument le permet : http://fr2.php.net/manual/fr/domdocument.validate.php
  * Mais d'une part ça emmet des warnings et d'autre part ça ne retourne qu'un booléen sans détails sur les erreurs trouvées
  * 
  * Pour y remédier on peut utiliser cette extention de classe "MyDOMDocument" : http://fr2.php.net/manual/fr/domdocument.validate.php#85792
  * Mais attention : il faut lui fournir un objet DOMDocument et load ou loadXML provoquent des warnings préliminaires si le XML est mal formé.
  * 
  * Ma solution est d'utiliser :
  * 1. dans un premier temps l'analyseur syntaxique XML xml_parse pour vérifier que le XML est bien formé
  * 2. dans un second temps l'extention de classe MyDOMDocument pour vérifier la conformité avec la DTD
  * 
  * J'en ai fait la fonction ci-dessous "analyser_XML($fichier)".
  * La classe "MyDOMDocument" est dans autochargée (elle se trouve ici : _inc/class.domdocument.php).
  * 
  * @param string    $fichier_adresse
  * @return string   'ok' ou un message d'erreur
  */
 private static function analyser_XML($fichier_adresse)
 {
   // Récupération du contenu du fichier
   $fichier_contenu = file_get_contents($fichier_adresse);
   $fichier_contenu = To::deleteBOM(To::utf8($fichier_contenu)); // Mettre en UTF-8 si besoin et retirer le BOM éventuel
   FileSystem::ecrire_fichier($fichier_adresse,$fichier_contenu); // Mettre à jour le fichier au cas où.
   // Analyse XML (s'arrête à la 1ère erreur trouvée)
   $xml_parser = xml_parser_create();
   $valid_XML = xml_parse($xml_parser , $fichier_contenu , TRUE);
   if(!$valid_XML)
   {
     return sprintf("Erreur XML ligne %d (%s)" , xml_get_current_line_number($xml_parser) , xml_error_string(xml_get_error_code($xml_parser)));
   }
   xml_parser_free($xml_parser);
   // Analyse DTD (renvoie un tableau d'erreurs, affiche la dernière)
   $xml = new DOMDocument;
   $xml -> load($fichier_adresse);
   $xml = new MyDOMDocument($xml);
   $valid_DTD = $xml->validate();
   if(!$valid_DTD)
   {
     return 'Erreur DTD : '.end($xml->errors);
   }
   // Tout est ok
   return 'ok';
 }
Example #16
0
 public function Parse($filename)
 {
     $root = new CNode(CNode::K_ROOT, $filename, CNode::T_ROOT);
     $filename = $root->Get(CNode::FLD_VAL);
     $xmlstring = file_get_contents($filename);
     if ($xmlstring === FALSE) {
         $root->SetErr("failed to read file {$filename}", CNode::E_FATAL);
         return $root;
     }
     $parser = xml_parser_create();
     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, 'characterData');
     // Build a Root node and initialize the node_stack...
     $this->node_stack = array();
     $this->cur_node = $root;
     // parse the data and free the parser...
     $result = xml_parse($parser, $xmlstring);
     if (!$result) {
         $err = 'XML error: ' . xml_error_string(xml_get_error_code($parser)) . ' at line ' . xml_get_current_line_number($parser);
         $root->SetErr("failed to parse file {$filename}, {$err}", CNode::E_FATAL);
     }
     xml_parser_free($parser);
     return $root;
 }
Example #17
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);
 }
Example #18
0
function xmlize($data, $WHITE=1)
{

    $data = trim($data);
    $vals = $index = $array = array();
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
    if ( !xml_parse_into_struct($parser, $data, $vals, $index) ) {
	die(sprintf("XML error: %s at line %d",
		    xml_error_string(xml_get_error_code($parser)),
		    xml_get_current_line_number($parser)));

    }
    xml_parser_free($parser);

    $i = 0;

    $tagname = $vals[$i]['tag'];
    if ( isset ($vals[$i]['attributes'] ) ) {
	$array[$tagname]['@'] = $vals[$i]['attributes'];
    } else {
	$array[$tagname]['@'] = array();
    }

    $array[$tagname]["#"] = xml_depth($vals, $i);

    return $array;
}
Example #19
0
 /**
  * Load translation data (TMX file reader)
  *
  * @param  string  $filename  TMX file to add, full path must be given for access
  * @param  string  $locale    Locale has no effect for TMX because TMX defines all languages within
  *                            the source file
  * @param  array   $option    OPTIONAL Options to use
  * @throws Zend_Translation_Exception
  */
 protected function _loadTranslationData($filename, $locale, array $options = array())
 {
     $options = array_merge($this->_options, $options);
     if ($options['clear']) {
         $this->_translate = array();
     }
     if (in_array('defined_language', $options) and !empty($options['defined_language'])) {
         $this->_defined = true;
     }
     if (!is_readable($filename)) {
         require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
     }
     $this->_file = xml_parser_create();
     xml_set_object($this->_file, $this);
     xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
     xml_set_element_handler($this->_file, "_startElement", "_endElement");
     xml_set_character_data_handler($this->_file, "_contentElement");
     if (!xml_parse($this->_file, file_get_contents($filename))) {
         $ex = sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($this->_file)), xml_get_current_line_number($this->_file));
         xml_parser_free($this->_file);
         require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception($ex);
     }
 }
Example #20
0
 function parseFile($persist = false)
 {
     if (empty($this->parser)) {
         error_log("No parser, so allocating new xml parser");
         $this->parser = xml_parser_create();
         xml_set_object($this->parser, $this);
         xml_set_element_handler($this->parser, "startHandler", "endHandler");
         xml_set_character_data_handler($this->parser, "cdataHandler");
     }
     if (!empty($this->file)) {
         // process & parse
         $fp = fopen($this->file, 'r');
         if ($fp) {
             // could read the file successfully
             while ($data = fread($fp, 4096)) {
                 xml_parse($this->parser, $data, feof($fp)) or error_log(sprintf("Error in parsing XML: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser)));
             }
             fclose($fp);
         } else {
             error_log("Could not read file (name: {$this->file}).");
         }
     } else {
         error_log("Could not parse. No file specified.");
     }
     if (!$persist) {
         xml_parser_free($this->parser);
     }
 }
Example #21
0
 function parse($xml)
 {
     $this->result = array();
     $this->context = $xml;
     $this->currentTag = '';
     $this->currentState = array();
     $this->xmlParser = xml_parser_create();
     xml_parser_set_option($this->xmlParser, XML_OPTION_TARGET_ENCODING, "UTF-8") . xml_set_object($this->xmlParser, $this);
     xml_set_element_handler($this->xmlParser, 'startElement', 'endElement');
     xml_set_character_data_handler($this->xmlParser, 'content');
     $this->trigger('before:import');
     try {
         if (!xml_parse($this->xmlParser, $xml)) {
             $this->xmlParserError = 'Line ' . xml_get_current_line_number($this->xmlParser) . ': ' . (xml_get_error_code($this->xmlParser) ? xml_error_string(xml_get_error_code($this->xmlParser)) : 'Unknown error');
         }
     } catch (Exception $e) {
         $this->xmlParserError = $e->getMessage();
     }
     xml_parser_free($this->xmlParser);
     if ($this->xmlParserError) {
         $this->raiseError($this->xmlParserError);
     } else {
         if ($this->getCurrentState()) {
             $this->raiseError('Wrong ending state: ' . $this->getCurrentState());
         } else {
         }
     }
     $this->trigger('after:import');
     return $this->result;
 }
 /**
  * Format resource metadata.
  *
  * @param resource $value
  *
  * @return string
  */
 protected function formatMetadata($value)
 {
     $props = array();
     switch (get_resource_type($value)) {
         case 'stream':
             $props = stream_get_meta_data($value);
             break;
         case 'curl':
             $props = curl_getinfo($value);
             break;
         case 'xml':
             $props = array('current_byte_index' => xml_get_current_byte_index($value), 'current_column_number' => xml_get_current_column_number($value), 'current_line_number' => xml_get_current_line_number($value), 'error_code' => xml_get_error_code($value));
             break;
     }
     if (empty($props)) {
         return '{}';
     }
     $formatted = array();
     foreach ($props as $name => $value) {
         $formatted[] = sprintf('%s: %s', $name, $this->indentValue($this->presentSubValue($value)));
     }
     $template = sprintf('{%s%s%%s%s}', PHP_EOL, self::INDENT, PHP_EOL);
     $glue = sprintf(',%s%s', PHP_EOL, self::INDENT);
     return sprintf($template, implode($glue, $formatted));
 }
Example #23
0
 function loadXML($file)
 {
     $this->_parser = xml_parser_create($this->getEncoding($file));
     // Auto detect for PHP4/PHP5
     xml_set_object($this->_parser, $this);
     xml_set_element_handler($this->_parser, "_saxStartElement", "_saxEndElement");
     xml_set_character_data_handler($this->_parser, "_saxCharacterData");
     xml_parser_set_option($this->_parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
     if ($fp = fopen($file, "r")) {
         $data = '';
         while (!feof($fp)) {
             $data .= fread($fp, 8192);
         }
         fclose($fp);
         // Strip slashes
         if (ini_get("magic_quotes_gpc")) {
             $data = stripslashes($data);
         }
         // XML parse
         if (!xml_parse($this->_parser, $data, true)) {
             trigger_error(sprintf("Language pack loading failed, XML error: %s at line %d.", xml_error_string(xml_get_error_code($this->_parser)), xml_get_current_line_number($this->_parser)), FATAL);
         }
     } else {
         trigger_error("Could not open XML language pack: " . $file, FATAL);
     }
     xml_parser_free($this->_parser);
 }
Example #24
0
 function parse($path, $data = '')
 {
     $this->state = 0;
     /* reader */
     if (!$this->v('reader')) {
         ARC2::inc('Reader');
         $this->reader =& new ARC2_Reader($this->a, $this);
     }
     $this->reader->setAcceptHeader('Accept: sparql-results+xml; q=0.9, */*; q=0.1');
     $this->reader->activate($path, $data);
     $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base;
     /* xml parser */
     $this->initXMLParser();
     /* parse */
     $first = true;
     while ($d = $this->reader->readStream()) {
         if (!xml_parse($this->xml_parser, $d, false)) {
             $error_str = xml_error_string(xml_get_error_code($this->xml_parser));
             $line = xml_get_current_line_number($this->xml_parser);
             $this->tmp_error = 'XML error: "' . $error_str . '" at line ' . $line . ' (parsing as ' . $this->getEncoding() . ')';
             return $this->addError($this->tmp_error);
         }
     }
     $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING);
     xml_parser_free($this->xml_parser);
     $this->reader->closeStream();
     return $this->done();
 }
Example #25
0
    /**
     * Load translation data (XMLTM file reader)
     *
     * @param  string  $locale    Locale/Language to add data for, identical with locale identifier,
     *                            see Zend_Locale for more information
     * @param  string  $filename  XMLTM file to add, full path must be given for access
     * @param  array   $option    OPTIONAL Options to use
     * @throws \Zend\Translator\Adapter\Exception\InvalidArgumentException
     * @throws \Zend\Translator\Adapter\Exception\InvalidFileTypeException
     * @return array
     */
    protected function _loadTranslationData($filename, $locale, array $options = array())
    {
        $this->_data = array();
        $this->_lang = $locale;
        if (!is_readable($filename)) {
            throw new InvalidArgumentException('Translation file \'' . $filename . '\' is not readable.');
        }

        $encoding    = $this->_findEncoding($filename);
        $this->_file = xml_parser_create($encoding);
        xml_set_object($this->_file, $this);
        xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
        xml_set_element_handler($this->_file, "_startElement", "_endElement");
        xml_set_character_data_handler($this->_file, "_contentElement");

        if (!xml_parse($this->_file, file_get_contents($filename))) {
            $ex = sprintf('XML error: %s at line %d',
                          xml_error_string(xml_get_error_code($this->_file)),
                          xml_get_current_line_number($this->_file));
            xml_parser_free($this->_file);
            throw new InvalidFileTypeException($ex);
        }

        return $this->_data;
    }
 function error($msg)
 {
     $errf = $this->error_func;
     $line = xml_get_current_line_number($this->parser);
     $col = xml_get_current_column_number($this->parser);
     $errf("{$msg} at line:{$line}, col:{$col}.");
 }
 function analyser($file = "")
 {
     global $charset;
     if (!($fp = @fopen($this->file, "r"))) {
         die("impossible d'ouvrir le fichier {$this->file}");
     }
     $data = fread($fp, filesize($this->file));
     $rx = "/<?xml.*encoding=[\\'\"](.*?)[\\'\"].*?>/m";
     if (preg_match($rx, $data, $m)) {
         $encoding = strtoupper($m[1]);
     } else {
         $encoding = "ISO-8859-1";
     }
     $this->analyseur = xml_parser_create($encoding);
     xml_parser_set_option($this->analyseur, XML_OPTION_TARGET_ENCODING, $charset);
     xml_parser_set_option($this->analyseur, XML_OPTION_CASE_FOLDING, true);
     xml_set_object($this->analyseur, $this);
     xml_set_element_handler($this->analyseur, "debutBalise", "finBalise");
     xml_set_character_data_handler($this->analyseur, "texte");
     fclose($fp);
     if (!xml_parse($this->analyseur, $data, TRUE)) {
         die(sprintf("erreur XML %s à la ligne: %d ( {$this->file} )\n\n", xml_error_string(xml_get_error_code($this->analyseur)), xml_get_current_line_number($this->analyseur)));
     }
     xml_parser_free($this->analyseur);
 }
Example #28
0
 function fetch($raw_xml, $final = true)
 {
     $this->log .= 'fetch() called.' . PHP_EOL;
     $this->log .= 'Raw XML:' . PHP_EOL . $raw_xml . PHP_EOL . PHP_EOL;
     $this->index = 0;
     $this->data = new stdClass();
     $this->stack = array();
     $this->stack[] =& $this->data;
     $this->parser = xml_parser_create('UTF-8');
     xml_set_object($this->parser, $this);
     xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
     xml_set_character_data_handler($this->parser, 'cdata');
     xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
     xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
     xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
     $start = 0;
     $length = strlen($raw_xml);
     $chunk_size = 32 * 1024 * 1024;
     $ret = true;
     while (true) {
         if (!($parsed_xml = xml_parse($this->parser, substr($raw_xml, $start, $chunk_size), $final = ($start += $chunk_size) >= $length ? true : false))) {
             $this->log .= $ret = sprintf('XML error: %s at line %d.', xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser));
             break;
         } else {
             if ($final) {
                 break;
             }
         }
     }
     xml_parser_free($this->parser);
     return $ret;
 }
Example #29
0
 /**
  * Load translation data (XLIFF file reader)
  *
  * @param  string  $locale    Locale/Language to add data for, identical with locale identifier,
  *                            see Zend_Locale for more information
  * @param  string  $filename  XLIFF file to add, full path must be given for access
  * @param  array   $option    OPTIONAL Options to use
  * @throws Zend_Translation_Exception
  * @return array
  */
 protected function _loadTranslationData($filename, $locale, array $options = array())
 {
     $this->_data = array();
     if (!is_readable($filename)) {
         require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
     }
     if (empty($options['useId'])) {
         $this->_useId = false;
     } else {
         $this->_useId = true;
     }
     $encoding = $this->_findEncoding($filename);
     $this->_target = $locale;
     $this->_file = xml_parser_create($encoding);
     xml_set_object($this->_file, $this);
     xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
     xml_set_element_handler($this->_file, "_startElement", "_endElement");
     xml_set_character_data_handler($this->_file, "_contentElement");
     if (!xml_parse($this->_file, file_get_contents($filename))) {
         $ex = sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($this->_file)), xml_get_current_line_number($this->_file));
         xml_parser_free($this->_file);
         require_once 'Zend/Translate/Exception.php';
         throw new Zend_Translate_Exception($ex);
     }
     return $this->_data;
 }
Example #30
0
function parseSegments($segments) {
    global $segsFound;
    global $mapOfSegments;
    global $characterData;
    global $map;

    // reset elements
    $segsFound = 0;
    $mapOfSegments = array();
    $dataFound = 0;
    $characterData = array();
    $map = array();

    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterDataFunction");

    if (!xml_parse($xml_parser, $segments)) {
	die(sprintf("XML error: %s at line %d",
		    xml_error_string(xml_get_error_code($xml_parser)),
		    xml_get_current_line_number($xml_parser)));
    }

    xml_parser_free($xml_parser);
    $map = $mapOfSegments[0];
    $map['STEPS'] = array_splice($mapOfSegments, 1, count($mapOfSegments));

    // Map character data into array
    for($i=0; $i<sizeof($map['STEPS']); $i++) {
	$map['STEPS'][$i]['TEXT'] = $characterData[$i]['TEXT'];
    }
}