<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 org.xwiki.wikistream.xar.internal.output;

import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.lang3.ObjectUtils;
import org.xwiki.model.internal.reference.LocalizedStringEntityReferenceSerializer;
import org.xwiki.model.reference.LocalDocumentReference;
import org.xwiki.wikistream.WikiStreamException;
import org.xwiki.wikistream.output.OutputStreamOutputTarget;
import org.xwiki.wikistream.output.OutputTarget;
import org.xwiki.wikistream.xar.internal.XARModel;
import org.xwiki.wikistream.xml.internal.output.WikiStreamXMLStreamWriter;

/**
 * 
 * @version $Id$
 * @since 5.2M2
 */
public class XARWikiWriter
{
    private static final LocalizedStringEntityReferenceSerializer TOSTRING_SERIALIZER =
        new LocalizedStringEntityReferenceSerializer();

    private static class Entry
    {
        public LocalDocumentReference reference;

        public Locale locale = Locale.ROOT;

        public int defaultAction = XARModel.ACTION_OVERWRITE;

        public Entry(LocalDocumentReference reference, Locale locale)
        {
            this.reference = reference;
            this.locale = locale;
        }
    }

    private final String name;

    private final Map<String, Object> wikiProperties;

    private final XAROutputProperties xarProperties;

    private final ZipArchiveOutputStream zipStream;

    private final List<Entry> entries = new LinkedList<Entry>();

    public XARWikiWriter(String name, Map<String, Object> wikiParameters, XAROutputProperties xarProperties)
        throws WikiStreamException
    {
        this.name = name;
        this.wikiProperties = wikiParameters;
        this.xarProperties = xarProperties;

        OutputTarget target = this.xarProperties.getTarget();
        if (target instanceof OutputStreamOutputTarget) {
            try {
                this.zipStream = new ZipArchiveOutputStream(((OutputStreamOutputTarget) target).getOutputStream());
                this.zipStream.setEncoding("UTF8");

                // By including the unicode extra fields, it is possible to extract XAR-files containing documents with
                // non-ascii characters in the document name using InfoZIP, and the filenames will be correctly
                // converted to the character set of the local file system.
                this.zipStream.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
            } catch (IOException e) {
                throw new WikiStreamException("Files to create zip output stream", e);
            }
        } else {
            throw new WikiStreamException("Unsupported output target [" + target + "]. Only ["
                + OutputStreamOutputTarget.class.getName() + "] is suppoted");
        }
    }

    public String getName()
    {
        return this.name;
    }

    public Map<String, Object> getMetadata()
    {
        return this.wikiProperties;
    }

    public OutputStream newEntry(LocalDocumentReference reference, Locale locale) throws WikiStreamException
    {
        StringBuilder path = new StringBuilder();

        // Add space name
        path.append(reference.getParent().getName()).append('/');

        // Add document name
        path.append(reference.getName());

        // Add language
        if (locale != null && !locale.equals(Locale.ROOT)) {
            path.append('.');
            path.append(locale);
        }

        // Add extension
        path.append(".xml");<commit_msg>XWIKI-9351: Introduce output XAR component for WikiStream * add support for folder as output target (where each wiki is exported in a separate XAR file)<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 org.xwiki.wikistream.xar.internal.output;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.lang3.ObjectUtils;
import org.xwiki.model.internal.reference.LocalizedStringEntityReferenceSerializer;
import org.xwiki.model.reference.LocalDocumentReference;
import org.xwiki.wikistream.WikiStreamException;
import org.xwiki.wikistream.output.FileOutputTarget;
import org.xwiki.wikistream.output.OutputStreamOutputTarget;
import org.xwiki.wikistream.output.OutputTarget;
import org.xwiki.wikistream.xar.internal.XARModel;
import org.xwiki.wikistream.xml.internal.output.WikiStreamXMLStreamWriter;

/**
 * @version $Id$
 * @since 5.2M2
 */
public class XARWikiWriter
{
    private static final LocalizedStringEntityReferenceSerializer TOSTRING_SERIALIZER =
        new LocalizedStringEntityReferenceSerializer();

    private static class Entry
    {
        public LocalDocumentReference reference;

        public Locale locale = Locale.ROOT;

        public int defaultAction = XARModel.ACTION_OVERWRITE;

        public Entry(LocalDocumentReference reference, Locale locale)
        {
            this.reference = reference;
            this.locale = locale;
        }
    }

    private final String name;

    private final Map<String, Object> wikiProperties;

    private final XAROutputProperties xarProperties;

    private final ZipArchiveOutputStream zipStream;

    private final List<Entry> entries = new LinkedList<Entry>();

    public XARWikiWriter(String name, Map<String, Object> wikiParameters, XAROutputProperties xarProperties)
        throws WikiStreamException
    {
        this.name = name;
        this.wikiProperties = wikiParameters;
        this.xarProperties = xarProperties;

        OutputTarget target = this.xarProperties.getTarget();

        try {
            if (target instanceof FileOutputTarget && ((FileOutputTarget) target).getFile().isDirectory()) {
                this.zipStream =
                    new ZipArchiveOutputStream(new File(((FileOutputTarget) target).getFile(), name + ".xar"));
            } else if (target instanceof OutputStreamOutputTarget) {
                this.zipStream = new ZipArchiveOutputStream(((OutputStreamOutputTarget) target).getOutputStream());
            } else {
                throw new WikiStreamException(String.format("Unsupported output target [%s]. Only [%s] is suppoted",
                    target, OutputStreamOutputTarget.class));
            }
        } catch (IOException e) {
            throw new WikiStreamException("Files to create zip output stream", e);
        }

        this.zipStream.setEncoding("UTF8");

        // By including the unicode extra fields, it is possible to extract XAR-files containing documents with
        // non-ascii characters in the document name using InfoZIP, and the filenames will be correctly
        // converted to the character set of the local file system.
        this.zipStream.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
    }

    public String getName()
    {
        return this.name;
    }

    public Map<String, Object> getMetadata()
    {
        return this.wikiProperties;
    }

    public OutputStream newEntry(LocalDocumentReference reference, Locale locale) throws WikiStreamException
    {
        StringBuilder path = new StringBuilder();

        // Add space name
        path.append(reference.getParent().getName()).append('/');

        // Add document name
        path.append(reference.getName());

        // Add language
        if (locale != null && !locale.equals(Locale.ROOT)) {
            path.append('.');
            path.append(locale);
        }

        // Add extension
        path.append(".xml");
{'fraction_non_alphanumeric': 0.08155479946386225, 'fraction_numerical': 0.004124136508918445, 'mean_word_length': 3.143528406663819, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1380513', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2400, 'n_words': 772}