function isXml($filename) { $xml = new XMLReader(); $xml->open($filename); $xml->setParserProperty(XMLReader::VALIDATE, true); return $xml->isValid(); }
/** * Creates a Map of devices from the xml file * * @param string $fileName path to the xml file to parse * @return Map of <deviceId ModelDevice> */ public static function parse($fileName, $validationSchema) { $devicesMap = array(); $deviceID = null; $groupID = null; $reader = new XMLReader(); $reader->open($fileName); $fullFileName = dirname(__FILE__) . DIRECTORY_SEPARATOR . $validationSchema; $reader->setRelaxNGSchema($fullFileName); libxml_use_internal_errors(TRUE); while ($reader->read()) { if (!$reader->isValid()) { throw new Exception(libxml_get_last_error()->message); } $nodeName = $reader->name; switch ($reader->nodeType) { case XMLReader::ELEMENT: switch ($nodeName) { case WURFL_Xml_Interface::DEVICE: $groupIDCapabilitiesMap = array(); $deviceID = $reader->getAttribute(WURFL_Xml_Interface::ID); $userAgent = $reader->getAttribute(WURFL_Xml_Interface::USER_AGENT); $fallBack = $reader->getAttribute(WURFL_Xml_Interface::FALL_BACK); $actualDeviceRoot = $reader->getAttribute(WURFL_Xml_Interface::ACTUAL_DEVICE_ROOT); $currentCapabilityNameValue = array(); if ($reader->isEmptyElement) { $device = new WURFL_Xml_ModelDevice($deviceID, $userAgent, $fallBack, $actualDeviceRoot); $devicesMap[$deviceID] = $device; } break; case WURFL_Xml_Interface::GROUP: $groupID = $reader->getAttribute(WURFL_Xml_Interface::GROUP_ID); $groupIDCapabilitiesMap[$groupID] = array(); break; case WURFL_Xml_Interface::CAPABILITY: $capabilityName = $reader->getAttribute(WURFL_Xml_Interface::CAPABILITY_NAME); $capabilityValue = $reader->getAttribute(WURFL_Xml_Interface::CAPABILITY_VALUE); $currentCapabilityNameValue[$capabilityName] = $capabilityValue; $groupIDCapabilitiesMap[$groupID][$capabilityName] = $capabilityValue; break; } break; case XMLReader::END_ELEMENT: if ($nodeName == WURFL_Xml_Interface::DEVICE) { $device = new WURFL_Xml_ModelDevice($deviceID, $userAgent, $fallBack, $actualDeviceRoot, $groupIDCapabilitiesMap); $devicesMap[$device->id] = $device; } break; } } // end of while $reader->close(); return $devicesMap; }
/** * testViewRss * */ public function testViewRss() { // Disable debug mode to avoid DebugKit interruption $debug = Configure::read('debug'); Configure::write('debug', 0); $this->testAction('/categories/view/1.rss', array('method' => 'GET', 'return' => 'contents')); // Restore debug setting Configure::write('debug', $debug); $expected_strings = array('Books', 'TestSlide1', 'TestSlide2'); foreach ($expected_strings as $str) { $this->assertRegExp("/" . preg_quote($str) . "/", $this->contents); } $z = new XMLReader(); $z->xml($this->contents, NULL, LIBXML_DTDVALID); $this->assertTrue($z->isValid()); }
public function __construct(XMLReader $reader, $validate) { $skipNodes = array(XMLReader::SIGNIFICANT_WHITESPACE); do { if (!$reader->read()) { throw new InvalidArgumentException('Unexpected end of document'); } if ($validate && !$reader->isValid()) { throw new InvalidArgumentException('Invalid document'); } } while (in_array($reader->nodeType, $skipNodes)); $fields = array('name', 'nodeType', 'value', 'isEmptyElement'); $this->_properties = array(); foreach ($fields as $field) { $this->_properties[$field] = $reader->{$field}; } }
function fn_exim_1c_get_external_file($filename) { list($dir_1c, $dir_1c_url, $dir_1c_images) = fn_rus_exim_1c_get_dir_1c(); if (!is_dir($dir_1c)) { fn_mkdir($dir_1c); } $file_path = $dir_1c . $filename; if (Registry::get('addons.rus_exim_1c.exim_1c_schema_version') == '2.07') { if (file_exists($file_path) && extension_loaded('XMLReader')) { $xml = new XMLReader(); $xml->open($file_path); $xml->setParserProperty(XMLReader::VALIDATE, true); if (!$xml->isValid()) { @unlink($file_path); } } } if (fn_exim_1c_file_is_image($filename)) { if (!is_dir($dir_1c_images)) { fn_mkdir($dir_1c_images); } $file_path = $dir_1c_images . $filename; } $file = @fopen($file_path, 'w'); if (!$file) { return false; } fwrite($file, fn_get_contents('php://input')); fclose($file); return true; }
while ($reader->read()) { } } if ($reader->isValid()) { print "file relaxNG: ok\n"; } else { print "file relaxNG: failed\n"; } $reader->close(); unlink($file); $reader = new XMLReader(); $reader->XML($xmlstring); if ($reader->setRelaxNGSchema($relaxngfile)) { while ($reader->read()) { } } if ($reader->isValid()) { print "string relaxNG: ok\n"; } else { print "string relaxNG: failed\n"; } $reader->close(); $reader = new XMLReader(); $reader->XML($xmlstring); if ($reader->setRelaxNGSchema('')) { echo 'failed'; } $reader->close(); ?> ===DONE===
<?php $reader = new XMLReader(); /* load xml document as a string */ $reader->XML(file_get_contents(dirname(__FILE__) . '/thedata.xml')); /* tell the parser to perform syntax validation */ $reader->setParserProperty(XMLREADER_VALIDATE, TRUE); // loop until end of document // @ blocks the parser errors (missing DTD in this case) while (@$reader->read()) { } echo "XML document is: " . ($reader->isValid() ? '' : ' not') . " valid.";
if ($is_allowedToEdit && !empty($choice) && $choice == 'exportqti2') { require_once api_get_path(SYS_CODE_PATH) . 'exercice/export/qti2/qti2_export.php'; $export = export_exercise_to_qti($exerciseId, true); $archive_path = api_get_path(SYS_ARCHIVE_PATH); $temp_dir_short = api_get_unique_id(); $temp_zip_dir = $archive_path . $temp_dir_short; if (!is_dir($temp_zip_dir)) { mkdir($temp_zip_dir, api_get_permissions_for_new_directories()); } $temp_zip_file = $temp_zip_dir . "/" . api_get_unique_id() . ".zip"; $temp_xml_file = $temp_zip_dir . "/qti2export_" . $exerciseId . '.xml'; file_put_contents($temp_xml_file, $export); $xmlReader = new XMLReader(); $xmlReader->open($temp_xml_file); $xmlReader->setParserProperty(XMLReader::VALIDATE, true); $isValid = $xmlReader->isValid(); if ($isValid) { $zip_folder = new PclZip($temp_zip_file); $zip_folder->add($temp_xml_file, PCLZIP_OPT_REMOVE_ALL_PATH); $name = 'qti2_export_' . $exerciseId . '.zip'; DocumentManager::file_send_for_download($temp_zip_file, true, $name); unlink($temp_zip_file); unlink($temp_xml_file); rmdir($temp_zip_dir); exit; //otherwise following clicks may become buggy } else { $errorXmlExport = Display::return_message(get_lang('ErrorWritingXMLFile'), 'error'); } } if ($origin != 'learnpath') {
private function validateFile($file_name) { $extension = pathinfo($file_name, PATHINFO_EXTENSION); $file_info = explode(".", $file_name); $file_info1 = explode("_", $file_info[0]); $file_id = $file_info1[1]; // verificam validitatea fisierului in fctie de extensie if ($extension == "xml") { $reader = new XMLReader(); $reader->open(DIR_FILE_FOR_MATCHING . $file_name); // Set parser options - you must set this in order to use isValid method $reader->setParserProperty(XMLReader::VALIDATE, TRUE); $a = 0; if (!$reader->isValid()) { // se sterge din baza de date si de pe server $this->model_account_customer_file_for_matching->deleteFile($file_id); $this->session->data['warning'] = $this->language->get('text_xml_file_is_not_valid'); return false; } else { // in cazul in care xml-ul este valid, verificam daca contine datele obligatorii specificate webservice-ului $xml = simplexml_load_file(DIR_FILE_FOR_MATCHING . $file_name); foreach ($xml->children()->children() as $child) { $this->xml_node[] = strtoupper(trim($child->getName())); } $xml_valid = 1; foreach ($this->mandatory_data as $value) { if (!in_array($value, $this->xml_node)) { $xml_valid = 0; break; } } if ($xml_valid == 0) { // se sterge din baza de date si de pe server $this->model_account_customer_file_for_matching->deleteFile($file_id); $this->session->data['warning'] = $this->language->get('text_xml_file_not_contain_mandatory_data'); return false; } else { return true; } } } else { if ($extension == "csv") { $csv_delimiter = $this->model_account_customer_file_for_matching->getCsvDelimiter($file_id); $filerow = array(); $filerow = @file(DIR_FILE_FOR_MATCHING . $file_name); $csv_header = explode($csv_delimiter, $filerow[0]); $csv_have_header = 1; // verificam daca fisierul csv are header foreach ($csv_header as $value) { if (is_numeric(trim($value))) { // se sterge din baza de date si de pe server $this->model_account_customer_file_for_matching->deleteFile($file_id); $this->session->data['warning'] = $this->language->get('text_csv_file_without_header'); return false; } } // transformam in litere mari $header = array(); foreach ($csv_header as $value) { $header[] = strtoupper(trim($value)); } // in cazul in care csv -ul are header, verificam daca headerul contine datele obligatorii specificate webservice-ului foreach ($this->mandatory_data as $value) { if (!in_array($value, $header)) { $csv_have_header = 0; break; } } if ($csv_have_header == 0) { // se sterge din baza de date si de pe server $this->model_account_customer_file_for_matching->deleteFile($file_id); $this->session->data['warning'] = $this->language->get('text_csv_file_not_contain_mandatory_data'); return false; } else { return true; } } } }
<?php $indent = 5; /* Number of spaces to indent per level */ $xml = new XMLReader(); $xml->open("dtdexample.xml"); $xml->setParserProperty(XMLREADER::LOADDTD, TRUE); $xml->setParserProperty(XMLREADER::VALIDATE, TRUE); while ($xml->read()) { /* Print node name indenting it based on depth and $indent var */ print str_repeat(" ", $xml->depth * $indent) . $xml->name . "\n"; if ($xml->hasAttributes) { $attCount = $xml->attributeCount; print str_repeat(" ", $xml->depth * $indent) . " Number of Attributes: " . $xml->attributeCount . "\n"; } } print "\n\nValid:\n"; var_dump($xml->isValid());
/** * Create a new XML node. * * \param XMLReader $reader * XML reader object that will be used to create * this node. * * \param bool $validate * Whether an exception should be raised (\c true) * or not (\c false) if the current node is not valid. * * \param bool $subtrees * Whether to explore subtrees (\c true) or not (\c false). */ public function __construct(\XMLReader $reader, $validate, $subtrees) { do { // We must silence read()/next() as old PHPs (5.3.x) emit warnings // which get caught by PHPUnit and other custom error handlers // when the methods fail and this is known to cause some issues. if ($subtrees && !@$reader->read() || !$subtrees && !@$reader->next()) { $error = libxml_get_last_error(); // We reached the end of the document. // This is not an error per-se, // but it causes read() to fail anyway. // We throw a special error which gets caught // and dealt with appropriately by the caller. if ($error === false) { throw new \InvalidArgumentException('End of document'); } switch ($error->code) { case self::XML_ERR_UNKNOWN_ENCODING: case self::XML_ERR_UNSUPPORTED_ENCODING: throw new \fpoirotte\XRL\Faults\UnsupportedEncodingException(); // Internal & memory errors are too hard to recreate // and are thus excluded from code coverage analysis. // @codeCoverageIgnoreStart // Internal & memory errors are too hard to recreate // and are thus excluded from code coverage analysis. // @codeCoverageIgnoreStart case self::XML_ERR_INTERNAL_ERROR: case self::XML_ERR_NO_MEMORY: throw new \fpoirotte\XRL\Faults\InternalErrorException(); // @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd case self::XML_ERR_INVALID_CHAR: throw new \fpoirotte\XRL\Faults\InvalidCharacterException(); // Generic error handling. // Generic error handling. default: throw new \fpoirotte\XRL\Faults\NotWellFormedException(); } } if ($validate && !$reader->isValid()) { throw new \fpoirotte\XRL\Faults\InvalidXmlRpcException(); } $subtrees = true; } while ($reader->nodeType === \XMLReader::SIGNIFICANT_WHITESPACE); $fields = array('isEmptyElement', 'localName', 'namespaceURI', 'nodeType', 'value'); $this->properties = array(); foreach ($fields as $field) { $this->properties[$field] = $reader->{$field}; } $name = $reader->localName; if ($reader->namespaceURI !== '') { $name = '{' . $reader->namespaceURI . '}' . $name; } $this->properties['name'] = $name; }
/** * validates the xml string * * @return boolean true if the xml string is valid * @access private */ private function validateString($xml_feed_string) { $reader = new XMLReader(); $reader->XML($xml_feed_string); if ($reader->setRelaxNGSchema(RELAX_NG_IMPORT_SCHEMA)) { while ($reader->read()) { } } if (!$reader->isValid()) { return false; } $reader->close(); return true; }
/** * Process a node from the XML Map * It is permitted for the XML to just define one or more tables without fields (when the 'use headers' option is used) * * Note: Calls itself recursively to process a tree * * @return bool returns true if all fine else false */ private function _getXmlNode($node_content, $current_record, $xml_path) { $jinput = JFactory::getApplication()->input; $csvilog = $jinput->get('csvilog', null, null); $current_node = ''; $xml_schema = new XMLReader(); /** * Add a wrapper to make the XML viable and ensure that self closing tags contain a space before the '/>' * The XML may still be invalid but that's down to what the user entered */ $node_content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<da_root>" . $node_content . '</da_root>'; $xml_schema->XML($node_content); // XML file to table map is valid XML - construct the arrays used in file extraction $use_read = true; // The XML could only be validated against a DTD if the syntax of the XML used for the map is made more complex $validate_xml = false; if ($validate_xml == true) { // Note: When the DTD is external, the property value must be set before the first read() $xml_schema->setParserProperty(XMLReader::VALIDATE, true); } while ($use_read ? $xml_schema->read() : $xml_schema->next()) { // Validation checking disabled because a DTD (or RELAX NG) schema is required. if ($validate_xml == true) { if ($xml_schema->isValid() == false) { $xml_schema->close(); return false; } } // Default to a reading a single node in the next loop $use_read = true; // Ignore any node associated with the root if ($xml_schema->name == 'da_root') { continue; } // Process start elements if ($xml_schema->nodeType == XMLReader::ELEMENT) { $self_closing = $xml_schema->isEmptyElement; // Ready to add a new node - but only if the last node was closed if (!empty($current_node)) { $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_XML_NODE_UNCLOSED', $current_node)); return false; } // A new node was found - Check whether this is a new record type if (empty($current_record)) { // New record type // Check for a self-closing node $self_closing = $xml_schema->isEmptyElement; $current_record = strtolower($xml_schema->name); $this->_xml_records[] = strtolower($current_record); // Store any attributes while ($xml_schema->moveToNextAttribute()) { // Note1: $xml_schema->hasValue only indicates whether the element can have a value, not whether it does // Note2: empty($xml_schema->value) always return true, regardless of the actual value $value = $xml_schema->value; if (!empty($value)) { if ($this->_isXmlFieldNameValid($xml_schema->value)) { $this->_xml_schema[$current_record]['attrs'][strtolower($xml_schema->name)] = trim($xml_schema->value); } else { $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_XML_FILE_MAP_NO_REFERENCE', $xml_schema->value)); $xml_schema->close(); return false; } } } // Check for a self-closing node if ($self_closing == true) { $current_record = ''; } } else { // New field type $current_node = strtolower($xml_schema->name); $current_path = $this->_getXmlNodePath($xml_path, $current_node); // Store any attributes while ($xml_schema->moveToNextAttribute()) { // Note1: $xml_schema->hasValue only indicates whether the element can have a value, not whether it does // Note2: empty($xml_schema->value) always return true, regardless of the actual value $value = $xml_schema->value; if (!empty($value)) { if ($this->_isXmlFieldNameValid($xml_schema->value)) { $this->_xml_schema[$current_record]['nodes'][$current_path]['attrs'][strtolower($xml_schema->name)] = trim($xml_schema->value); } else { $csvilog->AddStats('incorrect', JText::_('COM_CSVI_XML_FILE_MAP_NO_REFERENCE', $xml_schema->value)); $xml_schema->close(); return false; } } } $sub_node_content = $xml_schema->readInnerXML(); // Check whether there are any lower level nodes if (strstr($sub_node_content, '<') === false) { /** * Content has no embedded nodes - Assume a field name * Note: An empty node gives a blank field name which indicates an unwanted node * that is being mapped to prevent errors when processing the file */ if ($this->_isXmlFieldNameValid($sub_node_content)) { $this->_xml_schema[$current_record]['nodes'][$current_path]['field'] = trim($sub_node_content); } else { $this->_xml_schema[$current_record]['nodes'][$current_path]['field'] = ''; } } else { // There are embedded nodes - go down another level // Indicate a 'group' node by storing an empty field name $this->_xml_schema[$current_record]['nodes'][$current_path]['field'] = ''; // Push the node name to the path stack $this->_pushXmlNodePath($xml_path, $current_node); if ($this->_getXmlNode($sub_node_content, $current_record, $xml_path) == false) { $xml_schema->close(); return false; } // At the next read, skip to the next node at this level $use_read = false; // Close the node $current_node = ''; // Pop the last item off the path stack $this->_popXmlNodePath($xml_path); } // Check for a self-closing node if ($self_closing == true) { $current_node = ''; } } } else { if ($xml_schema->nodeType == XMLReader::END_ELEMENT) { // End of node found // Check for end of record if (!empty($current_record) && strtolower($xml_schema->name) == $current_record) { // End of record detected $current_record = ''; } else { if (!empty($current_node) && strtolower($xml_schema->name) == $current_node) { // End of current node detected $current_node = ''; } } } } } $xml_schema->close(); // Node not terminated if (!empty($current_node)) { $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_XML_NODE_NOT_CLOSED', $current_node)); return false; } if (empty($this->_xml_records)) { $csvilog->AddStats('incorrect', JText::_('COM_CSVI_XML_NO_RECORDS_DEFINED')); return false; } return true; }
<?php $indent = 5; /* Number of spaces to indent per level */ $reader = new XMLReader(); $reader->open('relaxNG.xml'); /* Example setting relaxNG using string: $reader->setRelaxNGSchemaSource(file_get_contents('relaxNG.rng')); */ if ($reader->setRelaxNGSchema('relaxNG.rng')) { while ($reader->read()) { /* Print node name indenting it based on depth and $indent var */ print str_repeat(" ", $reader->depth * $indent) . $reader->name . "\n"; } } print "\n"; if (!$reader->isValid()) { print "Document is not valid\n"; } else { print "Document is valid\n"; }
public function testScrape() { $parser = new \Seld\JsonLint\JsonParser(); $googleScraper = Builder::create($this->engines[0], array(array('foo', 'baz'), 'google')); $outDir = $googleScraper->getOutDir(); $this->assertFalse($googleScraper->scrape('bar')); $this->assertFalse($googleScraper->scrape('baz', 100)); $this->assertFalse($googleScraper->scrape('baz', 1, 'baz')); $this->assertFalse($googleScraper->scrape('baz', 1, true, 'foobad')); $this->assertFalse($googleScraper->scrape('baz', 1, true, 'UTC', 'faz')); $this->assertFalse($googleScraper->serialize('json')); $this->assertTrue($googleScraper->scrape('foo', 2, true, 'Europe/Berlin')); $this->assertCount(2, $googleScraper->getFetchedPages()); $this->assertCount(1, $googleScraper->getKeywords()); $this->assertTrue($googleScraper->scrape('baz', 2, true)); $this->assertCount(4, $googleScraper->getFetchedPages()); $this->assertCount(0, $googleScraper->getKeywords()); $this->assertFalse($googleScraper->scrapeAll()); $this->assertTrue($googleScraper->addKeywords(array('foobaz', 'foobar'))); $this->assertTrue($googleScraper->scrapeAll(2, true, 'America/Los_Angeles')); $this->assertCount(8, $googleScraper->getFetchedPages()); $this->assertCount(0, $googleScraper->getKeywords()); $this->assertFalse($googleScraper->serialize('baz')); $this->assertTrue($googleScraper->serialize('json', true)); $this->assertCount(0, $googleScraper->getFetchedPages()); $this->assertCount(8, $googleScraper->getSerializedPages()); $toCheck = array_map('Franzip\\SerpScraper\\Helpers\\FileSystemHelper::generateFileName', array_keys($googleScraper->getSerializedPages())); $this->assertTrue($googleScraper->save(true)); for ($i = 0; $i < count($toCheck); $i++) { $json = file_get_contents($outDir . DIRECTORY_SEPARATOR . $toCheck[$i]); $this->assertNull($parser->lint($json)); } $this->assertTrue($googleScraper->addKeywords(array('foo bad'))); $this->assertTrue($googleScraper->scrapeAll(3, true)); $this->assertCount(3, $googleScraper->getFetchedPages()); $this->assertTrue($googleScraper->serialize('xml', true)); $this->assertCount(0, $googleScraper->getFetchedPages()); $this->assertCount(3, $googleScraper->getSerializedPages()); $toCheck = array_map('Franzip\\SerpScraper\\Helpers\\FileSystemHelper::generateFileName', array_keys($googleScraper->getSerializedPages())); $this->assertTrue($googleScraper->save(true)); for ($i = 0; $i < count($toCheck); $i++) { $xml = new \XMLReader(); $xml->open($outDir . DIRECTORY_SEPARATOR . $toCheck[$i]); $xml->setParserProperty(\XMLReader::VALIDATE, true); $this->assertTrue($xml->isValid()); } $askScraper = Builder::create($this->engines[1], array(array('foo', 'baz'), 'ask')); $outDir = $askScraper->getOutDir(); $this->assertFalse($askScraper->scrape('bar')); $this->assertFalse($askScraper->scrape('baz', 100)); $this->assertFalse($askScraper->scrape('baz', 1, 'baz')); $this->assertFalse($askScraper->scrape('baz', 1, true, 'foobad')); $this->assertFalse($askScraper->scrape('baz', 1, true, 'UTC', 'faz')); $this->assertTrue($askScraper->scrape('foo', 2, true, 'Europe/Rome')); $this->assertCount(2, $askScraper->getFetchedPages()); $this->assertCount(1, $askScraper->getKeywords()); $this->assertTrue($askScraper->scrape('baz', 2, true)); $this->assertCount(4, $askScraper->getFetchedPages()); $this->assertCount(0, $askScraper->getKeywords()); $this->assertFalse($askScraper->scrapeAll()); $this->assertTrue($askScraper->addKeywords(array('foobaz', 'foobar'))); $this->assertTrue($askScraper->scrapeAll(2, true, 'America/Los_Angeles')); $this->assertCount(8, $askScraper->getFetchedPages()); $this->assertCount(0, $askScraper->getKeywords()); $this->assertFalse($askScraper->serialize('baz')); $this->assertTrue($askScraper->serialize('xml', true)); $this->assertCount(0, $askScraper->getFetchedPages()); $this->assertCount(8, $askScraper->getSerializedPages()); $toCheck = array_map('Franzip\\SerpScraper\\Helpers\\FileSystemHelper::generateFileName', array_keys($askScraper->getSerializedPages())); $this->assertTrue($askScraper->save(true)); $this->assertCount(0, $askScraper->getSerializedPages()); for ($i = 0; $i < count($toCheck); $i++) { $xml = new \XMLReader(); $xml->open($outDir . DIRECTORY_SEPARATOR . $toCheck[$i]); $xml->setParserProperty(\XMLReader::VALIDATE, true); $this->assertTrue($xml->isValid()); } $this->assertTrue($askScraper->addKeywords(array('foobaz'))); $this->assertTrue($askScraper->scrapeAll(3, true)); $this->assertTrue($askScraper->serialize('json', true)); $toCheck = array_map('Franzip\\SerpScraper\\Helpers\\FileSystemHelper::generateFileName', array_keys($askScraper->getSerializedPages())); $this->assertTrue($askScraper->save(true)); for ($i = 0; $i < count($toCheck); $i++) { $json = file_get_contents($outDir . DIRECTORY_SEPARATOR . $toCheck[$i]); $this->assertNull($parser->lint($json)); } $bingScraper = Builder::create($this->engines[2], array(array('foo', 'baz'), 'bing')); $outDir = $bingScraper->getOutDir(); $this->assertFalse($bingScraper->scrape('bar')); $this->assertFalse($bingScraper->scrape('baz', 100)); $this->assertFalse($bingScraper->scrape('baz', 1, 'baz')); $this->assertFalse($bingScraper->scrape('baz', 1, true, 'foobad')); $this->assertFalse($bingScraper->scrape('baz', 1, true, 'UTC', 'faz')); $this->assertFalse($bingScraper->serialize('json')); $this->assertTrue($bingScraper->scrape('foo', 2, true, 'Europe/Berlin')); $this->assertCount(2, $bingScraper->getFetchedPages()); $this->assertCount(1, $bingScraper->getKeywords()); $this->assertTrue($bingScraper->scrape('baz', 2, true)); $this->assertCount(4, $bingScraper->getFetchedPages()); $this->assertCount(0, $bingScraper->getKeywords()); $this->assertFalse($bingScraper->scrapeAll()); $this->assertTrue($bingScraper->addKeywords(array('foobaz', 'foobar'))); $this->assertTrue($bingScraper->scrapeAll(2, true, 'America/Los_Angeles')); $this->assertCount(8, $bingScraper->getFetchedPages()); $this->assertCount(0, $bingScraper->getKeywords()); $this->assertFalse($bingScraper->serialize('baz')); $this->assertTrue($bingScraper->serialize('json', true)); $this->assertCount(0, $bingScraper->getFetchedPages()); $this->assertCount(8, $bingScraper->getSerializedPages()); $toCheck = array_map('Franzip\\SerpScraper\\Helpers\\FileSystemHelper::generateFileName', array_keys($bingScraper->getSerializedPages())); $this->assertTrue($bingScraper->save(true)); for ($i = 0; $i < count($toCheck); $i++) { $json = file_get_contents($outDir . DIRECTORY_SEPARATOR . $toCheck[$i]); $this->assertNull($parser->lint($json)); } $this->assertTrue($bingScraper->addKeywords(array('foo bad'))); $this->assertTrue($bingScraper->scrapeAll(2, true)); $this->assertCount(2, $bingScraper->getFetchedPages()); $this->assertTrue($bingScraper->serialize('xml', true)); $this->assertCount(0, $bingScraper->getFetchedPages()); $this->assertCount(2, $bingScraper->getSerializedPages()); $toCheck = array_map('Franzip\\SerpScraper\\Helpers\\FileSystemHelper::generateFileName', array_keys($bingScraper->getSerializedPages())); $this->assertTrue($bingScraper->save(true)); for ($i = 0; $i < count($toCheck); $i++) { $xml = new \XMLReader(); $xml->open($outDir . DIRECTORY_SEPARATOR . $toCheck[$i]); $xml->setParserProperty(\XMLReader::VALIDATE, true); $this->assertTrue($xml->isValid()); } $yahooScraper = Builder::create($this->engines[3], array(array('foo', 'baz'), 'yahoo')); $outDir = $yahooScraper->getOutDir(); $this->assertFalse($yahooScraper->scrape('bar')); $this->assertFalse($yahooScraper->scrape('baz', 100)); $this->assertFalse($yahooScraper->scrape('baz', 1, 'baz')); $this->assertFalse($yahooScraper->scrape('baz', 1, true, 'foobad')); $this->assertFalse($yahooScraper->scrape('baz', 1, true, 'UTC', 'faz')); $this->assertTrue($yahooScraper->scrape('foo', 2, true, 'Europe/Rome')); $this->assertCount(2, $yahooScraper->getFetchedPages()); $this->assertCount(1, $yahooScraper->getKeywords()); $this->assertTrue($yahooScraper->scrape('baz', 2, true)); $this->assertCount(4, $yahooScraper->getFetchedPages()); $this->assertCount(0, $yahooScraper->getKeywords()); $this->assertFalse($yahooScraper->scrapeAll()); $this->assertTrue($yahooScraper->addKeywords(array('foobaz', 'foobar'))); $this->assertTrue($yahooScraper->scrapeAll(2, true, 'America/Los_Angeles')); $this->assertCount(8, $yahooScraper->getFetchedPages()); $this->assertCount(0, $yahooScraper->getKeywords()); $this->assertFalse($yahooScraper->serialize('baz')); $this->assertTrue($yahooScraper->serialize('xml', true)); $this->assertCount(0, $yahooScraper->getFetchedPages()); $this->assertCount(8, $yahooScraper->getSerializedPages()); $toCheck = array_map('Franzip\\SerpScraper\\Helpers\\FileSystemHelper::generateFileName', array_keys($yahooScraper->getSerializedPages())); $this->assertTrue($yahooScraper->save(true)); $this->assertCount(0, $yahooScraper->getSerializedPages()); for ($i = 0; $i < count($toCheck); $i++) { $xml = new \XMLReader(); $xml->open($outDir . DIRECTORY_SEPARATOR . $toCheck[$i]); $xml->setParserProperty(\XMLReader::VALIDATE, true); $this->assertTrue($xml->isValid()); } $this->assertTrue($yahooScraper->addKeywords(array('foobaz'))); $this->assertTrue($yahooScraper->scrapeAll(3, true)); $this->assertTrue($yahooScraper->serialize('json', true)); $toCheck = array_map('Franzip\\SerpScraper\\Helpers\\FileSystemHelper::generateFileName', array_keys($yahooScraper->getSerializedPages())); $this->assertTrue($yahooScraper->save(true)); for ($i = 0; $i < count($toCheck); $i++) { $json = file_get_contents($outDir . DIRECTORY_SEPARATOR . $toCheck[$i]); $this->assertNull($parser->lint($json)); } }
public function isValid() { return parent::isValid(); }
/** * Reads the configuration file and creates the class attributes * */ protected function initialize() { $reader = new XMLReader(); $reader->open(parent::getConfigFilePath()); $reader->setRelaxNGSchemaSource(self::WURFL_CONF_SCHEMA); libxml_use_internal_errors(TRUE); while ($reader->read()) { if (!$reader->isValid()) { throw new Exception(libxml_get_last_error()->message); } $name = $reader->name; switch ($reader->nodeType) { case XMLReader::ELEMENT: $this->_handleStartElement($name); break; case XMLReader::TEXT: $this->_handleTextElement($reader->value); break; case XMLReader::END_ELEMENT: $this->_handleEndElement($name); break; } } $reader->close(); if (isset($this->cache["dir"])) { $this->logDir = $this->cache["dir"]; } }