Transforms the xlink:href attribute of ezcontent and ezlocation links
from remote IDs to IDs, for usage by serialize/unserialize process.

@param \DOMDocument $xml
170192
protected function transformRemoteLinksToLinks(DOMDocument $xml)
    {
        $xpath = new DOMXPath($xml);
        $xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook');

        foreach (array('ezembedinline', 'ezembed', 'link', 'ezlink') as $tagName) {
            $xpathExpression = "//docbook:{$tagName}[starts-with( @xlink:href, 'ezcontent://' ) or starts-with( @xlink:href, 'ezlocation://' )]";
            /** @var \DOMElement $element */
            foreach ($xpath->query($xpathExpression) as $element) {
                preg_match('~^(.+)://([^#]*)?(#.*|\\s*)?$~', $element->getAttribute('xlink:href'), $matches);
                list(, $scheme, $remoteId, $fragment) = $matches;

                if ($scheme === 'ezcontent') {
                    $object = eZContentObject::fetchByRemoteID($remoteId);
                    if (!$object instanceof eZContentObject) {
                        $element->parentNode->removeChild($element);
                        continue;
                    }

                    $href = "{$scheme}://{$object->attribute('id')}{$fragment}";
                } else {
                    $node = eZContentObjectTreeNode::fetchByRemoteID($remoteId);
                    if (!$node instanceof eZContentObjectTreeNode) {
                        $element->parentNode->removeChild($element);
                        continue;
                    }

                    $href = "{$scheme}://{$node->attribute('node_id')}{$fragment}";
                }

                $element->setAttribute('xlink:href', $href);
            }
        }
    }
492380
public static function saveXmlNodeToString(\DOMNode $node)
    {
        $doc = XmlUtil::getOwnerDocument($node);
        $string = $doc->saveXML($node);
        return $string;
    }
14841
public function convert(DOMDocument $xmlDoc): void
    {
        foreach ($xmlDoc->getElementsByTagName('link') as $link) {
            /** @var \DOMElement $link */
            if (!$link->hasAttribute('custom:file')) {
                continue;
            }

            $content = null;
            $location = null;

            if ($link->hasAttribute('object_id')) {
                try {
                    $content = $this->loadService->loadContent($link->getAttribute('object_id'));
                    $location = $content->mainLocation;
                } catch (NotFoundException $e) {
                    $this->logger->warning(
                        'While generating links for xmltext, could not locate ' .
                        'Content object with ID ' . $link->getAttribute('object_id')
                    );
                } catch (UnauthorizedException $e) {
                    $this->logger->notice(
                        'While generating links for xmltext, unauthorized to load ' .
                        'Content object with ID ' . $link->getAttribute('object_id')
                    );
                }
            }

            if ($link->hasAttribute('node_id')) {
                try {
                    $location = $this->loadService->loadLocation($link->getAttribute('node_id'));
                    $content = $location->content;
                } catch (NotFoundException $e) {
                    $this->logger->warning(
                        'While generating links for xmltext, could not locate ' .
                        'Location with ID ' . $link->getAttribute('node_id')
                    );
                } catch (UnauthorizedException $e) {
                    $this->logger->notice(
                        'While generating links for xmltext, unauthorized to load ' .
                        'Location with ID ' . $link->getAttribute('node_id')
                    );
                }
            }

            if ($content !== null && $content->hasField('file')) {
                $field = $content->getField('file');
                if (!$field->isEmpty()) {
                    $url = $this->router->generate('ngsite_download', ['contentId' => $content->id, 'fieldId' => $field->id, 'isInline' => $link->hasAttribute('custom:inline')]);
                }
            }

            if (empty($url) && $location !== null) {
                $link->setAttribute('url', $this->router->generate($location));
            } elseif (!empty($url)) {
                $link->setAttribute('url', $url);
            } else {
                $link->setAttribute('url', '');
            }

            if ($link->hasAttribute('anchor_name')) {
                $link->setAttribute('url', $link->getAttribute('url') . '#' . $link->getAttribute('anchor_name'));
            }

            // With this we disable the original pre-converter
            // (and hopefully all other that touch link element)
            $link->removeAttribute('object_id');
            $link->removeAttribute('node_id');
            $link->removeAttribute('anchor_name');
        }
    }
1168606
public static function fromString(string $xml) : DOMDocument
    {
        if (trim($xml) === '') {
            throw InvalidArgumentException::invalidType('non-empty string', $xml);
        }

        $entityLoader = libxml_disable_entity_loader(true);
        $internalErrors = libxml_use_internal_errors(true);
        libxml_clear_errors();

        $domDocument = self::create();
        $options = LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_NONET;
        if (defined('LIBXML_COMPACT')) {
            $options |= LIBXML_COMPACT;
        }

        $loaded = $domDocument->loadXML($xml, $options);

        libxml_use_internal_errors($internalErrors);
        libxml_disable_entity_loader($entityLoader);

        if (!$loaded) {
            $error = libxml_get_last_error();
            libxml_clear_errors();

            throw new UnparseableXmlException($error);
        }

        libxml_clear_errors();

        foreach ($domDocument->childNodes as $child) {
            if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
                throw new RuntimeException(
                    'Dangerous XML detected, DOCTYPE nodes are not allowed in the XML body'
                );
            }
        }

        return $domDocument;
    }
908423
public function process(\DOMDocument $xml)
    {
        // matches:
        // - foo@bar.com
        // - <foo@bar.com>
        // - Some Name <foo@bar.com>
        // ignores leading and trailing whitespace
        // requires angled brackets when a name is given (that's what the
        //   two (?(1)) conditions do)
        // requires closing angled bracket if email address is given with an
        //   opening angled bracket but no name (that's what the (?(3))
        //   condition is for)
        $regex = '#^\s*(?P<name>[^<]+?)?\s*((?(1)<|<?)(?:mailto:)?'
            . '(?P<email>\b[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}\b)'
            . '(?(1)>|(?(3)>|>?)))\s*$#u';

        $xpath = new \DOMXPath($xml);
        $nodes = $xpath->query('//tag[@name="author"]/@description');

        /** @var \DOMElement $node */
        foreach ($nodes as $node) {
            // FIXME: #193
            if (preg_match($regex, $node->nodeValue, $matches)) {
                if ($matches['name']) {
                    $value = $matches['name'];
                } else {
                    // in case there were <> but no name... this cleans up the
                    // output a bit
                    $value = $matches['email'];
                }

                $node->nodeValue = $value;

                /** @var \DOMElement $parentNode */
                $parentNode = $node->parentNode;
                $parentNode->setAttribute(
                    'link',
                    'mailto:' . $matches['email']
                );
            }
        }

        return $xml;
    }
575877
public function oldImport(\SimpleXMLElement $xml) {
    $data = array();
    foreach ($xml->xpath('//trans-unit') as $unit) {
      $data[(string) $unit['id']]['#text'] = (string) $unit->target;
    }
    return Data::unflattenData($data);
  }
582983
public function process(\DOMDocument $xml)
    {
        $ignoreQry = '//tag[@name=\'return\' and @type=\'self\']'
            . '|//tag[@name=\'return\' and @type=\'$this\']'
            . '|//tag[@name=\'return\']/type[.=\'self\']'
            . '|//tag[@name=\'return\']/type[.=\'$this\']';

        $xpath = new \DOMXPath($xml);
        $nodes = $xpath->query($ignoreQry);

        /** @var \DOMElement $node */
        foreach ($nodes as $node) {
            // if a node with name 'type' is selected we need to reach one
            // level further.
            $docblock = ($node->nodeName == 'type')
                ? $node->parentNode->parentNode
                : $node->parentNode;

            /** @var \DOMElement $method  */
            $method = $docblock->parentNode;

            // if the method is not a method but a global function: error!
            if ($method->nodeName != 'method') {
                continue;
            }

            $type = $method->parentNode->getElementsByTagName('full_name')
                ->item(0)->nodeValue;

            // nodes with name type need to set their content; otherwise we set
            // an attribute of the class itself
            if ($node->nodeName == 'type') {
                $node->nodeValue = $type;

                // add a new tag @fluent to indicate that this is a fluent interface
                // we only add it here since there should always be a node `type`
                $fluent_tag = new \DOMElement('tag');
                $docblock->appendChild($fluent_tag);
                $fluent_tag->setAttribute('name', 'fluent');
                $fluent_tag->setAttribute(
                    'description',
                    'This method is part of a fluent interface and will return '
                    . 'the same instance'
                );
            } else {
                $node->setAttribute('type', $type);
            }

            // check if an excerpt is set and override that as well
            if ($node->hasAttribute('excerpt')
                && (($node->getAttribute('excerpt') == 'self')
                || ($node->getAttribute('excerpt') == '$this'))
            ) {
                $node->setAttribute('excerpt', $type);
            }
        }

        return $xml;
    }
336070
private function importXml(\DOMDocument $doc)
    {
        $importNodes=$doc->getElementsByTagName("import");
        for($i=$importNodes->length-1;$i>=0;$i--){
            $node = $importNodes->item($i);
            if($node->hasAttribute("file")){
                $file=$node->getAttribute("file");
                $path=rtrim(dirname($this->m_configFile),"/");
                $file=$path."/".ltrim($file,"/");
                if(file_exists($file)){
                    $_doc=new \DOMDocument();
                    $_doc->load($file);
                    $this->importXml($_doc);
                    $children=$_doc->documentElement->childNodes;
                    for($j=0;$j<$children->length;$j++){
                        $_node=$children->item($j);
                        if(is_a($_node,"DOMElement")){
                            $_node = $doc->importNode($_node, true);
                            $node->parentNode->insertBefore($_node,$node);
                        }
                    }
                    if(!in_array($file, $this->m_configFiles)){
                        $this->m_configFiles[]=$file;
                    }
                }
            }
            $node->parentNode->removeChild($node);
        }
    }
635731
protected function transform($xml)
    {
        if ( !$xml instanceof SimpleXMLElement ) {
            $xml = simplexml_load_string($xml);
        }

        $factory = new StructureFactory($this->fs);
        $files = new Collection();
        foreach ( $xml->file as $i => $file ) {
            /** @var SimpleXMLElement $file */
            $raw = json_decode(json_encode(simplexml_load_string($file->saveXML())), true);
            $files->add(new Structure\File($raw, $factory));
        }


        return $files;
    }
1359073
function serializeContentObjectAttribute( $package, $objectAttribute )
    {

        $DOMNode = $this->createContentObjectAttributeDOMNode( $objectAttribute );
        $xmlString = $objectAttribute->attribute( 'data_text' );

        if ( $xmlString != '' )
        {
            $doc = new DOMDocument( '1.0', 'utf-8' );
            $success = $doc->loadXML( $xmlString );

            /* For all links found in the XML, do the following:
             * - add "href" attribute fetching it from ezurl table.
             * - remove "id" attribute.
             */

            $links = $doc->getElementsByTagName( 'link' );
            $embeds = $doc->getElementsByTagName( 'embed' );
            $objects = $doc->getElementsByTagName( 'object' );
            $embedsInline = $doc->getElementsByTagName( 'embed-inline' );

            eZXMLTextType::transformLinksToRemoteLinks( $links );
            eZXMLTextType::transformLinksToRemoteLinks( $embeds );
            eZXMLTextType::transformLinksToRemoteLinks( $objects );
            eZXMLTextType::transformLinksToRemoteLinks( $embedsInline );

            $importedRootNode = $DOMNode->ownerDocument->importNode( $doc->documentElement, true );
            $DOMNode->appendChild( $importedRootNode );
        }

        return $DOMNode;
    }
981527
protected function createTreeNodeDOMElement( DOMDocument $domDocument, eZContentObjectTreeNode $node )
    {
        $nodeElement = $domDocument->createElement( 'Node' );

        // Set attributes.
        $nodeElement->setAttribute( 'nodeID', $node->attribute( 'node_id' ) );
        $nodeElement->setAttribute( 'parentID', $node->attribute( 'parent_node_id' ) );

        // Get child information.
        $conditions = array( 'Depth' => 1,
                             'DepthOperator' => 'eq' );
        $nodeElement->setAttribute( 'childCount', eZContentObjectTreeNode::subTreeCountByNodeID( $conditions, $node->attribute( 'node_id' ) ) );

        // Get child information for tree structure.
        $conditions['ClassFilterType'] = 'include';
        $conditions['ClassFilterArray'] = eZINI::instance( 'contentstructuremenu.ini' )->variable( 'TreeMenu', 'ShowClasses' );
        $nodeElement->setAttribute( 'childMenuCount', eZContentObjectTreeNode::subTreeCountByNodeID( $conditions, $node->attribute( 'node_id' ) ) );

        // Get access rights element.
        $nodeElement->appendChild( $this->createAccessDOMElement( $domDocument, $node ) );

        // Get object element.
        $nodeElement->appendChild( $this->createObjectDOMElement( $domDocument, $node->attribute( 'object' ) ) );

        return $nodeElement;
    }
338106
public function FromXML($xml)
	{
		$sxo = new SimpleXMLElement($xml);
		
		if($sxo->encoding == "base64")
		{
			$this->Data = base64_decode($sxo->data);
		}
		else
		{
			$this->Data = $sxo->data;
		}
		
		$this->Name = $attachment->name;
		$this->Type = $attachment->type;
		$this->Size = $attachment->size;
		$this->Extension = $attachment->extension;
	}
434794
public function loadDomDocument(\DOMDocument $domDocument)
    {
        $this->_dom = $domDocument;
        foreach ($this->_getNodesForCssSelector('[style]') as $node) {
            $node->setAttribute(
                'inlinestyle-original-style',
                $node->getAttribute('style')
            );
        }
    }
476918
private function loadXml($file)
    {
        $internal = libxml_use_internal_errors(true);

        $doc = new DOMDocument();
        $doc->preserveWhiteSpace = false;
        $doc->validateOnParse = true;

        if (!@$doc->load($file)) {
            $errors = libxml_get_errors();

            libxml_use_internal_errors($internal);

            throw XmlException::createUsingErrors($errors);
        }

        libxml_use_internal_errors($internal);

        return new DOMXPath($doc);
    }
1104795
protected function transform(DOMDocument $element)
    {
        foreach ($this->transformers as $transformer) {
            $transformer($element);
        }

        return $element;
    }
27619
public function addXML($xml)
    {
        $next_elem = $this->createDocumentFragment();
        $next_elem->appendXML($xml);
        $this->doc->appendChild($next_elem);

    }
702409
protected function generateLinkXml($href, $hreflang, $rel = null)
    {
        if (null == $rel) {
            $rel = self::REL_ALTERNATE;
        }

        $xml = '<xhtml:link rel="' . $rel
            . '" hreflang="' . $hreflang
            . '" href="' . Utils::encode($href) . '" />';

        return $xml;
    }