/*
 * Copyright (c) 2025 Rostock University Library
 * Licensed under the MIT License. See LICENSE file in the project root.
 */
package de.uni.rostock.ub.wsrb.edition.webapp.util;

import java.io.StringWriter;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.Serializer;

public final class WSRBUtil {

    public static final String XML_NS_TEI = "http://www.tei-c.org/ns/1.0";

    public static final String TEI_FILE_EXTENSION = ".tei.xml";

    /**
     * private constructor for utility class
     */
    private WSRBUtil() {
    }

    /**
     * flattens a List, which may contain entries, that should also be separated by ","
     * @param list the list of Strings
     * @return a set of separated Strings
     */
    public static Set<String> flattenParamList(List<String> list) {
        Set<String> result = new TreeSet<>();
        for (String param : list) {
            for (String p : param.split(",")) {
                if (!"".equals(p.trim())) {
                    result.add(p.trim());
                }
            }
        }
        return result;
    }

    /** creates a Serializer for a Saxon processor 
     *  that writes into a StringWriter
     * @param p the Saxon XSLT processor
     * @param sw the StringWriter
     * @return a new Serializer
     */
    public static Serializer createSerializer(Processor p, StringWriter sw) {
        Serializer out = p.newSerializer(sw);
        out.setOutputProperty(Serializer.Property.METHOD, "html");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");
        return out;
    }

}
