All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softwaremagico.tm.xml.XmlFactory Maven / Gradle / Ivy

package com.softwaremagico.tm.xml;

/*-
 * #%L
 * Think Machine 4E (Rules)
 * %%
 * Copyright (C) 2017 - 2024 Softwaremagico
 * %%
 * This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
 *  Valencia (Spain).
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; If not, see .
 * #L%
 */

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.softwaremagico.tm.Element;
import com.softwaremagico.tm.character.Selection;
import com.softwaremagico.tm.exceptions.InvalidXmlElementException;
import com.softwaremagico.tm.file.PathManager;
import com.softwaremagico.tm.file.modules.ModuleManager;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public abstract class XmlFactory> {

    private static XmlMapper objectMapper;

    //Id -> Element
    private Map elements = null;
    private List elementList = null;

    public XmlFactory() {

    }

    public abstract String getXmlFile();

    public static ObjectMapper getObjectMapper() {
        if (objectMapper == null) {
            objectMapper = XmlMapper.builder()
                    .configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true)
                    .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
                    .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                    .enable(SerializationFeature.INDENT_OUTPUT)
                    .enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS).serializationInclusion(JsonInclude.Include.NON_EMPTY)
//                    .disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)
                    .addModule(new JavaTimeModule())
                    .build();
        }
        return objectMapper;
    }

    public T getElement(Selection selection) throws InvalidXmlElementException {
        if (elements == null) {
            getElements();
        }
        final T element = elements.get(selection.getId());
        if (element == null) {
            throw new InvalidXmlElementException(this.getClass().getName() + " has no element with selection '" + selection + "'.");
        }
        return element;
    }

    public T getElement(String id) throws InvalidXmlElementException {
        if (elements == null) {
            getElements();
        }
        final T element = elements.get(id);
        if (element == null) {
            throw new InvalidXmlElementException(this.getClass().getName() + " has no element with id '" + id + "'.");
        }
        return element;
    }

    public String getTranslatedText(String id) throws InvalidXmlElementException {
        return getElement(id).getName().getTranslatedText();
    }

    public List getElements(Collection ids) throws InvalidXmlElementException {
        return getElements().stream().filter(t -> ids.contains(t.getId())).collect(Collectors.toList());
    }

    public List getElementsByGroup(String group) throws InvalidXmlElementException {
        return getElements().stream().filter(t -> Objects.equals(group, t.getGroup())).collect(Collectors.toList());
    }

    public abstract List getElements() throws InvalidXmlElementException;

    public List readXml(Class entityClass) throws InvalidXmlElementException {
        try {
            if (elementList == null) {
                elementList = readXml(entityClass, ModuleManager.DEFAULT_MODULE);
            }
            return elementList;
        } catch (IOException e) {
            throw new InvalidXmlElementException("Error reading xml for '" + entityClass + "'", e);
        }
    }

    public List readXml(Class entityClass, String moduleName) throws IOException {
        final Path filePath = Paths.get("../" + PathManager.getModulePath(moduleName) + getXmlFile());
        return readXml(Files.readString(filePath), entityClass);
    }

    public List readXml(String xmlContent, Class entityClass) throws JsonProcessingException {
        final List elements = getObjectMapper().readerForListOf(entityClass).readValue(xmlContent);
        final AtomicInteger order = new AtomicInteger();
        elements.forEach(element -> element.setOrder(order.getAndIncrement()));
        this.elements = new HashMap<>();
        elements.forEach(element -> this.elements.put(element.getId(), element));
        return elements;
    }

    public void validate() throws InvalidXmlElementException {
        for (T element : getElements()) {
            element.validate();
        }
    }

    public List getRestrictedToUpbringing(String uprising) throws InvalidXmlElementException {
        return getElements().stream().filter(t -> t.getRestrictions().getRestrictedToUpbringing().contains(uprising)).collect(Collectors.toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy