<commit_before><?php
/*
Copyright 2002-2012 MarkLogic Corporation.  All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
namespace MarkLogic\MLPHP;

/**
 * Represents an XML document.
 *
 * @package MLPHP
 * @author <NAME> <<EMAIL>>
 */
class XMLDocument extends Document
{
    private $dom; // @var DOMDocument object

    /**
     * Create an XML document object.
     *
     * @param RESTClient $restClient A REST client object.
     * @param string $uri A document URI.
     */
    public function __construct($restClient, $uri = null)
    {
        parent::__construct($restClient, $uri);
        $this->dom = new \DOMDocument();
        $this->contentType = 'application/xml';
    }

    /**
     * Read an XML document from the database.
     *
     * @param string $uri A document URI.
     * @param array $params Optional additional parameters to pass when reading.
     * @return string The document content.
     */
    public function read($uri = null, $params = array())
    {
        $this->uri = (isset($uri)) ? (string)$uri : $this->uri;
        $params = array_merge(array('format' => 'xml'), $params);
        return parent::read($this->uri, $params);
    }

    /**
     * Write an XML document to the database.
     *
     * @param string $uri A document URI.
     * @param array $params Optional additional parameters to pass when writing.
     * @return Document $this
     */
    public function write($uri = null, $params = array())
    {
        $this->uri = (isset($uri)) ? (string)$uri : $this->uri;
        $params = array_merge(array('format' => 'xml'), $params);
        return parent::write($this->uri, $params);
    }

    /**
     * Get the document as a DOMDocument object.
     *
     * @return DOMDocument|null A DOMDocument object.
     */
    public function getAsDOMDocument()
    {
        return $this->dom->loadXML($this->getContent());
    }

}<commit_msg>Test for valid XML before writing<commit_after><?php
/*
Copyright 2002-2012 MarkLogic Corporation.  All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
namespace MarkLogic\MLPHP;

/**
 * Represents an XML document.
 *
 * @package MLPHP
 * @author <NAME> <<EMAIL>>
 */
class XMLDocument extends Document
{
    private $dom; // @var DOMDocument object

    /**
     * Create an XML document object.
     *
     * @param RESTClient $restClient A REST client object.
     * @param string $uri A document URI.
     */
    public function __construct($restClient, $uri = null)
    {
        parent::__construct($restClient, $uri);
        $this->dom = new \DOMDocument();
        $this->contentType = 'application/xml';
    }

    /**
     * Read an XML document from the database.
     *
     * @param string $uri A document URI.
     * @param array $params Optional additional parameters to pass when reading.
     * @return string The document content.
     */
    public function read($uri = null, $params = array())
    {
        $this->uri = (isset($uri)) ? (string)$uri : $this->uri;
        $params = array_merge(array('format' => 'xml'), $params);
        return parent::read($this->uri, $params);
    }

    /**
     * Write an XML document to the database.
     *
     * @param string $uri A document URI.
     * @param array $params Optional additional parameters to pass when writing.
     * @return Document $this
     */
    public function write($uri = null, $params = array())
    {
        if ($this->isValidXML($this->getContent())) {
            $this->uri = (isset($uri)) ? (string)$uri : $this->uri;
            $params = array_merge(array('format' => 'xml'), $params);
            return parent::write($this->uri, $params);
        } else {
            throw new \Exception('Attempting to write invalid XML content');
        }
    }

    /**
     * Check if XML content is valid.
     *
     * @return boolean true or false.
     */
    public function isValidXML($xml)
    {
        $doc = new \DOMDocument();
        return $doc->loadXML($xml) === true;
    }

    /**
     * Get the document as a DOMDocument object.
     *
     * @return DOMDocument|null A DOMDocument object.
     */
    public function getAsDOMDocument()
    {
        return $this->dom->loadXML($this->getContent());
    }

}
{'fraction_non_alphanumeric': 0.120130493187488, 'fraction_numerical': 0.004605641911341394, 'mean_word_length': 2.6704225352112676, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2869066', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1420, 'n_words': 518}