<commit_before>package org.limeprotocol.serialization.jackson;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.limeprotocol.Document;
import org.limeprotocol.JsonDocument;
import org.limeprotocol.MediaType;
import org.limeprotocol.PlainDocument;
import org.reflections.Reflections;

import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class SerializationUtil {
    private static Map<MediaType, Class<? extends Document>> documentTypesMap;

    static {
        Reflections reflections = new Reflections("org.limeprotocol.*");

        Set<Class<? extends Document>> documentTypes = reflections.getSubTypesOf(Document.class);

        documentTypesMap = new HashMap<>(documentTypes.size());
        for (Class<? extends Document> documentType : documentTypes) {
            if (!Modifier.isAbstract(documentType.getModifiers())) {
                Document document = null;
                try {
                    document = documentType.getConstructor().newInstance();
                } catch (Exception e) {
                    System.out.println("Document does not have an empty constructor:" + documentType);
                }
                if (document != null) {
                    documentTypesMap.put(document.getMediaType(), documentType);
                }
            }
        }
    }

    public static Document deserializeDocument(ObjectMapper mapper, ObjectNode node, String documentName) {

        JsonNode typeNode = node.get("type");

        if (typeNode == null) {
            return null;
        }

        JsonNode documentNode = node.get(documentName);

        node.remove(documentName);
        node.remove("type");

        MediaType mediaType = mapper.convertValue(typeNode, MediaType.class);

        Class<?> clazz = findDocumentClassFor(mediaType);
        if (clazz == null) {

            if(typeNode.asText().endsWith("+json")){
                clazz = JsonDocument.class;
                JsonDocument jsonDocument = (JsonDocument) mapper.convertValue(documentNode, clazz);
                jsonDocument.setMediaType(MediaType.parse(typeNode.asText()));
                return jsonDocument;
            }

            return new PlainDocument(documentNode.asText(), MediaType.parse(typeNode.asText()));
        }
        return (Document) mapper.convertValue(documentNode, clazz);
    }

    public static Class<? extends Document> findDocumentClassFor(MediaType mediaType) {
        return documentTypesMap.get(mediaType);
    }


}
<commit_msg>Using Logger instead of System.out to register a warning<commit_after>package org.limeprotocol.serialization.jackson;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.limeprotocol.Document;
import org.limeprotocol.JsonDocument;
import org.limeprotocol.MediaType;
import org.limeprotocol.PlainDocument;
import org.reflections.Reflections;

import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

public class SerializationUtil {
    private static Map<MediaType, Class<? extends Document>> documentTypesMap;

    static {
        Reflections reflections = new Reflections("org.limeprotocol.*");

        Set<Class<? extends Document>> documentTypes = reflections.getSubTypesOf(Document.class);

        documentTypesMap = new HashMap<>(documentTypes.size());
        for (Class<? extends Document> documentType : documentTypes) {
            if (!Modifier.isAbstract(documentType.getModifiers())) {
                Document document = null;
                try {
                    document = documentType.getConstructor().newInstance();
                } catch (Exception e) {
                    Logger.getAnonymousLogger().warning("Document does not have an empty constructor:" + documentType);
                }
                if (document != null) {
                    documentTypesMap.put(document.getMediaType(), documentType);
                }
            }
        }
    }

    public static Document deserializeDocument(ObjectMapper mapper, ObjectNode node, String documentName) {

        JsonNode typeNode = node.get("type");

        if (typeNode == null) {
            return null;
        }

        JsonNode documentNode = node.get(documentName);

        node.remove(documentName);
        node.remove("type");

        MediaType mediaType = mapper.convertValue(typeNode, MediaType.class);

        Class<?> clazz = findDocumentClassFor(mediaType);
        if (clazz == null) {

            if(typeNode.asText().endsWith("+json")){
                clazz = JsonDocument.class;
                JsonDocument jsonDocument = (JsonDocument) mapper.convertValue(documentNode, clazz);
                jsonDocument.setMediaType(MediaType.parse(typeNode.asText()));
                return jsonDocument;
            }

            return new PlainDocument(documentNode.asText(), MediaType.parse(typeNode.asText()));
        }
        return (Document) mapper.convertValue(documentNode, clazz);
    }

    public static Class<? extends Document> findDocumentClassFor(MediaType mediaType) {
        return documentTypesMap.get(mediaType);
    }


}

{'fraction_non_alphanumeric': 0.10090389227079874, 'fraction_numerical': 0.0, 'mean_word_length': 2.736733287388008, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '1894043', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1350, 'n_words': 288}