/*
 * 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;

import java.io.FileOutputStream;

import org.apache.xml.security.utils.XMLUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.security.cert.X509Certificate;  // juste pour le main
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

/**
 *   classe pour encapsuler la verification d'une signature
 *  Implementation avec la librarie Apache Xml Security (d'ou le prefixe APXS)
 *  Montre un traitement basique = reporter une signature globale dans
 *  tous les nodes "bordereau" // NB c'est un report textuel pas une signature
 */
public  class APXSSignatureReporter extends APXSSignatureVerifier{


    /**
     *  Constructeur sans parametre
     */
    public APXSSignatureReporter() {
       
    };
 
    
    @Override
    public boolean postTraitement(String outfilename) throws Exception{
        if( signatureFound() ) {
            // 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);
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xp = xpf.newXPath();
            xp.setNamespaceContext(nsCtx);
            NodeList nodesBordereaux = (NodeList)xp.evaluate("//n:Bordereau", doc, XPathConstants.NODESET);
              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();
              setPosttraitOK(true);
              return true;
        }
        return false;
    }
   
    @Override
   public boolean hasPostTrait() { return true;} 


    static {
        org.apache.xml.security.Init.init();
    }

    /**
     * 
     * @param args
     */
//    public  static void main( String args[]) {
//
//        APXSSignatureReporter verifier = new APXSSignatureReporter();
//         // check args
//        String fileToVerify = null;
//        String fileToGenerate = null;
//        if (args.length > 1) {
//            fileToVerify = args[0];
//            fileToGenerate = args[1];
//        } else {
//
//            System.err.println("Usage java APXSGlobalSignatureReporter <fichier_a_verifier> <fichier_a_generer>");
//            System.exit(1);
//        }
//
//   
//
//        try {
//            verifier.initialize();
//            verifier.verifyFile(fileToVerify);
//            for (int i = 0; i < verifier.getCertVectorSize(); ++i) {
//                X509Certificate cert = verifier.getCert(i);
//                if (cert != null) {
//                    System.out.println("PRINTING CERT=");
//                    System.out.println(cert);
//                }
//            }
//            verifier.postTraitement(fileToGenerate);
//            System.out.println(verifier.getStatusAsString());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

}

