WSDLUtilities.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.wsdl;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.FileWriter;
  25. import java.io.IOException;
  26. import java.io.OutputStream;
  27. import java.io.Writer;
  28. import java.net.URI;
  29. import java.util.ArrayList;
  30. import java.util.HashMap;
  31. import java.util.List;

  32. import javax.wsdl.Binding;
  33. import javax.wsdl.Definition;
  34. import javax.wsdl.Port;
  35. import javax.wsdl.Service;
  36. import javax.wsdl.Types;
  37. import javax.wsdl.WSDLException;
  38. import javax.wsdl.extensions.schema.Schema;
  39. import javax.xml.namespace.QName;
  40. import javax.xml.transform.TransformerException;

  41. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  42. import org.openspcoop2.utils.xml.DynamicNamespaceContext;
  43. import org.openspcoop2.utils.xml.PrettyPrintXMLUtils;
  44. import org.openspcoop2.utils.xml.XMLException;
  45. import org.openspcoop2.utils.xml.XPathExpressionEngine;
  46. import org.openspcoop2.utils.xml.XPathNotFoundException;
  47. import org.openspcoop2.utils.xml.XPathReturnType;
  48. import org.openspcoop2.utils.xml.XSDUtils;
  49. import org.w3c.dom.Attr;
  50. import org.w3c.dom.Comment;
  51. import org.w3c.dom.Document;
  52. import org.w3c.dom.Element;
  53. import org.w3c.dom.NamedNodeMap;
  54. import org.w3c.dom.Node;
  55. import org.w3c.dom.NodeList;
  56. import org.w3c.dom.Text;

  57. import com.ibm.wsdl.xml.WSDLReaderImpl;
  58. import com.ibm.wsdl.xml.WSDLWriterImpl;



  59. /**
  60.  * Utilities per i WSDL
  61.  *
  62.  *
  63.  * @author Poli Andrea (apoli@link.it)
  64.  * @author $Author$
  65.  * @version $Rev$, $Date$
  66.  */


  67. public class WSDLUtilities {

  68.    
  69. //  public static WSDLUtilities getInstance(){
  70. //      return new WSDLUtilities(null); // senza xmlUtils
  71. //  }
  72.     public static WSDLUtilities getInstance(AbstractXMLUtils xmlUtils){
  73.         return new WSDLUtilities(xmlUtils);
  74.     }
  75.    
  76.    
  77.    

  78.     private AbstractXMLUtils xmlUtils = null;
  79.     public WSDLUtilities(AbstractXMLUtils xmlUtils){
  80.         this.xmlUtils = xmlUtils;
  81.     }
  82.    
  83.    
  84.    
  85.    
  86.    
  87.    
  88.     /* ---------------- METODI STATICI PUBBLICI ------------------------- */
  89.    
  90.     /** WSDLReader */
  91.     public WSDLReaderImpl getWSDLReader(boolean verbose,boolean importDocuments){
  92.         WSDLReaderImpl wsdlReader = new WSDLReaderImpl();
  93.         wsdlReader.setFeature("javax.wsdl.verbose", verbose);
  94.         wsdlReader.setFeature("javax.wsdl.importDocuments", importDocuments);
  95.         return wsdlReader;
  96.     }
  97.    
  98.    
  99.    
  100.     // IS WSDL
  101.     public boolean isWSDL(byte[]wsdl) throws org.openspcoop2.utils.wsdl.WSDLException {
  102.         try{
  103.             if(this.xmlUtils==null){
  104.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  105.             }
  106.            
  107.             if(!this.xmlUtils.isDocument(wsdl)){
  108.                 return false;
  109.             }
  110.             Document docXML = this.xmlUtils.newDocument(wsdl);
  111.             Element elemXML = docXML.getDocumentElement();
  112.             return isWSDL(elemXML);
  113.         }catch(Exception e){
  114.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  115.         }
  116.     }
  117.     public boolean isWSDL(Document wsdl) throws org.openspcoop2.utils.wsdl.WSDLException {
  118.         Element elemXML = wsdl.getDocumentElement();
  119.         return isWSDL(elemXML);
  120.     }
  121.    
  122.     public boolean isWSDL(Element wsdl) throws org.openspcoop2.utils.wsdl.WSDLException {
  123.         return isWSDL((Node)wsdl);
  124.     }
  125.     public boolean isWSDL(Node wsdl) throws org.openspcoop2.utils.wsdl.WSDLException {
  126.         try{
  127.             if(wsdl == null){
  128.                 throw new Exception("Documento wsdl da verificare non definito");
  129.             }
  130.             //System.out.println("LOCAL["+wsdl.getLocalName()+"]  NAMESPACE["+wsdl.getNamespaceURI()+"]");
  131.             if(!"definitions".equals(wsdl.getLocalName())){
  132.                 return false;
  133.             }
  134.             if(!"http://schemas.xmlsoap.org/wsdl/".equals(wsdl.getNamespaceURI())){
  135.                 return false;
  136.             }
  137.             return true;
  138.         }catch(Exception e){
  139.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  140.         }
  141.     }
  142.    
  143.    
  144.    
  145.    
  146.     // TARGET NAMESPACE
  147.    
  148.     public String getTargetNamespace(byte[]xsd) throws org.openspcoop2.utils.wsdl.WSDLException {
  149.         try{
  150.             if(this.xmlUtils==null){
  151.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  152.             }
  153.            
  154.             if(!this.xmlUtils.isDocument(xsd)){
  155.                 throw new Exception("Wsdl non e' un documento valido");
  156.             }
  157.             Document docXML = this.xmlUtils.newDocument(xsd);
  158.             Element elemXML = docXML.getDocumentElement();
  159.             return getTargetNamespace(elemXML);
  160.         }catch(Exception e){
  161.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  162.         }
  163.     }
  164.    
  165.     public String getTargetNamespace(Document xsd) throws org.openspcoop2.utils.wsdl.WSDLException {
  166.         Element elemXML = xsd.getDocumentElement();
  167.         return getTargetNamespace(elemXML);
  168.     }
  169.    
  170.     public String getTargetNamespace(Element elemXML) throws org.openspcoop2.utils.wsdl.WSDLException {
  171.         return getTargetNamespace((Node)elemXML);
  172.     }
  173.    
  174.     public String getTargetNamespace(Node elemXML) throws org.openspcoop2.utils.wsdl.WSDLException {
  175.         try{
  176.             if(this.xmlUtils==null){
  177.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  178.             }
  179.            
  180.             if(elemXML == null){
  181.                 throw new Exception("Wsdl non e' un documento valido");
  182.             }
  183.             //System.out.println("LOCAL["+elemXML.getLocalName()+"]  NAMESPACE["+elemXML.getNamespaceURI()+"]");
  184.             if(!"definitions".equals(elemXML.getLocalName())){
  185.                 throw new Exception("Root element non e' un definition wsdl ("+elemXML.getLocalName()+")");
  186.             }
  187.             String targetNamespace = this.xmlUtils.getAttributeValue(elemXML, "targetNamespace");
  188.             //System.out.println("TARGET["+targetNamespace+"]");
  189.             return targetNamespace;
  190.         }catch(Exception e){
  191.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  192.         }
  193.     }
  194.    
  195.     public String getTargetNamespace(Schema schema){
  196.         NamedNodeMap attributi = schema.getElement().getAttributes();
  197.         if(attributi!=null){
  198.             for(int i=0; i<attributi.getLength(); i++){
  199.                 Node a = attributi.item(i);
  200.                 //System.out.println("aaa ["+a.getLocalName()+"] ["+a.getPrefix()+"] ["+a.getNodeValue()+"]");
  201.                 if("targetNamespace".equals(a.getLocalName())){
  202.                     return a.getNodeValue();
  203.                 }
  204.             }
  205.         }
  206.         return null;
  207.     }
  208.    
  209.    
  210.     // IMPORT
  211.     public String getImportNamespace(Node elemXML) throws org.openspcoop2.utils.wsdl.WSDLException {
  212.         try{
  213.             if(this.xmlUtils==null){
  214.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  215.             }
  216.             if(elemXML == null){
  217.                 throw new Exception("Non e' un import valido");
  218.             }
  219.             //System.out.println("LOCAL["+elemXML.getLocalName()+"]  NAMESPACE["+elemXML.getNamespaceURI()+"]");
  220.             if(!"import".equals(elemXML.getLocalName())){
  221.                 throw new Exception("Root element non e' un import wsdl ("+elemXML.getLocalName()+")");
  222.             }
  223.             String targetNamespace = this.xmlUtils.getAttributeValue(elemXML, "namespace");
  224.             //System.out.println("TARGET["+targetNamespace+"]");
  225.             return targetNamespace;
  226.         }catch(Exception e){
  227.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  228.         }
  229.     }
  230.     public String getImportLocation(Node elemXML) throws org.openspcoop2.utils.wsdl.WSDLException {
  231.         try{
  232.             if(this.xmlUtils==null){
  233.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  234.             }
  235.             if(elemXML == null){
  236.                 throw new Exception("Non e' un import valido");
  237.             }
  238.             //System.out.println("LOCAL["+elemXML.getLocalName()+"]  NAMESPACE["+elemXML.getNamespaceURI()+"]");
  239.             if(!"import".equals(elemXML.getLocalName())){
  240.                 throw new Exception("Root element non e' un import wsdl ("+elemXML.getLocalName()+")");
  241.             }
  242.             String targetNamespace = this.xmlUtils.getAttributeValue(elemXML, "location");
  243.             //System.out.println("TARGET["+targetNamespace+"]");
  244.             return targetNamespace;
  245.         }catch(Exception e){
  246.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  247.         }
  248.     }
  249.    
  250.     // SCHEMA LOCATION
  251.     public void updateLocation(Node elemXML, String newLocation) throws org.openspcoop2.utils.wsdl.WSDLException {
  252.        
  253.         try{
  254.             if(this.xmlUtils==null){
  255.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  256.             }
  257.             if(elemXML == null){
  258.                 throw new Exception("Non e' un import valido");
  259.             }
  260.             //System.out.println("LOCAL["+elemXML.getLocalName()+"]  NAMESPACE["+elemXML.getNamespaceURI()+"]");
  261.             if(!"import".equals(elemXML.getLocalName())){
  262.                 throw new Exception("Root element non e' un import wsdl ("+elemXML.getLocalName()+")");
  263.             }
  264.        
  265.             if(elemXML!=null && elemXML.getAttributes()!=null && elemXML.getAttributes().getLength()>0){
  266.                
  267.     //          try{
  268.     //              System.out.println(" PRIMA: "+this.xmlUtils.toString(schemaImportInclude));
  269.     //          }catch(Exception e){System.out.println("ERRORE PRIMA");}
  270.                
  271.                 Attr oldLocation = (Attr) elemXML.getAttributes().getNamedItem("location");
  272.                 this.xmlUtils.removeAttribute(oldLocation, (Element)elemXML);
  273.                
  274.     //          try{
  275.     //              System.out.println(" REMOVE: "+this.xmlUtils.toString(schemaImportInclude));
  276.     //          }catch(Exception e){System.out.println("ERRORE REMOVE");}
  277.                
  278.                 oldLocation.setValue(newLocation);
  279.                 this.xmlUtils.addAttribute(oldLocation, (Element)elemXML);
  280.                
  281.     //          try{
  282.     //              System.out.println(" DOPO: "+this.xmlUtils.toString(schemaImportInclude));
  283.     //          }catch(Exception e){System.out.println("ERRORE DOPO");}
  284.             }
  285.         }catch(Exception e){
  286.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  287.         }
  288.     }
  289.    
  290.    
  291.    
  292.    
  293.    
  294.    
  295.    
  296.    
  297.    
  298.     /* ---------------- WRITE TO ------------------------- */
  299.    
  300.     public void writeWsdlTo(Definition wsdl, String absoluteFilename) throws WSDLException, IOException, org.openspcoop2.utils.wsdl.WSDLException{
  301.         writeWsdlTo(wsdl,absoluteFilename,false);
  302.     }
  303.     public void writeWsdlTo(Definition wsdl, String absoluteFilename,boolean prettyPrint) throws WSDLException, IOException, org.openspcoop2.utils.wsdl.WSDLException{
  304.         if(wsdl == null) return;
  305.         WSDLWriterImpl writer = new WSDLWriterImpl();
  306.         if(prettyPrint){
  307.             writeWsdlTo(wsdl, new File(absoluteFilename), prettyPrint);
  308.         }else{
  309.             writer.writeWSDL(wsdl, new FileWriter(absoluteFilename));
  310.         }
  311.     }
  312.    
  313.     public void writeWsdlTo(Definition wsdl, File file) throws WSDLException, IOException, org.openspcoop2.utils.wsdl.WSDLException{
  314.         writeWsdlTo(wsdl,file,false);
  315.     }
  316.     public void writeWsdlTo(Definition wsdl, File file,boolean prettyPrint) throws WSDLException, IOException, org.openspcoop2.utils.wsdl.WSDLException{
  317.         if(wsdl == null) return;
  318.         WSDLWriterImpl writer = new WSDLWriterImpl();
  319.         if(prettyPrint){
  320.             FileOutputStream fout = null;
  321.             try{
  322.                 fout = new FileOutputStream(file);
  323.                 writeWsdlTo(wsdl, fout, prettyPrint);
  324.             }finally{
  325.                 try{
  326.                     if(fout!=null) {
  327.                         fout.flush();
  328.                     }
  329.                 }catch(Exception eClose){}
  330.                 try{
  331.                     if(fout!=null) {
  332.                         fout.close();
  333.                     }
  334.                 }catch(Exception eClose){
  335.                     // close
  336.                 }
  337.             }
  338.         }else{
  339.             writer.writeWSDL(wsdl, new FileWriter(file));
  340.         }
  341.     }
  342.    
  343.     public void writeWsdlTo(Definition wsdl, OutputStream out) throws WSDLException, IOException, org.openspcoop2.utils.wsdl.WSDLException{
  344.         writeWsdlTo(wsdl,out,false);
  345.     }
  346.     public void writeWsdlTo(Definition wsdl, OutputStream out,boolean prettyPrint) throws WSDLException, IOException, org.openspcoop2.utils.wsdl.WSDLException{
  347.         if(wsdl == null) return;
  348.         WSDLWriterImpl writer = new WSDLWriterImpl();
  349.         if(prettyPrint){
  350.             out.write(prettyPrintWsdl(wsdl).getBytes());
  351.         }else{
  352.             writer.writeWSDL(wsdl, out);
  353.         }
  354.     }
  355.    
  356.     public void writeWsdlTo(Definition wsdl, Writer writer)throws WSDLException, IOException, org.openspcoop2.utils.wsdl.WSDLException{
  357.         writeWsdlTo(wsdl,writer,false);
  358.     }
  359.     public void writeWsdlTo(Definition wsdl, Writer writer,boolean prettyPrint)throws WSDLException, IOException, org.openspcoop2.utils.wsdl.WSDLException{
  360.         if(wsdl == null) return;
  361.         WSDLWriterImpl writerWsdl = new WSDLWriterImpl();
  362.         if(prettyPrint){
  363.             writer.append(prettyPrintWsdl(wsdl));
  364.         }else{
  365.             writerWsdl.writeWSDL(wsdl, writer);
  366.         }
  367.     }
  368.    
  369.     private String prettyPrintWsdl(Definition wsdl) throws WSDLException, IOException, org.openspcoop2.utils.wsdl.WSDLException{
  370.         try{
  371.             if(this.xmlUtils==null){
  372.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  373.             }
  374.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  375.             writeWsdlTo(wsdl, bout, false);
  376.             bout.flush();
  377.             bout.close();
  378.             Document wsdlDocument = this.xmlUtils.newDocument(bout.toByteArray());
  379.             return PrettyPrintXMLUtils.prettyPrintWithTrAX(wsdlDocument);
  380.         }catch(Exception e){
  381.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  382.         }
  383.     }
  384.    
  385.    
  386.    
  387.    
  388.    
  389.     /* ------------------ GENERAZIONE DEFINITION ---------------------------- */
  390.     /** NOTA: le importsDocument riguardano i WSDL Imports e non gli xsd imports!!!!!!! */
  391.     /**
  392.      * Legge un WSDL dal path in parametro e ne ritorna la Definition.
  393.      * @param file Il file da cui leggere il WSDL.
  394.      * @return La definition corretta o null in caso di path mancante o errori.
  395.      */
  396.     public Definition readWSDLFromFile(File file) throws WSDLException{
  397.         return readWSDLFromFile(file, false, true);
  398.     }
  399.     public Definition readWSDLFromFile(File file,boolean verbose,boolean importsDocument) throws WSDLException{
  400.         try{
  401.             if (file == null) throw new Exception("Path non definito");
  402.             WSDLReaderImpl reader = getWSDLReader(verbose, importsDocument);
  403.             try {
  404.                 Definition def = reader.readWSDL(file.getAbsolutePath());
  405.                 return def;
  406.             } catch (WSDLException e) {
  407.                 throw e;
  408.             }
  409.         }catch(Exception e){
  410.             throw new WSDLException("WSDLDefinitorio.readWSDLFromLocation(String path)","Lettura del wsdl non riuscita: "+e.getMessage());
  411.         }
  412.     }
  413.     /**
  414.      * Legge un WSDL dal path in parametro e ne ritorna la Definition.
  415.      * @param path Il path da cui leggere il WSDL.
  416.      * @return La definition corretta o null in caso di path mancante o errori.
  417.      */
  418.     public Definition readWSDLFromLocation(String path) throws WSDLException{
  419.         return readWSDLFromLocation(path, false, true);
  420.     }
  421.     public Definition readWSDLFromLocation(String path,boolean verbose,boolean importsDocument) throws WSDLException{
  422.         try{
  423.             if (path == null) throw new Exception("Path non definito");
  424.             WSDLReaderImpl reader = getWSDLReader(verbose, importsDocument);
  425.             try {
  426.                 Definition def = reader.readWSDL(path);
  427.                 return def;
  428.             } catch (WSDLException e) {
  429.                 throw e;
  430.             }
  431.         }catch(Exception e){
  432.             throw new WSDLException("WSDLDefinitorio.readWSDLFromLocation(String path)","Lettura del wsdl non riuscita: "+e.getMessage());
  433.         }
  434.     }
  435.     /**
  436.      * Legge un WSDL dal path in parametro e ne ritorna la Definition.
  437.      * @param wsdl I bytes da cui leggere il WSDL.
  438.      * @return La definition corretta o null in caso di path mancante o errori.
  439.      */
  440.     public Definition readWSDLFromBytes(byte[] wsdl) throws WSDLException{
  441.         return readWSDLFromBytes(wsdl, false, true);
  442.     }
  443.     public Definition readWSDLFromBytes(byte[] wsdl,boolean verbose,boolean importsDocument) throws WSDLException{
  444.         try{
  445.             if(this.xmlUtils==null){
  446.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  447.             }
  448.             if (wsdl == null) throw new Exception("Bytes non definiti");
  449.             WSDLReaderImpl reader = getWSDLReader(verbose, importsDocument);
  450.             try {
  451.                 Document document = this.xmlUtils.newDocument(wsdl);
  452.                 Definition def = reader.readWSDL(null,document);
  453.                 return def;
  454.             } catch (WSDLException e) {
  455.                 throw e;
  456.             }
  457.         }catch(Exception e){
  458.             throw new WSDLException("WSDLDefinitorio.readWSDLFromBytes(byte[] wsdl)","Lettura del wsdl non riuscita: "+e.getMessage());
  459.         }
  460.     }
  461.     /**
  462.      * Legge un WSDL dal path in parametro e ne ritorna la Definition.
  463.      * @param doc Document da cui leggere il WSDL.
  464.      * @return La definition corretta o null in caso di path mancante o errori.
  465.      */
  466.     public Definition readWSDLFromDocument(Document doc) throws WSDLException{
  467.         return readWSDLFromDocument(doc, false, true);
  468.     }
  469.     public Definition readWSDLFromDocument(Document doc,boolean verbose,boolean importsDocument) throws WSDLException{
  470.         try{
  471.             if (doc == null) throw new Exception("Document non definito");
  472.             WSDLReaderImpl reader = getWSDLReader(verbose, importsDocument);
  473.             try {
  474.                 Definition def = reader.readWSDL(null,doc);
  475.                 return def;
  476.             } catch (WSDLException e) {
  477.                 throw e;
  478.             }
  479.         }catch(Exception e){
  480.             throw new WSDLException("WSDLDefinitorio.readWSDLFromDocument(Document doc)","Lettura del wsdl non riuscita: "+e.getMessage());
  481.         }
  482.     }
  483.     /**
  484.      * Legge un WSDL dal path in parametro e ne ritorna la Definition.
  485.      * @param elem Element da cui leggere il WSDL.
  486.      * @return La definition corretta o null in caso di path mancante o errori.
  487.      */
  488.     public Definition readWSDLFromElement(Element elem) throws WSDLException{
  489.         return readWSDLFromElement(elem, false, true);
  490.     }
  491.     public Definition readWSDLFromElement(Element elem,boolean verbose,boolean importsDocument) throws WSDLException{
  492.         try{
  493.             if (elem == null) throw new Exception("Element non definito");
  494.             WSDLReaderImpl reader = getWSDLReader(verbose, importsDocument);
  495.             try {
  496.                 String param = null;
  497.                 Definition def = reader.readWSDL(param,elem);
  498.                 return def;
  499.             } catch (WSDLException e) {
  500.                 throw e;
  501.             }
  502.         }catch(Exception e){
  503.             throw new WSDLException("WSDLDefinitorio.readWSDLFromElement(Element elem)","Lettura del wsdl non riuscita: "+e.getMessage());
  504.         }
  505.     }
  506.     /**
  507.      * Legge un WSDL dal path in parametro e ne ritorna la Definition.
  508.      * @param uri La URI da cui leggere il WSDL.
  509.      * @return La definition corretta o null in caso di path mancante o errori.
  510.      */
  511.     public Definition readWSDLFromURI(URI uri) throws WSDLException{
  512.         return readWSDLFromURI(uri, false, true);
  513.     }
  514.     public Definition readWSDLFromURI(URI uri,boolean verbose,boolean importsDocument) throws WSDLException{
  515.         try{
  516.             if (uri == null) throw new Exception("URI non definita");
  517.             WSDLReaderImpl reader = getWSDLReader(verbose, importsDocument);
  518.             try {
  519.                 Definition def = reader.readWSDL(null,uri.getPath());
  520.                 return def;
  521.             } catch (WSDLException e) {
  522.                 throw e;
  523.             }
  524.         }catch(Exception e){
  525.             throw new WSDLException("WSDLDefinitorio.readWSDLFromURI(URI uri)","Lettura del wsdl non riuscita: "+e.getMessage());
  526.         }
  527.     }
  528.    
  529.    
  530.    
  531.    
  532.    
  533.    
  534.    
  535.     /* ------------------ READ URL ---------------------------- */
  536.    
  537.     public String getServiceEndpoint(byte [] wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  538.         return this.getServiceEndpoint(wsdl, null);
  539.     }
  540.     public String getServiceEndpoint(byte [] wsdl, String portType) throws org.openspcoop2.utils.wsdl.WSDLException{
  541.         try{
  542.             Document d = this.xmlUtils.newDocument(wsdl);
  543.             this.removeImports(d);
  544.             this.removeSchemiIntoTypes(d);
  545.             return this.getServiceEndpoint(new DefinitionWrapper(d, this.xmlUtils, false, false),portType);
  546.         }catch(Exception e){
  547.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  548.         }
  549.     }
  550.     public String getServiceEndpoint(Definition wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  551.         return this.getServiceEndpoint(wsdl, null);
  552.     }
  553.     public String getServiceEndpoint(Definition wsdl, String portType) throws org.openspcoop2.utils.wsdl.WSDLException{
  554.         try {
  555.             return this.getServiceEndpoint(new DefinitionWrapper(wsdl, this.xmlUtils),portType);
  556.         }catch(Exception e){
  557.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  558.         }
  559.     }
  560.     public String getServiceEndpoint(DefinitionWrapper wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  561.         return this.getServiceEndpoint(wsdl, null);
  562.     }
  563.     public String getServiceEndpoint(DefinitionWrapper wsdl, String portType) throws org.openspcoop2.utils.wsdl.WSDLException{
  564.         try {
  565.             if(wsdl==null || wsdl.getServices()==null || wsdl.getServices().isEmpty()) {
  566.                 return null;
  567.             }
  568.             if(portType==null) {
  569.                 if(wsdl.getServices().size()>1) {
  570.                     throw new Exception("Found more than one service, select port type");
  571.                 }
  572.                 Service service = (Service) wsdl.getServices().values().iterator().next();
  573.                 if(service.getPorts()==null || service.getPorts().isEmpty()) {
  574.                     return null;
  575.                 }
  576.                 if(service.getPorts().size()>1) {
  577.                     throw new Exception("Found more than one port, select port type");
  578.                 }
  579.                 List<?> list = ((Port) service.getPorts().values().iterator().next()).getExtensibilityElements();
  580.                 if(list==null || list.isEmpty()) {
  581.                     return null;
  582.                 }
  583.                 for (Object object : list) {
  584.                     if(object instanceof  javax.wsdl.extensions.soap.SOAPAddress) {
  585.                         javax.wsdl.extensions.soap.SOAPAddress addr = (javax.wsdl.extensions.soap.SOAPAddress) object;
  586.                         return addr.getLocationURI();
  587.                     }
  588.                     else if(object instanceof  javax.wsdl.extensions.soap12.SOAP12Address) {
  589.                         javax.wsdl.extensions.soap12.SOAP12Address addr = (javax.wsdl.extensions.soap12.SOAP12Address) object;
  590.                         return addr.getLocationURI();
  591.                     }
  592.                 }
  593.                 return null;
  594.             }
  595.             else {
  596.                 if(wsdl.getPortTypes()==null || wsdl.getPortTypes().isEmpty()) {
  597.                     return null;
  598.                 }
  599.                 QName qNamePT = null;
  600.                 for (Object o : wsdl.getPortTypes().keySet()) {
  601.                     if(o instanceof QName) {
  602.                         QName qName = (QName) o;
  603.                         if(portType.equals(qName.getLocalPart())) {
  604.                             qNamePT = qName;
  605.                             break;
  606.                         }
  607.                     }
  608.                 }
  609.                 if(qNamePT==null) {
  610.                     return null;
  611.                 }
  612.                 if(wsdl.getBindings()==null || wsdl.getBindings().isEmpty()) {
  613.                     return null;
  614.                 }
  615.                 QName qNameBinding = null;
  616.                 for (Object o : wsdl.getBindings().values()) {
  617.                     if(o instanceof Binding) {
  618.                         Binding bindingCheck = (Binding) o;
  619.                         if(bindingCheck.getPortType()!=null) {
  620.                             if(qNamePT.equals(bindingCheck.getPortType().getQName())) {
  621.                                 qNameBinding = bindingCheck.getQName();
  622.                                 break;
  623.                             }
  624.                         }
  625.                     }
  626.                 }
  627.                 if(qNameBinding==null) {
  628.                     return null;
  629.                 }
  630.                 for (Object o : wsdl.getServices().values()) {
  631.                     if(o instanceof Service) {
  632.                         Service serviceCheck = (Service) o;
  633.                         if(serviceCheck.getPorts()!=null && !serviceCheck.getPorts().isEmpty()) {
  634.                             for (Object oP : serviceCheck.getPorts().values()) {
  635.                                 if(oP instanceof Port) {
  636.                                     Port portCheck = (Port) oP;
  637.                                     if(portCheck.getBinding()!=null ) {
  638.                                         if(qNameBinding.equals(portCheck.getBinding().getQName())) {
  639.                                             List<?> list = portCheck.getExtensibilityElements();
  640.                                             if(list==null || list.isEmpty()) {
  641.                                                 return null;
  642.                                             }
  643.                                             for (Object object : list) {
  644.                                                 if(object instanceof  javax.wsdl.extensions.soap.SOAPAddress) {
  645.                                                     javax.wsdl.extensions.soap.SOAPAddress addr = (javax.wsdl.extensions.soap.SOAPAddress) object;
  646.                                                     return addr.getLocationURI();
  647.                                                 }
  648.                                                 else if(object instanceof  javax.wsdl.extensions.soap12.SOAP12Address) {
  649.                                                     javax.wsdl.extensions.soap12.SOAP12Address addr = (javax.wsdl.extensions.soap12.SOAP12Address) object;
  650.                                                     return addr.getLocationURI();
  651.                                                 }
  652.                                             }      
  653.                                         }
  654.                                     }
  655.                                 }
  656.                             }
  657.                         }
  658.                     }
  659.                 }
  660.                 return null;
  661.             }
  662.         }catch(Exception e){
  663.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  664.         }
  665.     }
  666.    
  667.    
  668.    
  669.    
  670.    
  671.     /* ---------------- METODI ADD ------------------------- */
  672.     public void addSchemaIntoTypes(Document wsdl, Node schema) throws org.openspcoop2.utils.wsdl.WSDLException{
  673.         try{
  674.             NodeList list = wsdl.getChildNodes();
  675.             if(list!=null){
  676.                 for(int i=0; i<list.getLength(); i++){
  677.                     Node child = list.item(i);
  678.                     if("definitions".equals(child.getLocalName())){
  679.                         NodeList listDefinition = child.getChildNodes();
  680.                         if(listDefinition!=null){
  681.                             for(int j=0; j<listDefinition.getLength(); j++){
  682.                                 Node childDefinition = listDefinition.item(j);
  683.                                 if("types".equals(childDefinition.getLocalName())){
  684.                                    
  685.                                     childDefinition.appendChild(wsdl.createTextNode("\n\t\t"));
  686.                                     childDefinition.appendChild(schema);
  687.                                     childDefinition.appendChild(wsdl.createTextNode("\n"));
  688.                                    
  689.                                 }
  690.                             }
  691.                         }
  692.                     }
  693.                 }
  694.             }
  695.         }catch(Exception e){
  696.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante l'aggiunto di uno schema nell'elemento Types: "+e.getMessage(),e);
  697.         }
  698.     }
  699.    
  700.     public void addImportSchemaIntoTypes(Document wsdl, String targetNamespace, String location) throws org.openspcoop2.utils.wsdl.WSDLException{
  701.        
  702.         // NOTA: types deve esistere
  703.        
  704.         try{
  705.             NodeList list = wsdl.getChildNodes();
  706.             if(list!=null){
  707.                 for(int i=0; i<list.getLength(); i++){
  708.                     Node child = list.item(i);
  709.                     if("definitions".equals(child.getLocalName())){
  710.                         NodeList listDefinition = child.getChildNodes();
  711.                         if(listDefinition!=null){
  712.                             for(int j=0; j<listDefinition.getLength(); j++){
  713.                                 Node childDefinition = listDefinition.item(j);
  714.                                 if("types".equals(childDefinition.getLocalName())){
  715.                                    
  716.                                     Element importSchemaElement = wsdl.createElementNS("http://www.w3.org/2001/XMLSchema", "schema");
  717.                                     importSchemaElement.setAttribute("targetNamespace", targetNamespace);
  718.                                    
  719.                                     Element importElement = wsdl.createElementNS("http://www.w3.org/2001/XMLSchema", "import");
  720.                                     importElement.setAttribute("namespace", targetNamespace);
  721.                                     importElement.setAttribute("schemaLocation", location);
  722.                            
  723.                                     importSchemaElement.appendChild(wsdl.createTextNode("\n\t\t\t"));
  724.                                     importSchemaElement.appendChild(importElement);
  725.                                     importSchemaElement.appendChild(wsdl.createTextNode("\n\t\t"));
  726.                                    
  727.                                     childDefinition.appendChild(wsdl.createTextNode("\n\t\t"));
  728.                                     childDefinition.appendChild(importSchemaElement);
  729.                                     childDefinition.appendChild(wsdl.createTextNode("\n\n"));
  730.                                 }
  731.                             }
  732.                         }
  733.                     }
  734.                 }
  735.             }
  736.         }catch(Exception e){
  737.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante l'aggiunto di uno schema nell'elemento Types: "+e.getMessage(),e);
  738.         }
  739.     }
  740.    
  741.    
  742.    
  743.    
  744.    
  745.    
  746.     /* ---------------- METODI GET ------------------------- */
  747.            
  748.     public Node getIfExistsDefinitionsElementIntoWSDL(Document wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  749.        
  750.         try{
  751.             NodeList list = wsdl.getChildNodes();
  752.             if(list!=null){
  753.                 for(int i=0; i<list.getLength(); i++){
  754.                     Node child = list.item(i);
  755.                     if("definitions".equals(child.getLocalName())){
  756.                         return child;
  757.                     }
  758.                 }
  759.             }
  760.             return null;
  761.         }catch(Exception e){
  762.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante la lettura del wsdl: "+e.getMessage(),e);
  763.         }
  764.     }
  765.        
  766.    

  767.    
  768.    
  769.    
  770.    
  771.    
  772.     /* ---------------- METODI READ IMPORTS ------------------------- */
  773.    
  774.     public List<Node> readImports(Document wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  775.        
  776.         try{
  777.             List<Node> imports = new ArrayList<Node>();
  778.            
  779.             NodeList list = wsdl.getChildNodes();
  780.             if(list!=null){
  781.                 for(int i=0; i<list.getLength(); i++){
  782.                     Node child = list.item(i);
  783.                     if("definitions".equals(child.getLocalName())){
  784.                         NodeList listDefinition = child.getChildNodes();
  785.                         if(listDefinition!=null){
  786.                             for(int j=0; j<listDefinition.getLength(); j++){
  787.                                 Node childDefinition = listDefinition.item(j);
  788.                                 if("import".equals(childDefinition.getLocalName())){
  789.                                     imports.add(childDefinition);
  790.                                 }
  791.                             }
  792.                         }
  793.                     }
  794.                 }
  795.             }
  796.             return imports;
  797.         }catch(Exception e){
  798.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante la lettura del wsdl: "+e.getMessage(),e);
  799.         }
  800.     }
  801.    
  802.     public List<Node> readImportsAndIncludesSchemaIntoTypes(Document wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  803.         return readImportsIncludesSchemaIntoTypes(wsdl,true,true);
  804.     }
  805.     public List<Node> readImportsSchemaIntoTypes(Document wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  806.         return readImportsIncludesSchemaIntoTypes(wsdl,true,false);
  807.     }
  808.     public List<Node> readIncludesSchemaIntoTypes(Document wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  809.         return readImportsIncludesSchemaIntoTypes(wsdl,false,true);
  810.     }
  811.     private List<Node> readImportsIncludesSchemaIntoTypes(Document wsdl,boolean readImport,boolean readInclude) throws org.openspcoop2.utils.wsdl.WSDLException{
  812.        
  813.         try{
  814.             if(this.xmlUtils==null){
  815.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  816.             }
  817.             XSDUtils xsdUtils = new XSDUtils(this.xmlUtils);
  818.            
  819.             List<Node> imports = new ArrayList<Node>();
  820.            
  821.             NodeList list = wsdl.getChildNodes();
  822.             if(list!=null){
  823.                 for(int i=0; i<list.getLength(); i++){
  824.                     Node child = list.item(i);
  825.                     if("definitions".equals(child.getLocalName())){
  826.                         NodeList listDefinition = child.getChildNodes();
  827.                         if(listDefinition!=null){
  828.                             for(int j=0; j<listDefinition.getLength(); j++){
  829.                                 Node childDefinition = listDefinition.item(j);
  830.                                 if("types".equals(childDefinition.getLocalName())){
  831.                                     NodeList listTypes = childDefinition.getChildNodes();
  832.                                     if(listTypes!=null){
  833.                                         for(int h=0; h<listTypes.getLength(); h++){
  834.                                             Node childTypes = listTypes.item(h);
  835.                                             if("schema".equals(childTypes.getLocalName())){
  836.                                                
  837.                                                 String targetNamespaceSchema = null;
  838.                                                 if(targetNamespaceSchema==null){
  839.                                                     try{
  840.                                                         targetNamespaceSchema = xsdUtils.getTargetNamespace(childTypes);
  841.                                                     }catch(Exception e){}
  842.                                                 }
  843.                                                
  844.                                                 if(readImport){
  845.                                                     List<Node> importsSchemi = xsdUtils.readImports(targetNamespaceSchema,childTypes);
  846.                                                     if(importsSchemi!=null && importsSchemi.size()>0){
  847.                                                         imports.addAll(importsSchemi);
  848.                                                     }
  849.                                                 }
  850.                                                 if(readInclude){
  851.                                                     List<Node> includesSchemi = xsdUtils.readIncludes(targetNamespaceSchema,childTypes);
  852.                                                     if(includesSchemi!=null && includesSchemi.size()>0){
  853.                                                         imports.addAll(includesSchemi);
  854.                                                     }
  855.                                                 }
  856.                                                
  857.                                             }
  858.                                         }
  859.                                     }
  860.                                 }
  861.                             }
  862.                         }
  863.                     }
  864.                 }
  865.             }
  866.             return imports;
  867.         }catch(Exception e){
  868.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante la lettura del wsdl: "+e.getMessage(),e);
  869.         }
  870.     }
  871.    
  872.     public List<Node> readImportsAndIncludesFromSchemaXSD(Schema xsd) throws org.openspcoop2.utils.wsdl.WSDLException{
  873.         try{
  874.             if(this.xmlUtils==null){
  875.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  876.             }
  877.             XSDUtils xsdUtils = new XSDUtils(this.xmlUtils);
  878.             return xsdUtils.readImportsAndIncludes(getTargetNamespace(xsd),xsd.getElement());
  879.         }catch(Exception e){
  880.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  881.         }
  882.     }
  883.     public List<Node> readImportsFromSchemaXSD(Schema xsd) throws org.openspcoop2.utils.wsdl.WSDLException{
  884.         try{
  885.             if(this.xmlUtils==null){
  886.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  887.             }
  888.             XSDUtils xsdUtils = new XSDUtils(this.xmlUtils);
  889.             return xsdUtils.readImports(getTargetNamespace(xsd),xsd.getElement());
  890.         }catch(Exception e){
  891.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  892.         }
  893.     }
  894.     public List<Node> readIncludesFromSchemaXSD(Schema xsd) throws org.openspcoop2.utils.wsdl.WSDLException{
  895.         try{
  896.             if(this.xmlUtils==null){
  897.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  898.             }
  899.             XSDUtils xsdUtils = new XSDUtils(this.xmlUtils);
  900.             return xsdUtils.readIncludes(getTargetNamespace(xsd),xsd.getElement());
  901.         }catch(Exception e){
  902.             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  903.         }
  904.     }
  905.    
  906.    
  907.     /**
  908.      * Recupera l'array dei documenti in xsd:include
  909.      * @param wsdlNormalizzato
  910.      * @return array dei documenti in xsd:include
  911.      * @throws IOException
  912.      * @throws XMLException
  913.      * @throws org.openspcoop2.utils.wsdl.WSDLException
  914.      */
  915.     public List<byte[]> getSchemiXSD(Definition wsdlNormalizzato) throws IOException,TransformerException, XMLException, org.openspcoop2.utils.wsdl.WSDLException{
  916.        
  917.         if(this.xmlUtils==null){
  918.             throw new org.openspcoop2.utils.wsdl.WSDLException("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  919.         }
  920.        
  921.         // Il wsdl deve contenere solo schemi xsd completi
  922.        
  923.         List<byte[]> v = new ArrayList<byte[]>();
  924.        
  925.         Types types = wsdlNormalizzato.getTypes();
  926.         List<?> xsdTypes = types.getExtensibilityElements();
  927.         for (int i = 0; i< xsdTypes.size(); i++){
  928.             Schema schema = (Schema) xsdTypes.get(i);
  929.             v.add(this.xmlUtils.toByteArray(schema.getElement()));
  930.         }
  931.         return v;
  932.        
  933.     }
  934.    
  935.     public List<Node> getSchemiXSD(Document wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  936.        
  937.         try{
  938.             if(this.xmlUtils==null){
  939.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  940.             }
  941.             XSDUtils xsdUtils = new XSDUtils(this.xmlUtils);
  942.            
  943.             List<Node> schemi = new ArrayList<Node>();
  944.            
  945.             NodeList list = wsdl.getChildNodes();
  946.             if(list!=null){
  947.                 for(int i=0; i<list.getLength(); i++){
  948.                     Node child = list.item(i);
  949.                     if("definitions".equals(child.getLocalName())){
  950.                         NodeList listDefinition = child.getChildNodes();
  951.                         if(listDefinition!=null){
  952.                             for(int j=0; j<listDefinition.getLength(); j++){
  953.                                 Node childDefinition = listDefinition.item(j);
  954.                                 if("types".equals(childDefinition.getLocalName())){
  955.                                     NodeList listTypes = childDefinition.getChildNodes();
  956.                                     if(listTypes!=null){
  957.                                         for(int h=0; h<listTypes.getLength(); h++){
  958.                                             Node childTypes = listTypes.item(h);
  959.                                            
  960.                                             if("schema".equals(childTypes.getLocalName()) && xsdUtils.isXSDSchema(childTypes) ){
  961.                                                
  962.                                                 schemi.add(childTypes);
  963.                                                
  964.                                             }
  965.                                         }
  966.                                     }
  967.                                 }
  968.                             }
  969.                         }
  970.                     }
  971.                 }
  972.             }
  973.             return schemi;
  974.         }catch(Exception e){
  975.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante la lettura del wsdl: "+e.getMessage(),e);
  976.         }
  977.     }
  978.     public List<byte[]> getBytesSchemiXSD(Document wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  979.         List<Node> schemi = this.getSchemiXSD(wsdl);
  980.        
  981.         try{
  982.             if(this.xmlUtils==null){
  983.                 throw new Exception("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  984.             }
  985.             List<byte[]> schemiBytes = new ArrayList<byte[]>();
  986.             if(schemi!=null && schemi.size()>0){
  987.                 for (Node node : schemi) {
  988.                     schemiBytes.add(this.xmlUtils.toByteArray(node));      
  989.                 }
  990.             }
  991.             return schemiBytes;
  992.         }catch(Exception e){
  993.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante la lettura del wsdl: "+e.getMessage(),e);
  994.         }
  995.     }
  996.    
  997.    
  998.    
  999.    
  1000.    
  1001.    
  1002.     /* ---------------- METODI NORMALIZZAZIONE ------------------------- */
  1003.    
  1004.     public String normalizzazioneSchemaPerInserimentoInWsdl(Element schemaXSD,Element wsdl,
  1005.             HashMap<String,String> prefixForWSDL,String uniquePrefixWSDL,boolean docImportato,String targetNamespaceParent) throws org.openspcoop2.utils.wsdl.WSDLException{
  1006.        
  1007.         String targetNamespace = null;
  1008.                
  1009.         if(docImportato){
  1010.            
  1011.             // GESTIONE DOCUMENTO IMPORTATO
  1012.             //System.out.println("***** IMPORT");
  1013.            
  1014.             // Esamino targetNamespace e prefisso dell'xsd per verificarne la presenza nel definitions del wsdl
  1015.             targetNamespace = readPrefixForWsdl(schemaXSD, wsdl, prefixForWSDL, uniquePrefixWSDL, true);
  1016.        
  1017.             // Rimozione completa di tutti gli attributi all'infuori del TargetNamespace e dell'associato prefix
  1018.             NamedNodeMap attributi = schemaXSD.getAttributes();
  1019.             List<Attr> attributiDaMantenere = new ArrayList<Attr>();    
  1020.             if(attributi!=null && attributi.getLength()>0){
  1021.                 for (int i = (attributi.getLength()-1); i >= 0; i--) {
  1022.                     Attr attr = (Attr) attributi.item(i);
  1023.                     //System.out.println("REMOVE NAME["+attr.getName()+"] LOCAL_NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] URI["+attr.getNamespaceURI()+"]...");
  1024.                     /*
  1025.                      *  REMOVE NAME[xmlns:tns] LOCAL_NAME[tns] VALUE[http://example.ResponseError] URI[http://www.w3.org/2000/xmlns/]...
  1026.                         REMOVE NAME[xmlns:_p3_n4_tns] LOCAL_NAME[null] VALUE[http://example.ResponseError] URI[null]...
  1027.                         REMOVE NAME[xmlns:Q1] LOCAL_NAME[Q1] VALUE[http://example.libCodifiche] URI[http://www.w3.org/2000/xmlns/]...
  1028.                         REMOVE NAME[xmlns] LOCAL_NAME[xmlns] VALUE[http://www.w3.org/2001/XMLSchema] URI[http://www.w3.org/2000/xmlns/]...
  1029.                         REMOVE NAME[targetNamespace] LOCAL_NAME[targetNamespace] VALUE[http://example.ResponseError] URI[null]...
  1030.                         REMOVE NAME[elementFormDefault] LOCAL_NAME[elementFormDefault] VALUE[qualified] URI[null]...
  1031.                         REMOVE NAME[attributeFormDefault] LOCAL_NAME[attributeFormDefault] VALUE[unqualified] URI[null]...
  1032.                      **/
  1033.                     if(targetNamespace.equals(attr.getNodeValue()) ||
  1034.                             (!attr.getNodeName().equals("xmlns") && !attr.getNodeName().startsWith("xmlns:"))
  1035.                         ){
  1036.                         //System.out.println("REMOVE NON EFFETTUATO");
  1037.                         attributiDaMantenere.add(attr);
  1038.                     }
  1039.                     _correctPrefix(attr, targetNamespace, prefixForWSDL, schemaXSD, attributiDaMantenere);
  1040.                    
  1041.                     //schemaXSD.removeAttributeNode(attr);
  1042.                     this.xmlUtils.removeAttribute(attr, schemaXSD);
  1043.                 }
  1044.             }
  1045. //          IL Codice sotto non può funzionare, poichè in axiom la struttura NamedNodeMap non viene aggiornata dopo la remove
  1046. //          while(attributi.getLength()>0){
  1047. //              Attr attr = (Attr) attributi.item(0);
  1048. //              System.out.println("REMOVE NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] URI["+attr.getNamespaceURI()+"]...");
  1049. //              if(targetNamespace.equals(attr.getNodeValue())){
  1050. //                  System.out.println("REMOVE NON EFFETTUATO");
  1051. //                  attributiDaMantenere.add(attr);
  1052. //              }
  1053. //              schemaXSD.removeAttributeNode(attr);
  1054. //          }
  1055.            
  1056. //          // DEBUG DOPO ELIMINAZIONE
  1057. //          attributi = schemaXSD.getAttributes();
  1058. //          System.out.println("TEST: "+attributi.getLength());
  1059. //          for (int i = 0; i < attributi.getLength(); i++) {
  1060. //              Attr attr = (Attr) attributi.item(i);
  1061. //              System.out.println("TEST NAME["+attr.getName()+"] LOCAL_NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] URI["+attr.getNamespaceURI()+"]...");
  1062. //          }
  1063.            
  1064.             while(attributiDaMantenere.size()>0){
  1065.                 //schemaXSD.setAttributeNode(attributiDaMantenere.remove(0));
  1066.                 Attr attr = attributiDaMantenere.remove(0);
  1067.                 //System.out.println("RE-ADD NAME["+attr.getName()+"] LOCAL_NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] URI["+attr.getNamespaceURI()+"]...");
  1068.                 this.xmlUtils.addAttribute(attr, schemaXSD);
  1069.             }
  1070.            
  1071. //          // DEBUG DOPO AGGIUNTA
  1072. //          attributi = schemaXSD.getAttributes();
  1073. //          System.out.println("TEST2: "+attributi.getLength());
  1074. //          for (int i = 0; i < attributi.getLength(); i++) {
  1075. //              Attr attr = (Attr) attributi.item(i);
  1076. //              System.out.println("TEST2 NAME["+attr.getName()+"] LOCAL_NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] URI["+attr.getNamespaceURI()+"]...");
  1077. //          }
  1078.            
  1079.         }else{
  1080.            
  1081.             // GESTIONE DOCUMENTO INCLUSO
  1082.             //System.out.println("***** INCLUDE");
  1083.            
  1084.             targetNamespace = targetNamespaceParent;
  1085.            
  1086.             // Rimozione completa di tutti gli attributi
  1087.             NamedNodeMap attributi = schemaXSD.getAttributes();
  1088.             List<Attr> attributiDaMantenere = new ArrayList<Attr>();    
  1089.             if(attributi!=null && attributi.getLength()>0){
  1090.                 for (int i = (attributi.getLength()-1); i >= 0; i--) {
  1091.                     Attr attr = (Attr) attributi.item(i);
  1092.                     //System.out.println("REMOVE NAME["+attr.getName()+"] LOCAL_NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] URI["+attr.getNamespaceURI()+"]...");
  1093.                     if(targetNamespace.equals(attr.getNodeValue()) ||
  1094.                             (!attr.getNodeName().equals("xmlns") && !attr.getNodeName().startsWith("xmlns:"))
  1095.                         ){
  1096.                         //System.out.println("REMOVE NON EFFETTUATO");
  1097.                         attributiDaMantenere.add(attr);
  1098.                     }
  1099.                     _correctPrefix(attr, targetNamespace, prefixForWSDL, schemaXSD, attributiDaMantenere);
  1100.                    
  1101.                     //schemaXSD.removeAttributeNode(attr);
  1102.                     this.xmlUtils.removeAttribute(attr, schemaXSD);
  1103.                 }
  1104.             }
  1105. //          IL Codice sotto non può funzionare, poichè in axiom la struttura NamedNodeMap non viene aggiornata dopo la remove
  1106. //          while(attributi.getLength()>0){
  1107. //              Attr attr = (Attr) attributi.item(0);
  1108. //              //System.out.println("REMOVE NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"]...");
  1109. //              schemaXSD.removeAttributeNode(attr);
  1110. //          }
  1111.            
  1112. //          // DEBUG DOPO ELIMINAZIONE
  1113. //          attributi = schemaXSD.getAttributes();
  1114. //          System.out.println("TEST: "+attributi.getLength());
  1115. //          for (int i = 0; i < attributi.getLength(); i++) {
  1116. //              Attr attr = (Attr) attributi.item(i);
  1117. //              System.out.println("TEST NAME["+attr.getName()+"] LOCAL_NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] URI["+attr.getNamespaceURI()+"]...");
  1118. //          }
  1119.            
  1120.             // Aggiungo targetNamespace e altri prefix associati al namespace
  1121.             boolean foundTargetNamespace = false;
  1122.             while(attributiDaMantenere.size()>0){
  1123.                 //schemaXSD.setAttributeNode(attributiDaMantenere.remove(0));
  1124.                 Attr attr = attributiDaMantenere.remove(0);
  1125.                 if("targetNamespace".equals(attr.getName())){
  1126.                     foundTargetNamespace = true;
  1127.                 }
  1128.                 //System.out.println("RE-ADD NAME["+attr.getName()+"] LOCAL_NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] URI["+attr.getNamespaceURI()+"]...");
  1129.                 this.xmlUtils.addAttribute(attr, schemaXSD);
  1130.             }
  1131.             if(!foundTargetNamespace){
  1132.                 schemaXSD.setAttribute("targetNamespace", targetNamespace);
  1133.             }
  1134.            
  1135. //          // DEBUG DOPO AGGIUNTA
  1136. //          attributi = schemaXSD.getAttributes();
  1137. //          System.out.println("TEST2: "+attributi.getLength());
  1138. //          for (int i = 0; i < attributi.getLength(); i++) {
  1139. //              Attr attr = (Attr) attributi.item(i);
  1140. //              System.out.println("TEST2 NAME["+attr.getName()+"] LOCAL_NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] URI["+attr.getNamespaceURI()+"]...");
  1141. //          }
  1142.         }
  1143.        
  1144.         return targetNamespace;
  1145.     }
  1146.    
  1147.     private void _correctPrefix(Attr attr, String targetNamespace, HashMap<String,String> prefixForWSDL, Element schemaXSD, List<Attr> attributiDaMantenere) throws org.openspcoop2.utils.wsdl.WSDLException {
  1148.        
  1149.         XPathExpressionEngine xpathEngine = null;
  1150.         DynamicNamespaceContext dnc = null;
  1151.         String exprType = "//*/@type";
  1152.         String exprRef = "//*/@ref";
  1153.         List<String> exprs = new ArrayList<>();
  1154.         exprs.add(exprType);
  1155.         exprs.add(exprRef);
  1156.        
  1157.         if(attr.getNodeName().startsWith("xmlns:") && attr.getLocalName()!=null) {
  1158.             if(!targetNamespace.equals(attr.getNodeValue())){
  1159.                 boolean found = false;
  1160.                 String prefixDest = null;
  1161.                 String prefixSrc = null;
  1162.                 for (String prefix: prefixForWSDL.keySet()) {
  1163.                     String value = prefixForWSDL.get(prefix);
  1164.                     if(value.equals(attr.getNodeValue())) {
  1165.                         found = true;
  1166.                         prefixSrc = attr.getLocalName();
  1167.                         if(prefix.startsWith("xmlns:")) {
  1168.                             prefixDest = prefix.substring("xmlns:".length());
  1169.                         }
  1170.                         else {
  1171.                             prefixDest = prefix;
  1172.                         }
  1173.                         break;
  1174.                     }
  1175.                 }
  1176.                 if(found) {
  1177.                     //System.out.println("DEVO FARE REPLACE DI PREFIX '"+prefixSrc+"' con '"+prefixDest+"'");
  1178.                    
  1179.                     for (String expr : exprs) {
  1180.                         if(xpathEngine==null) {
  1181.                             xpathEngine = new XPathExpressionEngine();
  1182.                             dnc = new DynamicNamespaceContext();
  1183.                             dnc.findPrefixNamespace(schemaXSD);
  1184.                         }
  1185.                         Object res = null;
  1186.                         try {
  1187.                             res = xpathEngine.getMatchPattern(schemaXSD, dnc, expr, XPathReturnType.NODESET);
  1188.                         }catch(XPathNotFoundException notFound) {
  1189.                         }
  1190.                         catch(Exception e) {
  1191.                             throw new org.openspcoop2.utils.wsdl.WSDLException(e.getMessage(),e);
  1192.                         }
  1193.                         if(res!=null) {
  1194.                             //System.out.println("XPATH RESULT ("+expr+"): "+res.getClass().getName());
  1195.                             if(res instanceof NodeList) {
  1196.                                 NodeList nodeList = (NodeList) res;
  1197.                                 for (int j = 0; j < nodeList.getLength(); j++) {
  1198.                                     Node n = nodeList.item(j);
  1199.                                     //System.out.println("i["+j+"] type["+n.getClass().getName()+"] value["+n.getNodeValue()+"]");
  1200.                                     if(n.getNodeValue()!=null && n.getNodeValue().startsWith(prefixSrc+":")) {
  1201.                                         n.setNodeValue(n.getNodeValue().replace(prefixSrc+":", prefixDest+":"));
  1202.                                     }
  1203.                                 }
  1204.                             }
  1205.                         }
  1206. //                      else {
  1207. //                          System.out.println("XPATH RESULT ("+expr+") NULL");
  1208. //                      }  
  1209.                     }
  1210.                    
  1211.                 }
  1212.                 else {
  1213.                     //System.out.println("REMOVE NON EFFETTUATO, DECL IN WSDL NON PRESENTE");
  1214.                     attributiDaMantenere.add(attr);
  1215.                 }
  1216.             }
  1217.         }
  1218.     }
  1219.    
  1220.     public String readPrefixForWsdl(Element schemaXSD,Element wsdl,
  1221.             HashMap<String,String> prefixForWSDL,String uniquePrefixWSDL,boolean targetNamespaceObbligatorio) throws org.openspcoop2.utils.wsdl.WSDLException{
  1222.            
  1223.         String targetNamespace = null;
  1224.        
  1225.         //System.out.println("TARGETNAMESPACE="+docElement.getNamespaceURI());
  1226.         NamedNodeMap attributi = schemaXSD.getAttributes();
  1227.         if(attributi!=null){
  1228.             // ricerca targetNamespace XSD
  1229.             for(int i=0; i<attributi.getLength(); i++){
  1230.                 Node attr = attributi.item(i);
  1231.                 //System.out.println("NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] PREFIX["+attr.getPrefix()+"]");
  1232.                 if("targetNamespace".equals(attr.getLocalName())){
  1233.                     targetNamespace =attr.getNodeValue();
  1234.                     break;
  1235.                 }
  1236.             }
  1237.             if(targetNamespace==null){
  1238.                 if(targetNamespaceObbligatorio)
  1239.                     throw new org.openspcoop2.utils.wsdl.WSDLException("Target namespace non trovato");
  1240.                 else
  1241.                     return null;
  1242.             }
  1243.             // ricerca prefisso associato al target namespace dell'XSD
  1244.             String prefixTargetNamespaceXSD = null;
  1245.             for(int i=0; i<attributi.getLength(); i++){
  1246.                 Node attr = attributi.item(i);
  1247.                 //System.out.println("NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] PREFIX["+attr.getPrefix()+"]");
  1248.                 if(targetNamespace.equals(attr.getNodeValue()) && "xmlns".equals(attr.getPrefix())){
  1249.                     //System.out.println("FOUND");
  1250.                     prefixTargetNamespaceXSD =attr.getLocalName();
  1251.                     break;
  1252.                 }
  1253.             }
  1254.             //System.out.println("PREFIX!!! ["+prefixTargetNamespaceXSD+"] ["+targetNamespaceXSD+"]");
  1255.            
  1256.             // Cerco il prefix nel definition del wsdl, ed in caso non sia definito lo registra per una successiva aggiunta
  1257.             NamedNodeMap attributi_wsdl = wsdl.getAttributes();
  1258.             if(attributi_wsdl!=null){
  1259.                 boolean findIntoWSDL = false;
  1260.                 for(int i=0; i<attributi_wsdl.getLength(); i++){
  1261.                     Node attr = attributi_wsdl.item(i);
  1262.                     //System.out.println("WSDL ATTR   NAME["+attr.getLocalName()+"] VALUE["+attr.getNodeValue()+"] PREFIX["+attr.getPrefix()+"]");
  1263.                     if(targetNamespace.equals(attr.getNodeValue()) && "xmlns".equals(attr.getPrefix())){
  1264.                         if(prefixTargetNamespaceXSD==null){
  1265.                             findIntoWSDL = (attr.getLocalName()==null);
  1266.                             break;
  1267.                         }
  1268.                         else if(prefixTargetNamespaceXSD.equals(attr.getLocalName())){
  1269.                             findIntoWSDL = true;
  1270.                             break;
  1271.                         }
  1272.                     }  
  1273.                 }
  1274.                 if(!findIntoWSDL){
  1275.                     String newPrefix = null;
  1276.                     if(prefixTargetNamespaceXSD==null){
  1277.                         //System.out.println("AGGIUNGO IN WSDL XMLNS (unique:"+uniquePrefixWSDL+"): "+targetNamespace);
  1278.                         if(uniquePrefixWSDL!=null){
  1279.                             newPrefix = "xmlns:"+uniquePrefixWSDL;
  1280.                         }
  1281.                         else{
  1282.                             newPrefix = "xmlns";
  1283.                         }
  1284.                     }else{
  1285.                         //System.out.println("AGGIUNGO IN WSDL XMLNS (unique:"+uniquePrefixWSDL+"):"+prefixTargetNamespaceXSD+" "+targetNamespace);
  1286.                         if(uniquePrefixWSDL!=null){
  1287.                             newPrefix = "xmlns:"+uniquePrefixWSDL+prefixTargetNamespaceXSD;
  1288.                         }else{
  1289.                             newPrefix = "xmlns:"+prefixTargetNamespaceXSD;
  1290.                         }
  1291.                     }
  1292.                    
  1293.                     //System.out.println("SET NEW ATTRIBUTE NS LOCALNAME["+newPrefix+"] ...");
  1294.                     schemaXSD.setAttribute(newPrefix, targetNamespace);
  1295.                     //System.out.println("SET NEW ATTRIBUTE NS LOCALNAME["+newPrefix+"] ["+targetNamespace+"] OK");
  1296.                    
  1297.                     if(prefixForWSDL.containsKey(newPrefix)==false){
  1298.                         prefixForWSDL.put(newPrefix, targetNamespace);
  1299.                     }
  1300.                     else{
  1301.                         String namespace = prefixForWSDL.get(newPrefix);
  1302.                         if(namespace.equals(targetNamespace)==false){
  1303.                             throw new org.openspcoop2.utils.wsdl.WSDLException("Rilevati due prefissi a cui sono stati associati namespace differenti. \nPrefix["+
  1304.                                     newPrefix+"]=Namespace["+namespace+"]\nPrefix["+newPrefix+"]=Namespace["+targetNamespace+"]");
  1305.                         }
  1306.                     }
  1307.                 }
  1308.             }
  1309.         }
  1310.        
  1311.         return targetNamespace;
  1312.     }
  1313.    
  1314.    
  1315.    
  1316.    
  1317.    
  1318.    
  1319.    
  1320.    
  1321.    
  1322.     /* ---------------- METODI REMOVE ------------------------- */
  1323.    
  1324.     /**
  1325.      * Rimuove i wsdl:import dal documento
  1326.      *
  1327.      * @param document
  1328.      */
  1329.     public void removeImports(Document document){
  1330.         removeImport(document,null);
  1331.     }
  1332.     public void removeImport(Document document,Node importNode){
  1333.         // rimuovo eventuale import per i definitori, visto che nella versione byte[] dal db, gli import non possono essere risolti
  1334.         NodeList list = document.getChildNodes();
  1335.         if(list!=null){
  1336.             for(int i=0; i<list.getLength(); i++){
  1337.                 Node child = list.item(i);
  1338.                 if("definitions".equals(child.getLocalName())){
  1339.                     NodeList listDefinition = child.getChildNodes();
  1340.                     if(listDefinition!=null){
  1341.                         for(int j=0; j<listDefinition.getLength(); j++){
  1342.                             Node childDefinition = listDefinition.item(j);
  1343.                             if("import".equals(childDefinition.getLocalName())){
  1344.                                 if(importNode==null){
  1345.                                     //  System.out.println("REMOVE IMPORT");
  1346.                                     child.removeChild(childDefinition);
  1347.                                 }else{
  1348.                                     if(importNode.equals(childDefinition)){
  1349.                                         //System.out.println("REMOVE MIRATA");
  1350.                                         child.removeChild(childDefinition);
  1351.                                     }
  1352.                                 }
  1353.                             }
  1354.                         }
  1355.                     }
  1356.                 }
  1357.             }
  1358.         }
  1359.     }
  1360.    
  1361.     /**
  1362.      * Rimuove gli schemi dal wsdl
  1363.      *
  1364.      * @param document
  1365.      * @throws org.openspcoop2.utils.wsdl.WSDLException
  1366.      */
  1367.     public void removeSchemiIntoTypes(Document document) throws org.openspcoop2.utils.wsdl.WSDLException{
  1368.        
  1369.         try{
  1370.             NodeList list = document.getChildNodes();
  1371.             if(list!=null){
  1372.                 for(int i=0; i<list.getLength(); i++){
  1373.                     Node child = list.item(i);
  1374.                     if("definitions".equals(child.getLocalName())){
  1375.                         NodeList listDefinition = child.getChildNodes();
  1376.                         if(listDefinition!=null){
  1377.                             for(int j=0; j<listDefinition.getLength(); j++){
  1378.                                 Node childDefinition = listDefinition.item(j);
  1379.                                 if("types".equals(childDefinition.getLocalName())){
  1380.                                     NodeList listTypes = childDefinition.getChildNodes();
  1381.                                     if(listTypes!=null){
  1382.                                         boolean onlySchemaAndComment = true;
  1383.                                         for(int h=0; h<listTypes.getLength(); h++){
  1384.                                             Node childTypes = listTypes.item(h);
  1385.                                             //System.out.println("?? IMPORT SCHEMA: "+childTypes.getLocalName());
  1386.                                             if("schema".equals(childTypes.getLocalName())){
  1387.                                                 //System.out.println("REMOVE IMPORT SCHEMA");
  1388.                                                 childDefinition.removeChild(childTypes);
  1389.                                             }
  1390.                                             else{
  1391.                                                 if( !(childTypes instanceof Text) && !(childTypes instanceof Comment) ){
  1392.                                                     onlySchemaAndComment = false;
  1393.                                                 }
  1394.                                             }
  1395.                                         }
  1396.                                         if(onlySchemaAndComment){
  1397.                                             listTypes = childDefinition.getChildNodes();
  1398.                                             if(listTypes!=null){
  1399.                                                 for(int h=0; h<listTypes.getLength(); h++){
  1400.                                                     childDefinition.removeChild(listTypes.item(h));
  1401.                                                 }
  1402.                                             }
  1403.                                         }
  1404.                                     }
  1405.                                 }
  1406.                             }
  1407.                         }
  1408.                     }
  1409.                 }
  1410.             }

  1411.         }catch(Exception e){
  1412.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante la lettura del wsdl: "+e.getMessage(),e);
  1413.         }
  1414.     }
  1415.    
  1416.     /**
  1417.      * Rimuove l'elemento types dal wsdl
  1418.      *
  1419.      * @param wsdl
  1420.      * @throws org.openspcoop2.utils.wsdl.WSDLException
  1421.      */
  1422.     public void removeTypes(Document wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  1423.        
  1424.         try{
  1425.             NodeList list = wsdl.getChildNodes();
  1426.             if(list!=null){
  1427.                 for(int i=0; i<list.getLength(); i++){
  1428.                     Node child = list.item(i);
  1429.                     if("definitions".equals(child.getLocalName())){
  1430.                         NodeList listDefinition = child.getChildNodes();
  1431.                         if(listDefinition!=null){
  1432.                             for(int j=0; j<listDefinition.getLength(); j++){
  1433.                                 Node childDefinition = listDefinition.item(j);
  1434.                                 if("types".equals(childDefinition.getLocalName())){
  1435.                                     //System.out.println("REMOVE TYPES");
  1436.                                     child.removeChild(childDefinition);
  1437.                                 }
  1438.                             }
  1439.                         }
  1440.                     }
  1441.                 }
  1442.             }
  1443.         }catch(Exception e){
  1444.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante la rimozione dell'elemento Types: "+e.getMessage(),e);
  1445.         }
  1446.     }
  1447.    
  1448.     public void removeImportsFromSchemaXSD(Schema xsd) throws org.openspcoop2.utils.wsdl.WSDLException{
  1449.         if(this.xmlUtils==null){
  1450.             throw new org.openspcoop2.utils.wsdl.WSDLException("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  1451.         }
  1452.         XSDUtils xsdUtils = new XSDUtils(this.xmlUtils);
  1453.         xsdUtils.removeImports(xsd.getElement());
  1454.     }
  1455.     public void removeIncludesFromSchemaXSD(Schema xsd) throws org.openspcoop2.utils.wsdl.WSDLException{
  1456.         if(this.xmlUtils==null){
  1457.             throw new org.openspcoop2.utils.wsdl.WSDLException("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  1458.         }
  1459.         XSDUtils xsdUtils = new XSDUtils(this.xmlUtils);
  1460.         xsdUtils.removeIncludes(xsd.getElement());
  1461.     }
  1462.     public void removeImportsAndIncludesFromSchemaXSD(Schema xsd) throws org.openspcoop2.utils.wsdl.WSDLException{
  1463.         if(this.xmlUtils==null){
  1464.             throw new org.openspcoop2.utils.wsdl.WSDLException("XMLUtils not initialized in WSDLUtilities, use static instance 'getInstance(AbstractXMLUtils xmlUtils)'");
  1465.         }
  1466.         XSDUtils xsdUtils = new XSDUtils(this.xmlUtils);
  1467.         xsdUtils.removeImportsAndIncludes(xsd.getElement());
  1468.     }
  1469.    
  1470.     /**
  1471.      * Rimuove TUTTI gli Import da una definizione.
  1472.      * @param definition La definizione da modificare.
  1473.      */
  1474.     public void removeAllImports(Definition definition) throws WSDLException{
  1475.         if (definition != null) {
  1476.             DefinitionWrapper wsdl = new DefinitionWrapper(definition,this.xmlUtils);
  1477.             wsdl.removeAllImports();
  1478.         }else{
  1479.             throw new WSDLException("removeAllImports(Definition definition)","WSDL non fornito");
  1480.         }
  1481.     }
  1482.    
  1483.     /**
  1484.      * Rimuove TUTTI i messaggi da una definizione.
  1485.      * @param definition La definizione da modificare.
  1486.      */
  1487.     public void removeAllMessages(Definition definition) throws WSDLException{
  1488.         if (definition != null) {
  1489.             DefinitionWrapper wsdl = new DefinitionWrapper(definition,this.xmlUtils);
  1490.             wsdl.removeAllMessages();
  1491.         }else{
  1492.             throw new WSDLException("removeAllMessages(Definition definition)","WSDL non fornito");
  1493.         }
  1494.     }
  1495.    
  1496.     /**
  1497.      * Rimuove TUTTI i PortType da una definizione.
  1498.      * @param definition La definizione da modificare.
  1499.      */
  1500.     public void removeAllPortTypes(Definition definition) throws WSDLException{
  1501.         if (definition != null) {
  1502.             DefinitionWrapper wsdl = new DefinitionWrapper(definition,this.xmlUtils);
  1503.             wsdl.removeAllPortTypes();
  1504.         }else{
  1505.             throw new WSDLException("removeAllPortTypes(Definition definition)","WSDL non fornito");
  1506.         }
  1507.     }

  1508.     /**
  1509.      * Rimuove TUTTI i Binding da una definizione.
  1510.      * @param definition La definizione da modificare.
  1511.      */
  1512.     public void removeAllBindings(Definition definition) throws WSDLException{
  1513.         if (definition != null) {
  1514.             DefinitionWrapper wsdl = new DefinitionWrapper(definition,this.xmlUtils);
  1515.             wsdl.removeAllBindings();
  1516.         }else{
  1517.             throw new WSDLException("removeAllBindings(Definition definition)","WSDL non fornito");
  1518.         }
  1519.     }
  1520.    
  1521.     /**
  1522.      * Rimuove TUTTI i Service da una definizione.
  1523.      * @param definition La definizione da modificare.
  1524.      */
  1525.     public void removeAllServices(Definition definition) throws WSDLException{
  1526.         if (definition != null) {
  1527.             DefinitionWrapper wsdl = new DefinitionWrapper(definition,this.xmlUtils);
  1528.             wsdl.removeAllServices();
  1529.         }else{
  1530.             throw new WSDLException("removeAllServices(Definition definition)","WSDL non fornito");
  1531.         }
  1532.     }
  1533.    
  1534.    
  1535.    
  1536.    
  1537.    
  1538.    
  1539.    
  1540.    
  1541.    
  1542.    
  1543.    
  1544.    
  1545.    
  1546.    
  1547.     /* ---------------- TYPES ------------------------- */

  1548.     public boolean existsTypes(Document document) throws org.openspcoop2.utils.wsdl.WSDLException{
  1549.        
  1550.         try{
  1551.             NodeList list = document.getChildNodes();
  1552.             if(list!=null){
  1553.                 for(int i=0; i<list.getLength(); i++){
  1554.                     Node child = list.item(i);
  1555.                     if("definitions".equals(child.getLocalName())){
  1556.                         NodeList listDefinition = child.getChildNodes();
  1557.                         if(listDefinition!=null){
  1558.                             for(int j=0; j<listDefinition.getLength(); j++){
  1559.                                 Node childDefinition = listDefinition.item(j);
  1560.                                 if("types".equals(childDefinition.getLocalName())){
  1561.                                     return true;
  1562.                                 }
  1563.                             }
  1564.                         }
  1565.                     }
  1566.                 }
  1567.             }

  1568.         }catch(Exception e){
  1569.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante la lettura del wsdl: "+e.getMessage(),e);
  1570.         }
  1571.        
  1572.         return false;
  1573.     }
  1574.    
  1575.     public Node getIfExistsTypesElementIntoWSDL(Document wsdl) throws org.openspcoop2.utils.wsdl.WSDLException{
  1576.        
  1577.         try{
  1578.             NodeList list = wsdl.getChildNodes();
  1579.             if(list!=null){
  1580.                 for(int i=0; i<list.getLength(); i++){
  1581.                     Node child = list.item(i);
  1582.                     if("definitions".equals(child.getLocalName())){
  1583.                         NodeList listDefinition = child.getChildNodes();
  1584.                         if(listDefinition!=null){
  1585.                             for(int j=0; j<listDefinition.getLength(); j++){
  1586.                                 Node childDefinition = listDefinition.item(j);
  1587.                                 if("types".equals(childDefinition.getLocalName())){
  1588.                                     return childDefinition;
  1589.                                 }
  1590.                             }
  1591.                         }
  1592.                     }
  1593.                 }
  1594.             }
  1595.             return null;
  1596.         }catch(Exception e){
  1597.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante la lettura del wsdl: "+e.getMessage(),e);
  1598.         }
  1599.     }
  1600.    
  1601.     public Node addEmptyTypesIfNotExists(Document document) throws org.openspcoop2.utils.wsdl.WSDLException{
  1602.        
  1603.         try{
  1604.             boolean exists = false;
  1605.             NodeList list = document.getChildNodes();
  1606.             if(list!=null){
  1607.                 for(int i=0; i<list.getLength(); i++){
  1608.                     Node child = list.item(i);
  1609.                     if("definitions".equals(child.getLocalName())){
  1610.                         NodeList listDefinition = child.getChildNodes();
  1611.                         if(listDefinition!=null){
  1612.                             for(int j=0; j<listDefinition.getLength(); j++){
  1613.                                 Node childDefinition = listDefinition.item(j);
  1614.                                 if("types".equals(childDefinition.getLocalName())){
  1615.                                     exists = true;
  1616.                                     break;
  1617.                                 }
  1618.                             }
  1619.                         }
  1620.                         if(!exists){
  1621.                             String name = "types";
  1622.                             if(child.getPrefix()!=null && !"".equals(child.getPrefix())){
  1623.                                 name = child.getPrefix()+":"+name;
  1624.                             }
  1625.                             Node n = this.xmlUtils.getFirstNotEmptyChildNode(child,false);
  1626.                             //System.out.println("FIRST ["+n.getLocalName()+"] ["+n.getPrefix()+"] ["+n.getNamespaceURI()+"]");
  1627.                             Node type = document.createElementNS(child.getNamespaceURI(), name);
  1628.                             child.insertBefore(type,n);
  1629.                             return type;
  1630.                         }
  1631.                     }
  1632.                 }
  1633.             }

  1634.             return null;
  1635.            
  1636.         }catch(Exception e){
  1637.             throw new org.openspcoop2.utils.wsdl.WSDLException("Riscontrato errore durante la lettura del wsdl: "+e.getMessage(),e);
  1638.         }
  1639.     }
  1640. }