<commit_before>/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.model.export;

import static java.util.Objects.requireNonNull;

import com.google.common.annotations.Beta;
import java.io.OutputStream;
import java.util.Optional;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.common.YangConstants;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.Submodule;
import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;

public final class YinExportUtils {
    private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();

    private YinExportUtils() {
        // Hidden on purpose
    }

    /**
     * Returns well-formed file name of YIN file as defined in RFC6020.
     *
     * @param name Module or submodule name
     * @param revision Revision of module or submodule
     * @return well-formed file name of YIN file as defined in RFC6020.
     */
    public static String wellFormedYinName(final String name, final Optional<Revision> revision) {
        return wellFormedYinName(name, revision.map(Revision::toString).orElse(null));
    }

    /**
     * Returns well-formed file name of YIN file as defined in RFC6020.
     *
     * @param name name Module or submodule name
     * @param revision Revision of module or submodule
     * @return well-formed file name of YIN file as defined in RFC6020.
     */
    public static String wellFormedYinName(final String name, final String revision) {
        if (revision == null) {
            return name + YangConstants.RFC6020_YIN_FILE_EXTENSION;
        }
        return requireNonNull(name) + '@' + revision +  YangConstants.RFC6020_YIN_FILE_EXTENSION;
    }

    /**
     * Write a module as a YIN text into specified {@link OutputStream}. Supplied module must have the
     * {@link ModuleEffectiveStatement} trait.
     *
     * @param module Module to be exported
     * @throws IllegalArgumentException if the module is not an ModuleEffectiveStatement or if it declared
     *                                  representation is not available.
     * @throws NullPointerException if any of of the parameters is null
     * @throws XMLStreamException if an input-output error occurs
     * @deprecated Prefer {@link #writeModuleAsYinText(ModuleEffectiveStatement, OutputStream)}.
     */
    @Beta
    @Deprecated(forRemoval = true)
    public static void writeModuleAsYinText(final Module module, final OutputStream output) throws XMLStreamException {
        writeModuleAsYinText(module.asEffectiveStatement(), output);
    }

    /**
     * Write a module as a YIN text into specified {@link OutputStream}. Supplied module must have the
     * {@link ModuleEffectiveStatement} trait.
     *
     * @param module Module to be exported
     * @throws IllegalArgumentException if the module's declared representation is not available.
     * @throws NullPointerException if any of of the parameters is null
     * @throws XMLStreamException if an input-output error occurs
     */
    @Beta
    public static void writeModuleAsYinText(final ModuleEffectiveStatement module, final OutputStream output)
            throws XMLStreamException {
        writeReaderToOutput(YinXMLEventReaderFactory.defaultInstance().createXMLEventReader(module), output);
    }

    /**
     * Write a submodule as a YIN text into specified {@link OutputStream}. Supplied submodule must have the
     * {@link SubmoduleEffectiveStatement} trait.
     *
     * @param parentModule Parent module
     * @param submodule Submodule to be exported
     * @throws IllegalArgumentException if the parent module is not a ModuleEffectiveStatement, if the submodule is not
     *                                  a SubmoduleEffectiveStatement or if its declared representation is not available
     * @throws NullPointerException if any of of the parameters is null
     * @throws XMLStreamException if an input-output error occurs
     * @deprecated Prefer {@link #writeSubmoduleAsYinText(ModuleEffectiveStatement, SubmoduleEffectiveStatement,
     *             OutputStream)}.
     */
    @Beta
    @Deprecated(forRemoval = true)
    public static void writeSubmoduleAsYinText(final Module parentModule, final Submodule submodule,
            final OutputStream output) throws XMLStreamException {
        writeSubmoduleAsYinText(parentModule.asEffectiveStatement(), submodule.asEffectiveStatement(), output);
    }

    /**
     * Write a submodule as a YIN text into specified {@link OutputStream}. Supplied submodule must have the
     * {@link SubmoduleEffectiveStatement} trait.<commit_msg>Remove deprecated YingExportUtils methods<commit_after>/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.model.export;

import static java.util.Objects.requireNonNull;

import com.google.common.annotations.Beta;
import java.io.OutputStream;
import java.util.Optional;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.common.YangConstants;
import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;

public final class YinExportUtils {
    private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();

    private YinExportUtils() {
        // Hidden on purpose
    }

    /**
     * Returns well-formed file name of YIN file as defined in RFC6020.
     *
     * @param name Module or submodule name
     * @param revision Revision of module or submodule
     * @return well-formed file name of YIN file as defined in RFC6020.
     */
    public static String wellFormedYinName(final String name, final Optional<Revision> revision) {
        return wellFormedYinName(name, revision.map(Revision::toString).orElse(null));
    }

    /**
     * Returns well-formed file name of YIN file as defined in RFC6020.
     *
     * @param name name Module or submodule name
     * @param revision Revision of module or submodule
     * @return well-formed file name of YIN file as defined in RFC6020.
     */
    public static String wellFormedYinName(final String name, final String revision) {
        if (revision == null) {
            return name + YangConstants.RFC6020_YIN_FILE_EXTENSION;
        }
        return requireNonNull(name) + '@' + revision +  YangConstants.RFC6020_YIN_FILE_EXTENSION;
    }

    /**
     * Write a module as a YIN text into specified {@link OutputStream}. Supplied module must have the
     * {@link ModuleEffectiveStatement} trait.
     *
     * @param module Module to be exported
     * @throws IllegalArgumentException if the module's declared representation is not available.
     * @throws NullPointerException if any of of the parameters is null
     * @throws XMLStreamException if an input-output error occurs
     */
    @Beta
    public static void writeModuleAsYinText(final ModuleEffectiveStatement module, final OutputStream output)
            throws XMLStreamException {
        writeReaderToOutput(YinXMLEventReaderFactory.defaultInstance().createXMLEventReader(module), output);
    }

    /**
     * Write a submodule as a YIN text into specified {@link OutputStream}. Supplied submodule must have the
     * {@link SubmoduleEffectiveStatement} trait.
{'fraction_non_alphanumeric': 0.07371428571428572, 'fraction_numerical': 0.007314285714285714, 'mean_word_length': 3.6523125996810206, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1571585', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2301, 'n_words': 735}