XSDValidator.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.generic_project.utils;


  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.InputStream;
  24. import java.lang.reflect.Constructor;
  25. import java.util.ArrayList;
  26. import java.util.List;

  27. import org.slf4j.Logger;
  28. import org.openspcoop2.generic_project.exception.ServiceException;
  29. import org.openspcoop2.generic_project.exception.ValidationException;
  30. import org.openspcoop2.utils.beans.BaseBean;
  31. import org.openspcoop2.utils.xml.AbstractValidatoreXSD;
  32. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  33. import org.openspcoop2.utils.xml.XMLException;
  34. import org.openspcoop2.utils.xml.XMLUtils;
  35. import org.openspcoop2.utils.xml.XSDResourceResolver;
  36. import org.w3c.dom.ls.LSResourceResolver;

  37. /**
  38.  * XSDValidator
  39.  *
  40.  * @author Poli Andrea (apoli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */
  44. public class XSDValidator {

  45.     /** XSD Validator */
  46.     private AbstractValidatoreXSD xsdValidator = null;

  47.     public XSDValidator(Logger log,Class<?> cXmlRoot,String xsdPath) throws ServiceException{
  48.         this(log, cXmlRoot, org.openspcoop2.utils.xml.ValidatoreXSD.class, xsdPath, new String[]{});
  49.     }
  50.     public XSDValidator(Logger log,Class<?> cXmlRoot,String xsdPath,String ... xsdImported) throws ServiceException{
  51.         this(log, cXmlRoot, org.openspcoop2.utils.xml.ValidatoreXSD.class, xsdPath, xsdImported);
  52.     }
  53.     public XSDValidator(Logger log,Class<?> cXmlRoot,Class<?> xsdValidatorImpl,String xsdPath) throws ServiceException{
  54.         this(log, cXmlRoot, xsdValidatorImpl, xsdPath, new String[]{});
  55.     }
  56.     public XSDValidator(Logger log,Class<?> cXmlRoot,Class<?> xsdValidatorImpl,String xsdPath,String ... xsdImported) throws ServiceException{

  57.         /* --- XSD Validator -- */
  58.         if(xsdPath == null){
  59.             throw new ServiceException("New Instance failure: xsdPath is null");
  60.         }
  61.         InputStream isSchema = null;
  62.         List<InputStream> listInputStreams = new ArrayList<InputStream>();
  63.         XSDResourceResolver xsdResourceResolver = null;
  64.         try {
  65.        
  66.             // check schema
  67.             try{
  68.                 File f = new File(xsdPath);
  69.                 if(f.exists()){
  70.                     isSchema = new FileInputStream(f);
  71.                 }else{
  72.                     isSchema = cXmlRoot.getResourceAsStream(xsdPath);
  73.                     if(isSchema==null){
  74.                         isSchema = cXmlRoot.getResourceAsStream("/"+xsdPath);
  75.                     }
  76.                 }
  77.                 if(isSchema==null){
  78.                     throw new Exception("Creating InputStream from xsdPath["+xsdPath+"] failure");
  79.                 }
  80.             }catch (Exception e) {
  81.                 try{
  82.                     if(isSchema!=null){
  83.                         isSchema.close();
  84.                     }
  85.                 }catch(Exception eClose){
  86.                     // close
  87.                 }
  88.                 throw new ServiceException("Init xsd schema failure: "+e.getMessage(),e);
  89.             }
  90.            
  91.             // check schema imported
  92.             if(xsdImported!=null && xsdImported.length>0){
  93.            
  94.                 xsdResourceResolver = new XSDResourceResolver();
  95.            
  96.                 for (int i = 0; i < xsdImported.length; i++) {
  97.                     try{
  98.                         File f = new File(xsdImported[i]);
  99.                         InputStream is = null;
  100.                         if(f.exists()){
  101.                             is = new FileInputStream(f);
  102.                         }else{
  103.                             is = cXmlRoot.getResourceAsStream(xsdImported[i]);
  104.                             if(is==null){
  105.                                 is = cXmlRoot.getResourceAsStream("/"+xsdImported[i]);
  106.                             }
  107.                         }
  108.                         if(is==null){
  109.                             throw new Exception("Creating InputStream from xsdPath["+i+"]["+xsdImported[i]+"] failure");
  110.                         }
  111.                         xsdResourceResolver.addResource(f.getName(), is);
  112.                         listInputStreams.add(is);
  113.                     }catch (Exception e) {
  114.                         try{
  115.                             if(isSchema!=null){
  116.                                 isSchema.close();
  117.                             }
  118.                         }catch(Exception eClose){
  119.                             // close
  120.                         }
  121.                         for (InputStream inputStream : listInputStreams) {
  122.                             try{
  123.                                 if(inputStream!=null){
  124.                                     inputStream.close();
  125.                                 }
  126.                             }catch(Exception eClose){}
  127.                         }
  128.                         throw new ServiceException("Init xsd schema failure: "+e.getMessage(),e);
  129.                     }
  130.                 }
  131.             }
  132.            
  133.             // init schema Validator
  134.             try{
  135.                 if(xsdResourceResolver!=null){
  136.                     Constructor<?> constructor = xsdValidatorImpl.getConstructor(Logger.class,String.class,LSResourceResolver.class,InputStream.class);
  137.                     this.xsdValidator = (AbstractValidatoreXSD) constructor.newInstance(log,"org.apache.xerces.jaxp.validation.XMLSchemaFactory",xsdResourceResolver,isSchema);
  138.                 }else{
  139.                     Constructor<?> constructor = xsdValidatorImpl.getConstructor(Logger.class,String.class,InputStream.class);
  140.                     this.xsdValidator = (AbstractValidatoreXSD) constructor.newInstance(log,"org.apache.xerces.jaxp.validation.XMLSchemaFactory",isSchema);
  141.                 }
  142.             }catch (Exception e) {
  143.                 throw new ServiceException("Init xsd schema failure: "+e.getMessage(),e);
  144.             }
  145.         }finally{
  146.             try{
  147.                 if(isSchema!=null){
  148.                     isSchema.close();
  149.                 }
  150.             }catch(Exception eClose){
  151.                 // close
  152.             }
  153.             for (InputStream inputStream : listInputStreams) {
  154.                 try{
  155.                     if(inputStream!=null){
  156.                         inputStream.close();
  157.                     }
  158.                 }catch(Exception eClose){
  159.                     // close
  160.                 }
  161.             }
  162.         }
  163.     }

  164.     public AbstractValidatoreXSD getXsdValidator() {
  165.         return this.xsdValidator;
  166.     }

  167.    
  168.     private static AbstractXMLUtils xmlUtils = null;
  169.     private static synchronized void initXmlUtils(){
  170.         if(xmlUtils==null){
  171.             xmlUtils = XMLUtils.getInstance();
  172.         }
  173.     }
  174.     public static void validate(BaseBean object,Logger log, AbstractValidatoreXSD xsdValidator) throws ServiceException, ValidationException {

  175.         if(object==null){
  176.             throw new ServiceException("Paramter object is not defined");
  177.         }
  178.         if(log==null){
  179.             throw new ServiceException("Paramter log is not defined");
  180.         }
  181.         if(xsdValidator==null){
  182.             throw new ServiceException("Paramter xsdValidator is not defined");
  183.         }
  184.        
  185.         String xml = null;
  186.         try{
  187.             xml = object.toXml();
  188.         }catch(Exception e){
  189.             log.debug("XMLSerialization error: "+e.getMessage(),e);
  190.             throw new ValidationException(e.getMessage(),e);
  191.         }
  192.        
  193.         if(xmlUtils==null){
  194.             initXmlUtils();
  195.         }

  196.         try{
  197.             xsdValidator.valida(xmlUtils.newDocument(xml.getBytes()));
  198.         }catch(XMLException xmlException){
  199.             throw new ValidationException("Object is not valid for xml structure: "+xmlException.getMessage(),xmlException);
  200.         }catch(Exception e){
  201.             throw new ValidationException("Object is not valid: "+e.getMessage(),e);
  202.         }

  203.     }
  204. }