XSDResourceResolver.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.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.net.URL;
  26. import java.util.HashMap;
  27. import java.util.Map;

  28. import org.openspcoop2.utils.CopyStream;
  29. import org.w3c.dom.ls.LSInput;
  30. import org.w3c.dom.ls.LSResourceResolver;

  31. /**
  32.  * Classe utilizzabile per Risolvere gli import di eventuali schemi
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */

  38. public class XSDResourceResolver implements LSResourceResolver {

  39.     private Map<String, byte[]> resources = new HashMap<String, byte[]>();
  40.    
  41.     public Map<String, byte[]> getResources() {
  42.         return this.resources;
  43.     }
  44.     public XSDResourceResolver(){}
  45.     public XSDResourceResolver(Map<String, byte[]> resources){
  46.         this.resources = resources;
  47.     }
  48.    
  49.     public void addResource(String systemId,byte[] resource){
  50.         this.resources.put(systemId, resource);
  51.     }
  52.    
  53.     public void addResource(String systemId,InputStream resource) throws IOException{
  54.         if(resource==null){
  55.             throw new IOException("InputStream is null");
  56.         }
  57.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  58. //      byte [] buffer = new byte[org.openspcoop2.utils.Utilities.DIMENSIONE_BUFFER];
  59. //      int letti = 0;
  60. //      while( (letti=resource.read(buffer))!=-1 ){
  61. //          bout.write(buffer, 0, letti);
  62. //      }
  63.         try {
  64.             CopyStream.copy(resource, bout);
  65.         }catch(Exception e) {
  66.             throw new IOException(e.getMessage(),e);
  67.         }
  68.         bout.flush();
  69.         bout.close();
  70.         addResource(systemId, bout.toByteArray());
  71.     }
  72.    
  73.     @Override
  74.     public LSInput resolveResource(String type, String namespaceURI,
  75.             String publicId, String systemId, String baseURI) {
  76.        
  77.         try{
  78.        
  79.             //System.out.println("RICHIESTO type["+type+"] namespaceURI["+namespaceURI+"] publicId["+publicId+"]" +
  80.             //      " systemId["+systemId+"] baseURI["+baseURI+"]");
  81.            
  82.             // Algoritmo:
  83.             // 1. Cerco con systemId completo      
  84.             byte[] resource = this.resources.get(systemId);
  85.            
  86.             if(resource==null){
  87.                
  88.                 String baseName = null;
  89.                 String parentName = null;
  90.                 if(systemId.startsWith("http://") || systemId.startsWith("https://") || systemId.startsWith("file://")){
  91.                     URL url = new URL(systemId);
  92.                     File fileUrl = new File(url.getFile());
  93.                     baseName = fileUrl.getName();
  94.                     if(fileUrl.getParentFile()!=null){
  95.                         parentName = fileUrl.getParentFile().getName();
  96.                     }
  97.                 }
  98.                 else{
  99.                     File f = new File(systemId);
  100.                     baseName = f.getName();
  101.                     if(f.getParentFile()!=null){
  102.                         parentName = f.getParentFile().getName();
  103.                     }
  104.                 }
  105.                
  106.                 //System.out.println("NON TROVATO, cerco con (BASE["+baseName+"] PARENT["+parentName+"])");
  107.                
  108.                 if(parentName!=null){
  109.                     //System.out.println("NON TROVATO, cerco con parent["+parentName+"]/baseName["+baseName+"]");
  110.                     resource = this.resources.get(parentName+"/"+baseName);
  111.                 }
  112.                
  113.                 if(resource==null){
  114.                     ///System.out.println("NON TROVATO, cerco con solo base name ["+baseName+"]");
  115.                     resource = this.resources.get(baseName);
  116.                 }
  117.                
  118.                 if(resource==null){
  119.                     if(baseURI!=null){
  120.                         //System.out.println("NON TROVATO, cerco con baseURI ["+baseURI+"]");
  121.                         String ricerca = null;
  122.                         if(baseURI.startsWith("http://") || baseURI.startsWith("file://")){
  123.                             URL url = new URL(baseURI);
  124.                             File fileUrl = new File(url.getFile());
  125.                             String baseNameParent = fileUrl.getName();
  126.                             if(baseURI.length()>baseNameParent.length()){
  127.                                 String prefix = baseURI.substring(0, baseURI.length()-baseNameParent.length());
  128.                                 ricerca = prefix + baseName;
  129.                             }
  130.                         }
  131.                         else{
  132.                             File f = new File(baseURI);
  133.                             if(f.getParentFile()!=null){
  134.                                 String prefix = f.getParentFile().getAbsolutePath();
  135.                                 ricerca = prefix + File.separatorChar + baseName;
  136.                             }
  137.                         }
  138.                         //System.out.println("RICERCO COME ["+ricerca+"]");
  139.                         resource = this.resources.get(ricerca);
  140.                     }
  141.                 }
  142.             }
  143.            
  144.             if(resource!=null){
  145.                 //System.out.println("TROVATO ["+new String(resource)+"]");
  146.                 return new LSInputImpl(type, namespaceURI, publicId, systemId, baseURI, resource);
  147.             }
  148.             else{
  149.                 //System.out.println("NON TROVATO");
  150.                 throw new Exception("non trovata tra le risorse registrate (type["+type+"] namespaceURI["+namespaceURI+"] publicId["+publicId+"] systemId["+systemId+"] baseURI["+baseURI+"])");
  151.             }
  152.            
  153.         }catch(Exception e){
  154.             throw new RuntimeException("Risoluzione risorsa non riuscita: "+e.getMessage(),e);
  155.         }
  156.        
  157.     }

  158. }