Checks whether the file is valid according to an XML schema.

@Then /^the file "([^"]+)" should be a valid document according to "([^"]+)"$/

@param string $xmlFile
@param string $schemaPath relative to features/bootstrap/schema
425601
public function xmlShouldBeValid($xmlFile, $schemaPath)
    {
        $dom = new DomDocument();
        $dom->load($this->workingDir . '/' . $xmlFile);

        $dom->schemaValidate(__DIR__ . '/schema/' . $schemaPath);
    }
370187
public function validate(\DOMDocument $document, $xsdPath)
    {
        $state = libxml_use_internal_errors(true);
        $result = $document->schemaValidate($xsdPath);
        $this->errors = $this->extractErrors();
        libxml_use_internal_errors($state);

        return $result;
    }
860783
private function validateSchema(Schema $schema)
    {
        $this->jsonValidator->check(
            json_decode(json_encode($schema->jsonSerialize())),
            json_decode(file_get_contents($schema->getSchema()))
        );

        return $this->jsonValidator->isValid();
    }
1138478
def SchemaValidate(self, xsd):
        
        ret = libxml2mod.xmlTextReaderSchemaValidate(self._o, xsd)
        return ret
625044
public function loadXmlFile($path, $parse = false)
    {
        $this->preserveWhiteSpace = false;
        $bool = file_exists($path);

        if (!$bool) {
            $this->error = 'file_not_exists';
            return false;
        }

        $bool = @$this->load($path);
        if (!$bool) {
            $this->error = 'loading_file_error';
            return false;
        }

        if ($parse && !@$this->validate()) {
            $this->error = 'parse_file_error';
            return false;
        }

        return true;
    }
1017922
public function validate(string $metaDataFilePath, string $metaDataVersion, array $metaData)
    {
        $this->currentValidationMetaDataVersion = $metaDataVersion;
        $this->metaDataFilePath = $metaDataFilePath;

        $supportedMetaDataKeys = $this->metaDataSchemataProvider->getFlippedMetaDataSchemaForVersion($this->currentValidationMetaDataVersion);
        foreach ($metaData as $metaDataKey => $value) {
            if (is_scalar($value)) {
                $this->validateMetaDataKey($supportedMetaDataKeys, (string) $metaDataKey);
            } elseif (true === \is_array($value)) {
                $this->validateMetaDataSection($supportedMetaDataKeys, $metaDataKey, $value);
            } else {
                throw new UnsupportedMetaDataValueTypeException('The value type "' . \gettype($value) . '" is not supported in metadata version ' . $this->currentValidationMetaDataVersion);
            }
        }
    }
407522
public function fileXmlShouldBeLike($path, PyStringNode $text)
    {
        $path = $this->workingDir . '/' . $path;
        Assert::assertFileExists($path);

        $fileContent = trim(file_get_contents($path));

        $fileContent = preg_replace('/time="(.*)"/', 'time="-IGNORE-VALUE-"', $fileContent);

        $dom = new DOMDocument();
        $dom->loadXML($text);
        $dom->formatOutput = true;

        Assert::assertEquals(trim($dom->saveXML(null, LIBXML_NOEMPTYTAG)), $fileContent);
    }
721415
public function check_database_schema(xmldb_structure $schema, array $options = null) {
        $alloptions = array(
            'extratables' => true,
            'missingtables' => true,
            'extracolumns' => true,
            'missingcolumns' => true,
            'changedcolumns' => true,
        );

        $typesmap = array(
            'I' => XMLDB_TYPE_INTEGER,
            'R' => XMLDB_TYPE_INTEGER,
            'N' => XMLDB_TYPE_NUMBER,
            'F' => XMLDB_TYPE_NUMBER, // Nobody should be using floats!
            'C' => XMLDB_TYPE_CHAR,
            'X' => XMLDB_TYPE_TEXT,
            'B' => XMLDB_TYPE_BINARY,
            'T' => XMLDB_TYPE_TIMESTAMP,
            'D' => XMLDB_TYPE_DATETIME,
        );

        $options = (array)$options;
        $options = array_merge($alloptions, $options);

        // Note: the error descriptions are not supposed to be localised,
        //       it is intended for developers and skilled admins only.
        $errors = array();

        /** @var string[] $dbtables */
        $dbtables = $this->mdb->get_tables(false);
        /** @var xmldb_table[] $tables */
        $tables = $schema->getTables();

        foreach ($tables as $table) {
            $tablename = $table->getName();

            if ($options['missingtables']) {
                // Missing tables are a fatal problem.
                if (empty($dbtables[$tablename])) {
                    $errors[$tablename][] = "table is missing";
                    continue;
                }
            }

            /** @var database_column_info[] $dbfields */
            $dbfields = $this->mdb->get_columns($tablename, false);
            /** @var xmldb_field[] $fields */
            $fields = $table->getFields();

            foreach ($fields as $field) {
                $fieldname = $field->getName();
                if (empty($dbfields[$fieldname])) {
                    if ($options['missingcolumns']) {
                        // Missing columns are a fatal problem.
                        $errors[$tablename][] = "column '$fieldname' is missing";
                    }
                } else if ($options['changedcolumns']) {
                    $dbfield = $dbfields[$fieldname];

                    if (!isset($typesmap[$dbfield->meta_type])) {
                        $errors[$tablename][] = "column '$fieldname' has unsupported type '$dbfield->meta_type'";
                    } else {
                        $dbtype = $typesmap[$dbfield->meta_type];
                        $type = $field->getType();
                        if ($type == XMLDB_TYPE_FLOAT) {
                            $type = XMLDB_TYPE_NUMBER;
                        }
                        if ($type != $dbtype) {
                            if ($expected = array_search($type, $typesmap)) {
                                $errors[$tablename][] = "column '$fieldname' has incorrect type '$dbfield->meta_type', expected '$expected'";
                            } else {
                                $errors[$tablename][] = "column '$fieldname' has incorrect type '$dbfield->meta_type'";
                            }
                        } else {
                            if ($field->getNotNull() != $dbfield->not_null) {
                                if ($field->getNotNull()) {
                                    $errors[$tablename][] = "column '$fieldname' should be NOT NULL ($dbfield->meta_type)";
                                } else {
                                    $errors[$tablename][] = "column '$fieldname' should allow NULL ($dbfield->meta_type)";
                                }
                            }
                            if ($dbtype == XMLDB_TYPE_TEXT) {
                                // No length check necessary - there is one size only now.

                            } else if ($dbtype == XMLDB_TYPE_NUMBER) {
                                if ($field->getType() == XMLDB_TYPE_FLOAT) {
                                    // Do not use floats in any new code, they are deprecated in XMLDB editor!

                                } else if ($field->getLength() != $dbfield->max_length or $field->getDecimals() != $dbfield->scale) {
                                    $size = "({$field->getLength()},{$field->getDecimals()})";
                                    $dbsize = "($dbfield->max_length,$dbfield->scale)";
                                    $errors[$tablename][] = "column '$fieldname' size is $dbsize, expected $size ($dbfield->meta_type)";
                                }

                            } else if ($dbtype == XMLDB_TYPE_CHAR) {
                                // This is not critical, but they should ideally match.
                                if ($field->getLength() != $dbfield->max_length) {
                                    $errors[$tablename][] = "column '$fieldname' length is $dbfield->max_length, expected {$field->getLength()} ($dbfield->meta_type)";
                                }

                            } else if ($dbtype == XMLDB_TYPE_INTEGER) {
                                // Integers may be bigger in some DBs.
                                $length = $field->getLength();
                                if ($length > 18) {
                                    // Integers are not supposed to be bigger than 18.
                                    $length = 18;
                                }
                                if ($length > $dbfield->max_length) {
                                    $errors[$tablename][] = "column '$fieldname' length is $dbfield->max_length, expected at least {$field->getLength()} ($dbfield->meta_type)";
                                }

                            } else if ($dbtype == XMLDB_TYPE_BINARY) {
                                // Ignore binary types.
                                continue;

                            } else if ($dbtype == XMLDB_TYPE_TIMESTAMP) {
                                $errors[$tablename][] = "column '$fieldname' is a timestamp, this type is not supported ($dbfield->meta_type)";
                                continue;

                            } else if ($dbtype == XMLDB_TYPE_DATETIME) {
                                $errors[$tablename][] = "column '$fieldname' is a datetime, this type is not supported ($dbfield->meta_type)";
                                continue;

                            } else {
                                // Report all other unsupported types as problems.
                                $errors[$tablename][] = "column '$fieldname' has unknown type ($dbfield->meta_type)";
                                continue;
                            }

                            // Note: The empty string defaults are a bit messy...
                            if ($field->getDefault() != $dbfield->default_value) {
                                $default = is_null($field->getDefault()) ? 'NULL' : $field->getDefault();
                                $dbdefault = is_null($dbfield->default_value) ? 'NULL' : $dbfield->default_value;
                                $errors[$tablename][] = "column '$fieldname' has default '$dbdefault', expected '$default' ($dbfield->meta_type)";
                            }
                        }
                    }
                }
                unset($dbfields[$fieldname]);
            }

            // Check for extra columns (indicates unsupported hacks) - modify install.xml if you want to pass validation.
            foreach ($dbfields as $fieldname => $dbfield) {
                if ($options['extracolumns']) {
                    $errors[$tablename][] = "column '$fieldname' is not expected ($dbfield->meta_type)";
                }
            }
            unset($dbtables[$tablename]);
        }

        if ($options['extratables']) {
            // Look for unsupported tables - local custom tables should be in /local/xxxx/db/install.xml file.
            // If there is no prefix, we can not say if table is ours, sorry.
            if ($this->generator->prefix !== '') {
                foreach ($dbtables as $tablename => $unused) {
                    if (strpos($tablename, 'pma_') === 0) {
                        // Ignore phpmyadmin tables.
                        continue;
                    }
                    if (strpos($tablename, 'test') === 0) {
                        // Legacy simple test db tables need to be eventually removed,
                        // report them as problems!
                        $errors[$tablename][] = "table is not expected (it may be a leftover after Simpletest unit tests)";
                    } else {
                        $errors[$tablename][] = "table is not expected";
                    }
                }
            }
        }

        return $errors;
    }
949566
protected function _queryAssertion($assertionXpath)
    {
        if ($this->encrypted) {
            $xpath = new DOMXPath($this->decryptedDocument);
        } else {
            $xpath = new DOMXPath($this->document);
        }

        $xpath->registerNamespace('samlp', OneLogin_Saml2_Constants::NS_SAMLP);
        $xpath->registerNamespace('saml', OneLogin_Saml2_Constants::NS_SAML);
        $xpath->registerNamespace('ds', OneLogin_Saml2_Constants::NS_DS);
        $xpath->registerNamespace('xenc', OneLogin_Saml2_Constants::NS_XENC);

        $assertionNode = '/samlp:Response/saml:Assertion';
        $signatureQuery = $assertionNode . '/ds:Signature/ds:SignedInfo/ds:Reference';
        $assertionReferenceNode = $xpath->query($signatureQuery)->item(0);
        if (!$assertionReferenceNode) {
            // is the response signed as a whole?
            $signatureQuery = '/samlp:Response/ds:Signature/ds:SignedInfo/ds:Reference';
            $responseReferenceNode = $xpath->query($signatureQuery)->item(0);
            if ($responseReferenceNode) {
                $uri = $responseReferenceNode->attributes->getNamedItem('URI')->nodeValue;
                if (empty($uri)) {
                    $id = $responseReferenceNode->parentNode->parentNode->parentNode->attributes->getNamedItem('ID')->nodeValue;
                } else {
                    $id = substr($responseReferenceNode->attributes->getNamedItem('URI')->nodeValue, 1);
                }
                $nameQuery = "/samlp:Response[@ID='$id']/saml:Assertion" . $assertionXpath;
            } else {
                $nameQuery = "/samlp:Response/saml:Assertion" . $assertionXpath;
            }
        } else {
            $uri = $assertionReferenceNode->attributes->getNamedItem('URI')->nodeValue;
            if (empty($uri)) {
                $id = $assertionReferenceNode->parentNode->parentNode->parentNode->attributes->getNamedItem('ID')->nodeValue;
            } else {
                $id = substr($assertionReferenceNode->attributes->getNamedItem('URI')->nodeValue, 1);
            }
            $nameQuery = $assertionNode."[@ID='$id']" . $assertionXpath;
        }

        return $xpath->query($nameQuery);
    }
139613
protected function loadElementsFromXmlFileWithPath($xmlFile, $nodePath)
    {
        $xml = file_get_contents($xmlFile);
        // Remove whitespaces between closing tag and opening tag.
        $xml = preg_replace('/>\s+</', '><', $xml);

        $doc = new \DOMDocument();
        $doc->loadXML($xml);

        $xpath = new \DOMXpath($doc);
        $elements = $xpath->query($nodePath);

        return $elements;
    }
326688
protected function validate()
    {
        if (empty($this->schemaFilesets)) {
            throw new BuildException("You must specify a fileset of XML schemas.", $this->getLocation());
        }

        // Make sure the output directory is set.
        if ($this->outputDirectory === null) {
            throw new BuildException("The output directory needs to be defined!", $this->getLocation());
        }

        if ($this->validate) {
            if (!$this->xsdFile) {
                throw new BuildException("'validate' set to TRUE, but no XSD specified (use 'xsd' attribute).", $this->getLocation());
            }
        }
    }
1046310
public function seeXmlResponseMatchesXpath($xpath)
    {
        $structure = new XmlStructure($this->connectionModule->_getResponseContent());
        $this->assertTrue($structure->matchesXpath($xpath), 'xpath not matched');
    }
1230214
public static function installDB($xmlFile)
    {
        if (file_exists($xmlFile)) {
            $app = ApplicationFacade::getFacadeApplication();
            $db = $app->make(Connection::class);
            /* @var Connection $db */
            $db->beginTransaction();

            $parser = Schema::getSchemaParser(simplexml_load_file($xmlFile));
            $parser->setIgnoreExistingTables(false);
            $toSchema = $parser->parse($db);

            $fromSchema = $db->getSchemaManager()->createSchema();
            $comparator = new SchemaComparator();
            $schemaDiff = $comparator->compare($fromSchema, $toSchema);
            $saveQueries = $schemaDiff->toSaveSql($db->getDatabasePlatform());

            foreach ($saveQueries as $query) {
                $db->query($query);
            }

            $db->commit();

            $result = new stdClass();
            $result->result = false;
        } else {
            $result = false;
        }

        return $result;
    }
879571
public function loadXML(string $XMLfile): void
    {
        $file = new FileManager();
        $this->xmlData = $file->loadLittleFile($XMLfile);
    }
1170208
public static function GetXmlReaderInstanceForSourceFile($filePath, $fileName = null) {
        if (!isset($fileName)) {
            $fileName = substr($filePath, strrpos($filePath, "/") + 1);
        }
        $xmlFilesLoaded = apc_fetch(\Puzzlout\Framework\Enums\CacheKeys::XmlFilesLoaded);
        if (array_key_exists($fileName, $xmlFilesLoaded)) {
            $this->xmlReader = $xmlFilesLoaded[$fileName];
        } else {
            $this->xmlReader = isset($fileName) ?
                    new \Puzzlout\Framework\Core\XmlReader(null, $configFileName) :
                    new \Puzzlout\Framework\Core\XmlReader($filePath);
            $xmlFilesLoaded[$configFileName] = $this->xmlReader;
            apc_add(\Puzzlout\Framework\Enums\CacheKeys::XmlFilesLoaded, $xmlFilesLoaded);
        }
    }
705332
protected function validateSchema()
    {
        $schemaPath = $this->getOption('schemaPath');

        if ($schemaPath === null) {
            return;
        }

        $schemaData = json_decode(file_get_contents($schemaPath));

        if (!class_exists('\\JsonSchema\\Validator')) {
            throw new \RuntimeException(
                'If you want to validate a JSON schema, you must require package "justinrainbow/json-schema"-'
            );
        }

        $validator = new \JsonSchema\Validator();
        $validator->check($this->getContents(), $schemaData);

        if (!$validator->isValid()) {
            $errors = [];

            foreach ((array) $validator->getErrors() as $error) {
                $errors[] = ($error['property'] ? $error['property'].' : ' : '').$error['message'];
            }

            throw DecodeException::create($this->getPath(), ['errors' => $errors]);
        }
    }
878068
public static function isValidXmlFragment($xml, $doc = null)
    {
        if ($doc == null) {
            $doc = new DOMDocument("1.0", "ISO-8859-1");
        }

        $fragment = $doc->createDocumentFragment();
        @$fragment->appendXML($xml);
        $node = @$doc->appendChild($fragment);

        return $node !== false;
    }