SchemaCallback.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.wadl;

  21. import org.slf4j.Logger;

  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.utils.Utilities;
  26. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  27. import org.openspcoop2.utils.xml.XMLException;
  28. import org.openspcoop2.utils.xml.XSDUtils;
  29. import org.w3c.dom.Element;
  30. import org.xml.sax.InputSource;

  31. /**
  32.  * SchemaCallback che riceve gli schemi, presenti nella grammatica, durante la lettura di un wadl
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  *
  38.  */
  39. public class SchemaCallback implements org.jvnet.ws.wadl.ast.WadlAstBuilder.SchemaCallback {

  40.     /*
  41.      * La classe può essere utilizzata pre raccogliere gli schemi direttamente riferiti nel WADL.
  42.      * Gli altri schemi volendo possono essere aggiunti tramite il metodo 'addResource'
  43.      **/
  44.    
  45.     private Map<String, byte[]> resources = new HashMap<String, byte[]>();
  46.     private Map<String, List<String>> mappingNamespaceLocations = new HashMap<>();
  47.     private Logger log;
  48.     private AbstractXMLUtils xmlUtils = null;
  49.     private XSDUtils xsdUtils = null;
  50.     private boolean processInclude;
  51.     private boolean processInlineSchema;
  52.    
  53.     public SchemaCallback(Logger log, AbstractXMLUtils xmlUtils, boolean processInclude, boolean processInlineSchema){
  54.         this.log = log;
  55.         this.xmlUtils = xmlUtils;
  56.         this.xsdUtils = new XSDUtils(this.xmlUtils);
  57.         this.processInclude = processInclude;
  58.         this.processInlineSchema = processInlineSchema;
  59.     }
  60.    
  61.     @Override
  62.     public void processSchema(InputSource isSource) {
  63.         if(this.processInclude){
  64.             try{
  65.            
  66.                 String publicId = isSource.getPublicId();
  67.                 String systemId = isSource.getSystemId();
  68.                 byte[] resource = Utilities.getAsByteArray(isSource.getByteStream());
  69.            
  70.                 this.addResource(this.xsdUtils.getBaseNameXSDLocation(systemId), resource);
  71.                
  72.                 if(this.log!=null)
  73.                     this.log.debug("SCHEMA CALLBACK (import) ["+publicId+"]  ["+systemId+"] ");
  74.                 else
  75.                     System.out.println("SCHEMA CALLBACK (import) ["+publicId+"]  ["+systemId+"] ");
  76.                
  77.             }catch(Exception e){
  78.                 throw new RuntimeException(e.getMessage(),e);
  79.             }
  80.         }
  81.     }

  82.     @Override
  83.     public void processSchema(String systemId, Element element) {
  84.         if(this.processInlineSchema){
  85.             try{
  86.                
  87.                 byte[] resource = this.xmlUtils.toByteArray(element);
  88.            
  89.                 this.addResource(this.xsdUtils.getBaseNameXSDLocation(systemId), resource);
  90.                
  91.                 if(this.log!=null)
  92.                     this.log.debug("SCHEMA CALLBACK (inline) ["+systemId+"] ");
  93.                 else
  94.                     System.out.println("SCHEMA CALLBACK (inline) ["+systemId+"] ");
  95.                
  96.             }catch(Exception e){
  97.                 throw new RuntimeException(e.getMessage(),e);
  98.             }
  99.         }
  100.     }

  101.     public Map<String, byte[]> getResources() {
  102.         return this.resources;
  103.     }

  104.     public Map<String, List<String>> getMappingNamespaceLocations() {
  105.         return this.mappingNamespaceLocations;
  106.     }
  107.    
  108.     public void addResource(String systemId,byte[]resource) throws XMLException{
  109.        
  110.         if(this.resources.containsKey(systemId)==false){
  111.        
  112.             // registro risorsa con systemid
  113.             this.resources.put(systemId, resource);
  114.    
  115.             // registro namespace
  116.             this.xsdUtils.registraMappingNamespaceLocations(resource, systemId, this.mappingNamespaceLocations);
  117.            
  118.         }
  119.        
  120.     }

  121. }