<commit_before>package plugin.exporter;

import org.w3c.dom.Document;
import plugin.OperationNameIcon;
import plugin.PluginsSettings;

import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.awt.*;
import java.io.*;
import java.util.HashMap;

/**
 * Created by joseph on 20/10/16.
 */
public class DefaultDiderotDocumentationExporter extends DefaultDiderotProjectExporter
{
	static private HashMap<String, OperationNameIcon> availableOperations = new HashMap<>();

	static
	{
		availableOperations.put("Generate documentation", new OperationNameIcon("generateHtmlDocumentation", "exportweb"));
	}

	//method strongly inspired by kayz1's solution here: http://stackoverflow.com/questions/6214703/copy-entire-directory-contents-to-another-directory
	static public boolean copyDirectory(File source, File destination)
	{
		if (source.isDirectory())
		{
			if (!destination.exists())
			{
				destination.mkdirs();
			}

			String files[] = source.list();

			for (String file : files)
			{
				File srcFile = new File(source, file);
				File destFile = new File(destination, file);

				if(!copyDirectory(srcFile, destFile))
				{
					return false;
				}
			}
		}
		else
		{
			InputStream in = null;
			OutputStream out = null;

			try
			{
				in = new FileInputStream(source);
				out = new FileOutputStream(destination);

				byte[] buffer = new byte[1024];

				int length;
				while ((length = in.read(buffer)) > 0)
				{
					out.write(buffer, 0, length);
				}
			}
			catch (Exception e)
			{
				try
				{
					in.close();
				}
				catch (IOException e1)
				{
					e1.printStackTrace();
					return false;
				}

				try
				{
					out.close();
				}
				catch (IOException e1)
				{
					e1.printStackTrace();
					return false;
				}
				return false;
			}
		}

		return true;
	}

	@Override
	public HashMap<String, OperationNameIcon> getAvailableExportingOperations()
	{
		return availableOperations;
	}

	@Override
	public String getPluginName()
	{
		return "Diredot default documentation exporter";
	}

	@Override
	public String getPluginAuthor()
	{
		return "<NAME>";
	}

	@Override
	public String getPluginContactInformation()
	{
		return "https://github.com/JosephCaillet/Diderot";
	}

	@Override
	public String getPluginVersion()
	{
		return "0.1";
	}

	@Override
	public String getPluginDescription()
	{
		return "Generate Html documentation of your routes";
	}

	public void generateHtmlDocumentation()
	{
		String fileName = PluginsSettings.getPropertyValue(getPluginName() + "documentationFolder");
		if(fileName == null)
		{
			fileName = PluginsSettings.getPropertyValue("Diderot default project exporter" + "projectFileName", ".");
		}

		JFileChooser fileChooser = new JFileChooser(fileName);
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		if(JFileChooser.APPROVE_OPTION != fileChooser.showOpenDialog(parent))
		{
			return;
		}

		String docPath = fileChooser.getSelectedFile().getAbsolutePath();
		if(!docPath.endsWith("/"))
		{
			docPath = docPath + "/";
		}

		PluginsSettings.setPropertyValue(getPluginName() + "documentationFolder", docPath);

		try
		{
			Document xmlSaveDocument = createDocument();

			StreamSource streamSource = new StreamSource("generateDocumentation.xsl");

			Transformer xmlTransformer = TransformerFactory.newInstance().newTransformer(streamSource);
			xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
			xmlTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

			copyDirectory(new File("documentationTemplate/"), new File(docPath));

			File f = new File(docPath + "index.html");
			xmlTransformer.transform(new DOMSource(xmlSaveDocument), new StreamResult(f));

<commit_msg>directory chooser for documentation generation set the opened directory as selected<commit_after>package plugin.exporter;

import org.w3c.dom.Document;
import plugin.OperationNameIcon;
import plugin.PluginsSettings;

import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.awt.*;
import java.io.*;
import java.util.HashMap;

/**
 * Created by joseph on 20/10/16.
 */
public class DefaultDiderotDocumentationExporter extends DefaultDiderotProjectExporter
{
	static private HashMap<String, OperationNameIcon> availableOperations = new HashMap<>();

	static
	{
		availableOperations.put("Generate documentation", new OperationNameIcon("generateHtmlDocumentation", "exportweb"));
	}

	//method strongly inspired by kayz1's solution here: http://stackoverflow.com/questions/6214703/copy-entire-directory-contents-to-another-directory
	static public boolean copyDirectory(File source, File destination)
	{
		if (source.isDirectory())
		{
			if (!destination.exists())
			{
				destination.mkdirs();
			}

			String files[] = source.list();

			for (String file : files)
			{
				File srcFile = new File(source, file);
				File destFile = new File(destination, file);

				if(!copyDirectory(srcFile, destFile))
				{
					return false;
				}
			}
		}
		else
		{
			InputStream in = null;
			OutputStream out = null;

			try
			{
				in = new FileInputStream(source);
				out = new FileOutputStream(destination);

				byte[] buffer = new byte[1024];

				int length;
				while ((length = in.read(buffer)) > 0)
				{
					out.write(buffer, 0, length);
				}
			}
			catch (Exception e)
			{
				try
				{
					in.close();
				}
				catch (IOException e1)
				{
					e1.printStackTrace();
					return false;
				}

				try
				{
					out.close();
				}
				catch (IOException e1)
				{
					e1.printStackTrace();
					return false;
				}
				return false;
			}
		}

		return true;
	}

	@Override
	public HashMap<String, OperationNameIcon> getAvailableExportingOperations()
	{
		return availableOperations;
	}

	@Override
	public String getPluginName()
	{
		return "Diredot default documentation exporter";
	}

	@Override
	public String getPluginAuthor()
	{
		return "<NAME>";
	}

	@Override
	public String getPluginContactInformation()
	{
		return "https://github.com/JosephCaillet/Diderot";
	}

	@Override
	public String getPluginVersion()
	{
		return "0.1";
	}

	@Override
	public String getPluginDescription()
	{
		return "Generate Html documentation of your routes";
	}

	public void generateHtmlDocumentation()
	{
		String fileName = PluginsSettings.getPropertyValue(getPluginName() + "documentationFolder");
		if(fileName == null)
		{
			fileName = PluginsSettings.getPropertyValue("Diderot default project exporter" + "projectFileName", ".");
		}

		JFileChooser fileChooser = new JFileChooser(fileName);
		fileChooser.setSelectedFile(new File("."));
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		if(JFileChooser.APPROVE_OPTION != fileChooser.showOpenDialog(parent))
		{
			return;
		}

		String docPath = fileChooser.getSelectedFile().getAbsolutePath();
		if(!docPath.endsWith("/"))
		{
			docPath = docPath + "/";
		}

		PluginsSettings.setPropertyValue(getPluginName() + "documentationFolder", docPath);

		try
		{
			Document xmlSaveDocument = createDocument();

			StreamSource streamSource = new StreamSource("generateDocumentation.xsl");

			Transformer xmlTransformer = TransformerFactory.newInstance().newTransformer(streamSource);
			xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
			xmlTransformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

			copyDirectory(new File("documentationTemplate/"), new File(docPath));

			File f = new File(docPath + "index.html");
			xmlTransformer.transform(new DOMSource(xmlSaveDocument), new StreamResult(f));


{'fraction_non_alphanumeric': 0.11467219107969778, 'fraction_numerical': 0.006824274920789666, 'mean_word_length': 4.171392564587271, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 30}, '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': '255595', 'n_tokens_mistral': 2921, 'n_tokens_neox': 2437, 'n_words': 514}