AbstractValidatoreXSD.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.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.InputStream;

  24. import javax.xml.XMLConstants;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.dom.DOMSource;
  27. import javax.xml.transform.stream.StreamSource;
  28. import javax.xml.validation.Schema;
  29. import javax.xml.validation.SchemaFactory;
  30. import javax.xml.validation.Validator;

  31. import org.slf4j.Logger;
  32. import org.w3c.dom.Node;
  33. import org.w3c.dom.ls.LSResourceResolver;
  34. import org.xml.sax.ErrorHandler;
  35. import org.xml.sax.SAXException;


  36. /**
  37.  * Classe utilizzata per effettuare una validazione con schema xsd di un messaggio.
  38.  *
  39.  *
  40.  * @author Poli Andrea (apoli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */

  44. public abstract class AbstractValidatoreXSD {


  45.     /** StreamSource */
  46.     private Schema schema;
  47.     public Schema getSchema() {
  48.         return this.schema;
  49.     }
  50.    
  51.    
  52.    
  53.     /** ----------------- COSTRUTTORE SCHEMA -----------------
  54.    
  55.     /**
  56.      * Costruttore
  57.      *
  58.      * @param schema
  59.      */
  60.     public AbstractValidatoreXSD(Schema schema)throws Exception{
  61.         try{
  62.             if(schema == null)
  63.                 throw new Exception("Schema is null?");
  64.             this.schema = schema;
  65.         }catch(Exception e){
  66.             throw new Exception("Riscontrato errore durante la costruzione dello schema (InputStream): "+e.getMessage(),e);
  67.         }
  68.     }
  69.    
  70.    
  71.    
  72.    
  73.     /** ----------------- COSTRUTTORE INPUT STREAM ----------------- */
  74.    
  75.     private static final String FACTORY_DEFAULT = "FactoryDefault";
  76.    
  77.     /**
  78.      * Costruttore InputStream
  79.      *
  80.      * @param inputStream
  81.      */
  82.     public AbstractValidatoreXSD(Logger log,InputStream inputStream)throws Exception{
  83.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,inputStream);
  84.     }
  85.     public AbstractValidatoreXSD(Logger log,String schemaFactory,InputStream inputStream)throws Exception{
  86.         try{
  87.             StreamSource streamSource = new StreamSource(inputStream);  
  88.             this.initializeSchema(log,schemaFactory,null,streamSource);
  89.         }catch(Exception e){
  90.             throw new Exception("Riscontrato errore durante la costruzione dello schema (InputStream): "+e.getMessage(),e);
  91.         }
  92.     }
  93.    
  94.     public AbstractValidatoreXSD(Logger log,LSResourceResolver lsResourceResolver,InputStream inputStream)throws Exception{
  95.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,lsResourceResolver,inputStream);
  96.     }
  97.     public AbstractValidatoreXSD(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,InputStream inputStream)throws Exception{
  98.         try{
  99.             StreamSource streamSource = new StreamSource(inputStream);  
  100.             this.initializeSchema(log,schemaFactory,lsResourceResolver,streamSource);
  101.         }catch(Exception e){
  102.             throw new Exception("Riscontrato errore durante la costruzione dello schema (InputStream): "+e.getMessage(),e);
  103.         }
  104.     }
  105.    
  106.     public AbstractValidatoreXSD(Logger log,InputStream... inputStream)throws Exception{
  107.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,inputStream);
  108.     }
  109.     public AbstractValidatoreXSD(Logger log,String schemaFactory,InputStream... inputStream)throws Exception{
  110.         try{
  111.             StreamSource [] ss = new StreamSource[inputStream.length];
  112.             for(int i=0; i<inputStream.length; i++){
  113.                 ss[i] = new StreamSource(inputStream[i]);
  114.             }
  115.             this.initializeSchema(log,schemaFactory,null,ss);
  116.         }catch(Exception e){
  117.             throw new Exception("Riscontrato errore durante la costruzione dello schema (InputStream): "+e.getMessage(),e);
  118.         }
  119.     }
  120.    
  121.     public AbstractValidatoreXSD(Logger log,LSResourceResolver lsResourceResolver,InputStream... inputStream)throws Exception{
  122.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,lsResourceResolver,inputStream);
  123.     }
  124.     public AbstractValidatoreXSD(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,InputStream... inputStream)throws Exception{
  125.         try{
  126.             StreamSource [] ss = new StreamSource[inputStream.length];
  127.             for(int i=0; i<inputStream.length; i++){
  128.                 ss[i] = new StreamSource(inputStream[i]);
  129.             }
  130.             this.initializeSchema(log,schemaFactory,lsResourceResolver,ss);
  131.         }catch(Exception e){
  132.             throw new Exception("Riscontrato errore durante la costruzione dello schema (InputStream): "+e.getMessage(),e);
  133.         }
  134.     }
  135.    
  136.    
  137.    
  138.     /** ----------------- COSTRUTTORE FILE -----------------
  139.    
  140.     /**
  141.      * Costruttore File
  142.      *
  143.      * @param file
  144.      */
  145.     public AbstractValidatoreXSD(Logger log,File file)throws Exception{
  146.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,file);
  147.     }
  148.     public AbstractValidatoreXSD(Logger log,String schemaFactory,File file)throws Exception{
  149.         try{
  150.             StreamSource streamSource = new StreamSource(file);
  151.             this.initializeSchema(log,schemaFactory,null,streamSource);
  152.         }catch(Exception e){
  153.             throw new Exception("Riscontrato errore durante la costruzione dello schema (File:"+file.getName()+"): "+e.getMessage(),e);
  154.         }
  155.     }
  156.    
  157.     public AbstractValidatoreXSD(Logger log,LSResourceResolver lsResourceResolver,File file)throws Exception{
  158.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,lsResourceResolver,file);
  159.     }
  160.     public AbstractValidatoreXSD(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,File file)throws Exception{
  161.         try{
  162.             StreamSource streamSource = new StreamSource(file);
  163.             this.initializeSchema(log,schemaFactory,lsResourceResolver,streamSource);
  164.         }catch(Exception e){
  165.             throw new Exception("Riscontrato errore durante la costruzione dello schema (File:"+file.getName()+"): "+e.getMessage(),e);
  166.         }
  167.     }
  168.    
  169.     public AbstractValidatoreXSD(Logger log,File... file)throws Exception{
  170.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,file);
  171.     }
  172.     public AbstractValidatoreXSD(Logger log,String schemaFactory,File... file)throws Exception{
  173.         try{
  174.             StreamSource [] ss = new StreamSource[file.length];
  175.             for(int i=0; i<file.length; i++){
  176.                 ss[i] = new StreamSource(file[i]);
  177.             }
  178.             this.initializeSchema(log,schemaFactory,null,ss);
  179.         }catch(Exception e){
  180.             throw new Exception("Riscontrato errore durante la costruzione dello schema (Files): "+e.getMessage(),e);
  181.         }
  182.     }
  183.    
  184.     public AbstractValidatoreXSD(Logger log,LSResourceResolver lsResourceResolver,File... file)throws Exception{
  185.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,lsResourceResolver,file);
  186.     }
  187.     public AbstractValidatoreXSD(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,File... file)throws Exception{
  188.         try{
  189.             StreamSource [] ss = new StreamSource[file.length];
  190.             for(int i=0; i<file.length; i++){
  191.                 ss[i] = new StreamSource(file[i]);
  192.             }
  193.             this.initializeSchema(log,schemaFactory,lsResourceResolver,ss);
  194.         }catch(Exception e){
  195.             throw new Exception("Riscontrato errore durante la costruzione dello schema (Files): "+e.getMessage(),e);
  196.         }
  197.     }
  198.    
  199.    
  200.    
  201.     /** ----------------- COSTRUTTORE URL -----------------
  202.    
  203.     /**
  204.      * Costruttore url
  205.      *
  206.      * @param url
  207.      */
  208.     public AbstractValidatoreXSD(Logger log,String url)throws Exception{
  209.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,url);
  210.     }
  211.     public AbstractValidatoreXSD(Logger log,String schemaFactory,String url)throws Exception{
  212.         try{
  213.             StreamSource streamSource = new StreamSource(url);  
  214.             this.initializeSchema(log,schemaFactory,null,streamSource);
  215.         }catch(Exception e){
  216.             throw new Exception("Riscontrato errore durante la costruzione dello schema (URL:"+url+"): "+e.getMessage(),e);
  217.         }
  218.     }
  219.    
  220.     public AbstractValidatoreXSD(Logger log,LSResourceResolver lsResourceResolver,String url)throws Exception{
  221.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,lsResourceResolver,url);
  222.     }
  223.     public AbstractValidatoreXSD(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,String url)throws Exception{
  224.         try{
  225.             StreamSource streamSource = new StreamSource(url);  
  226.             this.initializeSchema(log,schemaFactory,lsResourceResolver,streamSource);
  227.         }catch(Exception e){
  228.             throw new Exception("Riscontrato errore durante la costruzione dello schema (URL:"+url+"): "+e.getMessage(),e);
  229.         }
  230.     }
  231.    
  232.     public AbstractValidatoreXSD(Logger log,String... url)throws Exception{
  233.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,url);
  234.     }
  235.     public AbstractValidatoreXSD(Logger log,String schemaFactory,String... url)throws Exception{
  236.         try{
  237.             StreamSource [] ss = new StreamSource[url.length];
  238.             for(int i=0; i<url.length; i++){
  239.                 ss[i] = new StreamSource(url[i]);
  240.             }
  241.             this.initializeSchema(log,schemaFactory,null,ss);
  242.         }catch(Exception e){
  243.             StringBuilder sb = new StringBuilder();
  244.             if(url!=null) {
  245.                 for (String u : url) {
  246.                     if(sb.length()>0) {
  247.                         sb.append(", ");
  248.                     }
  249.                     sb.append(u);
  250.                 }
  251.             }
  252.             throw new Exception("Riscontrato errore durante la costruzione dello schema (URL:"+sb.toString()+"): "+e.getMessage(),e);
  253.         }
  254.     }
  255.    
  256.     public AbstractValidatoreXSD(Logger log,LSResourceResolver lsResourceResolver,String... url)throws Exception{
  257.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,lsResourceResolver,url);
  258.     }
  259.     public AbstractValidatoreXSD(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,String... url)throws Exception{
  260.         try{
  261.             StreamSource [] ss = new StreamSource[url.length];
  262.             for(int i=0; i<url.length; i++){
  263.                 ss[i] = new StreamSource(url[i]);
  264.             }
  265.             this.initializeSchema(log,schemaFactory,lsResourceResolver,ss);
  266.         }catch(Exception e){
  267.             StringBuilder sb = new StringBuilder();
  268.             if(url!=null) {
  269.                 for (String u : url) {
  270.                     if(sb.length()>0) {
  271.                         sb.append(", ");
  272.                     }
  273.                     sb.append(u);
  274.                 }
  275.             }
  276.             throw new Exception("Riscontrato errore durante la costruzione dello schema (URL:"+sb.toString()+"): "+e.getMessage(),e);
  277.         }
  278.     }
  279.    
  280.    
  281.    
  282.    
  283.    
  284.    
  285.    
  286.    
  287.     /** ----------------- COSTRUTTORE NODE -----------------
  288.    
  289.     /**
  290.      * Costruttore node
  291.      *
  292.      * @param schema
  293.      */
  294.     public AbstractValidatoreXSD(Logger log,Node schema)throws Exception{
  295.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,schema);
  296.     }
  297.     public AbstractValidatoreXSD(Logger log,String schemaFactory,Node schema)throws Exception{
  298.         try{
  299.             DOMSource streamSource = new DOMSource(schema);
  300.             this.initializeSchema(log,schemaFactory,null,streamSource);
  301.         }catch(Exception e){
  302.             throw new Exception("Riscontrato errore durante la costruzione dello schema (Node): "+e.getMessage(),e);
  303.         }
  304.     }
  305.    
  306.     public AbstractValidatoreXSD(Logger log,LSResourceResolver lsResourceResolver,Node schema)throws Exception{
  307.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,lsResourceResolver,schema);
  308.     }
  309.     public AbstractValidatoreXSD(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,Node schema)throws Exception{
  310.         try{
  311.             DOMSource streamSource = new DOMSource(schema);
  312.             this.initializeSchema(log,schemaFactory,lsResourceResolver,streamSource);
  313.         }catch(Exception e){
  314.             throw new Exception("Riscontrato errore durante la costruzione dello schema (Node): "+e.getMessage(),e);
  315.         }
  316.     }
  317.    
  318.     public AbstractValidatoreXSD(Logger log,Node... schema)throws Exception{
  319.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,schema);
  320.     }
  321.     public AbstractValidatoreXSD(Logger log,String schemaFactory,Node... schema)throws Exception{
  322.         try{
  323.             DOMSource [] ss = new DOMSource[schema.length];
  324.             for(int i=0; i<schema.length; i++){
  325.                 ss[i] = new DOMSource(schema[i]);
  326.             }
  327.             this.initializeSchema(log,schemaFactory,null,ss);
  328.         }catch(Exception e){
  329.             throw new Exception("Riscontrato errore durante la costruzione dello schema (Node): "+e.getMessage(),e);
  330.         }
  331.     }
  332.    
  333.     public AbstractValidatoreXSD(Logger log,LSResourceResolver lsResourceResolver,Node... schema)throws Exception{
  334.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,lsResourceResolver,schema);
  335.     }
  336.     public AbstractValidatoreXSD(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,Node... schema)throws Exception{
  337.         try{
  338.             DOMSource [] ss = new DOMSource[schema.length];
  339.             for(int i=0; i<schema.length; i++){
  340.                 ss[i] = new DOMSource(schema[i]);
  341.             }
  342.             this.initializeSchema(log,schemaFactory,lsResourceResolver,ss);
  343.         }catch(Exception e){
  344.             throw new Exception("Riscontrato errore durante la costruzione dello schema (Node): "+e.getMessage(),e);
  345.         }
  346.     }
  347.    
  348.    
  349.    
  350.    
  351.    
  352.    
  353.    
  354.    
  355.    
  356.    
  357.    
  358.    
  359.    
  360.    
  361.     /** ----------------- COSTRUTTORE SOURCE GENERICO -----------------
  362.    
  363.     /**
  364.      * Costruttore source
  365.      *
  366.      * @param source
  367.      */
  368.     public AbstractValidatoreXSD(Logger log,Source source)throws Exception{
  369.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,source);
  370.     }
  371.     public AbstractValidatoreXSD(Logger log,String schemaFactory,Source source)throws Exception{
  372.         try{
  373.             this.initializeSchema(log,schemaFactory,null,source);
  374.         }catch(Exception e){
  375.             throw new Exception("Riscontrato errore durante la costruzione dello schema (Source): "+e.getMessage(),e);
  376.         }
  377.     }
  378.    
  379.     public AbstractValidatoreXSD(Logger log,LSResourceResolver lsResourceResolver,Source source)throws Exception{
  380.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,lsResourceResolver,source);
  381.     }
  382.     public AbstractValidatoreXSD(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,Source source)throws Exception{
  383.         try{
  384.             this.initializeSchema(log,schemaFactory,lsResourceResolver,source);
  385.         }catch(Exception e){
  386.             throw new Exception("Riscontrato errore durante la costruzione dello schema (Source): "+e.getMessage(),e);
  387.         }
  388.     }
  389.    
  390.     public AbstractValidatoreXSD(Logger log,Source... source)throws Exception{
  391.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,source);
  392.     }
  393.     public AbstractValidatoreXSD(Logger log,String schemaFactory,Source... source)throws Exception{
  394.         try{
  395.             this.initializeSchema(log,schemaFactory,null,source);
  396.         }catch(Exception e){
  397.             throw new Exception("Riscontrato errore durante la costruzione dello schema (Source): "+e.getMessage(),e);
  398.         }
  399.     }
  400.    
  401.     public AbstractValidatoreXSD(Logger log,LSResourceResolver lsResourceResolver,Source... source)throws Exception{
  402.         this(log,AbstractValidatoreXSD.FACTORY_DEFAULT,lsResourceResolver,source);
  403.     }
  404.     public AbstractValidatoreXSD(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,Source... source)throws Exception{
  405.         try{
  406.             this.initializeSchema(log,schemaFactory,lsResourceResolver,source);
  407.         }catch(Exception e){
  408.             throw new Exception("Riscontrato errore durante la costruzione dello schema (Source): "+e.getMessage(),e);
  409.         }
  410.     }
  411.    
  412.    
  413.    
  414.    
  415.    
  416.    
  417.    
  418.    
  419.    
  420.    
  421.    
  422.    
  423.    
  424.    
  425.     /** ----------------- INIT SCHEMA ----------------- **/
  426.    
  427.     public abstract AbstractXMLUtils getXMLUtils();
  428.    
  429.     /**
  430.      * Metodo che si occupa di inizializzare lo schema per la validazione.
  431.      *
  432.      *
  433.      */
  434.     private void initializeSchema(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,Source streamSource) throws Exception{
  435.         try{
  436.            
  437.             // La gestione dello schemaFactory e' servito per il seguente motivo:
  438.             // UndeclaredPrefix: Cannot resolve 'messaggioSII:xxxxMessaggioSIIType' as a QName: the prefix 'messaggioSII' is not declared.
  439.             // After some debugging, I've found out that this is a bug of the JAXP api's built in to the JDK.
  440.             // You can fix it by making sure that you use the Xerces version of the SchemaFactory, and not the JDK internal one.
  441.             // The algorithm for choosing a SchemaFactory is explained at http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/SchemaFactory.html#newInstance(java.lang.String).
  442.             // It comes down to setting the System property "javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema" to the value "org.apache.xerces.jaxp.validation.XMLSchemaFactory".
  443.             // Note that just adding Xerces to your classpath won't fix this, for reasons explained at http://xerces.apache.org/xerces2-j/faq-general.html#faq-4
  444.            
  445.             SchemaFactory factory = null;
  446.             if(schemaFactory!=null && !AbstractValidatoreXSD.FACTORY_DEFAULT.equals(schemaFactory)){
  447.                 factory =  SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI,
  448.                         schemaFactory, this.getClass().getClassLoader());
  449.                 if(this.getXMLUtils().isDisabledDTDs()) {
  450.                     factory.setFeature(this.getXMLUtils().getFeatures_disallow_doctype_decl(), true);
  451.                 }
  452.             }
  453.             else{
  454.                 factory = this.getXMLUtils().getSchemaFactory();
  455.             }
  456.            
  457. //          String oldSchemaFactorySetup = null;
  458. //          String propertySystem = "javax.xml.validation.SchemaFactory:"+XMLConstants.W3C_XML_SCHEMA_NS_URI;
  459. //          if(schemaFactory!=null && !AbstractValidatoreXSD.FACTORY_DEFAULT.equals(schemaFactory)){
  460. //              oldSchemaFactorySetup = System.getenv(propertySystem);
  461. //              if(oldSchemaFactorySetup==null){
  462. //                  oldSchemaFactorySetup = System.getProperty(propertySystem);
  463. //                  if(oldSchemaFactorySetup==null){
  464. //                      oldSchemaFactorySetup = "";
  465. //                  }
  466. //              }
  467. //              System.setProperty(propertySystem, schemaFactory);
  468. //          }
  469. //          SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  470. //          if(this.getXMLUtils().isDisabledDTDs()) {
  471. //              factory.setFeature(this.getXMLUtils().getFeatures_disallow_doctype_decl(), true);
  472. //          }
  473.             log.info("SchemaFactory["+factory.getClass().getName()+"]");
  474.             if(lsResourceResolver!=null){
  475.                 factory.setResourceResolver(lsResourceResolver);
  476.             }
  477.             this.schema = factory.newSchema(streamSource);
  478. //          if(schemaFactory!=null && !AbstractValidatoreXSD.FACTORY_DEFAULT.equals(schemaFactory) && oldSchemaFactorySetup!=null){
  479. //              log.debug("Ripristino oldSchemaFactory ["+oldSchemaFactorySetup+"]");
  480. //              System.setProperty(propertySystem, oldSchemaFactorySetup);
  481. //          }
  482.         }catch (Exception e) {
  483.             throw new Exception("Riscontrato errore durante l'inizializzazione dello schema: "+e.getMessage(),e);
  484.         }
  485.     }
  486.     private void initializeSchema(Logger log,String schemaFactory,LSResourceResolver lsResourceResolver,Source[] streamSource) throws Exception{
  487.         try{
  488.            
  489.             // La gestione dello schemaFactory e' servito per il seguente motivo:
  490.             // UndeclaredPrefix: Cannot resolve 'messaggioSII:xxxxMessaggioSIIType' as a QName: the prefix 'messaggioSII' is not declared.
  491.             // After some debugging, I've found out that this is a bug of the JAXP api's built in to the JDK.
  492.             // You can fix it by making sure that you use the Xerces version of the SchemaFactory, and not the JDK internal one.
  493.             // The algorithm for choosing a SchemaFactory is explained at http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/SchemaFactory.html#newInstance(java.lang.String).
  494.             // It comes down to setting the System property "javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema" to the value "org.apache.xerces.jaxp.validation.XMLSchemaFactory".
  495.             // Note that just adding Xerces to your classpath won't fix this, for reasons explained at http://xerces.apache.org/xerces2-j/faq-general.html#faq-4
  496.            
  497.             SchemaFactory factory = null;
  498.             if(schemaFactory!=null && !AbstractValidatoreXSD.FACTORY_DEFAULT.equals(schemaFactory)){
  499.                 factory =  SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI,
  500.                         schemaFactory, this.getClass().getClassLoader());
  501.                 if(this.getXMLUtils().isDisabledDTDs()) {
  502.                     factory.setFeature(this.getXMLUtils().getFeatures_disallow_doctype_decl(), true);
  503.                 }
  504.             }
  505.             else{
  506.                 factory = this.getXMLUtils().getSchemaFactory();
  507.             }
  508.            
  509. //          String oldSchemaFactorySetup = null;
  510. //          String propertySystem = "javax.xml.validation.SchemaFactory:"+XMLConstants.W3C_XML_SCHEMA_NS_URI;
  511. //          if(schemaFactory!=null && !AbstractValidatoreXSD.FACTORY_DEFAULT.equals(schemaFactory)){
  512. //              oldSchemaFactorySetup = System.getenv(propertySystem);
  513. //              if(oldSchemaFactorySetup==null){
  514. //                  oldSchemaFactorySetup = System.getProperty(propertySystem);
  515. //                  if(oldSchemaFactorySetup==null){
  516. //                      oldSchemaFactorySetup = "";
  517. //                  }
  518. //              }
  519. //              System.setProperty(propertySystem, schemaFactory);
  520. //          }
  521. //          SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  522. //          if(this.getXMLUtils().isDisabledDTDs()) {
  523. //              factory.setFeature(this.getXMLUtils().getFeatures_disallow_doctype_decl(), true);
  524. //          }
  525.             log.info("SchemaFactory["+factory.getClass().getName()+"]");
  526.             if(lsResourceResolver!=null){
  527.                 factory.setResourceResolver(lsResourceResolver);
  528.             }
  529.             this.schema = factory.newSchema(streamSource);
  530. //          if(schemaFactory!=null && !AbstractValidatoreXSD.FACTORY_DEFAULT.equals(schemaFactory) && oldSchemaFactorySetup!=null){
  531. //              log.debug("Ripristino oldSchemaFactory ["+oldSchemaFactorySetup+"]");
  532. //              System.setProperty(propertySystem, oldSchemaFactorySetup);
  533. //          }
  534.         }catch (Exception e) {
  535.             throw new Exception("Riscontrato errore durante l'inizializzazione dello schema: "+e.getMessage(),e);
  536.         }
  537.     }
  538.    
  539.    
  540.    
  541.    
  542.    
  543.     /** ----------------- VALIDAZIONI -----------------
  544.    
  545.    
  546.     /**
  547.      * Metodo che effettua la validazione xsd.
  548.      *
  549.      * @param nodeXML Node
  550.      */
  551.     public void valida(Node nodeXML) throws Exception{
  552.         valida(new DOMSourceFix(nodeXML));
  553.     }
  554.     public void valida(Node nodeXML,ErrorHandler errorHandler) throws Exception{
  555.         valida(new DOMSourceFix(nodeXML),errorHandler);
  556.     }
  557.    
  558.     /**
  559.      * Metodo che effettua la validazione xsd.
  560.      *
  561.      * @param nodeXML Node
  562.      */
  563.     public void valida(Node nodeXML,boolean streamSource) throws Exception{
  564.         this.valida(nodeXML,streamSource,null);
  565.     }
  566.     public abstract byte[] getAsByte(Node nodeXML) throws XMLException;
  567.     public void valida(Node nodeXML,boolean streamSource,ErrorHandler errorHandler) throws Exception{
  568.         if(streamSource){
  569.             // Risolve il problema di validare gli attributi
  570.             ByteArrayInputStream bin = new ByteArrayInputStream(this.getAsByte(nodeXML));
  571.             valida(new StreamSource(bin),errorHandler);
  572.         }else{
  573.             valida(nodeXML,errorHandler);
  574.         }
  575.     }
  576.    
  577.     /**
  578.      * Metodo che effettua la validazione xsd.
  579.      *
  580.      * @param inputStreamXML Input Stream di un contenuto xml
  581.      */
  582.     public void valida(InputStream inputStreamXML) throws Exception{
  583.         valida(new StreamSource(inputStreamXML));
  584.     }
  585.     public void valida(InputStream inputStreamXML,ErrorHandler errorHandler) throws Exception{
  586.         valida(new StreamSource(inputStreamXML),errorHandler);
  587.     }
  588.    
  589.     /**
  590.      * Metodo che effettua la validazione xsd.
  591.      *
  592.      * @param fileXML File xml
  593.      */
  594.     public void valida(File fileXML) throws Exception{
  595.         valida(new StreamSource(fileXML));
  596.     }
  597.     public void valida(File fileXML,ErrorHandler errorHandler) throws Exception{
  598.         valida(new StreamSource(fileXML),errorHandler);
  599.     }
  600.    
  601.     /**
  602.      * Metodo che effettua la validazione xsd.
  603.      *
  604.      * @param urlXML URL di un file xml
  605.      */
  606.     public void valida(String urlXML) throws Exception{
  607.         valida(new StreamSource(urlXML));
  608.     }
  609.     public void valida(String urlXML,ErrorHandler errorHandler) throws Exception{
  610.         valida(new StreamSource(urlXML),errorHandler);
  611.     }
  612.    
  613.     /**
  614.      * Metodo che effettua la validazione xsd.
  615.      *
  616.      * @param source Sorgente da validare
  617.      */
  618.     public void valida(Source source) throws Exception{
  619.         this.valida(source, null);
  620.     }
  621.     public void valida(Source source,ErrorHandler errorHandler) throws Exception{
  622.         Validator validator  = this.schema.newValidator();
  623.         try {
  624.             if(errorHandler!=null){
  625.                 validator.setErrorHandler(errorHandler);
  626.             }
  627.             validator.validate(source);
  628.         } catch (SAXException e) {
  629.             // instance document is invalid!
  630.             throw e;
  631.         }
  632.     }
  633.    
  634.    
  635. }