package org.jivesoftware.smackx.ikey.util.canonicalization;

import org.apache.xml.security.c14n.CanonicalizationException;
import org.apache.xml.security.parser.XMLParserException;
import org.jivesoftware.smack.packet.Element;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public interface ElementCanonicalizer {

    default byte[] canonicalize(Element e) throws XMLParserException, IOException, CanonicalizationException {
        String xml = e.toXML().toString();
        return canonicalize(xml);
    }

    default byte[] canonicalize(CharSequence xml) throws XMLParserException, IOException, CanonicalizationException {
        return canonicalize(xml.toString().getBytes(StandardCharsets.UTF_8));
    }

    byte[] canonicalize(byte[] xml) throws XMLParserException, IOException, CanonicalizationException;

    default String removeInterElementWhitespace(String xml) {
        return xml.replaceAll(">\\s*<", "><");
    }
}
