<commit_before>/*
 * $Id$
 *
 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer in the documentation and/or other materials
 *      provided with the distribution.
 *    * Neither the name of the jCoderZ.org Project nor the names of
 *      its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written
 *      permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.jcoderz.commons.taskdefs;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.transform.Transformer;

import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.XMLAbstractTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.jcoderz.commons.util.Constants;
import org.jcoderz.commons.util.IoUtil;


/**
 * Xtreme Documentation Ant task.
 *
 * @author <NAME>
 */
public class XtremeDocs
    extends Task
{
    private static final String FORMAT_PDF = "PDF";

    private static final String FORMAT_HTML = "HTML";

    private static final String FORMAT_ALL = "ALL";

    private static final String FORMAT_NONE = "NONE";

    private static final String TYPE_QUALITY_REPORT = "Quality-Report";
    
    private static final String TYPE_KPI_STATS = "KPI-Stats";
    
    private static final String TYPE_KPI_REPORT = "KPI-Report";

    private static final String TYPE_RELEASE_NOTES = "Release-Notes";

    private static final String TYPE_TEST_SPEC = "TestSpec";

    private static final String TYPE_USE_CASE = "UseCase";

    private static final String TYPE_SAD = "SAD";

    private static final String IMAGE_DIR = "images";

    private static final String APIDOC_DIR = "apidoc";

    private static final String DEFAULT_COMPANY_NAME = "jCoderZ.org";

    private static final String DEFAULT_COMPANY_LOGO = "jcoderz-org";

    private static final boolean DEFAULT_VALIDATION_ONLY_FLAG = false;

    /** The output directory. */
    private File mOutDir;

    /** XEP home directory. */
    private File mXepHome;

    /** The input file. */
    private File mInFile;

    /** terminate ant build on error. */
    private boolean mFailOnError;

    /** Type of document. (SAD|UseCase) */
    private String mType;

    /** Format of document. (NONE|HTML|PDF|ALL) */
    private String mFormat;

    private String mTypeLowerCase;

    private final Path mDocletPath = new Path(getProject());

    private final Path mClassPath = new Path(getProject());

    private final List mFormatters = new ArrayList();

    /** Source path - list of SourceDirectory. */
    private final List mSources = new ArrayList();

    /** Cruise Control label */
    private String mCcLabel;

    /** company name */
    private String mCompanyName = DEFAULT_COMPANY_NAME;

    /** company logo without suffix */
    private String mCompanyLogo = DEFAULT_COMPANY_LOGO;

    /** flag for execution of validation tasks only */
    private boolean mValidationOnly = DEFAULT_VALIDATION_ONLY_FLAG;

    /** List of Variables set as Properties in the Transformer context. */
    private final List mTransformerProperties = new ArrayList();

    private HibernateInfoData mHibernateInfoData;

    /**
     * Add the given property to be sent to the transformer.
     *
     * @param var the property to be sent to the transformer.
     */
    public void addParam (org.apache.tools.ant.types.Environment.Variable var)
    {
        mTransformerProperties.add(var);
    }

    void setXdocTransformerParams (Transformer transformer)
    {
        // TODO: Implement this in the corresponding first pass style
        // sheets
        // or remove this!
        transformer.setParameter("basedir", getProject().getBaseDir()
            .toString());
        transformer.setParameter("cclabel", mCcLabel);
        transformer.setParameter("user", System.getProperty("user.name"));
        transformer.setParameter("companyname", mCompanyName);
        transformer.setParameter("companylogo", mCompanyLogo);
        final Iterator i = mTransformerProperties.iterator();
        while (i.hasNext())
        {
            final org.apache.tools.ant.types.Environment.Variable var = (org.apache.tools.ant.types.Environment.Variable) i
                .next();
            transformer.setParameter(var.getKey(), var.getValue());
        }
    }

    /**
     * Sets the XML input file that contains the document.
     *
     * @param f the XML input file (log message info).
     */
    public void setIn (File f)
    {
        mInFile = f;
    }

    /**
     * Set the destination directory into which the result files should
     * be copied to. This parameter is required.
     *
     * @param dir the name of the destination directory.
     */
    public void setOut (File dir)
    {
        mOutDir = dir;
    }

    /**
     * Set the XEP home directory.
     *
     * @param dir the name of the XEP home directory.
     */
    public void setXephome (File dir)
    {
        mXepHome = dir;
    }

    /**
     * Set the document type.
     *
     * @param type the document type.
     */
    public void setType (String type)
    {
        mType = type;
        mTypeLowerCase = type.toLowerCase(Constants.SYSTEM_LOCALE);
    }

    /**
     * Set the document format.
     *
     * @param format the document format.
     */
    public void setFormat (String format)
    {
        mFormat = format;
    }

    /**
     * Set the document type.
     *
     * @param label the cruise control label.
     */
    public void setCclabel (String label)
    {
        mCcLabel = label;
    }

    /**
     * Set the name of the company or organisation.
     *
     * @param companyName The mCompanyName to set.
     */
    public void setCompanyName (String companyName)
    {
        mCompanyName = companyName;
    }

    /**
     * Set the flag, whether only validation should be executed or not.
     *
     * @param validationOnly The mValidationOnly to set.
     */
    public void setValidationOnly (boolean validationOnly)
    {
        mValidationOnly = validationOnly;
    }

    /**
     * Set the name of the company logo without suffix.
     *
     * @param companyLogo The mCompanyLogo to set.
     */
    public void setCompanyLogo (String companyLogo)
    {
        mCompanyLogo = companyLogo;
    }

    /**
     * Set whether we should fail on an error.
     *
     * @param b Whether we should fail on an error.
     */
    public void setFailonerror (boolean b)
    {
        mFailOnError = b;
    }

    /**
     * Additional path that is used to find the javadoc doclets.
     *
     * @return a path.
     */
    public Path createDocletpath ()
    {
        return mDocletPath;
    }

    /**
     * The classpath that is used to find the classes for the DocBook
     * formatters.
     *
     * @return a path.
     */
    public Path createClasspath ()
    {
        return mClassPath;
    }

    /**
     * Creates a new FormatterInfoData object used as nested element to
     * describe the DocBook formatters.
     *
     * @return a new FormatterInfoData object.
     */
    public FormatterInfoData createFormatter ()
    {
        final FormatterInfoData result = FormatterInfoData.create();
        mFormatters.add(result);
        return result;
    }

    /**
     * Creates a new HibernateInfoData object used as nested element to
     * describe the Hibernate Generator options.
     *
     * @return a new FormatterInfoData object.
     */
    public HibernateInfoData createHibernate ()
    {
        final HibernateInfoData hibernateInfoData = HibernateInfoData.create();
        mHibernateInfoData = hibernateInfoData;
        return hibernateInfoData;
    }

    /**
     * Returns the classpath element.
     *
     * @return the classpath element.
     */
    public Path getClassPath ()
    {
        return mClassPath;
    }

    /**
     * Returns <tt>true</tt> if this task (or subtasks) should fail on
     * any error; <tt>false</tt> otherwise.
     *
     * @return <tt>true</tt> if this task (or subtasks) should fail on
     *         any error; <tt>false</tt> otherwise.
     */
    public boolean failOnError ()
    {
        return mFailOnError;
    }

    /**
     * Set the source path to be used for this task run.
     *
     * @param src an Ant FileSet object containing the compilation
     *        source path.
     */
    public void addSrc (SourceDirectory src)
    {
        mSources.add(src);
    }

    /**
     * Execute this task.
     *
     * @throws BuildException An building exception occurred.
     */
    public void execute ()
        throws BuildException
    {
        try
        {
            checkAttributes();
            log("Generating documentation into directory " + mOutDir);
            final File imageDir = new File(mOutDir, IMAGE_DIR);
            // convertPackageHtml2DocBook();
            final File filePassOne = transformPassOne(mInFile);
            if (TYPE_SAD.equals(mType))
            {
                generateApiDocs(filePassOne);
                generateSadDiagrams(filePassOne);
            }
            else if (TYPE_USE_CASE.equals(mType))
            {
                if (!mValidationOnly)
                {
                    generateUseCaseDiagrams(filePassOne, imageDir);
                    exportToXmi(filePassOne, imageDir);
                    AntTaskUtil.renderDotFiles(this, imageDir, mFailOnError);
                    exportToHbCfg(filePassOne);
                    exportToHbm(filePassOne);
                }
            }
            else if (TYPE_KPI_STATS.equals(mType))
            {
                final File kpiFile = transformPassTwo(filePassOne);
                File newFile = new File(AntTaskUtil.stripFileExtension(AntTaskUtil
                    .stripFileExtension(kpiFile.getAbsolutePath())));
                kpiFile.renameTo(newFile);
            }
            else if (TYPE_KPI_REPORT.equals(mType))
            {
                generateKeyPerformanceDiagrams(filePassOne, imageDir);
                AntTaskUtil.renderGnuplotFiles(this, imageDir, mFailOnError);
            }
            else if (TYPE_TEST_SPEC.equals(mType))
            {
                // Nothing to do
            }
            else if (TYPE_QUALITY_REPORT.equals(mType))
            {
                // Nothing to do
            }
            else if (TYPE_RELEASE_NOTES.equals(mType))
            {<commit_msg>deactivated xmi export<commit_after>/*
 * $Id$
 *
 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer in the documentation and/or other materials
 *      provided with the distribution.
 *    * Neither the name of the jCoderZ.org Project nor the names of
 *      its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written
 *      permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.jcoderz.commons.taskdefs;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.transform.Transformer;

import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.XMLAbstractTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.jcoderz.commons.util.Constants;
import org.jcoderz.commons.util.IoUtil;


/**
 * Xtreme Documentation Ant task.
 *
 * @author <NAME>
 */
public class XtremeDocs
    extends Task
{
    private static final String FORMAT_PDF = "PDF";

    private static final String FORMAT_HTML = "HTML";

    private static final String FORMAT_ALL = "ALL";

    private static final String FORMAT_NONE = "NONE";

    private static final String TYPE_QUALITY_REPORT = "Quality-Report";
    
    private static final String TYPE_KPI_STATS = "KPI-Stats";
    
    private static final String TYPE_KPI_REPORT = "KPI-Report";

    private static final String TYPE_RELEASE_NOTES = "Release-Notes";

    private static final String TYPE_TEST_SPEC = "TestSpec";

    private static final String TYPE_USE_CASE = "UseCase";

    private static final String TYPE_SAD = "SAD";

    private static final String IMAGE_DIR = "images";

    private static final String APIDOC_DIR = "apidoc";

    private static final String DEFAULT_COMPANY_NAME = "jCoderZ.org";

    private static final String DEFAULT_COMPANY_LOGO = "jcoderz-org";

    private static final boolean DEFAULT_VALIDATION_ONLY_FLAG = false;

    /** The output directory. */
    private File mOutDir;

    /** XEP home directory. */
    private File mXepHome;

    /** The input file. */
    private File mInFile;

    /** terminate ant build on error. */
    private boolean mFailOnError;

    /** Type of document. (SAD|UseCase) */
    private String mType;

    /** Format of document. (NONE|HTML|PDF|ALL) */
    private String mFormat;

    private String mTypeLowerCase;

    private final Path mDocletPath = new Path(getProject());

    private final Path mClassPath = new Path(getProject());

    private final List mFormatters = new ArrayList();

    /** Source path - list of SourceDirectory. */
    private final List mSources = new ArrayList();

    /** Cruise Control label */
    private String mCcLabel;

    /** company name */
    private String mCompanyName = DEFAULT_COMPANY_NAME;

    /** company logo without suffix */
    private String mCompanyLogo = DEFAULT_COMPANY_LOGO;

    /** flag for execution of validation tasks only */
    private boolean mValidationOnly = DEFAULT_VALIDATION_ONLY_FLAG;

    /** List of Variables set as Properties in the Transformer context. */
    private final List mTransformerProperties = new ArrayList();

    private HibernateInfoData mHibernateInfoData;

    /**
     * Add the given property to be sent to the transformer.
     *
     * @param var the property to be sent to the transformer.
     */
    public void addParam (org.apache.tools.ant.types.Environment.Variable var)
    {
        mTransformerProperties.add(var);
    }

    void setXdocTransformerParams (Transformer transformer)
    {
        // TODO: Implement this in the corresponding first pass style
        // sheets
        // or remove this!
        transformer.setParameter("basedir", getProject().getBaseDir()
            .toString());
        transformer.setParameter("cclabel", mCcLabel);
        transformer.setParameter("user", System.getProperty("user.name"));
        transformer.setParameter("companyname", mCompanyName);
        transformer.setParameter("companylogo", mCompanyLogo);
        final Iterator i = mTransformerProperties.iterator();
        while (i.hasNext())
        {
            final org.apache.tools.ant.types.Environment.Variable var = (org.apache.tools.ant.types.Environment.Variable) i
                .next();
            transformer.setParameter(var.getKey(), var.getValue());
        }
    }

    /**
     * Sets the XML input file that contains the document.
     *
     * @param f the XML input file (log message info).
     */
    public void setIn (File f)
    {
        mInFile = f;
    }

    /**
     * Set the destination directory into which the result files should
     * be copied to. This parameter is required.
     *
     * @param dir the name of the destination directory.
     */
    public void setOut (File dir)
    {
        mOutDir = dir;
    }

    /**
     * Set the XEP home directory.
     *
     * @param dir the name of the XEP home directory.
     */
    public void setXephome (File dir)
    {
        mXepHome = dir;
    }

    /**
     * Set the document type.
     *
     * @param type the document type.
     */
    public void setType (String type)
    {
        mType = type;
        mTypeLowerCase = type.toLowerCase(Constants.SYSTEM_LOCALE);
    }

    /**
     * Set the document format.
     *
     * @param format the document format.
     */
    public void setFormat (String format)
    {
        mFormat = format;
    }

    /**
     * Set the document type.
     *
     * @param label the cruise control label.
     */
    public void setCclabel (String label)
    {
        mCcLabel = label;
    }

    /**
     * Set the name of the company or organisation.
     *
     * @param companyName The mCompanyName to set.
     */
    public void setCompanyName (String companyName)
    {
        mCompanyName = companyName;
    }

    /**
     * Set the flag, whether only validation should be executed or not.
     *
     * @param validationOnly The mValidationOnly to set.
     */
    public void setValidationOnly (boolean validationOnly)
    {
        mValidationOnly = validationOnly;
    }

    /**
     * Set the name of the company logo without suffix.
     *
     * @param companyLogo The mCompanyLogo to set.
     */
    public void setCompanyLogo (String companyLogo)
    {
        mCompanyLogo = companyLogo;
    }

    /**
     * Set whether we should fail on an error.
     *
     * @param b Whether we should fail on an error.
     */
    public void setFailonerror (boolean b)
    {
        mFailOnError = b;
    }

    /**
     * Additional path that is used to find the javadoc doclets.
     *
     * @return a path.
     */
    public Path createDocletpath ()
    {
        return mDocletPath;
    }

    /**
     * The classpath that is used to find the classes for the DocBook
     * formatters.
     *
     * @return a path.
     */
    public Path createClasspath ()
    {
        return mClassPath;
    }

    /**
     * Creates a new FormatterInfoData object used as nested element to
     * describe the DocBook formatters.
     *
     * @return a new FormatterInfoData object.
     */
    public FormatterInfoData createFormatter ()
    {
        final FormatterInfoData result = FormatterInfoData.create();
        mFormatters.add(result);
        return result;
    }

    /**
     * Creates a new HibernateInfoData object used as nested element to
     * describe the Hibernate Generator options.
     *
     * @return a new FormatterInfoData object.
     */
    public HibernateInfoData createHibernate ()
    {
        final HibernateInfoData hibernateInfoData = HibernateInfoData.create();
        mHibernateInfoData = hibernateInfoData;
        return hibernateInfoData;
    }

    /**
     * Returns the classpath element.
     *
     * @return the classpath element.
     */
    public Path getClassPath ()
    {
        return mClassPath;
    }

    /**
     * Returns <tt>true</tt> if this task (or subtasks) should fail on
     * any error; <tt>false</tt> otherwise.
     *
     * @return <tt>true</tt> if this task (or subtasks) should fail on
     *         any error; <tt>false</tt> otherwise.
     */
    public boolean failOnError ()
    {
        return mFailOnError;
    }

    /**
     * Set the source path to be used for this task run.
     *
     * @param src an Ant FileSet object containing the compilation
     *        source path.
     */
    public void addSrc (SourceDirectory src)
    {
        mSources.add(src);
    }

    /**
     * Execute this task.
     *
     * @throws BuildException An building exception occurred.
     */
    public void execute ()
        throws BuildException
    {
        try
        {
            checkAttributes();
            log("Generating documentation into directory " + mOutDir);
            final File imageDir = new File(mOutDir, IMAGE_DIR);
            // convertPackageHtml2DocBook();
            final File filePassOne = transformPassOne(mInFile);
            if (TYPE_SAD.equals(mType))
            {
                generateApiDocs(filePassOne);
                generateSadDiagrams(filePassOne);
            }
            else if (TYPE_USE_CASE.equals(mType))
            {
                if (!mValidationOnly)
                {
                    generateUseCaseDiagrams(filePassOne, imageDir);
                    //exportToXmi(filePassOne, imageDir);
                    AntTaskUtil.renderDotFiles(this, imageDir, mFailOnError);
                    exportToHbCfg(filePassOne);
                    exportToHbm(filePassOne);
                }
            }
            else if (TYPE_KPI_STATS.equals(mType))
            {
                final File kpiFile = transformPassTwo(filePassOne);
                File newFile = new File(AntTaskUtil.stripFileExtension(AntTaskUtil
                    .stripFileExtension(kpiFile.getAbsolutePath())));
                kpiFile.renameTo(newFile);
            }
            else if (TYPE_KPI_REPORT.equals(mType))
            {
                generateKeyPerformanceDiagrams(filePassOne, imageDir);
                AntTaskUtil.renderGnuplotFiles(this, imageDir, mFailOnError);
            }
            else if (TYPE_TEST_SPEC.equals(mType))
            {
                // Nothing to do
            }
            else if (TYPE_QUALITY_REPORT.equals(mType))
            {
                // Nothing to do
            }
            else if (TYPE_RELEASE_NOTES.equals(mType))
            {
{'fraction_non_alphanumeric': 0.0825948975806784, 'fraction_numerical': 0.0004121501875283353, 'mean_word_length': 2.3315941233008375, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4935345', 'n_tokens_mistral': 6825, 'n_tokens_neox': 6525, 'n_words': 2088}