// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.xml.security.Init;
import org.apache.xml.security.c14n.CanonicalizationException;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
import org.xml.sax.SAXException;

public class XmlCanon {
    private static void test(String s) throws InvalidCanonicalizerException, UnsupportedEncodingException, CanonicalizationException, ParserConfigurationException, IOException, SAXException {
        System.out.println(s);
        Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
        String s2 = new String(c14n.canonicalize(s.getBytes(Canonicalizer.ENCODING)), Canonicalizer.ENCODING);
        System.out.println(s2);
    }
    public static void main(String[] args) throws Exception {
        Init.init();
        String s1 = "<all xmlns='http://fun'><one ref='1'>a</one><one ref='2'>bb</one><one ref='3'>ccc</one></all>";
        test(s1);
        String s2 = "<all xmlns=\"http://fun\">\r\n" +
                    "    <one xmlns=\"http://fun\" ref=\"1\">a</one  >\r\n" +
                    "    <one xmlns=\"http://fun\" ref=\"2\">bb</one >\r\n" +
                    "    <one xmlns=\"http://fun\" ref=\"3\">ccc</one>\r\n" +
                    "</all>";
        test(s2);
    }
}
