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

import org.apache.xml.security.c14n.CanonicalizationException;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.parser.XMLParserException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;


public class XmlSecElementCanonicalizer implements ElementCanonicalizer {

    private final Canonicalizer canonicalizer;

    public XmlSecElementCanonicalizer(Canonicalizer canonicalizer) {
        this.canonicalizer = canonicalizer;
    }

    @Override
    public byte[] canonicalize(byte[] xml) throws XMLParserException, IOException, CanonicalizationException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        canonicalizer.canonicalize(xml, out, true);
        return out.toByteArray();
    }
}
