SDILottoUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.protocol.sdi.utils;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  24. import org.openspcoop2.utils.xml.DynamicNamespaceContext;
  25. import org.openspcoop2.utils.xml.XMLUtils;
  26. import org.openspcoop2.utils.xml.XPathExpressionEngine;
  27. import org.openspcoop2.utils.xml.XPathNotFoundException;
  28. import org.openspcoop2.utils.xml.XPathReturnType;
  29. import org.w3c.dom.Element;
  30. import org.w3c.dom.Node;
  31. import org.w3c.dom.NodeList;

  32. /**
  33.  * SDIUtils
  34.  *
  35.  * @author Andrea Poli (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class SDILottoUtils {
  40.    
  41.     public static List<byte[]> splitLotto(byte[] lotto) throws Exception {
  42.        
  43.         List<byte[]> risultato = new ArrayList<byte[]>();
  44.        
  45.         AbstractXMLUtils xmlUtils = XMLUtils.getInstance();
  46.         XPathExpressionEngine xpathEngine = new XPathExpressionEngine();
  47.        
  48.         Element lottoElement = null;
  49.         DynamicNamespaceContext dnc = null;
  50.         try{
  51.             lottoElement = xmlUtils.newElement(lotto);
  52.             dnc = new DynamicNamespaceContext();
  53.             dnc.findPrefixNamespace(lottoElement);
  54.         }catch(Exception e){
  55.             throw new Exception("Lettura lotto di fattura non riuscita: "+e.getMessage(),e);
  56.         }
  57.        
  58.         NodeList nl = null;
  59.         try{
  60.             nl = (NodeList) xpathEngine.getMatchPattern(lottoElement, dnc, "//FatturaElettronicaBody", XPathReturnType.NODESET);
  61.         }catch(XPathNotFoundException notFound){
  62.             throw new Exception("La fattura non contiene alcun body?: "+notFound.getMessage(),notFound);
  63.         }catch(Exception e){
  64.             throw new Exception("Estrazione body dal lotto di fattura non riuscita: "+e.getMessage(),e);
  65.         }
  66.         if(nl==null){
  67.             throw new Exception("La fattura non contiene alcun body? (null)");
  68.         }
  69.         if(nl.getLength()<1){
  70.             throw new Exception("La fattura non contiene alcun body? (size): "+nl.getLength());
  71.         }
  72.        
  73.         if(nl.getLength()==1){
  74.             // non si tratta di un lotto, ma di una fattura con un body solamente
  75.             risultato.add(lotto);
  76.             return risultato;
  77.         }
  78.        
  79.         List<Node> listBody = new ArrayList<Node>();
  80.         try{
  81.             for (int i = 0; i < nl.getLength(); i++) {
  82.                 Node n = nl.item(i);
  83.                 listBody.add(n);
  84.                 lottoElement.removeChild(n);
  85.             }
  86.         }catch(Exception e){
  87.             throw new Exception("Elaborazione lotto di fattura (step1) non riuscita: "+e.getMessage(),e);
  88.         }
  89.         int i = 0;
  90.         try{
  91.             for (; i < listBody.size(); i++) {
  92.                 Node n = listBody.get(i);
  93.                 lottoElement.appendChild(n);
  94.                 risultato.add(xmlUtils.toByteArray(lottoElement));
  95.                 lottoElement.removeChild(n);
  96.             }
  97.         }catch(Exception e){
  98.             throw new Exception("Elaborazione lotto di fattura (step2-"+i+") non riuscita: "+e.getMessage(),e);
  99.         }
  100.        
  101.         return risultato;
  102.     }
  103.    
  104. }