SchemaXSDAccordoServizio.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.core.registry.wsdl;

  21. import java.io.File;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;

  24. import javax.xml.parsers.ParserConfigurationException;
  25. import javax.xml.transform.TransformerException;
  26. import javax.xml.transform.TransformerFactoryConfigurationError;

  27. import org.openspcoop2.core.registry.constants.CostantiRegistroServizi;
  28. import org.openspcoop2.utils.resources.FileSystemUtilities;
  29. import org.openspcoop2.utils.xml.PrettyPrintXMLUtils;
  30. import org.openspcoop2.utils.xml.SchemaXSD;
  31. import org.openspcoop2.utils.xml.XMLException;
  32. import org.w3c.dom.Element;
  33. import org.xml.sax.SAXException;


  34. /**
  35.  * Rappresentazione di uno schema xsd presente in un accordo di servizio
  36.  *
  37.  * @author Nardi Lorenzo (nardi@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class SchemaXSDAccordoServizio extends SchemaXSD {

  42.     private TipoSchemaXSDAccordoServizio tipoSchema;
  43.     public TipoSchemaXSDAccordoServizio getTipoSchema() {
  44.         return this.tipoSchema;
  45.     }
  46.    
  47.    
  48.     public SchemaXSDAccordoServizio(byte[] xsd,String fileName,TipoSchemaXSDAccordoServizio tipo) throws IOException,SAXException,ParserConfigurationException, XMLException{
  49.         this(xsd, fileName, new File(tipo.getDirectory()+File.separatorChar+fileName), tipo);
  50.     }
  51.     public SchemaXSDAccordoServizio(byte[] xsd,String fileName,File fileSource,TipoSchemaXSDAccordoServizio tipo) throws IOException,SAXException,ParserConfigurationException, XMLException{
  52.         super(xsd,fileName,fileSource,org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT);
  53.         this.tipoSchema = tipo;
  54.     }
  55.     public SchemaXSDAccordoServizio(Element xsd,String fileName,TipoSchemaXSDAccordoServizio tipo) throws IOException,SAXException,ParserConfigurationException{
  56.         this(xsd, fileName, new File(tipo.getDirectory()+File.separatorChar+fileName), tipo);
  57.     }
  58.     public SchemaXSDAccordoServizio(Element xsd,String fileName,File fileSource,TipoSchemaXSDAccordoServizio tipo) throws IOException,SAXException,ParserConfigurationException{
  59.         super(xsd,fileName,fileSource,org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT);
  60.         this.tipoSchema = tipo;
  61.     }
  62.    
  63.     public static SchemaXSDAccordoServizio creaSchema(byte[] xsd,File file) throws IOException,SAXException,ParserConfigurationException, XMLException{
  64.         if(file==null){
  65.             throw new IOException("File non esistente");
  66.         }
  67.         if(file.getParentFile()==null){
  68.             throw new IOException("File non risiede in una cartella");
  69.         }
  70.         String parent = file.getParentFile().getName();
  71.         if(parent==null){
  72.             throw new IOException("File non risiede in una cartella (null?)");
  73.         }
  74.         SchemaXSDAccordoServizio schema = null;
  75.         if(CostantiRegistroServizi.ALLEGATI_DIR.equals(parent)){
  76.             schema = new SchemaXSDAccordoServizio(xsd,file.getName(),file,TipoSchemaXSDAccordoServizio.ALLEGATO);
  77.         }
  78.         else if(CostantiRegistroServizi.SPECIFICA_SEMIFORMALE_DIR.equals(parent)){
  79.             schema = new SchemaXSDAccordoServizio(xsd,file.getName(),file,TipoSchemaXSDAccordoServizio.SPECIFICA_SEMIFORMALE);
  80.         }
  81.         else{
  82.             throw new ParserConfigurationException("Directory contenente lo schema diverso da "+CostantiRegistroServizi.ALLEGATI_DIR+" e da "+CostantiRegistroServizi.SPECIFICA_SEMIFORMALE_DIR);
  83.         }
  84.         return schema;
  85.     }
  86.    
  87.     public static SchemaXSDAccordoServizio creaSchema(Element xsd,File file) throws IOException,SAXException,ParserConfigurationException{
  88.         if(file==null){
  89.             throw new IOException("File non esistente");
  90.         }
  91.         if(file.getParentFile()==null){
  92.             throw new IOException("File non risiede in una cartella");
  93.         }
  94.         String parent = file.getParentFile().getName();
  95.         if(parent==null){
  96.             throw new IOException("File non risiede in una cartella (null?)");
  97.         }
  98.         SchemaXSDAccordoServizio schema = null;
  99.         if(CostantiRegistroServizi.ALLEGATI_DIR.equals(parent)){
  100.             schema = new SchemaXSDAccordoServizio(xsd,file.getName(),file,TipoSchemaXSDAccordoServizio.ALLEGATO);
  101.         }
  102.         else if(CostantiRegistroServizi.SPECIFICA_SEMIFORMALE_DIR.equals(parent)){
  103.             schema = new SchemaXSDAccordoServizio(xsd,file.getName(),file,TipoSchemaXSDAccordoServizio.SPECIFICA_SEMIFORMALE);
  104.         }
  105.         else{
  106.             throw new ParserConfigurationException("Directory contenente lo schema diverso da "+CostantiRegistroServizi.ALLEGATI_DIR+" e da "+CostantiRegistroServizi.SPECIFICA_SEMIFORMALE_DIR);
  107.         }
  108.         return schema;
  109.     }
  110.    
  111.     @Override
  112.     public void writeTo(File folder) throws ParserConfigurationException, TransformerFactoryConfigurationError, FileNotFoundException, TransformerException, IOException, XMLException{
  113.         this.writeTo(folder,false);
  114.     }
  115.     @Override
  116.     public void writeTo(File folder,boolean prettyPrint) throws ParserConfigurationException, TransformerFactoryConfigurationError, FileNotFoundException, TransformerException, IOException, XMLException{
  117.         if(super.getFilename()==null){
  118.             throw new FileNotFoundException("File name non impostato");
  119.         }
  120.        
  121.         File out = new File(folder,this.tipoSchema.getDirectory()+File.separatorChar+super.getFilename());
  122.         try{
  123.             FileSystemUtilities.mkdirParentDirectory(out);
  124.         }catch(Exception e){
  125.             throw new TransformerException(e.getMessage(),e);
  126.         }
  127.        
  128.         if(prettyPrint){
  129.             PrettyPrintXMLUtils.prettyPrintWithTrAX(this.getXml(), out);
  130.         }else{
  131.             org.openspcoop2.message.xml.MessageXMLUtils xmlUtils = org.openspcoop2.message.xml.MessageXMLUtils.DEFAULT;
  132.             xmlUtils.writeTo(this.getXml(), out);
  133.         }
  134.     }
  135. }