/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2008 L.Meckert - laurent.meckert@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.plugin.signature;


/**
 *   classe pour encapsuler la verification d'une signature
 * Implementation avec la librarie xmlsec / libxml en JNI (d'ou le prefixe LXJNI)
 *  NE SAIT RIEN FAIRE (reste a implementer dans la partie C)
 */
public  class LXJNISignatureReporter extends LXJNISignatureVerifier{

    /**
     *  Constructeur sans parametre
     */
    public LXJNISignatureReporter() {};
 
    @Override
    public boolean postTraitement(String outfilename) throws Exception{
        if( signatureFound() ) {
            nativereportSignature( outfilename);
            // Maintenant le travali consiste a inserer dans chaque element Bordereau 
            // une copie de ce bloc signature a savoir le node sigElement trouve plus haut
            //    NodeList nodesBordereaux = XPathAPI.selectNodeList(doc,
            //                                                                "//Bordereau", nscontext);
            //                   for (int i = 0; i < nodesBordereaux.getLength(); i++) {
            //                        Element bordereauElement = (Element) nodesBordereaux.item(i);
            //                         bordereauElement.appendChild(sigElement.cloneNode(true));
            
            //                   }
            //                   // Sauver le tout dans un fichier 
            //                   FileOutputStream fout = new FileOutputStream(outfilename);
            //                   XMLUtils.outputDOM(doc, fout, true);
            //                   fout.close();
            return true;
            }
        return false;
    }

    

      //Methodes appelle en C 

    /**
     * 
     * @param outFilename
     * @return int 0/1 succes/echec
     */
    public native int nativereportSignature(String outFilename);

    /**
     * 
     * @param args
     */
    public  static void main( String args[]) {
        LXJNISignatureReporter reporter = new LXJNISignatureReporter();
        try {
            reporter.initialize();
            if(reporter.getStatus() == 0) {
            reporter.verifyFile(args[0]);

            System.out.println("statut apres verification=" + reporter.getStatus());
            System.out.println("Element signature as xmlString =" + reporter.getSignatureAsXmlString());
            reporter.postTraitement(args[1]);

            }
            reporter.terminate();
        } catch(Exception e) {

            e.printStackTrace();
        }


   }
};

