SchemaXSD.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.utils.xml;

  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.utils.resources.FileSystemUtilities;
  28. import org.w3c.dom.Element;
  29. import org.xml.sax.SAXException;


  30. /**
  31.  * Rappresentazione di uno schema xsd
  32.  *
  33.  * @author Nardi Lorenzo (nardi@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */

  37. public class SchemaXSD {
  38.     private Element xml;
  39.     private String filename;
  40.     private File source;
  41.     private org.openspcoop2.utils.xml.AbstractXMLUtils xmlUtils;
  42.    
  43.     public SchemaXSD(Element xml, String filename, File source,org.openspcoop2.utils.xml.AbstractXMLUtils xmlUtils){
  44.         this.xmlUtils = xmlUtils;
  45.         this.xml = xml;
  46.         this.filename = filename;
  47.         this.source = source;
  48.     }
  49.     public SchemaXSD(byte[] xml, String filename, File source,org.openspcoop2.utils.xml.AbstractXMLUtils xmlUtils) throws IOException,SAXException,ParserConfigurationException, XMLException{
  50.         this.xmlUtils = xmlUtils;
  51.         this.xml = this.xmlUtils.newElement(xml);
  52.         this.filename = filename;
  53.         this.source = source;
  54.     }

  55.     public Element getXml() {
  56.         return this.xml;
  57.     }
  58.    
  59.     public String getFilename() {
  60.         return this.filename;
  61.     }
  62.    
  63.     public File getSource() {
  64.         return this.source;
  65.     }
  66.    
  67.     public String getSourceAbsolutePath() {
  68.         return this.source.getAbsolutePath();
  69.     }
  70.    
  71.    
  72.     public boolean compareSource(SchemaXSD definitorio){
  73.         return this.source.getAbsolutePath().equalsIgnoreCase(definitorio.getSource().getAbsolutePath());
  74.     }
  75.    
  76.     public boolean compareFilename(SchemaXSD definitorio){
  77.         return this.filename.equalsIgnoreCase(definitorio.getFilename());
  78.     }
  79.    
  80.     public void writeTo(File folder) throws ParserConfigurationException, TransformerFactoryConfigurationError, FileNotFoundException, TransformerException, IOException, XMLException{
  81.         writeTo(folder,false);
  82.     }
  83.     public void writeTo(File folder,boolean prettyPrint) throws ParserConfigurationException, TransformerFactoryConfigurationError, FileNotFoundException, TransformerException, IOException, XMLException{
  84.         if(this.filename==null){
  85.             throw new FileNotFoundException("File name non impostato");
  86.         }
  87.        
  88.         File out = new File(folder.getAbsolutePath() + File.separator + getFilename());
  89.         try{
  90.             FileSystemUtilities.mkdirParentDirectory(out);
  91.         }catch(Exception e){
  92.             throw new TransformerException(e.getMessage(),e);
  93.         }
  94.        
  95.         if(prettyPrint){
  96.             PrettyPrintXMLUtils.prettyPrintWithTrAX(this.xml, out);
  97.         }else{
  98.             this.xmlUtils.writeTo(this.xml, out);
  99.         }
  100.            
  101.     }
  102.    
  103.     public byte[] toByteArray() throws ParserConfigurationException, TransformerFactoryConfigurationError, FileNotFoundException, TransformerException, IOException, XMLException{
  104.         return toByteArray(false);
  105.     }
  106.     public byte[] toByteArray(boolean prettyPrint) throws ParserConfigurationException, TransformerFactoryConfigurationError, FileNotFoundException, TransformerException, IOException, XMLException{
  107.        
  108.         if(prettyPrint){
  109.             return PrettyPrintXMLUtils.prettyPrintWithTrAX(this.xml).getBytes();
  110.         }else{
  111.             return this.xmlUtils.toByteArray(this.xml);
  112.         }

  113.     }
  114.    
  115. }