public function save($path, $format = Document::XML)
    {
        switch ($format) {
            case Document::XML:
                $format = 'saveXML';
                break;
            case Document::HTML:
                $format = 'saveHTML';
                break;
            case Document::JSON:
                $format = 'toJson';
                break;
            default:
                throw new DomException('Invalid format', 2);
        }

        $tmp = Storage::temp($this->$format(), 'tmp', '~xml-');

        if ($tmp === false) {
            throw new DomException('Can\'t create tmp file', 2);
        } elseif (copy($tmp, $path) === false) {
            throw new DomException('Can\'t copy tmp file to ' . $path, 2);
        } else {
            unlink($tmp);
        }
    }