public function convert(DOMDocument $document)
{
$document = clone $document;
$xpath = new DOMXPath($document);
$xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook');
$linkAttributeExpression = "starts-with( @xlink:href, 'ezlocation://' ) or starts-with( @xlink:href, 'ezcontent://' )";
$xpathExpression = "//docbook:link[{$linkAttributeExpression}]|//docbook:ezlink";
/** @var \DOMElement $link */
foreach ($xpath->query($xpathExpression) as $link) {
// Set resolved href to number character as a default if it can't be resolved
$hrefResolved = '#';
$href = $link->getAttribute('xlink:href');
$location = null;
preg_match('~^(.+://)?([^#]*)?(#.*|\\s*)?$~', $href, $matches);
list(, $scheme, $id, $fragment) = $matches;
if ($scheme === 'ezcontent://') {
try {
$contentInfo = $this->contentService->loadContentInfo($id);
$location = $this->locationService->loadLocation($contentInfo->mainLocationId);
$hrefResolved = $this->urlAliasRouter->generate($location) . $fragment;
} catch (APINotFoundException $e) {
if ($this->logger) {
$this->logger->warning('While generating links for richtext, could not locate ' . 'Content object with ID ' . $id);
}
} catch (APIUnauthorizedException $e) {
if ($this->logger) {
$this->logger->notice('While generating links for richtext, unauthorized to load ' . 'Content object with ID ' . $id);
}
}
} elseif ($scheme === 'ezlocation://') {
try {
$location = $this->locationService->loadLocation($id);
$hrefResolved = $this->urlAliasRouter->generate($location) . $fragment;
} catch (APINotFoundException $e) {
if ($this->logger) {
$this->logger->warning('While generating links for richtext, could not locate ' . 'Location with ID ' . $id);
}
} catch (APIUnauthorizedException $e) {
if ($this->logger) {
$this->logger->notice('While generating links for richtext, unauthorized to load ' . 'Location with ID ' . $id);
}
}
} else {
$hrefResolved = $href;
}
$hrefAttributeName = 'xlink:href';
// For embeds set the resolved href to the separate attribute
// Original href needs to be preserved in order to generate link parameters
// This will need to change with introduction of UrlService and removal of URL link
// resolving in external storage
if ($link->localName === 'ezlink') {
$hrefAttributeName = 'href_resolved';
}
$link->setAttribute($hrefAttributeName, $hrefResolved);
}
return $document;
}