/** * Normalize an xml code resolving some common errors. * * @param string $xml The xml code to fix * @param string $filename The name of the file from which the xml code has been read from * * @throws Exception Throws an Exception in case of problems (for instance if the xml string is not a valid XML document) * * @return string */ public static function normalizeString($xml, $filename = '') { $doc = Utilities::stringToDOMDocument($xml, $filename); $xslt = static::getXsltDOMDocument(); $error = null; $preUseInternalErrors = libxml_use_internal_errors(true); try { $processor = new XSLTProcessor(); $processor->importStyleSheet($xslt); $normalized = $processor->transformToXml($doc); if ($normalized === false) { $error = Utilities::explainLibXmlErrors($filename); } } catch (Exception $x) { libxml_use_internal_errors($preUseInternalErrors); libxml_clear_errors(); throw $x; } if (isset($error)) { throw new Exception($error); } $doc = Utilities::stringToDOMDocument($normalized, $filename); $doc->formatOutput = true; return $doc->saveXML(); }
/** * Check if an xml code satisfy the schema. * * @param string $xml The xml code to verify * @param string $filename The name of the file from which the xml code has been read from * * @return string[]|null Return null of all is ok, a list of errors otherwise */ public static function checkString($xml, $filename = '') { $doc = Utilities::stringToDOMDocument($xml, $filename); $errors = null; $preUseInternalErrors = libxml_use_internal_errors(true); try { if (!$doc->schemaValidateSource(static::getSchema())) { $errors = Utilities::explainLibXmlErrors($filename); } } catch (Exception $x) { libxml_use_internal_errors($preUseInternalErrors); libxml_clear_errors(); throw $x; } libxml_use_internal_errors($preUseInternalErrors); return $errors; }
/** * @param string|SimpleXMLElement $xml * @param AbstractPlatform $platform * @param bool $checkXml * @param bool $normalizeXml * @param callable|null $tableFilter * * @throws Exception * * @return Schema */ public static function fromDocument($xml, AbstractPlatform $platform, $checkXml = true, $normalizeXml = false, $tableFilter = null) { if ($checkXml || $normalizeXml) { if (is_a($xml, '\\SimpleXMLElement')) { $xml = $xml->asXML(); } if ($normalizeXml) { $xml = Normalizer::normalizeString($xml); } if ($checkXml) { $errors = Checker::checkString($xml); if (isset($errors)) { throw new Exception(implode("\n", $errors)); } } } if (is_a($xml, '\\SimpleXMLElement')) { $xDoc = $xml; } else { $preUseInternalErrors = libxml_use_internal_errors(true); libxml_clear_errors(); $xDoc = @simplexml_load_string($xml); if (!is_object($xDoc)) { $errors = Utilities::explainLibXmlErrors(); libxml_clear_errors(); libxml_use_internal_errors($preUseInternalErrors); throw new Exception(implode("\n", $errors)); } libxml_use_internal_errors($preUseInternalErrors); } $schema = new Schema(); foreach ($xDoc->table as $xTable) { if (isset($tableFilter) && $tableFilter((string) $xTable['name']) === false) { continue; } static::parseTable($schema, $xTable, $platform); } return $schema; }