<commit_before>package imj2.tools;

import static net.sourceforge.aprog.tools.Tools.baseName;
import static net.sourceforge.aprog.tools.Tools.debugPrint;
import static net.sourceforge.aprog.tools.Tools.getResourceAsStream;
import static net.sourceforge.aprog.tools.Tools.unchecked;
import static net.sourceforge.aprog.xml.XMLTools.getNode;
import static net.sourceforge.aprog.xml.XMLTools.getNodes;
import static net.sourceforge.aprog.xml.XMLTools.parse;
import static net.sourceforge.aprog.xml.XMLTools.write;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

import net.sourceforge.aprog.tools.CommandLineArgumentsParser;
import net.sourceforge.aprog.tools.IllegalInstantiationException;
import net.sourceforge.aprog.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 AperioXML2IMJXML {
	
	private AperioXML2IMJXML() {
		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("root", ""));
		final String suffix = arguments.get("suffix", ".xml");
		final File[] aperioXMLFiles = root.listFiles(RegexFilter.newSuffixFilter(suffix));
		final String author = arguments.get("author", "?");
		final boolean compress = true;
		final boolean useMnemonicAsDescription = true;
		
		for (final File aperioXMLFile : aperioXMLFiles) {
			debugPrint(aperioXMLFile);
			
			final Document aperioXML = parse(getResourceAsStream(aperioXMLFile.getPath()));
			final Element aperioRoot = aperioXML.getDocumentElement();
			final Document imjXML = parse("<annotations/>");
			final Element imjRoot  = imjXML.getDocumentElement();
			
			imjRoot.setAttribute("micronsPerPixel", aperioRoot.getAttribute("MicronsPerPixel"));
			
			final Element imjLabels = (Element) imjRoot.appendChild(imjXML.createElement("labels"));
			final Element imjRegions = (Element) imjRoot.appendChild(imjXML.createElement("regions"));
			final Map<String, String> labelIds = new HashMap<>();
			
			for (final Node aperioAnnotation : getNodes(aperioXML, "*//Annotation")) {
				final Element imjLabel = (Element) imjLabels.appendChild(imjXML.createElement("label"));
				final String mnemonic = ((Element) aperioAnnotation).getAttribute("Name");
				final String labelId = Integer.toString(labelIds.size());
				final String description = ((Element) getNode(aperioAnnotation, "*//Attribute[@Id=0]")).getAttribute("Name");
				
				labelIds.put(description, labelId);
				
				imjLabel.setAttribute("labelId", labelId);
				imjLabel.setAttribute("mnenmonic", mnemonic);
				
				if (useMnemonicAsDescription) {
					imjLabel.setAttribute("description", mnemonic);
				} else {
					imjLabel.setAttribute("description", description);
				}
				
				imjLabel.setAttribute("lineColor", ((Element) aperioAnnotation).getAttribute("LineColor"));
				
				debugPrint(labelId, mnemonic, description);<commit_msg>[IMJ][imj2] Fixed a defect in Aperio XML converter.<commit_after>package imj2.tools;

import static net.sourceforge.aprog.tools.Tools.baseName;
import static net.sourceforge.aprog.tools.Tools.debugPrint;
import static net.sourceforge.aprog.tools.Tools.getResourceAsStream;
import static net.sourceforge.aprog.tools.Tools.unchecked;
import static net.sourceforge.aprog.xml.XMLTools.getNode;
import static net.sourceforge.aprog.xml.XMLTools.getNodes;
import static net.sourceforge.aprog.xml.XMLTools.parse;
import static net.sourceforge.aprog.xml.XMLTools.write;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

import net.sourceforge.aprog.tools.CommandLineArgumentsParser;
import net.sourceforge.aprog.tools.IllegalInstantiationException;
import net.sourceforge.aprog.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 AperioXML2IMJXML {
	
	private AperioXML2IMJXML() {
		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("root", ""));
		final String suffix = arguments.get("suffix", ".xml");
		final File[] aperioXMLFiles = root.listFiles(RegexFilter.newSuffixFilter(suffix));
		final String author = arguments.get("author", "?");
		final boolean compress = true;
		
		for (final File aperioXMLFile : aperioXMLFiles) {
			debugPrint(aperioXMLFile);
			
			final Document aperioXML = parse(getResourceAsStream(aperioXMLFile.getPath()));
			final Element aperioRoot = aperioXML.getDocumentElement();
			final Document imjXML = parse("<annotations/>");
			final Element imjRoot  = imjXML.getDocumentElement();
			
			imjRoot.setAttribute("micronsPerPixel", aperioRoot.getAttribute("MicronsPerPixel"));
			
			final Element imjLabels = (Element) imjRoot.appendChild(imjXML.createElement("labels"));
			final Element imjRegions = (Element) imjRoot.appendChild(imjXML.createElement("regions"));
			final Map<String, String> labelIds = new HashMap<>();
			
			for (final Node aperioAnnotation : getNodes(aperioXML, "*//Annotation")) {
				final Element imjLabel = (Element) imjLabels.appendChild(imjXML.createElement("label"));
				final String mnemonic = ((Element) aperioAnnotation).getAttribute("Name");
				final String labelId = Integer.toString(labelIds.size());
				final String description = ((Element) getNode(aperioAnnotation, "*//Attribute[@Id=0]")).getAttribute("Name");
				
				labelIds.put(description, labelId);
				
				imjLabel.setAttribute("labelId", labelId);
				imjLabel.setAttribute("mnemonic", mnemonic);
				imjLabel.setAttribute("description", description);
				imjLabel.setAttribute("lineColor", ((Element) aperioAnnotation).getAttribute("LineColor"));
				
				debugPrint(labelId, mnemonic, description);
{'fraction_non_alphanumeric': 0.12004353910744829, 'fraction_numerical': 0.0048204011817757735, 'mean_word_length': 5.457831325301205, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '2388349', 'n_tokens_mistral': 2145, 'n_tokens_neox': 1906, 'n_words': 381}