Get the namespace of the document, if defined.

@param DOMDocument $document     Document to query on

@return string
432052
protected function initNamespace(DOMDocument $document, $namespace_prefix = null)
    {
        // @todo: check for conflicting namespace prefixes
        $this->document_namespace = trim($document->documentElement->namespaceURI);
        $namespace_prefix = trim($namespace_prefix);

        if ($this->hasNamespace()) {
            $this->namespace_prefix = empty($namespace_prefix) ? $this->getDefaultNamespacePrefix() : $namespace_prefix;

            $this->registerNamespace(
                $this->namespace_prefix,
                $this->document_namespace
            );
        }
    }
986805
public function convert(DOMDocument $document)
    {
        foreach ($this->converters as $converter) {
            $converter->convert($document);
        }

        return $document;
    }
593943
private function ExtractFunctions(\DOMDocument $document, $namespace = "")
    {
        $xpath = new DOMXPath($document);

        $entries = $xpath->evaluate(
            "//compound[@kind='file'] | //compound[@kind='namespace']", 
            $document
        );
        
        // First we store all functions and their overloads, after
        // we use this array to create FunctionElement objects and add
        // them to the symbols object.
        $functions = array();

        for($i = 0; $i < $entries->length; $i++)
        {
            $kind = $entries->item($i)->getAttribute("kind");
            $refid = $entries->item($i)->getAttribute("refid");
            $name = $entries->item($i)->childNodes->item(0)->nodeValue;

            if($kind == "namespace")
                $namespace = str_replace("::", "\\", $name);
            else
                $namespace = "\\";

            $file_doc = new DOMDocument();
            $file_doc->load($this->definitions_path . "/$refid.xml");

            $file_xpath = new DOMXPath($file_doc);

            $file_members = $file_xpath->evaluate(
                "//memberdef[@kind='function']", 
                $file_doc
            );

            for($member = 0; $member < $file_members->length; $member++)
            {
                $location = str_replace(
                    $this->headers_path, "", str_replace(
                        "\\", 
                        "/", 
                        $file_xpath->evaluate(
                            "location", 
                            $file_members->item($member)
                        )->item(0)->getAttribute("file")
                    )
                );

                $function_name = $file_xpath->evaluate(
                    "name", 
                    $file_members->item($member)
                )->item(0)->nodeValue;

                $function_type = $file_xpath->evaluate(
                    "type", 
                    $file_members->item($member)
                )->item(0)->nodeValue;
                
                $function_description = trim(
                    $file_xpath->evaluate(
                        "briefdescription", 
                        $file_members->item($member)
                    )->item(0)->nodeValue
                );
                
                $function_type = str_replace(
                    array(" *", " &"), 
                    array("*", "&"), 
                    $function_type
                );

                $function_overload = new \Peg\Lib\Definitions\Element\Overload(
                    $function_description
                );
                
                $function_overload->SetReturnType(
                    new \Peg\Lib\Definitions\Element\ReturnType(
                        $function_type
                    )
                );
                
                // Retrieve all function parameters
                $function_parameters = $file_xpath->evaluate(
                    "param", 
                    $file_members->item($member)
                );

                $parameters = array();

                for(
                    $function_parameter = 0; 
                    $function_parameter < $function_parameters->length; 
                    $function_parameter++
                )
                {
                    // Default generated param name in case it doesn't have one.
                   $param_name = "param" . $function_parameter;

                    if(is_object($file_xpath->evaluate("declname", $function_parameters->item($function_parameter))->item(0)))
                    {
                        $param_name = $file_xpath->evaluate(
                            "declname", 
                            $function_parameters->item(
                                $function_parameter
                            ))->item(0)->nodeValue
                        ;
                    }
                    else
                    {
                        $this->SendMessage(t("Skipping:") . " " . t("function") 
                            . " '" . $function_name . "' " 
                            . t("seems to be a macro with undocumented parameter types.") 
                        );

                        continue 2;
                    }

                    $param_type = str_replace(
                        array(" *", " &"), 
                        array("*", "&"), 
                        $file_xpath->evaluate(
                            "type", 
                            $function_parameters->item($function_parameter)
                        )->item(0)->nodeValue
                    );

                    // Check if parameter is array
                    $param_array = false;
                    if($file_xpath->evaluate("array", $function_parameters->item($function_parameter))->length > 0)
                    {
                        $array_value = $file_xpath->evaluate(
                            "array", 
                            $function_parameters->item(
                                $function_parameter)
                            )->item(0)->nodeValue
                        ;

                        if($array_value == "[]")
                        {
                            $param_array = true;
                        }
                        else
                        {
                            //$parameters[$function_parameter]["extra"] = $array_value;
                        }
                    }

                    // Get default value
                    $param_value = "";
                    if($file_xpath->evaluate("defval", $function_parameters->item($function_parameter))->length > 0)
                    {
                        $param_value = $file_xpath->evaluate(
                            "defval", 
                            $function_parameters->item(
                                $function_parameter)
                            )->item(0)->nodeValue
                        ;
                    }
                    
                    $parameter = new \Peg\Lib\Definitions\Element\Parameter(
                        $param_name, 
                        $param_type, 
                        $param_value
                    );
                    
                    $parameter->is_array = $param_array;
                    
                    $function_overload->AddParameter($parameter);
                }

                $functions[$function_name][] = [
                    $location,
                    $namespace,
                    $function_overload
                ];
            }
        }
        
        foreach($functions as $function_name=>$function_overloads)
        {
            $function = new \Peg\Lib\Definitions\Element\FunctionElement(
                $function_name
            );
            
            $header = "";
            $namespace = "";
            
            foreach($function_overloads as $overload)
            {
                if(!$header)
                {
                    $header = $overload[0];
                    $namespace = $overload[1];
                }
                
                $function->AddOverload($overload[2]);
            }
            
            $this->symbols->AddFunction(
                $function, 
                $header,
                $namespace
            );
        }
    }
1144048
public static function getDOMDocument($source) 
    {
        if ($source instanceof DOMDOCUMENT) {
            return $source;
        }
        $source = self::getDocumentID($source);
        return $source
        ? self::$documents[$id]['document']
        : null;
    }
950406
public function get_namespace($name = null)
    {
        $ns = $this->namespace;

        if ($name) {
            // an alias for BC
            if ($name == 'prefix') {
                $name = 'prefix_in';
            }

            return isset($ns[$name]) ? $ns[$name] : null;
        }

        unset($ns['prefix_in'], $ns['prefix_out']);

        return $ns;
    }
512224
public function getNodeForDocument($document)
    {
        $oid = $this->getObjectIdentifier($document);
        $this->assertDocumentExists($document);

        return $this->nodeMap[$this->documentNodeMap[$oid]];
    }
123243
protected function extractNamespace($file)
    {
        $namespace = false;

        foreach ($file as $line) {
            if (preg_match('/^namespace (?P<namespace>.*);$/i', $line, $matches)) {
                $namespace = $matches['namespace'];
                continue;
            }
        }
        return $namespace;
    }
1265761
public function dispatch(DOMDocument $document)
    {
        $documentNamespace = $document->documentElement->lookupNamespaceURI(null);
        // checking for null as ezxml has no default namespace...
        if ($documentNamespace === null) {
            $documentNamespace = $document->documentElement->lookupNamespaceURI('xhtml');
        }

        foreach ($this->mapping as $namespace => $validator) {
            if ($documentNamespace === $namespace) {
                if ($validator === null) {
                    return array();
                }

                return $validator->validate($document);
            }
        }

        throw new NotFoundException('Validator', $documentNamespace);
    }
620680
public static function lookupNamespace($prefix)
    {
        return isset(self::$_namespaces[$prefix]) ?
            self::$_namespaces[$prefix] :
            $prefix;
    }
1177338
public function parseDocument(\DOMDocument $document)
    {
        foreach ($this->registry[DocumentExtension::class] as $extension) {
            $doc = $extension->parseDocument($this, $document);
            if ($doc) {
                return $doc;
            }
        }

        return null;
    }
797495
public static function getDocumentCompany($document)
    {
        return $document->getDataValue('firma') ? \FlexiPeeHP\FlexiBeeRO::uncode(
                $document->getDataValue('firma')) : null;
    }
1311293
public function getUrlsafeIdentifier($document)
    {
        $id = $this->getNormalizedIdentifier($document);

        if (null !== $id) {
            return substr($id, 1);
        }
    }
404819
protected function document(Document $document = null): ?Document {
        if ($document === null) {
            return $this->document;
        }

        $this->document = $document;

        return $this->document;
    }
475116
private function namespaceName() {
    $node = new NameNode();
    $this->mustMatch(T_STRING, $node, NULL, TRUE);
    while ($this->tryMatch(T_NS_SEPARATOR, $node)) {
      $this->mustMatch(T_STRING, $node, NULL, TRUE);
    }
    return $node;
  }
1294102
public function getUrl($document)
    {
        $environment = $document->getEnvironment();

        return $environment->getUrl() . '.' . $this->kernel->getFileExtension();
    }
561864
private function ExtractVariables(\DOMDocument $document, $namespace = "")
    {
        $xpath = new DOMXPath($document);

        $entries = $xpath->evaluate(
            "//compound[@kind='file'] | //compound[@kind='namespace']", 
            $document
        );

        for($i = 0; $i < $entries->length; $i++)
        {
            $kind = $entries->item($i)->getAttribute("kind");
            $refid = $entries->item($i)->getAttribute("refid");
            $name = $entries->item($i)->childNodes->item(0)->nodeValue;

            if($kind == "namespace")
                $namespace = str_replace("::", "\\", $name);
            else
                $namespace = "\\";

            $file_doc = new DOMDocument();
            $file_doc->load($this->definitions_path . "/$refid.xml");

            $file_xpath = new DOMXPath($file_doc);

            $file_members = $file_xpath->evaluate(
                "//memberdef[@kind='variable']", 
                $file_doc
            );

            for($member = 0; $member < $file_members->length; $member++)
            {
                $location = str_replace(
                    $this->headers_path, 
                    "", 
                    str_replace(
                        "\\", 
                        "/", 
                        $file_xpath->evaluate(
                            "location", 
                            $file_members->item($member)
                        )->item(0)->getAttribute("file")
                    )
                );

                $global_variable_name = $file_xpath->evaluate(
                    "name", 
                    $file_members->item($member)
                )->item(0)->nodeValue;
                
                $global_variable_type = $file_xpath->evaluate(
                    "type", 
                    $file_members->item($member)
                )->item(0)->nodeValue;
                
                $global_variable_description = $file_xpath->evaluate(
                    "briefdescription", 
                    $file_members->item($member)
                )->item(0)->nodeValue;

                $variable = new \Peg\Lib\Definitions\Element\GlobalVariable(
                    $global_variable_name, 
                    str_replace(
                        array(" *", " &"), 
                        array("*", "&"), 
                        $global_variable_type
                    ),
                    $global_variable_description
                );
                
                $this->symbols->AddGlobalVar($variable, $location, $namespace);
            }
        }
    }
1003384
private function ExtractEnumerations(\DOMDocument $document, $namespace = "")
    {
        $xpath = new DOMXPath($document);

        $entries = $xpath->evaluate(
            "//compound[@kind='file'] | //compound[@kind='namespace']", 
            $document
        );

        for($i = 0; $i < $entries->length; $i++)
        {
            $kind = $entries->item($i)->getAttribute("kind");
            $refid = $entries->item($i)->getAttribute("refid");
            $name = $entries->item($i)->childNodes->item(0)->nodeValue;

            if($kind == "namespace")
                $namespace = str_replace("::", "\\", $name);
            else
                $namespace = "\\";

            $file_doc = new DOMDocument();
            $file_doc->load($this->definitions_path . "/$refid.xml");

            $file_xpath = new DOMXPath($file_doc);

            $file_members = $file_xpath->evaluate(
                "//memberdef[@kind='enum']", 
                $file_doc
            );

            for($member = 0; $member < $file_members->length; $member++)
            {
                $location = str_replace(
                    $this->headers_path, 
                    "", 
                    str_replace(
                        "\\", 
                        "/", 
                        $file_xpath->evaluate(
                            "location", 
                            $file_members->item($member)
                        )->item(0)->getAttribute("file")
                    )
                );

                $enum_name = $file_xpath->evaluate(
                    "name", 
                    $file_members->item($member)
                )->item(0)->nodeValue;
                
                $enum_description = $file_xpath->evaluate(
                    "briefdescription", 
                    $file_members->item($member)
                )->item(0)->nodeValue;

                // Just extract named enumerations
                // Anonymous enumerations go on constants.json
                if($enum_name{0} != "@")
                {
                    $enum_values = $file_xpath->evaluate(
                        "enumvalue", 
                        $file_members->item($member)
                    );
                    
                    $enum_options = array();

                    for(
                        $enum_value = 0; 
                        $enum_value < $enum_values->length; 
                        $enum_value++
                    )
                    {
                        $enum_options[] = $file_xpath->evaluate(
                            "name", 
                            $enum_values->item($enum_value)
                        )->item(0)->nodeValue;
                    }
                    
                    $enumeration = new \Peg\Lib\Definitions\Element\Enumeration(
                        $enum_name,
                        $enum_options,
                        $enum_description
                    );
                    
                    $this->symbols->AddEnumeration(
                        $enumeration, 
                        $location, 
                        $namespace
                    );
                }
            }
        }
    }