Ejemplo n.º 1
0
 /**
  * Validate the document against a schema.
  *
  * @param string $filename An optional filename of a given schema the document should validate against.
  * @throws \qtism\data\storage\xml\XmlStorageException
  * @throws \InvalidArgumentException
  */
 public function schemaValidate($filename = '')
 {
     if (empty($filename)) {
         $filename = XmlUtils::getSchemaLocation($this->getVersion());
     }
     if (is_readable($filename)) {
         $oldErrorConfig = libxml_use_internal_errors(true);
         $doc = $this->getDomDocument();
         if (@$doc->schemaValidate($filename) === false) {
             $libXmlErrors = libxml_get_errors();
             $formattedErrors = self::formatLibXmlErrors($libXmlErrors);
             libxml_clear_errors();
             libxml_use_internal_errors($oldErrorConfig);
             $msg = "The document could not be validated with XML Schema:\n{$formattedErrors}";
             throw new XmlStorageException($msg, XmlStorageException::XSD_VALIDATION, null, new LibXmlErrorCollection($libXmlErrors));
         }
     } else {
         $msg = "Schema '{$filename}' cannot be read. Does this file exist? Is it readable?";
         throw new InvalidArgumentException($msg);
     }
 }