SDIValidatoreNomeFile.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.validator;

  21. import it.gov.fatturapa.sdi.messaggi.v1_0.constants.TipiMessaggi;

  22. import java.net.ProtocolException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Locale;

  26. import org.openspcoop2.protocol.sdi.constants.SDICostanti;
  27. import org.openspcoop2.utils.regexp.RegularExpressionEngine;

  28. /**
  29.  * SDIValidatoreNomeFile
  30.  *
  31.  * @author Andrea Poli (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class SDIValidatoreNomeFile {

  36.     /**
  37.      * NOTE differenze tra progressivo nel nome del File, progressivo all'interno dei messaggi e messagiId
  38.      *
  39.      *
  40.      *  ---Fattura (Progressivo) ---
  41.      *  
  42.      *  Sembrano informazioni differenti.
  43.      *  
  44.      *  Progressivo interno alla fattura:
  45.      *  - descrizione del formatoTabellare: progressivo univoco, attribuito dal soggetto che trasmette, relativo ad ogni singolo documento fattura
  46.      *  - xsd -> <xs:element name="ProgressivoInvio" type="String10Type" /> dove String10Type = (\p{IsBasicLatin}{1,10})
  47.      *  
  48.      *  Progressivo fileName: è relativo al trasmittente
  49.      *  - il progressivo univoco del file è rappresentato da una stringa alfanumerica di lunghezza massima di 5 caratteri e con valori ammessi [a-z], [A-Z], [0-9].
  50.      *  
  51.      *  MessageId metainfomrazioni:
  52.      *  - descrizione: Identificativo del messaggio
  53.      *  - xsd -> <xsd:element name="MessageId" type="types:MessageId_Type"/> dove
  54.      *          <xsd:simpleType name="MessageId_Type">
  55.      *                  <xsd:restriction base="xsd:string">
  56.      *                        <xsd:minLength value="1" />
  57.      *                          <xsd:maxLength value="14" />
  58.      *                  </xsd:restriction>
  59.      *          </xsd:simpleType>
  60.      *  
  61.      *
  62.      *          
  63.      *  ---Fattura (IdPaese e IdCodice) ---
  64.      *  
  65.      *  Sembra la stessa informazione.
  66.      *  
  67.      *  IdTrasmittente interno alla fattura:
  68.      *  - descrizione del formatoTabellare: è l’identificativo univoco del soggetto trasmittente;
  69.      *    per i soggetti residenti in Italia, siano essi persone fisiche o giuridiche,
  70.      *    corrisponde al codice fiscale preceduto da IT; per i soggetti non residenti corrisponde al numero identificativo IVA
  71.      *    (dove i primi due caratteri rappresentano il paese secondo lo standard ISO 3166-1 alpha-2 code, ed i restanti,
  72.      *    fino ad un massimo di 28, il codice vero e proprio)
  73.      *    . idPaese: codice della nazione espresso secondo lo standard ISO 3166-1 alpha-2 code
  74.      *    . idCodice: codice identificativo fiscale
  75.      *  - xsd -> <xs:element name="IdTrasmittente" type="IdFiscaleType" /> dove
  76.      *           <xs:complexType name="IdFiscaleType">
  77.      *              <xs:sequence>
  78.      *                <xs:element name="IdPaese" type="NazioneType" />
  79.      *                <xs:element name="IdCodice" type="CodiceType" />
  80.      *              </xs:sequence>
  81.      *            </xs:complexType>
  82.      *            <xs:simpleType name="NazioneType">
  83.      *              <xs:restriction base="xs:string">
  84.      *                <xs:pattern value="[A-Z]{2}" />
  85.      *              </xs:restriction>
  86.      *            </xs:simpleType>
  87.      *            <xs:simpleType name="CodiceType">
  88.      *              <xs:restriction base="xs:string">
  89.      *                <xs:minLength value="1" />
  90.      *                <xs:maxLength value="28" />
  91.      *              </xs:restriction>
  92.      *            </xs:simpleType>
  93.      *  
  94.      *  IdTrasmittente fileName:
  95.      *  - il codice paese va espresso secondo lo standard ISO 3166-1 alpha-2 code;
  96.      *  - l’identificativo univoco del soggetto trasmittente, sia esso persona fisica o persona giuridica,
  97.      *    è rappresentato dal suo identificativo fiscale (codice fiscale nel caso di soggetto trasmittente residente in Italia,
  98.      *    identificativo proprio del paese di appartenenza nel caso di soggetto trasmittente residente all’estero);
  99.      *    la lunghezza di questo identificativo è di:
  100.      *    . 11 caratteri (minimo) e 16 caratteri (massimo) nel caso di codice paese IT;
  101.      *    . 2 caratteri (minimo) e 28 caratteri (massimo) altrimenti;
  102.      *    
  103.      *    
  104.      *    
  105.      *    
  106.      *  --- Messaggi (Progressivo) ---
  107.      *  
  108.      *  Sembrano informazioni differenti.
  109.      *  
  110.      *  Progressivo interno ai messaggi:
  111.      *  - non esiste
  112.      *  
  113.      *  Progressivo fileName: è relativo al file inviato
  114.      *  - Il Progressivo univoco deve essere una stringa alfanumerica di lunghezza massima 3 caratteri e
  115.      *  con valori ammessi [a-z], [A-Z], [0-9] che identifica univocamente ogni notifica / ricevuta relativa al file inviato.
  116.      *  
  117.      *  MessageId :
  118.      *  - descrizione: Identificativo del messaggio
  119.      *  - xsd -> <xsd:element name="MessageId" type="types:MessageId_Type"/> dove
  120.      *          <xsd:simpleType name="MessageId_Type">
  121.      *                  <xsd:restriction base="xsd:string">
  122.      *                        <xsd:minLength value="1" />
  123.      *                          <xsd:maxLength value="14" />
  124.      *                  </xsd:restriction>
  125.      *          </xsd:simpleType>
  126.      */
  127.    
  128.    
  129.    
  130.    
  131.    
  132.     private static List<String> CODICI_PAESE = null;
  133.     private static synchronized void initCodiciPaese(){
  134.         if(CODICI_PAESE==null){
  135.             CODICI_PAESE = new ArrayList<>();
  136.             String [] l = Locale.getISOCountries();
  137.             for (int i = 0; i < l.length; i++) {
  138.                 CODICI_PAESE.add(l[i]);
  139.             }
  140.         }
  141.     }
  142.     private static void validaCodicePaese(String codicePaese) throws ProtocolException{
  143.         if(CODICI_PAESE==null){
  144.             initCodiciPaese();
  145.         }
  146.         if(CODICI_PAESE.contains(codicePaese)==false){
  147.             throw new ProtocolException("CodicePaese ["+codicePaese+"] sconosciuto");
  148.         }
  149.     }
  150.    
  151.     public static void validaNomeFileFattura(String nomeFile, boolean isRicezione) throws ProtocolException{
  152.        
  153.         /**
  154.          * <codice Paese>< identificativo univoco del soggetto trasmittente >_<progressivoUnicoFile>
  155.          * dove:
  156.          * - il codice paese va espresso secondo lo standard ISO 3166-1 alpha-2 code;
  157.          * - l’identificativo univoco del soggetto trasmittente, sia esso persona fisica o persona giuridica,
  158.          *      è rappresentato dal suo identificativo fiscale (codice fiscale  nel caso di soggetto trasmittente residente in Italia,
  159.          *      identificativo proprio del paese di appartenenza nel caso di soggetto trasmittente residente all’estero);
  160.          *      la  lunghezza di questo identificativo è di: 11 caratteri (minimo) e 16 caratteri (massimo) nel caso di codice paese IT;
  161.          *      2 caratteri (minimo) e 28 caratteri (massimo) altrimenti;
  162.          * -il progressivo univoco del file è rappresentato da una stringa alfanumerica di lunghezza massima di 5 caratteri
  163.          *  e con valori ammessi [a-z], [A-Z], [0-9].
  164.          *
  165.          *  Il file deve essere firmato elettronicamente (come indicato al precedente paragrafo 2.1);
  166.          *  in base al formato di firma elettronica adottato, l’estensione del file assume il valore “.xml” (XAdES-BES) oppure “.xml.p7m” (CadES-BES) .
  167.          *  Il separatore tra il secondo ed il terzo elemento del nome file è il carattere underscore (“_”), codice ASCII 95.
  168.          *  Nel caso c) il nome del file deve rispettare la stessa nomenclatura e l’estensione del file può essere solo .zip.
  169.          *  
  170.          *  Es.:
  171.          *  ITAAABBB99T99X999W_00001.zip
  172.          *  ITAAABBB99T99X999W_00002.xml
  173.          *  ITAAABBB99T99X999W_00003.xml
  174.          *  ITAAABBB99T99X999W_00004.xml.p7m
  175.          */
  176.        
  177.         if(nomeFile==null){
  178.             throw new ProtocolException("NomeFile Fattura non fornito");
  179.         }
  180.         if(nomeFile.length()<10){ // 2(codicePaese)+2(minimoPaeseDiversoIT)+1(_)+1(progressivoUnivoco)+1(.)+3(xml o pdf)
  181.             throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (length="+nomeFile.length()+"<10)");
  182.         }
  183.         if(nomeFile.contains("_")==false){
  184.             throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto ('_' non presente)");
  185.         }
  186.         if(nomeFile.contains(".")==false){
  187.             throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto ('.' non presente)");
  188.         }
  189.         String [] splitUnderscore = nomeFile.split("_");
  190.         if(splitUnderscore.length!=2){
  191.             throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (split('_')="+splitUnderscore.length+"<>2)");
  192.         }
  193.         String leftCode = splitUnderscore[0];
  194.         String rightCode = splitUnderscore[1];
  195.         if(rightCode.contains(".")==false){
  196.             throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (rightUnderscore=["+rightCode+"] '.' non presente)");
  197.         }
  198.         String [] splitPoint = rightCode.split("\\.");
  199.         if(splitPoint.length!=2 && splitPoint.length!=3){
  200.             throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (split('.')="+splitPoint.length+"<>2/3)");
  201.         }
  202.        
  203.         // codicePaese
  204.         String codicePaese = nomeFile.substring(0, 2);
  205.         validaCodicePaese(codicePaese);
  206.        
  207.         // identificativoTrasmittente
  208.         @SuppressWarnings("unused")
  209.         String identificativoTrasmittente = null;
  210.         if("IT".equals(codicePaese)){
  211.             if(leftCode.length()<13){
  212.                 throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (leftUnderscore=["+leftCode+"] length="+leftCode.length()+"<13)");
  213.             }
  214.             if(leftCode.length()>18){
  215.                 throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (leftUnderscore=["+leftCode+"] length="+leftCode.length()+">18)");
  216.             }
  217.         }
  218.         else{
  219.             if(leftCode.length()<4){
  220.                 throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (leftUnderscore=["+leftCode+"] length="+leftCode.length()+"<4)");
  221.             }
  222.             if(leftCode.length()>30){
  223.                 throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (leftUnderscore=["+leftCode+"] length="+leftCode.length()+">30)");
  224.             }
  225.         }
  226.         identificativoTrasmittente = leftCode.substring(2);

  227.         // progressivoUnivoco
  228.         String progressivoUnivoco = splitPoint[0];
  229.         if(progressivoUnivoco.length()<1){
  230.             throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (progressivoUnivoco=["+progressivoUnivoco+"] length="+progressivoUnivoco.length()+"<1)");
  231.         }
  232.         if(progressivoUnivoco.length()>5){
  233.             throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (progressivoUnivoco=["+progressivoUnivoco+"] length="+progressivoUnivoco.length()+">5)");
  234.         }
  235.         try{
  236.             if(!RegularExpressionEngine.isMatch(progressivoUnivoco,"^[A-Za-z0-9]*$")){
  237.                 throw new ProtocolException("Sono presenti caratteri non alfanumerici");
  238.             }
  239.         }catch(Exception e){
  240.             throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (progressivoUnivoco=["+progressivoUnivoco+"] "+e.getMessage()+")");
  241.         }
  242.        
  243.         // extension
  244.         String ext = rightCode.substring((progressivoUnivoco+".").length(), rightCode.length());
  245.         if(ext==null){
  246.             throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (extension non presente)");
  247.         }
  248.         if(isRicezione){
  249.             if(!SDICostanti.SDI_FATTURA_ESTENSIONE_XML.equalsIgnoreCase(ext) &&
  250.                     !SDICostanti.SDI_FATTURA_ESTENSIONE_P7M.equalsIgnoreCase(ext) ){
  251.                 throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (extension=["+ext+"] non valida, attesa "+
  252.                         SDICostanti.SDI_FATTURA_ESTENSIONE_XML+" o "+
  253.                         SDICostanti.SDI_FATTURA_ESTENSIONE_P7M+")");
  254.             }
  255.         }
  256.         else{
  257.             if(!SDICostanti.SDI_FATTURA_ESTENSIONE_XML.equalsIgnoreCase(ext) &&
  258.                     !SDICostanti.SDI_FATTURA_ESTENSIONE_P7M.equalsIgnoreCase(ext) &&
  259.                     !SDICostanti.SDI_FATTURA_ESTENSIONE_ZIP.equalsIgnoreCase(ext)){
  260.                 throw new ProtocolException("NomeFile Fattura ["+nomeFile+"] con struttura non corretto (extension=["+ext+"] non valida, attesa "+
  261.                         SDICostanti.SDI_FATTURA_ESTENSIONE_XML+" o "+
  262.                         SDICostanti.SDI_FATTURA_ESTENSIONE_P7M+" o "+
  263.                         SDICostanti.SDI_FATTURA_ESTENSIONE_ZIP+")");
  264.             }
  265.         }
  266.            
  267.     }
  268.    
  269.     public static String getNomeFileFatturaSenzaEstensione(String nomeFileFattura){
  270.         String nomeFileFatturaSenzaEstensione = new String(nomeFileFattura);
  271.         if(nomeFileFatturaSenzaEstensione.toLowerCase().endsWith(SDICostanti.SDI_FATTURA_ESTENSIONE_XML)
  272.                 && nomeFileFatturaSenzaEstensione.length()>(SDICostanti.SDI_FATTURA_ESTENSIONE_XML.length()+1)){
  273.             nomeFileFatturaSenzaEstensione = nomeFileFatturaSenzaEstensione.substring(0,(nomeFileFatturaSenzaEstensione.length()-(SDICostanti.SDI_FATTURA_ESTENSIONE_XML.length()+1)));
  274.         }
  275.         else if(nomeFileFatturaSenzaEstensione.toLowerCase().endsWith(SDICostanti.SDI_FATTURA_ESTENSIONE_P7M)
  276.             && nomeFileFatturaSenzaEstensione.length()>(SDICostanti.SDI_FATTURA_ESTENSIONE_P7M.length()+1)){
  277.             nomeFileFatturaSenzaEstensione = nomeFileFatturaSenzaEstensione.substring(0,(nomeFileFatturaSenzaEstensione.length()-(SDICostanti.SDI_FATTURA_ESTENSIONE_P7M.length()+1)));
  278.         }
  279.         else if(nomeFileFatturaSenzaEstensione.toLowerCase().endsWith(SDICostanti.SDI_FATTURA_ESTENSIONE_ZIP)
  280.             && nomeFileFatturaSenzaEstensione.length()>(SDICostanti.SDI_FATTURA_ESTENSIONE_ZIP.length()+1)){
  281.             nomeFileFatturaSenzaEstensione = nomeFileFatturaSenzaEstensione.substring(0,(nomeFileFatturaSenzaEstensione.length()-(SDICostanti.SDI_FATTURA_ESTENSIONE_ZIP.length()+1)));
  282.         }
  283.         return nomeFileFatturaSenzaEstensione;
  284.     }
  285.    
  286.     public static String getNomeFileFatturaPerMessaggi(String nomeFileFattura){
  287.        
  288.         String nomeFileFatturaSenzaEstensione = getNomeFileFatturaSenzaEstensione(nomeFileFattura);
  289.        
  290.         if(nomeFileFatturaSenzaEstensione.length()>36){
  291.             return nomeFileFatturaSenzaEstensione.substring(0, 36);
  292.         }else{
  293.             return nomeFileFatturaSenzaEstensione;
  294.         }
  295.     }
  296.    
  297.     public static void validaNomeFileMessaggi(String nomeFile, TipiMessaggi tipoMessaggio) throws ProtocolException{
  298.         validaNomeFileMessaggi(null, nomeFile, tipoMessaggio);
  299.     }
  300.     public static void validaNomeFileMessaggi(String nomeFileFattura, String nomeFile, TipiMessaggi tipoMessaggio) throws ProtocolException{
  301.        
  302.         /**
  303.          * <NomeFilaFatturaRicevutoSenzaEstensione>_<TipoMessaggio>_<ProgressivoUnivoco>
  304.          * Il Nome del file fattura ricevuto senza estensione deve essere conforme alle regole definite nel precedente paragrafo.
  305.          * Nel caso in cui il nome file non sia conforme e la sua lunghezza sia superiore ai 36 caratteri il nome sarà troncato
  306.          * ed i caratteri oltre il 36-esimo non saranno presenti nella notifica di scarto.
  307.          * Il Tipo di messaggio può assumere i seguenti valori:
  308.          * - RC Ricevuta di consegna
  309.          * - NS Notifica di scarto
  310.          * - MC Notifica di mancata consegna
  311.          * - NE Notifica esito cedente / prestatore
  312.          * - MT File dei metadati
  313.          * - EC Notifica di esito cessionario / committente
  314.          * - SE Notifica di scarto esito cessionario / committente
  315.          * - DT Notifica decorrenza termini
  316.          * - AT Attestazione di avvenuta trasmissione della fattura con impossibilità di recapito
  317.          *
  318.          * Il Progressivo univoco deve essere una stringa alfanumerica di lunghezza massima 3 caratteri e con valori ammessi [a-z], [A-Z], [0-9]
  319.          * che identifica univocamente ogni notifica / ricevuta relativa al file inviato.
  320.          * Il carattere di separazione degli elementi componenti il nome file corrisponde all’underscore (“_”), codice ASCII 95, l’estensione è sempre “.xml”.
  321.          *
  322.          * Se il SdI ha ricevuto un file di tipo compresso (es.: ITAAABBB99T99X999W_00001.zip ) e non è possibile accedere al suo contenuto perché “corrotto”,
  323.          * il nome del file con il quale il SdI inoltra al  soggetto trasmittente la notifica di scarto è il seguente: ITAAABBB99T99X999W_00001_NS_001.xml
  324.          *
  325.          * NOTA: A queste regole di nomenclatura fa eccezione l’Attestazione di avvenuta trasmissione della fattura con impossibilità di recapito (rif. paragrafo 1.10);
  326.          * in questo caso, se il SdI ha ricevuto un file con nome  ITAAABBB99T99X999W_00001.xml, inoltra al soggetto trasmittente il seguente file .zip
  327.          * ITAAABBB99T99X999W_00001_AT_001.zip
  328.          * che al suo interno contiene il file ricevuto (ITAAABBB99T99X999W_00001.xml) e l’attestazione (ITAAABBB99T99X999W_00001_AT_001.xml).
  329.          */
  330.        
  331.         if(nomeFile==null){
  332.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+") non fornito");
  333.         }
  334.         if(nomeFile.length()<19){ // 10(fattura)+1(_)+2(tipo)+1(_)+1(progressivo)+4(.xml)
  335.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (length="+nomeFile.length()+"<19)");
  336.         }
  337.         if(nomeFile.contains("_")==false){
  338.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto ('_' non presente)");
  339.         }
  340.         if(nomeFile.contains(".")==false){
  341.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto ('.' non presente)");
  342.         }
  343.         String [] splitUnderscore = nomeFile.split("_");
  344.         if(splitUnderscore.length!=4){
  345.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (split('_')="+splitUnderscore.length+"<>4)");
  346.         }
  347.         String nomeFileFatturaInterno = splitUnderscore[0]+"_"+splitUnderscore[1];
  348.         if(nomeFileFattura!=null){
  349.            
  350.             String nomeFileFatturaSenzaEstensione = getNomeFileFatturaSenzaEstensione(nomeFileFattura);
  351.            
  352.             if(nomeFileFatturaSenzaEstensione.length()>36){
  353.                 if(nomeFileFatturaSenzaEstensione.substring(0, 36).equals(nomeFileFatturaInterno)==false){
  354.                     throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (nomeFattura.substring(36)=["+nomeFileFatturaSenzaEstensione+"] non riportato correttamente nel nome del file "+tipoMessaggio.name()+")");
  355.                 }
  356.             }else{
  357.                 if(nomeFileFatturaSenzaEstensione.equals(nomeFileFatturaInterno)==false){
  358.                     throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (nomeFattura=["+nomeFileFatturaSenzaEstensione+"] non riportato correttamente nel nome del file "+tipoMessaggio.name()+")");
  359.                 }
  360.             }
  361.         }
  362.         else{
  363.             // non posso effettuare validazione poiche' lo stesso nome file di fattura e' errato in origine
  364.         }
  365.        
  366.         if(nomeFile.length()<=nomeFileFatturaInterno.length()){
  367.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura non presenti)");
  368.         }
  369.         String nomeFileMessaggio = nomeFile.substring(nomeFileFatturaInterno.length());
  370.         if(nomeFileMessaggio==null){
  371.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura non presenti [null])");
  372.         }
  373.         if(nomeFileMessaggio.length()<9){
  374.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura ["+nomeFileMessaggio+"], lenght="+nomeFileMessaggio.length()+"<9)");
  375.         }
  376.         if(nomeFileMessaggio.startsWith("_")==false){
  377.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura ["+nomeFileMessaggio+"], '_' atteso dopo il nome della fattura)");
  378.         }
  379.         nomeFileMessaggio = nomeFileMessaggio.substring(1);
  380.                
  381.         if(nomeFileMessaggio.contains("_")==false){
  382.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura ["+nomeFileMessaggio+"], '_' non presente)");
  383.         }
  384.         if(nomeFileMessaggio.contains(".")==false){
  385.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura ["+nomeFileMessaggio+"], '.' non presente)");
  386.         }
  387.         String [] splitUnderscoreMessaggio = nomeFileMessaggio.split("_");
  388.         if(splitUnderscoreMessaggio.length!=2){
  389.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura ["+nomeFileMessaggio+"], split('_')="+splitUnderscoreMessaggio.length+"<>2)");
  390.         }
  391.         if(splitUnderscoreMessaggio[1].contains(".")==false){
  392.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura ["+nomeFileMessaggio+"], '.' non presente dopo il tipoMessaggio)");
  393.         }
  394.         String [] splitPointMessaggio = nomeFileMessaggio.split("\\.");
  395.         if(splitPointMessaggio.length!=2){
  396.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura ["+nomeFileMessaggio+"], split('.')="+splitPointMessaggio.length+"<>2)");
  397.         }
  398.         String [] splitPointMessaggioUnderscore = splitPointMessaggio[0].split("_");
  399.         if(splitPointMessaggioUnderscore.length!=2){
  400.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura ["+splitPointMessaggio[0]+"] senza ext, split('_')="+splitPointMessaggioUnderscore.length+"<>2)");
  401.         }
  402.        
  403.         // tipoMessaggio
  404.         String tipoMessaggioPresente = splitUnderscoreMessaggio[0];
  405.         if(tipoMessaggio.name().equals(tipoMessaggioPresente)==false){
  406.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (informazioni dopo nomeFattura ["+nomeFileMessaggio+"], tipoMessaggio '"+
  407.                     tipoMessaggioPresente+"' differente da quello atteso '"+tipoMessaggio.name()+"')");
  408.         }

  409.         // progressivoUnivoco
  410.         String progressivoUnivoco = splitPointMessaggioUnderscore[1];
  411.         if(progressivoUnivoco.length()<1){
  412.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (progressivoUnivoco=["+progressivoUnivoco+"] length="+progressivoUnivoco.length()+"<1)");
  413.         }
  414.         if(progressivoUnivoco.length()>3){
  415.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (progressivoUnivoco=["+progressivoUnivoco+"] length="+progressivoUnivoco.length()+">3)");
  416.         }
  417.         try{
  418.             if(!RegularExpressionEngine.isMatch(progressivoUnivoco,"^[A-Za-z0-9]*$")){
  419.                 throw new ProtocolException("Sono presenti caratteri non alfanumerici");
  420.             }
  421.         }catch(Exception e){
  422.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (progressivoUnivoco=["+progressivoUnivoco+"] "+e.getMessage()+")");
  423.         }
  424.        
  425.         // extension
  426.         String ext = splitPointMessaggio[1];
  427.         if(ext==null){
  428.             throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (extension non presente)");
  429.         }
  430.         if(TipiMessaggi.AT.equals(tipoMessaggio)){
  431.             if(!SDICostanti.SDI_FATTURA_ESTENSIONE_ZIP.equalsIgnoreCase(ext) ){
  432.                 throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (extension["+ext+"] non valida, attesa "+
  433.                         SDICostanti.SDI_FATTURA_ESTENSIONE_ZIP+")");
  434.             }
  435.         }
  436.         else{
  437.             if(!SDICostanti.SDI_FATTURA_ESTENSIONE_XML.equalsIgnoreCase(ext) ){
  438.                 throw new ProtocolException("NomeFile ("+tipoMessaggio.name()+")["+nomeFile+"] con struttura non corretto (extension["+ext+"] non valida, attesa "+
  439.                         SDICostanti.SDI_FATTURA_ESTENSIONE_XML+")");
  440.             }
  441.         }
  442.     }
  443. }