public function testIsXmlPrefix() { $prefixes = array('a', 'ns1', 'dssn', 'OWL', 'ns.3', 'Ab', 'a-b', 'X·Y', '_', '_5c', 'ÄÖÜäöüß'); $nonPrefixes = array('1a', 'abc:', '-hallo', '.ns', 'raute#', '#a', 'a/b', '/a', 'a×', ' ', 'o ha'); foreach ($prefixes as $p) { $this->assertTrue(Erfurt_Utils::isXmlPrefix($p)); } foreach ($nonPrefixes as $p) { $this->assertFalse(Erfurt_Utils::isXmlPrefix($p)); } }
/** * Adds a prefix for the namespace URI in a given graph. * * @param Erfurt_Rdf_Model|string $graph * @param string $namespace * @param string $prefix * @throws Erfurt_Namespaces_Exception */ public function addNamespacePrefix($graph, $namespace, $prefix) { // safety $prefix = (string) $prefix; $namespace = (string) $namespace; //lowercase prefix always (for best compatibility) $prefix = strtolower($prefix); $graphPrefixes = $this->getNamespacesForGraph($graph); // check if namespace is a valid URI require_once 'Erfurt/Uri.php'; if (!Erfurt_Uri::check($namespace)) { require_once 'Erfurt/Namespaces/Exception.php'; throw new Erfurt_Namespaces_Exception("Given namespace '{$namespace}' is not a valid URI."); } // check if prefix is a valid XML name require_once 'Erfurt/Utils.php'; if (!Erfurt_Utils::isXmlPrefix($prefix)) { require_once 'Erfurt/Namespaces/Exception.php'; throw new Erfurt_Namespaces_Exception("Given prefix '{$prefix}' is not a valid XML name."); } // check if prefix matches a URI scheme (http://www.iana.org/assignments/uri-schemes.html) if (array_key_exists($prefix, $this->_reservedNames)) { require_once 'Erfurt/Namespaces/Exception.php'; throw new Erfurt_Namespaces_Exception("Reserved name '{$prefix}' cannot be used as a namespace prefix."); } // check for existence of prefixes if (array_key_exists($prefix, $graphPrefixes)) { require_once 'Erfurt/Namespaces/Exception.php'; throw new Erfurt_Namespaces_Exception("Prefix '{$prefix}' already exists."); } // check for multiple prefixes if (!$this->_allowMultiplePrefixes and array_key_exists($namespace, array_flip($graphPrefixes))) { require_once 'Erfurt/Namespaces/Exception.php'; throw new Erfurt_Namespaces_Exception("Multiple prefixes for namespace '{$namespace}' not allowed."); } // add new prefix $graphPrefixes[$prefix] = $namespace; // save new set of prefixes $this->setNamespacesForGraph($graph, $graphPrefixes); }