<commit_before>package ch.openech.xml.write;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Some calculations between namespace URIs, Locations and Version. Ech convention specific.
 * Not pretty, but at least there are some JUnit-Tests
 * 
 */
public class EchNamespaceUtil {

	private static final String ECH_NS_BASE = "http://www.ech.ch/xmlns/eCH-";

	private static Map<String, String> LOCAL_COPIES = new HashMap<>();
	
	static {
		LOCAL_COPIES.put("http://www.open-ech.ch/xmlns/open-eCH/1", "/ch/openech/xmlns/Open-eCH-1-0.xsd");
		LOCAL_COPIES.put("http://www.ech.ch/xmlns/eCH-0147/1/eCH-0147T0.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T0.xsd");
		LOCAL_COPIES.put("http://www.ech.ch/xmlns/eCH-0147/1/eCH-0147T1.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T1.xsd");
		LOCAL_COPIES.put("http://www.ech.ch/xmlns/eCH-0147/1/eCH-0147T2.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T2.xsd");
		LOCAL_COPIES.put("eCH-0147T0.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T0.xsd");
		LOCAL_COPIES.put("eCH-0147T1.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T1.xsd");
		LOCAL_COPIES.put("eCH-0147T2.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T2.xsd");
		LOCAL_COPIES.put("http://www.ech.ch/xmlns/eCH-0211/1/eCH-0211-1-0.xsd", "/ch/ech/xmlns/eCH-0211-1-0.xsd");
	}
	
	public static InputStream getLocalCopyOfSchema(String namespaceLocation) {
		String fileName;
		if (!LOCAL_COPIES.containsKey(namespaceLocation)) {
			int pos = namespaceLocation.lastIndexOf("/");
			fileName = "/ch/ech/xmlns" + namespaceLocation.substring(pos);

		} else {
			fileName = LOCAL_COPIES.get(namespaceLocation);
		}
		return EchNamespaceUtil.class.getResourceAsStream(fileName);
	}

	// Aufgrund von Konventionen für URI und Location verwendbar
	public static int extractSchemaNumber(String namespaceLocationOrURI) {
		if (namespaceLocationOrURI.startsWith(ECH_NS_BASE) && namespaceLocationOrURI.length() >= ECH_NS_BASE.length() + 4) {
			return  Integer.parseInt(namespaceLocationOrURI.substring(ECH_NS_BASE.length(), ECH_NS_BASE.length() + 4));
		} else {
			return -1;
		}
	}
	
	// http://www.ech.ch/xmlns/eCH-0044/2/eCH-0044-2-0.xsd -> 2.0
	public static String extractSchemaVersion(String namespaceLocation) {
		if (namespaceLocation.startsWith(ECH_NS_BASE) && namespaceLocation.length() >= ECH_NS_BASE.length() + 6) {
			int versionStart = ECH_NS_BASE.length() + 5;
			int versionEnd = namespaceLocation.indexOf('/', versionStart);
			
			// now skip 3 '-'
			int minorVersionStart = namespaceLocation.indexOf('-', versionEnd);
			minorVersionStart = namespaceLocation.indexOf('-', minorVersionStart + 1);
			minorVersionStart = namespaceLocation.indexOf('-', minorVersionStart + 1) + 1;

			int minorVersionEnd = namespaceLocation.indexOf(".xsd", minorVersionStart);
			return  namespaceLocation.substring(versionStart, versionEnd) + "." + namespaceLocation.substring(minorVersionStart, minorVersionEnd);
		} else {
			return null;
		}
	}

	// Aufgrund von Konventionen für URI und Location verwendbar
	public static int extractSchemaMajorVersion(String namespaceLocationOrURI) {
		if (namespaceLocationOrURI.startsWith(ECH_NS_BASE) && namespaceLocationOrURI.length() >= ECH_NS_BASE.length() + 6) {
			int versionStart = ECH_NS_BASE.length() + 5;
			int versionEnd = namespaceLocationOrURI.indexOf('/', versionStart);
			if (versionEnd < 0) versionEnd = namespaceLocationOrURI.length();
			return  Integer.parseInt(namespaceLocationOrURI.substring(versionStart, versionEnd));
		} else {
			return -1;
		}
	}

	// Da nur die Location die Minor Version enthält für URI nicht anwendbar
	public static int extractSchemaMinorVersion(String namespaceLocation) {
		if (namespaceLocation.contains("0147")) {
			return -1;
		}
		if (namespaceLocation.startsWith(ECH_NS_BASE) && namespaceLocation.length() >= ECH_NS_BASE.length() + 6) {
			int versionStart = ECH_NS_BASE.length() + 5;
			int versionEnd = namespaceLocation.indexOf('/', versionStart);
			
			// now skip 3 '-'
			int minorVersionStart = namespaceLocation.indexOf('-', versionEnd);
			minorVersionStart = namespaceLocation.indexOf('-', minorVersionStart + 1);
			minorVersionStart = namespaceLocation.indexOf('-', minorVersionStart + 1) + 1;
			
			int minorVersionEnd = namespaceLocation.indexOf(".xsd", minorVersionStart);
			return  Integer.parseInt(namespaceLocation.substring(minorVersionStart, minorVersionEnd));
		} else {
			return -1;
		}
	}

	// http://www.ech.ch/xmlns/eCH-0044/2 -> http://www.ech.ch/xmlns/eCH-0044/2/eCH-0044-2-0.xsd
	public static String schemaLocation(String namespaceURI, String minor) {
		int major = extractSchemaMajorVersion(namespaceURI);
		String fileNamePart = namespaceURI.substring(ECH_NS_BASE.length() - 4, ECH_NS_BASE.length() + 4);
		return namespaceURI + "/" + fileNamePart + "-" + major + "-" + minor + ".xsd";
	}<commit_msg>die extract Methoden können auch mit Filenamen funktionieren<commit_after>package ch.openech.xml.write;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Some calculations between namespace URIs, Locations and Version. Ech convention specific.
 * Not pretty, but at least there are some JUnit-Tests
 * 
 */
public class EchNamespaceUtil {

	private static final String ECH_NS_BASE = "http://www.ech.ch/xmlns/eCH-";

	private static Map<String, String> LOCAL_COPIES = new HashMap<>();
	
	static {
		LOCAL_COPIES.put("http://www.open-ech.ch/xmlns/open-eCH/1", "/ch/openech/xmlns/Open-eCH-1-0.xsd");
		LOCAL_COPIES.put("http://www.ech.ch/xmlns/eCH-0147/1/eCH-0147T0.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T0.xsd");
		LOCAL_COPIES.put("http://www.ech.ch/xmlns/eCH-0147/1/eCH-0147T1.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T1.xsd");
		LOCAL_COPIES.put("http://www.ech.ch/xmlns/eCH-0147/1/eCH-0147T2.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T2.xsd");
		LOCAL_COPIES.put("eCH-0147T0.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T0.xsd");
		LOCAL_COPIES.put("eCH-0147T1.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T1.xsd");
		LOCAL_COPIES.put("eCH-0147T2.xsd", "/ch/ech/xmlns/eCH-0147_V1.2_T2.xsd");
		LOCAL_COPIES.put("http://www.ech.ch/xmlns/eCH-0211/1/eCH-0211-1-0.xsd", "/ch/ech/xmlns/eCH-0211-1-0.xsd");
	}
	
	public static InputStream getLocalCopyOfSchema(String namespaceLocation) {
		String fileName;
		if (!LOCAL_COPIES.containsKey(namespaceLocation)) {
			int pos = namespaceLocation.lastIndexOf("/");
			fileName = "/ch/ech/xmlns" + namespaceLocation.substring(pos);

		} else {
			fileName = LOCAL_COPIES.get(namespaceLocation);
		}
		return EchNamespaceUtil.class.getResourceAsStream(fileName);
	}

	// Aufgrund von Konventionen für URI und Location verwendbar
	public static int extractSchemaNumber(String s) {
		if (s.contains("0147")) {
			return 147;
		}
		if (s.contains("eCH-")) {
			int pos = s.lastIndexOf("eCH-") + 4;
			int end = pos;
			while (end < s.length() && Character.isDigit(s.charAt(end))) end++;
			
			try {
				return Integer.parseInt(s.substring(pos, end));
			} catch (Exception x) {
				System.err.println("Error parsing minor version of " + s);
				return -1;
			}
		} else {
			return -1;
		}
	}
	
	// http://www.ech.ch/xmlns/eCH-0044/2/eCH-0044-2-0.xsd -> 2.0
	public static String extractSchemaVersion(String namespaceLocation) {
		if (namespaceLocation.startsWith(ECH_NS_BASE) && namespaceLocation.length() >= ECH_NS_BASE.length() + 6) {
			int versionStart = ECH_NS_BASE.length() + 5;
			int versionEnd = namespaceLocation.indexOf('/', versionStart);
			
			// now skip 3 '-'
			int minorVersionStart = namespaceLocation.indexOf('-', versionEnd);
			minorVersionStart = namespaceLocation.indexOf('-', minorVersionStart + 1);
			minorVersionStart = namespaceLocation.indexOf('-', minorVersionStart + 1) + 1;

			int minorVersionEnd = namespaceLocation.indexOf(".xsd", minorVersionStart);
			return  namespaceLocation.substring(versionStart, versionEnd) + "." + namespaceLocation.substring(minorVersionStart, minorVersionEnd);
		} else {
			return null;
		}
	}

	public static int extractSchemaMajorVersion(String s) {
		if (s.contains("0147")) {
			return -1;
		}
		if (s.contains("eCH-")) {
			int pos = s.lastIndexOf("eCH-") + 4;
			// skip schemanumber
			while (pos < s.length() && Character.isDigit(s.charAt(pos))) pos++;
			// skip -
			pos++;

			int end = pos;
			while (end < s.length() && Character.isDigit(s.charAt(end))) end++;
			
			try {
				return Integer.parseInt(s.substring(pos, end));
			} catch (Exception x) {
				System.err.println("Error parsing minor version of " + s);
				return -1;
			}
		} else {
			return -1;
		}
	}

	// Da nur die Location die Minor Version enthält für URI nicht anwendbar
	public static int extractSchemaMinorVersion(String s) {
		if (s.contains("0147")) {
			return -1;
		}
		if (s.contains("eCH-")) {
			int pos = s.lastIndexOf("eCH-") + 4;
			// skip schemanumber
			while (pos < s.length() && Character.isDigit(s.charAt(pos))) pos++;
			// skip -
			pos++;
			// skip version
			while (pos < s.length() && Character.isDigit(s.charAt(pos))) pos++;
			// skip -
			pos++;

			int end = pos;
			while (end < s.length() && Character.isDigit(s.charAt(end))) end++;
			
			try {
				return Integer.parseInt(s.substring(pos, end));
			} catch (Exception x) {
				System.err.println("Error parsing minor version of " + s);
				return -1;
			}
		} else {
			return -1;
		}
	}

	// http://www.ech.ch/xmlns/eCH-0044/2 -> http://www.ech.ch/xmlns/eCH-0044/2/eCH-0044-2-0.xsd
	public static String schemaLocation(String namespaceURI, String minor) {
		int major = extractSchemaMajorVersion(namespaceURI);
		String fileNamePart = namespaceURI.substring(ECH_NS_BASE.length() - 4, ECH_NS_BASE.length() + 4);
		return namespaceURI + "/" + fileNamePart + "-" + major + "-" + minor + ".xsd";
	}
{'fraction_non_alphanumeric': 0.15415674500465887, 'fraction_numerical': 0.03447561859405736, 'mean_word_length': 3.9768160741885628, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 18, 'xml': 38}, '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': '6033946', 'n_tokens_mistral': 3909, 'n_tokens_neox': 3411, 'n_words': 605}