<commit_before>package imj3.draft;

import static imj3.tools.CommonTools.formatColor;
import static java.lang.Math.max;
import static multij.tools.Tools.array;
import static multij.tools.Tools.baseName;
import static multij.tools.Tools.debugPrint;
import static multij.tools.Tools.getResourceAsStream;
import static multij.xml.XMLTools.getNode;
import static multij.xml.XMLTools.getNodes;
import static multij.xml.XMLTools.parse;
import static multij.xml.XMLTools.write;

import imj3.core.Image2D;
import imj3.tools.IMJTools;

import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import multij.tools.CommandLineArgumentsParser;
import multij.tools.IllegalInstantiationException;
import multij.tools.RegexFilter;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author codistmonk (creation 2014-05-28)
 */
public final class AperioXML2SVG {
	
	private AperioXML2SVG() {
		throw new IllegalInstantiationException();
	}
	
	/**
	 * @param commandLineArguments
	 * <br>Must not be null
	 */
	public static final void main(final String[] commandLineArguments) {
		final CommandLineArgumentsParser arguments = new CommandLineArgumentsParser(commandLineArguments);
		final File root = new File(arguments.get("in", ""));
		final File outputRoot = new File(arguments.get("out", root.getPath()));
		final String suffix = arguments.get("suffix", ".xml");
		final File[] aperioFiles = root.listFiles(RegexFilter.newSuffixFilter(suffix));
		final String author = arguments.get("author", "unknown");
		final String seriesId = arguments.get("series", "");
		final String sourceClassesPath = arguments.get("classes", "");
		final Document classesXML = sourceClassesPath.isEmpty() ? parse("<classes nextId=\"1\"/>") : readXML(sourceClassesPath);
		final Element classesRoot = classesXML.getDocumentElement();
		final Map<String, String> classIds = new HashMap<>();
		final int[] nextId = { Integer.decode(classesRoot.getAttribute("nextId")) };
		
		for (final Node node : getNodes(classesXML, "//class")) {
			final Element element = (Element) node;
			
			classIds.put(element.getAttribute("description"), element.getAttribute("id"));
		}
		
		for (final File aperioFile : aperioFiles) {
			debugPrint("aperioFile:", aperioFile);
			
			final Document aperioXML = parse(getResourceAsStream(aperioFile.getPath()));
			final Document svg = parse("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:imj=\"IMJ\"/>");
			final Element svgRoot  = svg.getDocumentElement();
			
			double lastX = 0.0;
			double lastY = 0.0;
			final List<Region> regions = new ArrayList<>();
			
			{
				File imageFile = null;
				
				for (final String imageSuffix : array(".zip", ".svs")) {
					imageFile = new File(baseName(aperioFile.getName()) + imageSuffix);
					
					if (imageFile.isFile()) {
						break;
					}
					
					imageFile = null;
				}
				
				if (imageFile != null) {
					try {
						debugPrint("imageFile:", imageFile);
						final Image2D image = IMJTools.read(imageFile.getPath());
						lastX = image.getWidth() - 1;
						lastY = image.getHeight() - 1;
					} catch (final Exception exception) {
						exception.printStackTrace();
					}
				}
			}
			
			// collect_all_regions:
			for (final Node aperioAnnotation : getNodes(aperioXML, "*//Annotation")) {
				final String className = ((Element) aperioAnnotation).getAttribute("Name");
				final String classDescription = ((Element) getNode(aperioAnnotation, "*//Attribute")).getAttribute("Name");
				final String color = formatColor(Long.decode(((Element) aperioAnnotation).getAttribute("LineColor")));
				final String classId = classIds.computeIfAbsent(classDescription, d -> {
					final Element classElement = (Element) classesRoot.appendChild(classesXML.createElement("class"));
					final String result = Integer.toString(nextId[0]++);
					
					classElement.setAttribute("id", result);
					classElement.setAttribute("name", className);
					classElement.setAttribute("description", classDescription);
					classElement.setAttribute("preferredColor", color);
					
					return result;
				});
				
				// collect_class_regions:
				for (final Node aperioRegion : getNodes(aperioAnnotation, "*//Region")) {
					final StringBuilder points = new StringBuilder();
					boolean prependSpace = false;
					final Path2D region = new Path2D.Double();
					boolean regionIsEmpty = true;
					final double size = Double.parseDouble(((Element) aperioRegion).getAttribute("Area"));
					
					region.setWindingRule(Path2D.WIND_EVEN_ODD);
					
					for (final Node aperioVertex : getNodes(aperioRegion, "*//Vertex")) {
						if (prependSpace) {
							points.append(' ');
						} else {
							prependSpace = true;
						}<commit_msg>[imj3][draft][AperioXML2SVG] Fix a defect (name + description as key).<commit_after>package imj3.draft;

import static imj3.tools.CommonTools.formatColor;
import static java.lang.Math.max;
import static multij.tools.Tools.array;
import static multij.tools.Tools.baseName;
import static multij.tools.Tools.debugPrint;
import static multij.tools.Tools.getResourceAsStream;
import static multij.xml.XMLTools.getNode;
import static multij.xml.XMLTools.getNodes;
import static multij.xml.XMLTools.parse;
import static multij.xml.XMLTools.write;

import imj3.core.Image2D;
import imj3.tools.IMJTools;

import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import multij.tools.CommandLineArgumentsParser;
import multij.tools.IllegalInstantiationException;
import multij.tools.RegexFilter;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author codistmonk (creation 2014-05-28)
 */
public final class AperioXML2SVG {
	
	private AperioXML2SVG() {
		throw new IllegalInstantiationException();
	}
	
	/**
	 * @param commandLineArguments
	 * <br>Must not be null
	 */
	public static final void main(final String[] commandLineArguments) {
		final CommandLineArgumentsParser arguments = new CommandLineArgumentsParser(commandLineArguments);
		final File root = new File(arguments.get("in", ""));
		final File outputRoot = new File(arguments.get("out", root.getPath()));
		final String suffix = arguments.get("suffix", ".xml");
		final File[] aperioFiles = root.listFiles(RegexFilter.newSuffixFilter(suffix));
		final String author = arguments.get("author", "unknown");
		final String seriesId = arguments.get("series", "");
		final String sourceClassesPath = arguments.get("classes", "");
		final Document classesXML = sourceClassesPath.isEmpty() ? parse("<classes nextId=\"1\"/>") : readXML(sourceClassesPath);
		final Element classesRoot = classesXML.getDocumentElement();
		final Map<String, String> classIds = new HashMap<>();
		final int[] nextId = { Integer.decode(classesRoot.getAttribute("nextId")) };
		
		for (final Node node : getNodes(classesXML, "//class")) {
			final Element element = (Element) node;
			
			classIds.put(element.getAttribute("description"), element.getAttribute("id"));
		}
		
		for (final File aperioFile : aperioFiles) {
			debugPrint("aperioFile:", aperioFile);
			
			final Document aperioXML = parse(getResourceAsStream(aperioFile.getPath()));
			final Document svg = parse("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:imj=\"IMJ\"/>");
			final Element svgRoot  = svg.getDocumentElement();
			
			double lastX = 0.0;
			double lastY = 0.0;
			final List<Region> regions = new ArrayList<>();
			
			{
				File imageFile = null;
				
				for (final String imageSuffix : array(".zip", ".svs")) {
					imageFile = new File(baseName(aperioFile.getName()) + imageSuffix);
					
					if (imageFile.isFile()) {
						break;
					}
					
					imageFile = null;
				}
				
				if (imageFile != null) {
					try {
						debugPrint("imageFile:", imageFile);
						final Image2D image = IMJTools.read(imageFile.getPath());
						lastX = image.getWidth() - 1;
						lastY = image.getHeight() - 1;
					} catch (final Exception exception) {
						exception.printStackTrace();
					}
				}
			}
			
			// collect_all_regions:
			for (final Node aperioAnnotation : getNodes(aperioXML, "*//Annotation")) {
				final String className = ((Element) aperioAnnotation).getAttribute("Name");
				final String classDescription = ((Element) getNode(aperioAnnotation, "*//Attribute")).getAttribute("Name");
				final String color = formatColor(Long.decode(((Element) aperioAnnotation).getAttribute("LineColor")));
				final String classId = classIds.computeIfAbsent(className + ": " + classDescription, d -> {
					final Element classElement = (Element) classesRoot.appendChild(classesXML.createElement("class"));
					final String result = Integer.toString(nextId[0]++);
					
					classElement.setAttribute("id", result);
					classElement.setAttribute("name", className);
					classElement.setAttribute("description", classDescription);
					classElement.setAttribute("preferredColor", color);
					
					debugPrint(result, className, classDescription, color);
					
					return result;
				});
				
				// collect_class_regions:
				for (final Node aperioRegion : getNodes(aperioAnnotation, "*//Region")) {
					final StringBuilder points = new StringBuilder();
					boolean prependSpace = false;
					final Path2D region = new Path2D.Double();
					boolean regionIsEmpty = true;
					final double size = Double.parseDouble(((Element) aperioRegion).getAttribute("Area"));
					
					region.setWindingRule(Path2D.WIND_EVEN_ODD);
					
					for (final Node aperioVertex : getNodes(aperioRegion, "*//Vertex")) {
						if (prependSpace) {
							points.append(' ');
						} else {
							prependSpace = true;
						}
{'fraction_non_alphanumeric': 0.12810956862014347, 'fraction_numerical': 0.006894624056647722, 'mean_word_length': 4.210679611650486, 'pattern_counts': {'":': 1, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, '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': '2994622', 'n_tokens_mistral': 3744, 'n_tokens_neox': 3146, 'n_words': 706}