DynamicNamespaceContext.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.util.ArrayList;
  22. import java.util.Enumeration;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Properties;

  26. import org.w3c.dom.Attr;
  27. import org.w3c.dom.NamedNodeMap;
  28. import org.w3c.dom.Node;
  29. import org.w3c.dom.NodeList;

  30. /**
  31.  * Classe utilizzabile per raccogliere le informazioni sui namespaces di un messaggio Soap
  32.  *
  33.  * @author Andrea Poli (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */

  37. public class DynamicNamespaceContext implements javax.xml.namespace.NamespaceContext
  38. {

  39.     /** Variabile contenente i prefix */
  40.     private java.util.Properties context;
  41.     private String prefixChildSoapBody = null;
  42.     private boolean soapFault = false;
  43.     private boolean soapBodyEmpty = false;

  44.    
  45.     @Override
  46.     public Object clone(){
  47.         DynamicNamespaceContext dnc = new DynamicNamespaceContext();

  48.         if(this.context!=null){
  49.             dnc.context = new Properties();
  50.             if(this.context.size()>0){
  51.                 Iterator<?> it = this.context.keySet().iterator();
  52.                 while (it.hasNext()) {
  53.                     Object key = it.next();
  54.                     dnc.context.put(key, this.context.get(key));
  55.                 }
  56.             }
  57.         }
  58.        
  59.         if(this.prefixChildSoapBody!=null){
  60.             dnc.prefixChildSoapBody = new String(this.prefixChildSoapBody);
  61.         }
  62.        
  63.         dnc.soapFault = this.soapFault;
  64.        
  65.         dnc.soapBodyEmpty = this.soapBodyEmpty;
  66.        
  67.         return dnc;
  68.     }
  69.    
  70.    
  71.     public boolean isSoapBodyEmpty() {
  72.         return this.soapBodyEmpty;
  73.     }

  74.     public void setSoapBodyEmpty(boolean soapBodyEmpty) {
  75.         this.soapBodyEmpty = soapBodyEmpty;
  76.     }

  77.     public boolean isSoapFault() {
  78.         return this.soapFault;
  79.     }

  80.     public void setSoapFault(boolean soapFault) {
  81.         this.soapFault = soapFault;
  82.     }

  83.     public String getPrefixChildSoapBody() {
  84.         return this.prefixChildSoapBody;
  85.     }

  86.     public void setPrefixChildSoapBody(String prefixChildSoapBody) {
  87.         this.prefixChildSoapBody = prefixChildSoapBody;
  88.     }
  89.    
  90.     /**
  91.      * Costruttore
  92.      *
  93.      *
  94.      */
  95.     public DynamicNamespaceContext(){
  96.         this.context = new java.util.Properties();
  97.         this.context.put(javax.xml.XMLConstants.XML_NS_PREFIX,javax.xml.XMLConstants.XML_NS_URI);
  98.         this.context.put(javax.xml.XMLConstants.XMLNS_ATTRIBUTE,javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
  99.     }

  100.    
  101.     /**
  102.      * Imposta un nuovo namespace, se non ancora inserito
  103.      *
  104.      * @param prefix Prefisso
  105.      * @param url URL del Namespace, associato al prefisso
  106.      *
  107.      */
  108.     public void addNamespace(String prefix,String url){
  109.         if(this.context.containsKey(prefix)==false){
  110.             this.context.setProperty(prefix,url);
  111.         }
  112.     }

  113.     /**
  114.      * Dato come parametro un prefix, ritorna l'associato namespace
  115.      *
  116.      * @param prefix Prefisso
  117.      *
  118.      */
  119.     @Override
  120.     public String getNamespaceURI(String prefix)
  121.     {
  122.         /**
  123.          * getNamespaceURI(prefix) return value for specified prefixes
  124.          * -a. 'DEFAULT_NS_PREFIX ("")' ->  default Namespace URI in the current scope or
  125.          *                                  XMLConstants.NULL_NS_URI("") when there is no default Namespace URI in the current scope
  126.          * -b. 'bound prefix'           ->  Namespace URI bound to prefix in current scope
  127.          * -c. 'unbound prefix'         ->  XMLConstants.NULL_NS_URI("")
  128.          * -d. XMLConstants.XML_NS_PREFIX ("xml")   ->  XMLConstants.XML_NS_URI ("http://www.w3.org/XML/1998/namespace")
  129.          * -e. XMLConstants.XMLNS_ATTRIBUTE ("xmlns")  ->   XMLConstants.XMLNS_ATTRIBUTE_NS_URI ("http://www.w3.org/2000/xmlns/")
  130.          * -f. null  -> IllegalArgumentException is thrown
  131.          */
  132.        
  133.         //System.out.println("------ getNamespaceURI("+prefix+") -----");
  134.        
  135.         // -f. null
  136.         if(prefix==null){
  137.             //System.out.println("RETURN -f. null ");
  138.             throw new IllegalArgumentException("Prefix ["+prefix+"] not defined");
  139.         }
  140.        
  141.         // -a. 'DEFAULT_NS_PREFIX ("")'
  142.         if(javax.xml.XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)){
  143.             if(this.context.containsKey(prefix)){
  144.                 //System.out.println("RETURN ["+this.context.getProperty(prefix)+"] -a. 'DEFAULT_NS_PREFIX'");
  145.                 return this.context.getProperty(prefix);
  146.             }else{
  147.                 //System.out.println("RETURN ["+javax.xml.XMLConstants.NULL_NS_URI+"] -a. 'DEFAULT_NS_PREFIX' (nullUri)");
  148.                 return javax.xml.XMLConstants.NULL_NS_URI;
  149.             }
  150.         }
  151.        
  152.         // -d. XMLConstants.XML_NS_PREFIX
  153.         else if(javax.xml.XMLConstants.XML_NS_PREFIX.equals(prefix)){
  154.             //System.out.println("RETURN ["+this.context.getProperty(prefix)+"] -d. XMLConstants.XML_NS_PREFIX");
  155.             return this.context.getProperty(prefix); // inizializzato nel costruttore
  156.         }
  157.        
  158.         // -e. XMLConstants.XMLNS_ATTRIBUTE
  159.         else if(javax.xml.XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)){
  160.             //System.out.println("RETURN ["+this.context.getProperty(prefix)+"] -e. XMLConstants.XMLNS_ATTRIBUTE");
  161.             return this.context.getProperty(prefix); // inizializzato nel costruttore
  162.         }
  163.        
  164.         // -b. 'bound prefix'
  165.         else if(this.context.containsKey(prefix)){
  166.            
  167.             java.util.Enumeration<?> en = this.context.propertyNames();
  168.             while(en.hasMoreElements()){
  169.                 String key = (String) en.nextElement();
  170.                 if(key.equals(prefix)){
  171.                     //System.out.println("RETURN ["+this.context.getProperty(key)+"] -b. 'bound prefix' ");
  172.                     return this.context.getProperty(key);
  173.                 }
  174.             }  
  175.            
  176.         }
  177.        
  178.         // c. unbound prefix
  179.         //System.out.println("RETURN ["+javax.xml.XMLConstants.NULL_NS_URI+"] -c. 'unbound prefix'");
  180.         return javax.xml.XMLConstants.NULL_NS_URI;
  181.        
  182.     }

  183.     /**
  184.      * Dato come parametro un namespace, ritorna l'associato prefix
  185.      *
  186.      * @param namespace URL del Namespace, associato al prefisso
  187.      *
  188.      */
  189.     @Override
  190.     public String getPrefix(String namespace)
  191.     {
  192.         /**
  193.          * getPrefix(namespaceURI) return value for specified Namespace URIs
  194.          * -a. '<default Namespace URI>'    -> XMLConstants.DEFAULT_NS_PREFIX ("")
  195.          * -b. 'bound Namespace URI'        -> prefix bound to Namespace URI in the current scope, if multiple prefixes are bound to the Namespace URI in the current scope,
  196.          *                                      a single arbitrary prefix, whose choice is implementation dependent, is returned
  197.          * -c. 'unbound Namespace URI'      ->  null
  198.          * -d. XMLConstants.XML_NS_URI ("http://www.w3.org/XML/1998/namespace")   ->    XMLConstants.XML_NS_PREFIX ("xml")
  199.          * -e. XMLConstants.XMLNS_ATTRIBUTE_NS_URI ("http://www.w3.org/2000/xmlns/")  ->    XMLConstants.XMLNS_ATTRIBUTE ("xmlns")
  200.          * -f. null  -> IllegalArgumentException is thrown
  201.          **/
  202.        
  203.         //System.out.println("------ getPrefix("+namespace+") -----");
  204.        
  205.         // -f. null
  206.         if(namespace==null){
  207.             //System.out.println("RETURN -f. null ");
  208.             throw new IllegalArgumentException("Namespace ["+namespace+"] not defined");
  209.         }
  210.        
  211.         // -a. '<default Namespace URI>'
  212.         // -b. 'bound Namespace URI'
  213.         // -d. XMLConstants.XML_NS_URI
  214.         // -e. XMLConstants.XMLNS_ATTRIBUTE_NS_URI
  215.         if(this.context.containsValue(namespace)){
  216.            
  217.             java.util.Enumeration<?> en = this.context.propertyNames();
  218.             while(en.hasMoreElements()){
  219.                 String prefix = (String) en.nextElement();
  220.                 if(this.context.getProperty(prefix).equals(namespace)){
  221.                     /*
  222.                     if(javax.xml.XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)){
  223.                         System.out.println("RETURN ["+prefix+"] -a. '<default Namespace URI>'");
  224.                     }
  225.                     else if(javax.xml.XMLConstants.XML_NS_PREFIX.equals(prefix)){
  226.                         System.out.println("RETURN ["+prefix+"] -d. 'XMLConstants.XML_NS_URI'"); // inizializzato nel costruttore
  227.                     }
  228.                     else if(javax.xml.XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)){
  229.                         System.out.println("RETURN ["+prefix+"] -e. 'XMLConstants.XMLNS_ATTRIBUTE_NS_URI'"); // inizializzato nel costruttore
  230.                     }
  231.                     else{
  232.                         System.out.println("RETURN ["+prefix+"] -b. 'bound Namespace URI'"); // primo che incontro
  233.                     }*/
  234.                     return prefix;
  235.                 }
  236.             }  
  237.            
  238.         }
  239.        
  240.         // -c. 'unbound Namespace URI'
  241.         //System.out.println("RETURN ["+javax.xml.XMLConstants.NULL_NS_URI+"] -c. 'unbound Namespace URI'");
  242.         return javax.xml.XMLConstants.NULL_NS_URI;
  243.        
  244.        
  245.     }


  246.     /**
  247.      * Dato come parametro un namespace, ritorna i prefissi con quell'iterator (Non implementato)
  248.      *
  249.      * @param namespace URL del Namespace, associato al prefisso
  250.      *
  251.      */
  252.     @Override
  253.     public java.util.Iterator<String> getPrefixes(String namespace)
  254.     {  
  255.         /**
  256.          * getPrefixes(namespaceURI) return value for specified Namespace URIs
  257.          * -a. bound Namespace URI, including the <default Namespace URI> ->
  258.          *                              Iterator over prefixes bound to Namespace URI in the current scope in an arbitrary, implementation dependent, order
  259.          * -b. unbound Namespace URI  ->    empty Iterator
  260.          * -c. XMLConstants.XML_NS_URI ("http://www.w3.org/XML/1998/namespace")  -> Iterator with one element set to XMLConstants.XML_NS_PREFIX ("xml")
  261.          * -d. XMLConstants.XMLNS_ATTRIBUTE_NS_URI ("http://www.w3.org/2000/xmlns/") -> Iterator with one element set to XMLConstants.XMLNS_ATTRIBUTE ("xmlns")
  262.          * -e. null -> IllegalArgumentException is thrown
  263.          */
  264.        
  265.         //System.out.println("------ getPrefixes("+namespace+") -----");
  266.        
  267.         List<String> v = new ArrayList<>();
  268.        
  269.         // -e. null
  270.         if(namespace==null){
  271.             //System.out.println("RETURN -e. null ");
  272.             throw new IllegalArgumentException("Namespace ["+namespace+"] not defined");
  273.         }
  274.        
  275.         // -a. bound Namespace URI, including the <default Namespace URI>
  276.         // -c. XMLConstants.XML_NS_URI
  277.         // -d. XMLConstants.XMLNS_ATTRIBUTE_NS_URI
  278.         if(this.context.containsValue(namespace)){
  279.            
  280.             java.util.Enumeration<?> en = this.context.propertyNames();
  281.             while(en.hasMoreElements()){
  282.                 String prefix = (String) en.nextElement();
  283.                 if(this.context.getProperty(prefix).equals(namespace)){
  284.                     /*if(javax.xml.XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)){
  285.                         System.out.println("ADD ["+prefix+"] -a. bound Namespace URI  the <default Namespace URI>");
  286.                     }
  287.                     else if(javax.xml.XMLConstants.XML_NS_PREFIX.equals(prefix)){
  288.                         System.out.println("RETURN ["+prefix+"] -c. 'XMLConstants.XML_NS_URI'"); // inizializzato nel costruttore
  289.                     }
  290.                     else if(javax.xml.XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)){
  291.                         System.out.println("RETURN ["+prefix+"] -d. 'XMLConstants.XMLNS_ATTRIBUTE_NS_URI'"); // inizializzato nel costruttore
  292.                     }
  293.                     else{
  294.                         System.out.println("ADD ["+prefix+"] -a. bound Namespace URI");
  295.                     }*/
  296.                     v.add(prefix);
  297.                 }
  298.             }  
  299.             return v.iterator();
  300.            
  301.         }

  302.         // -b. unbound Namespace URI
  303.         //System.out.println("RETURN (empty iterator). '-b. unbound Namespace URI' ");
  304.         return v.iterator();
  305.     }
  306.    
  307.     public Enumeration<?> getPrefixes (){
  308.         return this.context.keys();
  309.     }
  310.    


  311.     /**
  312.      * Trova tutti i riferimenti ad attachments presenti all'interno di un nodo,
  313.      * fornito attraverso il parametro <var>node</var>.
  314.      * Ogni riferimento trovato (Content-ID), viene inserito all'interno del vector <var>href</var>.
  315.      *
  316.      * @param node Node da esaminare.
  317.      *
  318.      */
  319.     public void findPrefixNamespace(Node node) {
  320.         if(node == null)
  321.             return;

  322.         //System.out.println("ELEMENTO ["+node.getLocalName()+"] ["+node.getNamespaceURI()+"]");
  323.        
  324.         String namespace = node.getNamespaceURI();
  325.         if(namespace!=null && "".equals(namespace)==false){
  326.        
  327.             String prefix = node.getPrefix();
  328.             if(prefix==null){
  329.                 prefix = javax.xml.XMLConstants.DEFAULT_NS_PREFIX;
  330.             }
  331.            
  332.             // Esamino attributi del nodo
  333.             //System.out.println("PREFIX["+prefix+"]  NS["+node.getNamespaceURI()+"]");
  334.             this.addNamespace(prefix,node.getNamespaceURI());
  335.            
  336.         }
  337.                    
  338.         // child nodes
  339.        
  340.         NodeList list = node.getChildNodes();
  341.         if(list != null) {
  342.             int nodes = list.getLength();
  343.             for(int i=0;i<nodes;i++){
  344.                 Node child = list.item(i);
  345.                 findPrefixNamespace(child);
  346.             }
  347.         }
  348.        
  349.         // attributes
  350.        
  351.         NamedNodeMap  mapAttribute = node.getAttributes();
  352.         if(mapAttribute!=null && mapAttribute.getLength()>0) {
  353.             for(int i=0;i<mapAttribute.getLength();i++){
  354.                 Node n = mapAttribute.item(i);
  355.                
  356.                 //System.out.println("N["+i+"]: "+n.getClass().getName());
  357.                
  358.                 if(n instanceof Attr) {
  359.                     Attr attr = (Attr) n;
  360.                    
  361.                     if("http://www.w3.org/2000/xmlns/".equals(attr.getNamespaceURI())) {
  362.                    
  363.                         //System.out.println("ATTRIBUTO PREFIX["+attr.getPrefix()+"]  NS["+attr.getNamespaceURI()+"] ["+attr.getLocalName()+"] ["+attr.getNodeValue()+"]");
  364.                    
  365.                         String prefix = attr.getLocalName();
  366.                         if(prefix!=null && !"xmlns".equals(prefix) && "xmlns".equals(attr.getPrefix())){
  367.                            
  368.                             // Esamino attributi del nodo
  369.                             //System.out.println("ESAMINO ATTRIBUTO PREFIX["+prefix+"]  NS["+attr.getNodeValue()+"]");
  370.                             if(attr.getNodeValue()!=null && !"".equals(attr.getNodeValue())) {
  371.                                 this.addNamespace(prefix,attr.getNodeValue());
  372.                             }
  373.                            
  374.                         }
  375.                        
  376.                     }
  377.                    
  378.                 }
  379.             }
  380.         }
  381.     }
  382.    
  383. }