<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.doc;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.dom.DOMDocument;
import org.dom4j.dom.DOMElement;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.suigeneris.jrcs.rcs.Archive;
import org.suigeneris.jrcs.rcs.Version;
import org.xwiki.model.reference.AttachmentReference;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.internal.xml.DOMXMLWriter;
import com.xpn.xwiki.internal.xml.XMLWriter;

public class XWikiAttachment implements Cloneable
{
    private static final Logger LOGGER = LoggerFactory.getLogger(XWikiAttachment.class);

    private XWikiDocument doc;

    private int filesize;

    private String filename;

    private String author;

    private Version version;

    private String comment;

    private Date date;

    private XWikiAttachmentContent attachment_content;

    private XWikiAttachmentArchive attachment_archive;

    private boolean isMetaDataDirty = false;

    private AttachmentReference reference;

    public XWikiAttachment(XWikiDocument doc, String filename)
    {
        setDoc(doc);
        setFilename(filename);
    }

    public XWikiAttachment()
    {
        this.filesize = 0;
        this.filename = "";
        this.author = "";
        this.comment = "";
        this.date = new Date();
    }

    public AttachmentReference getReference()
    {
        if (this.reference == null) {
            this.reference = new AttachmentReference(this.filename, this.doc.getDocumentReference());
        }

        return this.reference;
    }

    public long getId()
    {
        if (this.doc == null) {
            return this.filename.hashCode();
        } else {
            return (this.doc.getFullName() + "/" + this.filename).hashCode();
        }
    }

    public void setDocId(long id)
    {
    }

    public long getDocId()
    {
        return this.doc.getId();
    }

    public void setId(long id)
    {
    }

    @Override
    public Object clone()
    {
        XWikiAttachment attachment = null;
        try {
            attachment = getClass().newInstance();
        } catch (Exception e) {
            // This should not happen
            LOGGER.error("exception while attach.clone", e);
        }

        attachment.setAuthor(getAuthor());
        attachment.setComment(getComment());
        attachment.setDate(getDate());
        attachment.setDoc(getDoc());
        attachment.setFilename(getFilename());
        attachment.setFilesize(getFilesize());
        attachment.setRCSVersion(getRCSVersion());
        attachment.setMetaDataDirty(isMetaDataDirty());
        if (getAttachment_content() != null) {
            attachment.setAttachment_content((XWikiAttachmentContent) getAttachment_content().clone());
            attachment.getAttachment_content().setAttachment(attachment);
        }
        if (getAttachment_archive() != null) {
            attachment.setAttachment_archive((XWikiAttachmentArchive) getAttachment_archive().clone());
            attachment.getAttachment_archive().setAttachment(attachment);
        }

        return attachment;
    }

    /**
     * @return the cached filesize in byte of the attachment, stored as metadata
     */
    public int getFilesize()
    {
        return this.filesize;
    }

    /**
     * Set cached filesize of the attachment that will be stored as metadata
     *
     * @param filesize in byte
     */
    public void setFilesize(int filesize)
    {<commit_msg>XWIKI-8398: Document must be set last in clone method.<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.doc;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.dom.DOMDocument;
import org.dom4j.dom.DOMElement;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.suigeneris.jrcs.rcs.Archive;
import org.suigeneris.jrcs.rcs.Version;
import org.xwiki.model.reference.AttachmentReference;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.internal.xml.DOMXMLWriter;
import com.xpn.xwiki.internal.xml.XMLWriter;

public class XWikiAttachment implements Cloneable
{
    private static final Logger LOGGER = LoggerFactory.getLogger(XWikiAttachment.class);

    private XWikiDocument doc;

    private int filesize;

    private String filename;

    private String author;

    private Version version;

    private String comment;

    private Date date;

    private XWikiAttachmentContent attachment_content;

    private XWikiAttachmentArchive attachment_archive;

    private boolean isMetaDataDirty = false;

    private AttachmentReference reference;

    public XWikiAttachment(XWikiDocument doc, String filename)
    {
        setDoc(doc);
        setFilename(filename);
    }

    public XWikiAttachment()
    {
        this.filesize = 0;
        this.filename = "";
        this.author = "";
        this.comment = "";
        this.date = new Date();
    }

    public AttachmentReference getReference()
    {
        if (this.reference == null) {
            this.reference = new AttachmentReference(this.filename, this.doc.getDocumentReference());
        }

        return this.reference;
    }

    public long getId()
    {
        if (this.doc == null) {
            return this.filename.hashCode();
        } else {
            return (this.doc.getFullName() + "/" + this.filename).hashCode();
        }
    }

    public void setDocId(long id)
    {
    }

    public long getDocId()
    {
        return this.doc.getId();
    }

    public void setId(long id)
    {
    }

    @Override
    public Object clone()
    {
        XWikiAttachment attachment = null;
        try {
            attachment = getClass().newInstance();
        } catch (Exception e) {
            // This should not happen
            LOGGER.error("exception while attach.clone", e);
        }

        attachment.setAuthor(getAuthor());
        attachment.setComment(getComment());
        attachment.setDate(getDate());
        attachment.setFilename(getFilename());
        attachment.setFilesize(getFilesize());
        attachment.setRCSVersion(getRCSVersion());
        attachment.setMetaDataDirty(isMetaDataDirty());
        if (getAttachment_content() != null) {
            attachment.setAttachment_content((XWikiAttachmentContent) getAttachment_content().clone());
            attachment.getAttachment_content().setAttachment(attachment);
        }
        if (getAttachment_archive() != null) {
            attachment.setAttachment_archive((XWikiAttachmentArchive) getAttachment_archive().clone());
            attachment.getAttachment_archive().setAttachment(attachment);
        }

        attachment.setDoc(getDoc());

        return attachment;
    }

    /**
     * @return the cached filesize in byte of the attachment, stored as metadata
     */
    public int getFilesize()
    {
        return this.filesize;
    }

    /**
     * Set cached filesize of the attachment that will be stored as metadata
     *
     * @param filesize in byte
     */
    public void setFilesize(int filesize)
    {
{'fraction_non_alphanumeric': 0.09244306418219461, 'fraction_numerical': 0.005797101449275362, 'mean_word_length': 2.956183456183456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '2898242', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2675, 'n_words': 742}