protected function saveImplementation($uri = '', $formatOutput = true)
    {
        $qtiComponent = $this->getDocumentComponent();

        if (!empty($qtiComponent)) {
            $this->setDomDocument(new DOMDocument('1.0', 'UTF-8'));

            if ($formatOutput == true) {
                $this->getDomDocument()->formatOutput = true;
            }

            try {
                // If overriden, beforeSave may alter a last time
                // the documentComponent prior serialization.
                // Note: in use only when saving to a file.
                if (empty($uri) === false) {
                    $this->beforeSave($this->getDocumentComponent(), $uri);
                }

                $factory = $this->createMarshallerFactory();
                $marshaller = $factory->createMarshaller($this->getDocumentComponent());
                $element = $marshaller->marshall($this->getDocumentComponent());

                $rootElement = $this->getDomDocument()->importNode($element, true);
                $this->getDomDocument()->appendChild($rootElement);
                $this->decorateRootElement($rootElement);

                if (empty($uri) === false) {

                    if (@$this->getDomDocument()->save($uri) === false) {
                        // An error occured while saving.
                        $msg = "An error occured while saving QTI-XML file at '${uri}'. Maybe the save location is not reachable?";
                        throw new XmlStorageException($msg, XmlStorageException::WRITE);
                    }
                } else {
                    if (($strXml = $this->getDomDocument()->saveXML()) !== false) {
                        return $strXml;
                    } else {
                        // An error occured while saving.
                        $msg = "An internal error occured while exporting QTI-XML as string.";
                        throw new XmlStorageException($msg, XmlStorageException::WRITE);
                    }
                }
            } catch (DOMException $e) {
                $msg = "An internal error occured while saving QTI-XML data.";
                throw new XmlStorageException($msg, XmlStorageException::UNKNOWN, $e);
            } catch (MarshallerNotFoundException $e) {
                $version = $this->getVersion();
                $problematicQtiClassName = $e->getQtiClassName();
                $msg = "'${problematicQtiClassName}' components are not supported in QTI version '${version}'.";
                throw new XmlStorageException($msg, XmlStorageException::VERSION, $e);
            }
        } else {
            $msg = "The document cannot be saved. No document component object to be saved.";
            throw new XmlStorageException($msg, XmlStorageException::WRITE);
        }
    }